Debian Codegraph @

Ext/lib/pwm_searchPFF.c

raw symbols ◀ ▶ SCIP
3 Debian patches touch this file
1 /*--------------------------------------------------------------------
2 * BUGS or limitations
3 * mask option not yet implemented.
4 *
5 * Extensions/revisions worth considering
6 * pwm_calc that calculates pwm scores for every position; pipe to
7 * selection programs that pull what I want.
8 *------------------------------------------------------------------*/
9 /*--------------------------------------------------------------------
10 * This version is a quick and dirty modification of Wyeth Wasserman's
11 * standalone pwm_searchPFF program.
12 *
13 * Boris Lenhard, August 2001
14 *
15 * Read pwm matrix
16 * Figure maximum and minimum possible scores
17 * Read sequences (fasta format) one at a time, and for each:
18 * Window through the sequence and complement
19 *
20 * Find all occurrences of pattern with
21 * matrix score > threshold
22 *
23 * If -a flag is set just print all the values, otherwise:
24 *
25 * If -b flag is not set,
26 * For each find, show seq name, location, find, score
27 * otherwise
28 * just show the best hit for this sequence
29 * If "-m" option is set, write out all input sequences to
30 * filename given, with finds replaced by 'n's.
31 *
32 * Exit: 0 for success, -1 otherwise.
33 *------------------------------------------------------------------*/
34 #include "pwm_search.h"
35
36 /*--------------------------------------------------------------------
37 * Announce
38 *
39 * Print a debugging message
40 *
41 * Returns 0
42 *------------------------------------------------------------------*/
43 int
44 announce(msg)
45 char *msg;
46 {
47 int retval = 0;
48
•49 fprintf(stderr,"%s",msg);
50
51 return(retval);
52 }
53
54 /***********************************************************************
55 * ERR_LOG and ERR_SHOW
56 *
57 * A pair of functions for saving up and then printing error messages.
58 * err_log stores away an error message each time it is called. When
59 * err_show is called it prints all the messages saved up so far.
60 *
61 * Neither function returns a value
62 **********************************************************************/
63 void
64 err_log(msg)
65 char *msg;
66 {
67 if ( __DEBUG__ )
68 announce("+++\nEntering err_log\n+++\n");
69
70 NUM_ERRS++;
71 if ( (__ERR__[NUM_ERRS-1] = (char *) malloc( 1+strlen(msg) ) ) == NULL )
72 __ERR__[NUM_ERRS - 1] = PANIC;
73 else
74 strcpy( __ERR__[NUM_ERRS - 1],msg );
75
76 if ( __DEBUG__ )
77 announce("+++\nLeaving err_log\n+++\n");
78 return;
79 }
80
81 void
82 err_show()
83 {
84 int err_num;
85 for ( err_num=0; err_num<NUM_ERRS; ++err_num )
86 fprintf(stderr,"%s\n",__ERR__[err_num]);
87 return;
88 }
89
•90
91 /*--------------------------------------------------------------------
•92 * SAVE_HIT - save location, strand and score of a hit in an array of such
93 *
•94 * Called by do_seq.
95 *
96 * Returns: 0 for success, -1 for failure.
97 *------------------------------------------------------------------*/
•98 int
99 save_hit(base,strand,score,hits,pnhit)
100 long base;
101 int strand;
102 double score;
103 struct HIT *hits;
104 long *pnhit;
105 {
106 int retval = 0;
107
•108 if ( *pnhit == MAXHITS )
109 {
110 err_log("SAVE_HIT: MAXHITS limit reached.");
111 retval = -1;
112 }
113
•114 hits[*pnhit].base = base;
115 hits[*pnhit].strand = strand;
116 hits[*pnhit].score = score;
117 *pnhit = *pnhit + 1;
118
119 return(retval);
120 }
121 /*--------------------------------------------------------------------
122 * GET_MATRIX - Read in pwm.
123 *
124 * Called by main.
125 *
126 * Returns: 0 for success, -1 for failure.
127 *------------------------------------------------------------------*/
128 int
129 get_matrix(struct arguments* pargs, double* pwm)
130 /* struct arguments *pargs; args from command line
131 double *pwm; array for pwm */
132 /* do own indexing; 5*pos + nt */
133 {
134 double counts[2*MAXCOUNTS];
135 double max_log;
136 double min_log;
137 double scratch[1+MAXCOUNTS];
138 int done = 0;
139 int nt;
140 int num_counts;
141 int pos;
142 int retval=0;
143 FILE *fp; /* stream for counts file */
144
145 if ( __DEBUG__ )
146 announce("+++\nEntering get_matrix\n+++\n");
147
148 /* Open the file */
149 if ( (fp=fopen(pargs->counts_file,"r")) == NULL )
150 {
151 err_log("GET_MATRIX: could not open specified file.");
152 retval = -1;
153 }
154
155 /* Read in the real numbers without regard to dimension */
156 else
157 {
158 for ( num_counts=0; !done && num_counts<MAXCOUNTS; ++num_counts )
159 {
160 if ( fscanf(fp,"%lf,%*c",scratch+num_counts) == EOF )
161 done = 1;
162 }
163 if ( !done )
164 {
165 err_log("GET_MATRIX: too many counts.");
166 retval = -1;
167 }
168 }
169
170 fclose(fp);
171 if ( !retval )
172 {
173
174 /* Put the weights where they belong, and put avg of ACGT for 'n' */
175 pargs->width = num_counts/4;
176 for ( pos=0; pos<pargs->width; ++pos )
177 {
178 for ( nt=0; nt<4; ++nt )
179 {
180 pwm[5*pos + nt] = scratch[(pargs->width)*nt + pos];
181 }
182
183 pwm[5*pos + 4] =
184 (pwm[5*pos + 0] +
185 pwm[5*pos + 1] +
186 pwm[5*pos + 2] +
187 pwm[5*pos + 3]
188 ) / 4;
189 }
190
191
192 /* Next the extreme scores */
193 pargs->max_score = 0;
194 pargs->min_score = 0;
195 for ( pos=0; pos<pargs->width; ++pos )
196 {
197 max_log = -10.0;
198 min_log = 10.0;
199 for ( nt=0; nt<4; ++nt )
200 {
201 max_log = ( max_log>pwm[5*pos+nt] ) ? max_log : pwm[5*pos+nt];
202 min_log = ( min_log<pwm[5*pos+nt] ) ? min_log : pwm[5*pos+nt];
203 }
204 pargs->max_score += max_log;
205 pargs->min_score += min_log;
206 }
207 }
208
209 if ( __DEBUG__ )
210 announce("+++\nLeaving get_matrix\n+++\n");
211
212 return (retval);
213 }
214
215 /*--------------------------------------------------------------------
•216 * GET_CMD_ARGS - Parse execute line, fill arg structure.
217 *
218 * Called by main.
219 *
220 * Returns: 0 for success, -1 for failure.
221 *------------------------------------------------------------------*/
222 int get_cmd_args(argc,argv,pargs)
223 int argc; /* argc as passed to main */
224 char **argv; /* argv as passed to main */
225 struct arguments *pargs; /* args from command line */
226 {
227 int retval = 0;
228 int arg_count = 4;
229
230 if ( __DEBUG__ )
231 announce("+++\nEntering get_cmd_args\n+++\n");
232
233 /* See if we got at least three arguments */
234 if ( argc < 4 )
235 { retval = -1;
236 err_log("GET_CMD_ARGS: Too few arguments.");
237 }
238
239 /* Get the arguments */
240 else
241 { strcpy(pargs->counts_file,argv[1]);
242 strcpy(pargs->seq_file,argv[2]);
243 pargs->threshold = atof(argv[3]);
244 pargs->best_only = 0;
245 pargs->print_all = 0;
246 pargs->mask_file[0] = '\0';
247 while (arg_count < argc)
248 {
249 if ( argv[arg_count][0]=='-' && argv[arg_count][1]=='b' )
250 {
251 pargs->best_only = 1;
252 arg_count++;
253 }
254 else if ( argv[arg_count][0]=='-' && argv[arg_count][1]=='a' )
255 {
256 pargs->print_all = 1;
257 arg_count++;
258 }
259 else if ( arg_count<argc-1 &&
260 argv[arg_count][0]=='-' && argv[arg_count][1]=='m' &&
261 argv[arg_count+1][0]!='\0'
262 )
263 {
264 strcpy(pargs->mask_file,argv[arg_count+1]);
265 arg_count = arg_count+2;
266 }
267 else if ( arg_count<argc-1 &&
268 argv[arg_count][0]=='-' && argv[arg_count][1]=='n' &&
269 argv[arg_count+1][0]!='\0'
270 )
271 {
272 strcpy(pargs->name,argv[arg_count+1]);
273 arg_count = arg_count+2;
274 }
275 else if ( arg_count<argc-1 &&
276 argv[arg_count][0]=='-' && argv[arg_count][1]=='c' &&
277 argv[arg_count+1][0]!='\0'
278 )
279 {
280 strcpy(pargs->class,argv[arg_count+1]);
281 arg_count = arg_count+2;
282 }
283 else
284 {
285 arg_count++;
286 }
287 }
288 }
289 if ( __DEBUG__ )
290 announce("+++\nLeaving get_cmd_args\n+++\n");
291 return( retval );
292 }
293
294 /*--------------------------------------------------------------------
295 * GET_SEQUENCE
296 *
297 * Get the next sequence from the input file (fasta format)
298 *
299 * Called by loop_on_seqs.
300 *
301 * Return 0 normally, -1 on error, 1 if called at EOF.
302 *------------------------------------------------------------------*/
•303 int get_sequence(fp,seq_id,sequence)
304 FILE *fp; /* file to read */
305 char *seq_id; /* name of sequence */
306 char *sequence; /* text of sequence */
307 {
308 char msg[2*MAX_LINE];
309 int c;
310 int done=0;
311 int position;
312 int retval = 0;
313 int word = 0;
314 int count = 0;
315 long base = 0L;
316 char line[MAX_LINE]; // was static
317 int at_eof = 0; // was static
318 int first_time=1; // was static
319
320 if ( __DEBUG__ )
321 {
322 announce("+++\nEntering Get_sequence\n+++\n");
323 }
324
325 if ( first_time )
326 {
327 first_time=0;
328 if ( fgets(line,MAX_LINE,fp)==NULL )
329 {
330 at_eof = 1;
331 }
332 }
333 if ( at_eof ) /* this time or last time */
334 {
335 retval = 1;
336 }
337
338 /* At this point, line should always be the first line of an entry */
339 /* Pull out the id */
340 if ( !retval )
341 {
342 strcpy(seq_id,line+1);
343 seq_id[ strlen(seq_id) -1 ] = '\0';
344 while (count < strlen(seq_id) && !word)
345 {
346 if (seq_id[count] == ' ')
347 {
348 word++;
349 seq_id[count]= '\0';
350 }
351 count++;
352 }
353
354 }
355
356 /* Read in the sequence */
357 while ( !retval && !done )
358 {
359 if ( __DEBUG__ )
360 {
361 announce("+++\nReading in...\n+++\n");
362 }
363
364 if ( fgets(line,MAX_LINE,fp) == NULL )
365 {
366 at_eof = 1;
367 done = 1;
368 }
369 else if ( line[0] == '>' )
370 {
371 done = 1;
372 }
373 else
374 {
375 for ( position=0; !retval && line[position]!='\0'; ++position)
376 {
377 c = line[position];
378 if ( !isdigit( c ) && !isspace( c ) )
379 {
380 if ( base >= SEQLEN )
381 {
382 err_log("GET_SEQUENCE: Sequence too long.");
383 retval = -1;
384 }
385 else
386 {
387 sequence[base++] = c;
388 }
389 }
390 }
391 }
392 }
393 sequence[base] = '\0';
394
395 if ( __DEBUG__ )
396 {
397 announce("+++\nLeaving Get_sequence\n+++\n");
398 sprintf(msg,"seq_id=%s\nlength=%ld\n",
399 seq_id, base
400 );
401 announce(msg);
402 }
403
404 return(retval);
405 }
406
407 /*--------------------------------------------------------------------
•408 * BEST_PULL - Copy back the best score saved
409 *
•410 * Called by do_seq
411 *
•412 * Returns: 0
413 *------------------------------------------------------------------*/
•414 int best_pull(pargs,pbase,pstrand,pscore)
415 struct arguments *pargs; /* args from command line */
•416 long *pbase; /* base where score occurs */
417 int *pstrand; /* strand where score occurs */
418 double *pscore; /* score of hit to pull back */
419 {
•420 *pbase = pargs->best_base;
421 if ( pargs->best_base >= 0 )
422 {
•423 *pscore = pargs->best_score;
424 *pstrand = pargs->best_strand;
425 }
•426 return(0);
427 }
428
•429 /*--------------------------------------------------------------------
430 * BEST_SAVE - Save the best score so far
431 *
432 * Called by do_seq
433 *
434 * Returns: 0
435 *------------------------------------------------------------------*/
436 int best_save(struct arguments* pargs, long base, int strand, double score)
437 //struct arguments *pargs; /* args from command line */
438 //long base; /* base where score occurs */
439 //int strand; /* strand where score occurs */
440 //double score; /* score of hit to save */
441 {
442 if ( pargs->best_base < 0 || score > pargs->best_score )
443 {
444 pargs->best_base = base;
445 pargs->best_score = score;
446 pargs->best_strand = strand;
447 }
448
•449 return(0);
450 }
451
452 /*--------------------------------------------------------------------
453 * MARK - write "width" dashes, to mark strand
454 *
455 * Called by output.
456 *
457 * Returns: 0 for success, -1 for failure.
458 *------------------------------------------------------------------*/
459 int
460 mark(width)
461 int width;
462 {
463 int pos;
464
465 for ( pos=0; pos<width; ++pos )
466 putchar('-');
467 putchar('\n');
468 }
469
470 /*--------------------------------------------------------------------
471 * OUTPUT - Print a find in its context.
472 *
473 * Called by do_seq.
474 *
475 * Returns: 0 for success, -1 for failure.
476 *------------------------------------------------------------------*/
477 int
478 output(pargs,seqid,base,seq,strand,score, outfp)
479 struct arguments *pargs; /* args from command line */
480 char *seqid; /* id of sequence in which pattern found */
481 long base; /* base of pattern nearest base 0 of top strand */
482 char *seq; /* the sequence itself, ascii, top strand */
483 int strand; /* find is on (0) top strand or (1) bottom */
484 double score; /* score of the find */
485 FILE *outfp;
486 {
487 int pos;
488 int retval = 0;
489
490 if ( __DEBUG__ )
491 announce("+++\nEntering output\n+++\n");
492
493 /* if 1
494 # printf("*********************************************\n");
495 printf("%s\tTFBS\t",seqid);
496
497 # printf("Score: %6.3f(%6.1f) in range %6.3f(0.0) to %6.3f(100.0)\n",
498 # score,
499 # 100*(score - pargs->min_score)/(pargs->max_score - pargs->min_score),
500 # pargs->min_score,
501 # pargs->max_score);
502
503 # printf("\n%ld\n",base+1);
504 */
505 fprintf(outfp, "%s\tTFBS\t%s\t%s\t",seqid,pargs->name,pargs->class);
506
507 if (strand)
508 {
509 fprintf(outfp, "-\t"); /* FIXED BY BORIS : 1 is for "-" strand */
510 }
511 else fprintf(outfp, "+\t"); /* FIXED BY BORIS : 0 is for "+" strand */
512
513 fprintf(outfp, "%6.3f\t%6.1f\t", score,
514 100*(score - pargs->min_score)/(pargs->max_score - pargs->min_score));
515
516 fprintf(outfp, "%ld\t%ld\t",base+1,base+pargs->width);
517
518 for ( pos=0; pos<pargs->width; ++pos )
519 {
520 putc(seq[base+pos], outfp);
521 }
522 putc('\n', outfp);
523
524 /* #endif */
525 if ( __DEBUG__ )
526 announce("+++\nLeaving output\n+++\n");
527
528 return( retval );
529 }
530
531 /*--------------------------------------------------------------------
•532 * DO_SEQ - Search through the given sequence with the given matrix
533 *
•534 * Called by loop_on_seqs
535 *
536 * Returns: 0 for success, -1 for failure.
537 *------------------------------------------------------------------*/
538 int
•539 do_seq(pargs,pwm,seqid,seq,outfp)
540 struct arguments *pargs; /* args from command line */
541 double *pwm; /* pwm from get_matrix */
542 char *seqid; /* id of sequence to work on */
543 char *seq; /* the sequence to work on */
544 FILE *outfp;
545 {
•546 double backward_score;
547 double forward_score;
548 double score;
549 long base;
550 int done = 0;
551 int nt;
552 int pos;
553 int retval = 0;
•554 int strand;
555 long l;
556 long nhit=0L;
557 struct HIT hits[MAXHITS];
558
•559 if ( __DEBUG__ )
560 announce("+++\nEntering do_seq.\n+++\n");
561
562 /* first make sure sequence is long enough */
563 for ( base=0; base < pargs->width; ++base )
564 {
•565 if ( seq[base] == '\0' )
566 done = 1;
567 }
568
•569 /* loop on windows */
570 pargs->best_base = -1;
571 for ( base=0; !retval && !done && seq[base+pargs->width-1]; ++base )
572 {
573 forward_score = 0.0;
574 backward_score = 0.0;
575 for ( pos=0; pos<pargs->width; ++pos )
576 {
577 nt = TRANS[seq[base+pos]];
578 forward_score += pwm[5*pos + nt];
579 nt = ( nt==4 ) ? 4 : 3-nt;
580 backward_score += pwm[5*(pargs->width - pos -1) + nt];
581 }
582 if ( forward_score > pargs->threshold )
583 {
584 if ( pargs->print_all )
585 {
586 if ( save_hit(base,0,forward_score,hits,&nhit) )
587 {
588 err_log("DO_SEQ: save_hit failed");
589 retval = -1;
590 }
591 }
592 else if ( pargs->best_only )
593 {
594 best_save(pargs,base,0,forward_score);
595 }
596 else if ( output(pargs,seqid,base,seq,0,forward_score,outfp) )
597 {
598 err_log("DO_SEQ: output failed");
599 retval = -1;
600 }
601 }
602 if ( backward_score > pargs->threshold )
603 {
604 if ( pargs->print_all )
605 {
606 if ( save_hit(base,1,backward_score,hits,&nhit) )
607 {
608 err_log("DO_SEQ: save_hit failed");
609 retval = -1;
610 }
611 }
612 else if ( pargs->best_only )
613 {
614 best_save(pargs,base,1,backward_score);
615 }
616 else if ( output(pargs,seqid,base,seq,1,backward_score, outfp) )
617 {
618 err_log("DO_SEQ: output failed");
619 retval = -1;
620 }
621 }
622
623 }
624
625 if ( pargs->print_all )
626 {
627 for ( l=0; l<nhit; ++l )
628 printf("%ld %.3f\n",1+hits[l].base,hits[l].score);
629 /*
630 printf("# forward strand hits\n\n");
631 for ( l=0; l<nhit; ++l )
632 {
633 if ( hits[l].strand == 0 )
634 printf("%ld %.3f\n",1+hits[l].base,hits[l].score);
635 }
636 printf("\n# comp strand hits\n\n");
637 for ( l=0; l<nhit; ++l )
638 {
639 if ( hits[l].strand == 1 )
640 printf("%ld %.3f\n",1+hits[l].base,hits[l].score);
641 }
642 */
643 }
644
645 else
646 {
647 best_pull(pargs,&base,&strand,&score);
648 if ( base>=0 )
649 {
650 if ( output(pargs,seqid,base,seq,strand,score,outfp) )
651 {
652 err_log("DO_SEQ: output failed");
653 retval = -1;
654 }
655 }
656 }
657
658 if ( __DEBUG__ )
659 announce("+++\nLeaving do_seq.\n+++\n");
660
661 return(retval);
•662
663 }
664
665 /*--------------------------------------------------------------------
666 * LOOP_ON_SEQS - Loop through the sequences of the input file,
667 * doing the search and output.
668 *
669 * Called by main.
670 *
671 * Returns: 0 for success, -1 for failure.
672 *------------------------------------------------------------------*/
673 int
674 loop_on_seqs(pargs,pwm,fp, outfp)
675 struct arguments *pargs; /* args from command line */
676 double *pwm; /* pwm, from get_matrix */
677 FILE *fp; /* sequence file pointer */
678 FILE *outfp; /* output file pointer */
679 {
680 char seq[SEQLEN+1];
681 char seqid[SEQNAMELEN+1];
682 int done = 0;
683 int retval=0;
684 if ( __DEBUG__ )
685 announce("+++\nEntering loop_on_seqs\n+++\n");
686
687 /* Main loop */
688 while ( !retval && !done )
689 {
690 done = get_sequence(fp,seqid,seq);
691 if ( done == -1 )
692 {
693 err_log("LOOP_ON_SEQS: get_sequence failed.");
694 retval = -1;
695 }
696 else if ( done == 0 )
697 {
698 if ( do_seq(pargs,pwm,seqid,seq,outfp) )
699 {
700 err_log("LOOP_ON_SEQS: do_seq failed.");
701 retval = -1;
702 }
703 }
704 }
705
706 if ( __DEBUG__ )
707 announce("+++\nLeaving loop_on_seqs\n+++\n");
708
709 return (retval);
710 }
711
712 int do_search(char* matrixfile,
713 char* seqfile,
714 float threshold,
715 char* tfname,
716 char* tfclass,
717 char* outfile)
718 /*was: main
719 int argc;
720 char **argv;*/
721 {
722 double pwm[2*MAXCOUNTS]; /* for pwm matrix */
723 /* do own indexing; 5*pos + nt */
724 int exitval = -1; /* exit value from main */
725 struct arguments args; /* command line args */
726 FILE *fp; /* for sequence input file */
727 FILE *outfp;
728 NUM_ERRS = 0;
729 if (__DEBUG__) fprintf(stderr, "%s %s %f %s %s %s\n", matrixfile, seqfile, threshold, tfname, tfclass, outfile);
730 if ( __DEBUG__ )
731 announce("+++\nEntering main.\n+++\n");
732
733 /* Parse command line arguments */
734 /*if ( get_cmd_args(argc,argv,&args) )
735 {
736 err_log(
737 "Usage: pwm_searchPFF pwm_file seq_file threshold [-a][-b]|[-m mask_file] [-n TFname] [-c TFclass]\n"
738 );
739 }*/
740
741 strcpy(args.counts_file, matrixfile);
742 strcpy(args.seq_file, seqfile);
743 args.threshold = threshold;
744 strcpy(args.name, tfname);
745 strcpy(args.class, tfclass);
746 args.print_all = 0;
747 args.best_only= 0;
748 /* Read in the pwm; calculate max/min score */
749 //else
750 if ( get_matrix(&args,pwm) )
751 {
752 err_log("MAIN: get_matrix failed.");
753 }
754
755 /* Open the sequence file */
756 else if ( (fp=fopen(args.seq_file,"r")) == NULL )
757 {
758 err_log("MAIN: open_seq_file failed.");
759 }
760 else if ( (outfp=fopen(outfile,"w")) == NULL )
761 {
762 err_log("MAIN: open_outfile failed.");
763 }
764
765 /* Loop on sequences */
766 else if ( loop_on_seqs(&args,pwm,fp,outfp) )
767 {
768 err_log("MAIN: loop_on_seqs failed.");
769 }
770
771 /* Normal completion */
772 else
773 {
774 exitval = 0;
775 }
776
777 /* Clean up and close out */
778 err_show();
779 fclose(fp);
780 fclose(outfp);
781 if ( __DEBUG__ )
782 announce("+++\nLeaving main.\n+++\n");
783
784 return(exitval);
785 }