1 /* @source notseq application
2 **
3 ** Excludes a set of sequences and writes out the remaining ones
4 **
5 ** @author Copyright (C) Gary Williams
6 ** @@
7 **
8 ** This program is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License
10 ** as published by the Free Software Foundation; either version 2
11 ** of the License, or (at your option) any later version.
12 **
13 ** This program is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with this program; if not, write to the Free Software
20 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 ******************************************************************************/
22
23 #include "emboss.h"
24
25
26
27
28 static void notseq_readfile(const AjPStr exclude, AjPStr *pattern);
29
30
31
32
33 /* @prog notseq ***************************************************************
34 **
35 ** Excludes a set of sequences and writes out the remaining ones
36 **
37 ******************************************************************************/
38
main(int argc,char ** argv)39 int main(int argc, char **argv)
40 {
41
42 AjPSeqall seqall;
43 AjPSeqout seqout;
44 AjPSeqout junkout;
45 AjPSeq seq = NULL;
46 AjPStr exclude = NULL;
47 AjPStr pattern = NULL;
48 AjPStr name = NULL;
49 AjPStr acc = NULL;
50
51 embInit("notseq", argc, argv);
52
53 seqout = ajAcdGetSeqoutall("outseq");
54 junkout = ajAcdGetSeqoutall("junkoutseq");
55 seqall = ajAcdGetSeqall("sequence");
56 exclude = ajAcdGetString("exclude");
57
58 notseq_readfile(exclude, &pattern);
59
60 while(ajSeqallNext(seqall, &seq))
61 {
62 ajStrAssignS(&name, ajSeqGetNameS(seq));
63 ajStrAssignS(&acc, ajSeqGetAccS(seq));
64
65 if(embMiscMatchPatternDelimC(name, pattern, ",;") ||
66 embMiscMatchPatternDelimC(acc, pattern, ",;"))
67 ajSeqoutWriteSeq(junkout, seq);
68 else
69 /* no match, so not excluded */
70 ajSeqoutWriteSeq(seqout, seq);
71
72 ajStrSetClear(&name);
73 ajStrSetClear(&acc);
74 }
75
76 ajSeqoutClose(seqout);
77 ajSeqoutClose(junkout);
78
79 ajSeqallDel(&seqall);
80 ajSeqDel(&seq);
81 ajSeqoutDel(&seqout);
82 ajSeqoutDel(&junkout);
83 ajStrDel(&exclude);
84 ajStrDel(&pattern);
85 ajStrDel(&name);
86 ajStrDel(&acc);
87
88 embExit();
89
90 return 0;
91 }
92
93
94
95
96 /* @funcstatic notseq_readfile ************************************************
97 **
98 ** If the list of names starts with a '@', open that file, read in
99 ** the list of names and replaces the input string with the names
100 **
101 ** Else simply copy the exclude list
102 **
103 ** @param [r] exclude [const AjPStr] names to search for or 'file'
104 ** @param [w] pattern [AjPStr*] names to search for or 'file'
105 ** @return [void]
106 ** @@
107 ******************************************************************************/
108
notseq_readfile(const AjPStr exclude,AjPStr * pattern)109 static void notseq_readfile(const AjPStr exclude, AjPStr *pattern)
110 {
111 AjPFile file = NULL;
112 AjPStr line;
113 AjPStr filename = NULL;
114 const char *p = NULL;
115
116 if(ajStrFindC(exclude, "@") != 0)
117 {
118 ajStrAssignS(pattern, exclude);
119 }
120 else
121 {
122 ajStrAssignS(&filename, exclude);
123 ajStrTrimC(&filename, "@"); /* remove the @ */
124 file = ajFileNewInNameS(filename);
125 if(file == NULL)
126 ajFatal("Cannot open the file of sequence names: '%S'", filename);
127
128 /* blank off the file name and replace with the sequence names */
129 ajStrSetClear(pattern);
130 line = ajStrNew();
131 while(ajReadlineTrim(file, &line))
132 {
133 p = ajStrGetPtr(line);
134
135 if(!*p || *p == '#' || *p == '!')
136 continue;
137
138 ajStrAppendS(pattern, line);
139 ajStrAppendC(pattern, ",");
140 }
141 ajStrDel(&line);
142 ajStrDel(&filename);
143
144 ajFileClose(&file);
145 }
146
147 return;
148 }
149