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