1 /* @source maskseq application
2 **
3 ** Mask off features of a sequence
4 **
5 ** @author Copyright (C) Gary Williams (gwilliam@hgmp.mrc.ac.uk)
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 maskfeat_FeatSeqMask(AjPSeq seq, const AjPStr type,
29 				 const AjPStr maskchar, AjBool cvttolower);
30 
31 static void maskfeat_StrToLower(AjPStr *str, ajint begin, ajint end);
32 
33 
34 
35 
36 /* @prog maskfeat *************************************************************
37 **
38 ** Mask off features of a sequence
39 **
40 ******************************************************************************/
41 
main(int argc,char ** argv)42 int main(int argc, char **argv)
43 {
44 
45     AjPSeqall seqall = NULL;
46     AjPSeq seq = NULL;
47     AjPSeqout seqout = NULL;
48     AjPStr type;
49     AjPStr maskchar;
50     AjBool cvttolower;
51 
52     embInit("maskfeat", argc, argv);
53 
54     seqall   = ajAcdGetSeqall("sequence");
55     seqout   = ajAcdGetSeqout("outseq");
56     type     = ajAcdGetString("type");
57     maskchar = ajAcdGetString("maskchar");
58     cvttolower  = ajAcdGetToggle("tolower");
59 
60     while(ajSeqallNext(seqall, &seq))
61     {
62 	/* mask the regions */
63 	maskfeat_FeatSeqMask(seq, type, maskchar, cvttolower);
64 
65 	ajSeqoutWriteSeq(seqout, seq);
66     }
67 
68     ajSeqoutClose(seqout);
69 
70     ajSeqallDel(&seqall);
71     ajSeqDel(&seq);
72     ajSeqoutDel(&seqout);
73     ajStrDel(&type);
74     ajStrDel(&maskchar);
75 
76     embExit();
77 
78     return 0;
79 }
80 
81 
82 
83 
84 /* @funcstatic maskfeat_FeatSeqMask *******************************************
85 **
86 ** Masks features of a sequence
87 **
88 ** @param [u] seq [AjPSeq] sequence
89 ** @param [r] type [const AjPStr] types of features to mask as
90 **                                wildcarded string
91 ** @param [r] maskchar [const AjPStr] character to mask with
92 ** @param [r] cvttolower [AjBool] if True then 'mask' by changing to lower-case
93 ** @return [void]
94 ** @@
95 ******************************************************************************/
96 
97 
maskfeat_FeatSeqMask(AjPSeq seq,const AjPStr type,const AjPStr maskchar,AjBool cvttolower)98 static void maskfeat_FeatSeqMask(AjPSeq seq, const AjPStr type,
99 				 const AjPStr maskchar, AjBool cvttolower)
100 {
101     AjIList    iter = NULL ;
102     AjPFeature gf   = NULL ;
103     AjPStr str = NULL;
104     const AjPFeattable feat;
105     char whiteSpace[] = " \t\n\r,;|";	/* skip whitespace and , ; | */
106     AjPStrTok tokens;
107     AjPStr key = NULL;
108     AjBool lower;
109 
110 
111     /*
112     ** want lower-case if 'cvttolower' or 'maskchar' is null
113     ** or it is the SPACE character
114     */
115     lower = (cvttolower ||
116 	     ajStrGetLen(maskchar) == 0 ||
117 	     ajStrMatchC(maskchar, " "));
118 
119 
120     /* get the feature table of the sequence */
121     feat = ajSeqGetFeat(seq);
122 
123     ajStrAssignS(&str, ajSeqGetSeqS(seq));
124 
125     /* For all features... */
126 
127     if(feat && ajFeattableGetSize(feat))
128     {
129 	iter = ajListIterNewread(feat->Features) ;
130 	while(!ajListIterDone(iter))
131 	{
132 	    gf = ajListIterGet(iter) ;
133 	    tokens = ajStrTokenNewC(type, whiteSpace);
134 	    while(ajStrTokenNextParse(tokens, &key))
135 		if(ajStrMatchWildS(ajFeatGetType(gf), key))
136 		{
137 		    if(lower)
138 			maskfeat_StrToLower(&str, ajFeatGetStart(gf)-1,
139 					    ajFeatGetEnd(gf)-1);
140 		    else
141 		        ajStrMaskRange(&str, ajFeatGetStart(gf)-1,
142                                        ajFeatGetEnd(gf)-1,
143                                        ajStrGetCharFirst(maskchar));
144 		}
145 
146 	    ajStrTokenDel( &tokens);
147 	    ajStrDel(&key);
148 	}
149 	ajListIterDel(&iter);
150     }
151 
152     ajSeqAssignSeqS(seq, str);
153 
154 
155     ajStrDel(&str);
156     ajStrDel(&key);
157 
158     return;
159 }
160 
161 
162 
163 
164 /* @funcstatic maskfeat_StrToLower *******************************************
165 **
166 ** Lower-case a part of a sequence string
167 **
168 ** @param [u] str [AjPStr *] sequence string
169 ** @param [r] begin [ajint] start position to be masked
170 ** @param [r] end [ajint] end position to be masked
171 ** @return [void]
172 ** @@
173 ******************************************************************************/
174 
maskfeat_StrToLower(AjPStr * str,ajint begin,ajint end)175 static void maskfeat_StrToLower(AjPStr *str, ajint begin, ajint end)
176 {
177 
178     AjPStr substr = ajStrNew();
179 
180     /* extract the region and lowercase */
181     ajStrAppendSubS(&substr, *str, begin, end);
182     ajStrFmtLower(&substr);
183 
184     /* remove and replace the lowercased region */
185     ajStrCutRange(str, begin, end);
186     ajStrInsertS(str, begin, substr);
187 
188     ajStrDel(&substr);
189 
190     return;
191 }
192