1 /* @source plotorf application
2 **
3 ** Plot potential open reading frames
4 **
5 ** @author Copyright (C) Alan Bleasby (ableasby@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 #include <string.h>
25 
26 
27 
28 
29 static void plotorf_norfs(const char *seq, const char *rev,
30 			  ajint n, float **x, float **y,
31 			  AjPInt *cnt, ajint beg, AjPStr const *starts,
32 			  ajint nstarts, AjPStr const *stops, ajint nstops);
33 static AjBool plotorf_isin(const char *p, AjPStr const *str, ajint n);
34 
35 
36 
37 
38 
39 /* @prog plotorf **************************************************************
40 **
41 ** Plot potential open reading frames
42 **
43 ******************************************************************************/
44 
main(int argc,char ** argv)45 int main(int argc, char **argv)
46 {
47     AjPSeq seq;
48     AjPStr str;
49     AjPStr rev;
50     AjPStr *starts = NULL;
51     AjPStr *stops  = NULL;
52     AjPStr start;
53     AjPStr stop;
54     ajint nstarts;
55     ajint nstops;
56 
57     AjPGraph graph;
58     AjPGraphdata data;
59 
60     float *x[6] = {NULL,NULL,NULL,NULL,NULL,NULL};
61     float *y[6] = {NULL,NULL,NULL,NULL,NULL,NULL};
62     AjPInt cnt;
63     ajint beg;
64     ajint end;
65 
66     ajint i;
67     ajint j;
68 
69     const char *ftit[6]=
70     {
71 	"F1","F2","F3","R1","R2","R3"
72     };
73 
74 
75     embInit("plotorf", argc, argv);
76     ajGraphicsSetPagesize(960, 960);
77 
78     seq       = ajAcdGetSeq("sequence");
79     graph     = ajAcdGetGraphxy("graph");
80     start     = ajAcdGetString("start");
81     stop      = ajAcdGetString("stop");
82 
83     ajStrFmtUpper(&start);
84     ajStrFmtUpper(&stop);
85 
86     nstarts = ajArrCommaList(start,&starts);
87     nstops  = ajArrCommaList(stop,&stops);
88 
89     beg = ajSeqGetBegin(seq);
90     end = ajSeqGetEnd(seq);
91 
92     str = ajStrNew();
93     cnt = ajIntNew();
94 
95     ajSeqFmtUpper(seq);
96     ajStrAssignSubC(&str,ajSeqGetSeqC(seq),beg-1,end-1);
97 
98     rev = ajStrNewC(ajStrGetPtr(str));
99     ajSeqstrReverse(&rev);
100 
101     for(i=0;i<6;++i)
102     {
103 	plotorf_norfs(ajStrGetPtr(str),ajStrGetPtr(rev),i,x,y,&cnt,beg,starts,
104 		      nstarts,stops,nstops);
105 	data = ajGraphdataNewI(2);
106 	data->numofpoints = 0;
107 
108 
109 	ajGraphDataAdd(graph,data);
110 	ajGraphxySetflagOverlay(graph,ajFalse);
111 	ajGraphxyShowYtick(graph, ajFalse);
112 	ajGraphdataSetTruescale(data,(float)beg,(float)end,0.0,1.0);
113 	ajGraphdataSetTypeC(data,"Multi 2D Plot Small");
114 	ajGraphdataSetYlabelC(data,"Orf");
115 	ajGraphdataSetXlabelC(data,"Sequence");
116 	ajGraphdataSetTitleC(data,ftit[i]);
117 
118 	for(j=0;j<ajIntGet(cnt,i);++j)
119 	    ajGraphdataAddposRect(data,y[i][j],0.0,
120 					      x[i][j],1.0,4,1);
121     }
122 
123 
124     ajGraphShowTitle(graph, ajTrue);
125     ajGraphxySetMinmax(graph,(float)beg,(float)end,0.0,1.0);
126 
127     ajGraphxySetYstartF(graph,0.0);
128     ajGraphxySetYendF(graph,2.0);
129     ajGraphSetTitleC(graph,"Potential codons (rectangles)");
130     ajGraphxyDisplay(graph,ajTrue);
131     ajGraphxyDel(&graph);
132 
133     ajStrDel(&str);
134     ajStrDel(&rev);
135     ajStrDel(&start);
136     ajStrDel(&stop);
137     ajIntDel(&cnt);
138 
139     for(i=0;i<nstarts;++i)
140 	ajStrDel(&starts[i]);
141     AJFREE(starts);
142     for(i=0;i<nstops;++i)
143 	ajStrDel(&stops[i]);
144     AJFREE(stops);
145     for(i=0;i<6;++i)
146     {
147 	AJFREE(x[i]);
148 	AJFREE(y[i]);
149     }
150 
151     ajSeqDel(&seq);
152 
153     embExit();
154 
155     return 0;
156 }
157 
158 
159 
160 
161 /* @funcstatic plotorf_norfs **************************************************
162 **
163 ** Undocumented.
164 **
165 ** @param [r] seq [const char*] nucleic sequence
166 ** @param [r] rev [const char*] reverse sequence
167 ** @param [r] n [ajint] length
168 ** @param [w] x [float**] xpos
169 ** @param [w] y [float**] ypos
170 ** @param [w] cnt [AjPInt*] orf count
171 ** @param [r] beg [ajint] sequence strat
172 ** @param [r] starts [AjPStr const *] start posns
173 ** @param [r] nstarts [ajint] number of starts
174 ** @param [r] stops [AjPStr const *] stop posns
175 ** @param [r] nstops [ajint] number of stops
176 ** @@
177 ******************************************************************************/
178 
plotorf_norfs(const char * seq,const char * rev,ajint n,float ** x,float ** y,AjPInt * cnt,ajint beg,AjPStr const * starts,ajint nstarts,AjPStr const * stops,ajint nstops)179 static void plotorf_norfs(const char *seq, const char *rev,
180 			  ajint n, float **x, float **y,
181 			  AjPInt *cnt, ajint beg, AjPStr const *starts,
182 			  ajint nstarts, AjPStr const *stops, ajint nstops)
183 {
184     ajint len;
185     ajint i;
186     ajint count;
187     AjBool inframe;
188     ajint po;
189     const char *p;
190     size_t stlen;
191 
192 
193     stlen = strlen(seq);
194     len   = (ajint) stlen;
195 
196     if(n<3)
197     {
198 	p  = seq;
199 	po = n%3;
200     }
201     else
202     {
203 	p  = rev;
204 	po = len%3;
205 	po -= n%3;
206 	if(po<0)
207 	    po += 3;
208     }
209 
210 
211     inframe = ajFalse;
212     count = 0;
213 
214     for(i=po;i<len-2;i+=3)
215     {
216 	if(plotorf_isin(&p[i],starts,nstarts))
217 	{
218 	    if(!inframe)
219 	    {
220 		++count;
221 		inframe=ajTrue;
222 		continue;
223 	    }
224 	}
225 
226 	if(plotorf_isin(&p[i],stops,nstops))
227 	    if(inframe)
228 		inframe=ajFalse;
229     }
230 
231     if(count)
232     {
233 	AJCNEW(x[n], count);
234 	AJCNEW(y[n], count);
235     }
236     ajIntPut(cnt,n,count);
237 
238 
239     count = 0;
240     inframe = ajFalse;
241 
242     for(i=po;i<len-2;i+=3)
243     {
244 	if(plotorf_isin(&p[i],starts,nstarts))
245 	    if(!inframe)
246 	    {
247 		if(ajIntGet(*cnt,n))
248 		{
249 		    if(n<3)
250 			x[n][count]=(float)(i+beg);
251 		    else
252 			x[n][count]=(float)((len-i-1)+beg);
253 		}
254 		++count;
255 		inframe = ajTrue;
256 		continue;
257 	    }
258 
259 
260 	if(plotorf_isin(&p[i],stops,nstops))
261 	    if(inframe)
262 	    {
263 		if(ajIntGet(*cnt,n))
264 		{
265 		    if(n<3)
266 			y[n][count-1]=(float)(i+beg);
267 		    else
268 			y[n][count-1]=(float)((len-i-1)+beg);
269 		}
270 		inframe = ajFalse;
271 	    }
272     }
273 
274     if(inframe)
275 	if(ajIntGet(*cnt,n))
276 	{
277 	    if(n<3)
278 		y[n][count-1]=(float)(len+beg-1);
279 	    else
280 		y[n][count-1]=(float) beg;
281 	}
282 
283     return;
284 }
285 
286 
287 
288 
289 /* @funcstatic plotorf_isin ***************************************************
290 **
291 ** True if codon at p occurs in string str
292 **
293 ** @param [r] p [const char*] codon
294 ** @param [r] str [AjPStr const *] sequence
295 ** @param [r] n [ajint] str length
296 ** @return [AjBool] true if found
297 ** @@
298 ******************************************************************************/
299 
plotorf_isin(const char * p,AjPStr const * str,ajint n)300 static AjBool plotorf_isin(const char *p, AjPStr const *str, ajint n)
301 {
302     ajint i;
303     AjBool ret;
304 
305     ret = ajFalse;
306 
307     for(i=0;i<n && !ret;++i)
308 	if(!strncmp(p,ajStrGetPtr(str[i]),3))
309 	    ret = ajTrue;
310 
311     return ret;
312 }
313