1 /* @source pscan application
2 **
3 ** Reports fingerprints in a protein sequence
4 ** @author Copyright (C) Alan Bleasby (ableasby@hgmp.mrc.ac.uk)
5 ** @@
6 **
7 ** This program is free software; you can redistribute it and/or
8 ** modify it under the terms of the GNU General Public License
9 ** as published by the Free Software Foundation; either version 2
10 ** of the License, or (at your option) any later version.
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 ******************************************************************************/
21 
22 #include "emboss.h"
23 
24 
25 
26 
27 static void pscan_print_hits(AjPFile outf, AjPList l, ajuint nmotifs,
28 			     ajuint begin);
29 
30 
31 
32 
33 /* @prog pscan ****************************************************************
34 **
35 ** Scans proteins using PRINTS
36 **
37 ******************************************************************************/
38 
main(int argc,char ** argv)39 int main(int argc, char **argv)
40 {
41     AjPSeqall seqall;
42     AjPSeq seq   = NULL;
43     AjPFile outf = NULL;
44     ajint begin;
45     ajint end;
46     ajuint emin;
47     ajuint emax;
48 
49     AjPFile mfile = NULL;
50     EmbPMatPrints s = NULL;
51     AjPList l = NULL;
52     AjPStr strand = NULL;
53     AjPStr substr = NULL;
54     AjPStr name = NULL;
55 
56     AjBool all;
57     AjBool ordered;
58 
59     ajint hits;
60     ajint nmotifs;
61 
62     embInit("pscan", argc, argv);
63 
64     seqall = ajAcdGetSeqall("sequence");
65     outf   = ajAcdGetOutfile("outfile");
66     emin   = ajAcdGetInt("emin");
67     emax   = ajAcdGetInt("emax");
68 
69     substr = ajStrNew();
70     name   = ajStrNew();
71 
72     all = ordered = ajTrue;
73 
74 
75 
76     while(ajSeqallNext(seqall, &seq))
77     {
78 	begin = ajSeqallGetseqBegin(seqall);
79 	end   = ajSeqallGetseqEnd(seqall);
80 
81 	ajStrAssignC(&name,ajSeqGetNameC(seq));
82 	strand = ajSeqGetSeqCopyS(seq);
83 
84 	ajStrAssignSubC(&substr,ajStrGetPtr(strand),begin-1,end-1);
85 
86 	nmotifs = 0;
87 	ajDebug("pscan sequence '%S'\n", ajSeqGetNameS(seq));
88 	embMatPrintsInit(&mfile);
89 	ajDebug("pscan prints file '%F'\n", mfile);
90 	l = ajListNew();
91 	while((s = embMatProtReadInt(mfile)))
92 	{
93 	    ajDebug("pscan file '%F' read\n", mfile);
94 	    if(s->n >= emin && s->n <= emax)
95 	    {
96 		hits=embMatProtScanInt(substr,name,s,&l,&all,&ordered,
97 					    1);
98 		if(hits)
99 		    ++nmotifs;
100 	    }
101 	    embMatProtDelInt(&s);
102 	}
103 
104 	pscan_print_hits(outf, l, nmotifs, begin);
105 	ajListFree(&l);
106 
107 
108 	ajStrDel(&strand);
109 	ajFileClose(&mfile);
110     }
111 
112     ajStrDel(&substr);
113     ajStrDel(&name);
114 
115     ajSeqallDel(&seqall);
116     ajSeqDel(&seq);
117     ajFileClose(&outf);
118 
119     embExit();
120 
121     return 0;
122 }
123 
124 
125 
126 
127 /* @funcstatic pscan_print_hits ***********************************************
128 **
129 ** Undocumented.
130 **
131 ** @param [u] outf [AjPFile] outfile
132 ** @param [u] l [AjPList] hits
133 ** @param [r] nmotifs [ajuint] number of hits
134 ** @param [r] begin [ajuint] start position
135 ** @@
136 ******************************************************************************/
137 
pscan_print_hits(AjPFile outf,AjPList l,ajuint nmotifs,ajuint begin)138 static void pscan_print_hits(AjPFile outf, AjPList l, ajuint nmotifs,
139 			     ajuint begin)
140 {
141     EmbPMatMatch mm;
142     ajuint i;
143     ajuint j;
144     AjBool found;
145     ajuint nleft;
146     ajuint maxelem = 0;
147     ajuint maxhpm;
148     ajuint hpm = 0;
149 
150     nleft = nmotifs;
151 
152 
153     ajFmtPrintF(outf,"\n\nCLASS 1\n");
154     ajFmtPrintF(outf,"Fingerprints with all elements in order\n\n");
155 
156 
157     found = ajTrue;
158 
159     while(found && nleft)
160     {
161 	found = ajFalse;
162 	maxelem = 0;
163 	for(i=0;i<nleft;++i)
164 	{
165 	    ajListPop(l,(void **)&mm);
166 	    if(mm->all && mm->ordered)
167 	    {
168 		maxelem = AJMAX(maxelem,mm->n);
169 		found = ajTrue;
170 	    }
171 	    hpm = mm->hpm;
172 
173 	    ajListPushAppend(l,(void *)mm);
174 	    for(j=1;j<hpm;++j)
175 	    {
176 		ajListPop(l,(void **)&mm);
177 		ajListPushAppend(l,(void *)mm);
178 	    }
179 	}
180 
181 
182 	if(found)
183 	{
184 	    found = ajFalse;
185 	    for(i=0;i<nleft;++i)
186 	    {
187 		ajListPop(l,(void **)&mm);
188 		hpm = mm->hpm;
189 
190 		if(mm->all && mm->ordered && maxelem==mm->n)
191 		    break;
192 		ajListPushAppend(l,(void *)mm);
193 
194 		for(j=1;j<hpm;++j)
195 		{
196 		    ajListPop(l,(void **)&mm);
197 		    ajListPushAppend(l,(void *)mm);
198 		}
199 	    }
200 
201 	    if(mm->all && mm->ordered && maxelem==mm->n)
202 	    {
203 		ajFmtPrintF(outf,"Fingerprint %s Elements %d\n",
204 			    ajStrGetPtr(mm->cod),mm->n);
205 		ajFmtPrintF(outf,"    Accession number %s\n",
206 			    ajStrGetPtr(mm->acc));
207 		ajFmtPrintF(outf,"    %s\n",ajStrGetPtr(mm->tit));
208 		ajFmtPrintF(outf,"  Element %d Threshold %d%% Score %d%%\n",
209 			    mm->element+1, mm->thresh, mm->score);
210 		ajFmtPrintF(outf,"             Start position %d Length %d\n",
211 			    mm->start+begin,mm->len);
212 
213 		embMatMatchDel(&mm);
214 	    }
215 
216 	    for(i=1;i<hpm;++i)
217 	    {
218 		ajListPop(l,(void **)&mm);
219 		ajFmtPrintF(outf,"  Element %d Threshold %d%% Score %d%%\n",
220 			    mm->element+1, mm->thresh, mm->score);
221 		ajFmtPrintF(outf,"             Start position %d Length %d\n",
222 			    mm->start+begin,mm->len);
223 
224 		embMatMatchDel(&mm);
225 	    }
226 
227 	    --nleft;
228 	}
229 
230     }
231 
232     ajFmtPrintF(outf,"\n\nCLASS 2\n");
233     ajFmtPrintF(outf,"All elements match but not all in the "
234 		"correct order\n\n");
235 
236     found = ajTrue;
237 
238     while(found && nleft)
239     {
240 	found = ajFalse;
241 	maxelem = 0;
242 
243 	for(i=0;i<nleft;++i)
244 	{
245 	    ajListPop(l,(void **)&mm);
246 	    if(mm->all)
247 	    {
248 		maxelem = AJMAX(maxelem,mm->n);
249 		found = ajTrue;
250 	    }
251 	    hpm = mm->hpm;
252 	    ajListPushAppend(l,(void *)mm);
253 
254 	    for(j=1;j<hpm;++j)
255 	    {
256 		ajListPop(l,(void **)&mm);
257 		ajListPushAppend(l,(void *)mm);
258 	    }
259 	}
260 
261 	if(found)
262 	{
263 
264 	    found=ajFalse;
265 	    for(i=0;i<nleft;++i)
266 	    {
267 		ajListPop(l,(void **)&mm);
268 		hpm = mm->hpm;
269 		if(mm->all && maxelem==mm->n)
270 		    break;
271 		ajListPushAppend(l,(void *)mm);
272 
273 		for(j=1;j<hpm;++j)
274 		{
275 		    ajListPop(l,(void **)&mm);
276 		    ajListPushAppend(l,(void *)mm);
277 		}
278 	    }
279 
280 	    if(mm->all && maxelem==mm->n)
281 	    {
282 		ajFmtPrintF(outf,"Fingerprint %s Elements %d\n",
283 			    ajStrGetPtr(mm->cod),mm->n);
284 		ajFmtPrintF(outf,"    Accession number %s\n",
285 			    ajStrGetPtr(mm->acc));
286 		ajFmtPrintF(outf,"    %s\n",ajStrGetPtr(mm->tit));
287 		ajFmtPrintF(outf,"  Element %d Threshold %d%% Score %d%%\n",
288 			    mm->element+1, mm->thresh, mm->score);
289 		ajFmtPrintF(outf,"             Start position %d Length %d\n",
290 			    mm->start+begin,mm->len);
291 
292 		embMatMatchDel(&mm);
293 	    }
294 
295 	    for(i=1;i<hpm;++i)
296 	    {
297 		ajListPop(l,(void **)&mm);
298 		ajFmtPrintF(outf,"  Element %d Threshold %d%% Score %d%%\n",
299 			    mm->element+1, mm->thresh, mm->score);
300 		ajFmtPrintF(outf,"             Start position %d Length %d\n",
301 			    mm->start+begin,mm->len);
302 
303 		embMatMatchDel(&mm);
304 	    }
305 
306 	    --nleft;
307 	}
308 
309     }
310 
311 
312 
313 
314     ajFmtPrintF(outf,"\n\nCLASS 3\n");
315     ajFmtPrintF(outf,
316 		"Not all elements match but those that do are in order\n\n");
317 
318     found = ajTrue;
319 
320     while(found && nleft)
321     {
322 	found  = ajFalse;
323 	maxhpm = 0;
324 	for(i=0;i<nleft;++i)
325 	{
326 	    ajListPop(l,(void **)&mm);
327 	    if(mm->ordered)
328 	    {
329 		maxhpm = AJMAX(maxelem,mm->hpm);
330 		found  = ajTrue;
331 	    }
332 	    hpm = mm->hpm;
333 	    ajListPushAppend(l,(void *)mm);
334 
335 	    for(j=1;j<hpm;++j)
336 	    {
337 		ajListPop(l,(void **)&mm);
338 		ajListPushAppend(l,(void *)mm);
339 	    }
340 	}
341 
342 
343 	if(found)
344 	{
345 	    found = ajFalse;
346 	    for(i=0;i<nleft;++i)
347 	    {
348 		ajListPop(l,(void **)&mm);
349 		hpm = mm->hpm;
350 
351 		if(mm->ordered && maxhpm==mm->hpm)
352 		    break;
353 		ajListPushAppend(l,(void *)mm);
354 
355 		for(j=1;j<hpm;++j)
356 		{
357 		    ajListPop(l,(void **)&mm);
358 		    ajListPushAppend(l,(void *)mm);
359 		}
360 	    }
361 
362 	    if(mm->ordered && maxhpm==mm->hpm)
363 	    {
364 		ajFmtPrintF(outf,"Fingerprint %s Elements %d\n",
365 			    ajStrGetPtr(mm->cod),mm->n);
366 		ajFmtPrintF(outf,"    Accession number %s\n",
367 			    ajStrGetPtr(mm->acc));
368 		ajFmtPrintF(outf,"    %s\n",ajStrGetPtr(mm->tit));
369 		ajFmtPrintF(outf,"  Element %d Threshold %d%% Score %d%%\n",
370 			    mm->element+1, mm->thresh, mm->score);
371 		ajFmtPrintF(outf,"             Start position %d Length %d\n",
372 			    mm->start+begin,mm->len);
373 
374 
375 		embMatMatchDel(&mm);
376 	    }
377 
378 	    for(i=1;i<hpm;++i)
379 	    {
380 		ajListPop(l,(void **)&mm);
381 		ajFmtPrintF(outf,"  Element %d Threshold %d%% Score %d%%\n",
382 			    mm->element+1, mm->thresh, mm->score);
383 		ajFmtPrintF(outf,"             Start position %d Length %d\n",
384 			    mm->start+begin,mm->len);
385 
386 		embMatMatchDel(&mm);
387 	    }
388 
389 	    --nleft;
390 	}
391 
392     }
393 
394 
395 
396 
397     ajFmtPrintF(outf,"\n\nCLASS 4\n");
398     ajFmtPrintF(outf,
399 		"Remaining partial matches\n\n");
400 
401     found = ajTrue;
402 
403     while(found && nleft)
404     {
405 	found = ajFalse;
406 	maxhpm = 0;
407 
408 	for(i=0;i<nleft;++i)
409 	{
410 	    ajListPop(l,(void **)&mm);
411 	    maxhpm = AJMAX(maxelem,mm->hpm);
412 	    found = ajTrue;
413 
414 	    hpm = mm->hpm;
415 	    ajListPushAppend(l,(void *)mm);
416 	    for(j=1;j<hpm;++j)
417 	    {
418 		ajListPop(l,(void **)&mm);
419 		ajListPushAppend(l,(void *)mm);
420 	    }
421 	}
422 
423 
424 	if(found)
425 	{
426 	    found = ajFalse;
427 	    for(i=0;i<nleft;++i)
428 	    {
429 		ajListPop(l,(void **)&mm);
430 		hpm = mm->hpm;
431 
432 		if(maxhpm==mm->hpm)
433 		    break;
434 		ajListPushAppend(l,(void *)mm);
435 
436 		for(j=1;j<hpm;++j)
437 		{
438 		    ajListPop(l,(void **)&mm);
439 		    ajListPushAppend(l,(void *)mm);
440 		}
441 	    }
442 
443 	    if(maxhpm==mm->hpm)
444 	    {
445 		ajFmtPrintF(outf,"Fingerprint %s Elements %d\n",
446 			    ajStrGetPtr(mm->cod),mm->n);
447 		ajFmtPrintF(outf,"    Accession number %s\n",
448 			    ajStrGetPtr(mm->acc));
449 		ajFmtPrintF(outf,"    %s\n",ajStrGetPtr(mm->tit));
450 		ajFmtPrintF(outf,"  Element %d Threshold %d%% Score %d%%\n",
451 			    mm->element+1, mm->thresh, mm->score);
452 		ajFmtPrintF(outf,"             Start position %d Length %d\n",
453 			    mm->start+begin,mm->len);
454 
455 
456 		embMatMatchDel(&mm);
457 	    }
458 
459 	    for(i=1;i<hpm;++i)
460 	    {
461 		ajListPop(l,(void **)&mm);
462 		ajFmtPrintF(outf,"  Element %d Threshold %d%% Score %d%%\n",
463 			    mm->element+1, mm->thresh, mm->score);
464 		ajFmtPrintF(outf,"             Start position %d Length %d\n",
465 			    mm->start+begin,mm->len);
466 
467 		embMatMatchDel(&mm);
468 	    }
469 
470 	    --nleft;
471 	}
472 
473     }
474 
475 
476     while(ajListPop(l,(void **)&mm))
477 	if(mm)
478 	    embMatMatchDel(&mm);
479 
480     return;
481 }
482