Debian Codegraph @

modhmm0.92b/hmmsearch.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <math.h>
5 #include <limits.h>
6
7 #include "structs.h"
8 #include "funcs.h"
9 #include "cmdline_hmmsearch.h"
10
11
12 #define NORM_LOG_LIKE 0
13 #define LOGODDS 1
14
15 #define HMM 20
16 #define SEQ 21
17
18 #define LONGEST_SEQ -1 /* Note: this constant is also defined in read_seqs.c */
19 #define FIRST_SEQ 1
20
21 #define LEAD_SEQ 10
22 #define ALL_SEQS 11
23
24 #define MAX_LINE 500
25
26 #define HMMFILEERROR -10
27 #define REPFILEERROR -11
28 #define SEQERROR -12
29 #define PATHERROR -13
30
31 extern int verbose;
32
33
34 static struct hmm_multi_s hmm;
35 static struct hmm_multi_s retrain_hmm;
36 static struct msa_sequences_multi_s *msa_seq_infop;
37 static struct sequences_multi_s seq_info;
38 static struct replacement_letter_multi_s replacement_letters;
39 static struct null_model_multi_s null_model;
40 double *subst_mtxp;
41 double *subst_mtxp_2;
42 double *subst_mtxp_3;
43 double *subst_mtxp_4;
44 double *aa_freqs;
45 double *aa_freqs_2;
46 double *aa_freqs_3;
47 double *aa_freqs_4;
48
49
50 void get_null_model_multi(FILE *nullfile);
51 double get_nullmodel_score_multi(struct letter_s *seq, struct letter_s *seq_2, struct letter_s *seq_3,
52 struct letter_s *seq_4, int seq_len, int multi_scoring_method);
53 double get_nullmodel_score_msa_multi(int seq_len, int prf_mode, int use_prior,
54 int use_gap_shares, int normalize,
55 int scoring_method, int multi_scoring_method, double *aa_freqs,
56 double *aa_freqs_2, double *aa_freqs_3, double *aa_freqs_4);
57 int get_scores(int use_labels, int scoring_method, int multi_scoring_method, helix_sites* hSites);
58 void get_post_prob_matrix(double **ret_post_prob_matrixp, double forw_score, struct forward_s *forw_mtx,
59 struct backward_s *backw_mtx, int seq_len);
60
61 helix_sites* get_helices(char* seq, char* hmmfilename, char* repfilename, char* path)
62 {
63
64 /* command line options */
65 helix_sites *hSites;
66 FILE *hmmfile; /* file to read hmms from */
67 FILE *seqnamefile; /* file for reading sequence names */
68 FILE *replfile; /* file for reading special replacement letters */
69 FILE *freqfile;
70 int use_labels;
71 int scoring_method;
72 int multi_scoring_method;
73 int helices;
74
75 /* temporary variables */
76 int i,j; /*standard loop indices */
77
78 /*init some variables */
79 hmmfile = NULL;
80 seqnamefile = NULL;
81 replfile = NULL;
82 freqfile = NULL;
83 use_labels = NO;
84 scoring_method = DOT_PRODUCT;
85 multi_scoring_method = JOINT_PROB;
86 subst_mtxp = NULL;
87 subst_mtxp_2 = NULL;
88 subst_mtxp_3 = NULL;
89 subst_mtxp_4 = NULL;
90 aa_freqs = NULL;
91 aa_freqs_2 = NULL;
92 aa_freqs_3 = NULL;
93 aa_freqs_4 = NULL;
94
95
96 /* Create the structure to return the helix sites in */
97 hSites = (helix_sites *) malloc_or_die(sizeof (helix_sites));
98 hSites->helix_count = 0;
99 hSites->helix = NULL;
100
101 /* compulsory options */
102 if(hmmfilename) {
103 if((hmmfile = fopen(hmmfilename, "r")) == NULL) {
104 hSites->helix_count = HMMFILEERROR;
105 return hSites;
106 }
107 } else {
108 hSites->helix_count = HMMFILEERROR;
109 return hSites;
110 }
111
112 if(!path) {
113 hSites->helix_count = PATHERROR;
114 return hSites;
115 }
116
117 if(!seq) {
118 hSites->helix_count = SEQERROR;
119 return hSites;
120 }
121
122 if(repfilename) {
123 if((replfile = fopen(repfilename, "r")) == NULL) {
124 hSites->helix_count = REPFILEERROR;
125 return hSites;
126 }
127 } else {
128 hSites->helix_count = REPFILEERROR;
129 return hSites;
130 }
131
132
133 /* get replacement letters */
134 get_replacement_letters_multi(replfile, &replacement_letters);
135
136 /* read null model - always null, don't bother */
137 null_model.a_size = -1;
138 null_model.nr_alphabets = -1;
139
140
141 /* get hmm and score all seqs against them */
142 /* get hmm from file */
143 readhmm(hmmfile, &hmm, path);
144
145 hmm.subst_mtx = subst_mtxp;
146 hmm.subst_mtx_2 = subst_mtxp_2;
147 hmm.subst_mtx_3 = subst_mtxp_3;
148 hmm.subst_mtx_4 = subst_mtxp_4;
149 hmm.replacement_letters = &replacement_letters;
150
151
152 /* check sequence file for labels + check nolabel flag */
153 use_labels = NO;
154
155 /* allocate space for msa_seq_info structs */
156 seq_info.seqs = malloc_or_die(sizeof(struct sequence_multi_s) * 1);
157 seq_info.nr_alphabets = hmm.nr_alphabets;
158 seq_info.nr_seqs = 1;
159 seq_info.longest_seq = 0;
160 seq_info.shortest_seq = INT_MAX;
161 seq_info.avg_seq_len = 0;
162
163
164 get_sequence_fasta_multi(seq, &seq_info, 0);
165 hmm.alphabet_type = DISCRETE;
166
167 seq_info.avg_seq_len = ((int)(seq_info.avg_seq_len / seq_info.nr_seqs));
168
169
170 /* get score info for this seq */
171
172 helices = get_scores(use_labels, scoring_method, multi_scoring_method, hSites);
173
174 /* deallocate seqinfo */
175 free(((seq_info.seqs))->seq_1);
176
177 free(msa_seq_infop);
178
179
180 /* deallocate hmm_info */
181 hmm_garbage_collection_multi(hmmfile, &hmm);
182
183
184 /* deallocate replacement letters */
185 if(replfile != NULL) {
186 if(replacement_letters.nr_rl_1 > 0) {
187 free(replacement_letters.letters_1);
188 free(replacement_letters.probs_1);
189 }
190 fclose(replfile);
191
192 }
193
194 return hSites;
195
196 }
197
198
199 int find_helices(char* seq, char* hmmfilename, char* repfilename, char* path)
200 {
201 /* command line options */
202 FILE *hmmfile; /* file to read hmms from */
203 FILE *seqnamefile; /* file for reading sequence names */
204 FILE *replfile; /* file for reading special replacement letters */
205 FILE *freqfile;
206 int use_labels;
207 int scoring_method;
208 int multi_scoring_method;
209 int helices;
210
211 /* temporary variables */
212 int i,j; /*standard loop indices */
213
214 /*init some variables */
215 hmmfile = NULL;
216 seqnamefile = NULL;
217 replfile = NULL;
218 freqfile = NULL;
219 use_labels = NO;
220 scoring_method = DOT_PRODUCT;
221 multi_scoring_method = JOINT_PROB;
222 subst_mtxp = NULL;
223 subst_mtxp_2 = NULL;
224 subst_mtxp_3 = NULL;
225 subst_mtxp_4 = NULL;
226 aa_freqs = NULL;
227 aa_freqs_2 = NULL;
228 aa_freqs_3 = NULL;
229 aa_freqs_4 = NULL;
230
231
232 /* compulsory options */
233 if(hmmfilename) {
234 if((hmmfile = fopen(hmmfilename, "r")) == NULL) {
235 return HMMFILEERROR;
236 }
237 } else {
238 return HMMFILEERROR;
239 }
240
241 if(!path) {
242 return PATHERROR;
243 }
244
245 if(!seq) {
246 return SEQERROR;
247 }
248
249 if(repfilename) {
250 if((replfile = fopen(repfilename, "r")) == NULL) {
251 return REPFILEERROR;
252 }
253 } else {
254 return REPFILEERROR;
255 }
256
257
258 /* get replacement letters */
259 get_replacement_letters_multi(replfile, &replacement_letters);
260
261 /* read null model - always null, don't bother */
262 null_model.a_size = -1;
263 null_model.nr_alphabets = -1;
264
265
266 /* get hmm and score all seqs against them */
267 /* get hmm from file */
268 readhmm(hmmfile, &hmm, path);
269
270 hmm.subst_mtx = subst_mtxp;
271 hmm.subst_mtx_2 = subst_mtxp_2;
272 hmm.subst_mtx_3 = subst_mtxp_3;
273 hmm.subst_mtx_4 = subst_mtxp_4;
274 hmm.replacement_letters = &replacement_letters;
275
276
277 /* check sequence file for labels + check nolabel flag */
278 use_labels = NO;
279
280 /* allocate space for msa_seq_info structs */
281 seq_info.seqs = malloc_or_die(sizeof(struct sequence_multi_s) * 1);
282 seq_info.nr_alphabets = hmm.nr_alphabets;
283 seq_info.nr_seqs = 1;
284 seq_info.longest_seq = 0;
285 seq_info.shortest_seq = INT_MAX;
286 seq_info.avg_seq_len = 0;
287
288
289 get_sequence_fasta_multi(seq, &seq_info, 0);
290 hmm.alphabet_type = DISCRETE;
291
292 seq_info.avg_seq_len = ((int)(seq_info.avg_seq_len / seq_info.nr_seqs));
293
294
295 /* get score info for this seq */
296
297 helices = get_scores(use_labels, scoring_method, multi_scoring_method, NULL);
298
299 /* deallocate seqinfo */
300 free(((seq_info.seqs))->seq_1);
301
302 free(msa_seq_infop);
303
304
305 /* deallocate hmm_info */
306 hmm_garbage_collection_multi(hmmfile, &hmm);
307
308
309 /* deallocate replacement letters */
310 if(replfile != NULL) {
311 if(replacement_letters.nr_rl_1 > 0) {
312 free(replacement_letters.letters_1);
313 free(replacement_letters.probs_1);
314 }
315 fclose(replfile);
316 }
317
318 return helices;
319 }
320
321
322
323
324
325 /*************************************************************************************************/
326 void get_null_model_multi(FILE *nullfile)
327 {
328 int i,j;
329 char s[MAX_LINE];
330
331 if(nullfile != NULL) {
332 /* read nullfile */
333 while(1) {
334 if(fgets(s, MAX_LINE, nullfile) == NULL) {
335 printf("Could not read null model file\n");
336 exit(0);
337 }
338 if(s[0] == '#' || s[0] == '\n') {
339 continue;
340 }
341 else {
342 null_model.nr_alphabets = atoi(s);
343 break;
344 }
345 }
346
347 for(i = 0; i < null_model.nr_alphabets; i++) {
348 while(1) {
349 if(fgets(s, MAX_LINE, nullfile) == NULL) {
350 printf("Could not read null model file\n");
351 exit(0);
352 }
353 if(s[0] == '#' || s[0] == '\n') {
354 continue;
355 }
356 else {
357 switch(i) {
358 case 0:
359 null_model.a_size = atoi(s);
360 null_model.emissions = (double*)malloc_or_die(null_model.a_size * sizeof(double));
361 break;
362 case 1:
363 null_model.a_size_2 = atoi(s);
364 null_model.emissions_2 = (double*)malloc_or_die(null_model.a_size_2 * sizeof(double));
365 break;
366 case 2:
367 null_model.a_size_3 = atoi(s);
368 null_model.emissions_3 = (double*)malloc_or_die(null_model.a_size_3 * sizeof(double));
369 break;
370 case 3:
371 null_model.a_size_4 = atoi(s);
372 null_model.emissions_4 = (double*)malloc_or_die(null_model.a_size_4 * sizeof(double));
373 break;
374 }
375 break;
376 }
377 }
378 j = 0;
379 switch(i) {
380 case 0:
381 while(j < null_model.a_size) {
382 if (fgets(s, MAX_LINE, nullfile) != NULL) {
383 if(s[0] != '#' && s[0] != '\n') {
384 *(null_model.emissions + j) = atof(s);
385 j++;
386 }
387 }
388 else {
389 printf("Could not read null model file\n");
390 exit(0);
391 }
392 }
393 break;
394 case 1:
395 while(j < null_model.a_size_2) {
396 if (fgets(s, MAX_LINE, nullfile) != NULL) {
397 if(s[0] != '#' && s[0] != '\n') {
398 *(null_model.emissions_2 + j) = atof(s);
399 j++;
400 }
401 }
402 else {
403 printf("Could not read null model file\n");
404 exit(0);
405 }
406 }
407 break;
408 case 2:
409 while(j < null_model.a_size_3) {
410 if (fgets(s, MAX_LINE, nullfile) != NULL) {
411 if(s[0] != '#' && s[0] != '\n') {
412 *(null_model.emissions_3 + j) = atof(s);
413 j++;
414 }
415 }
416 else {
417 printf("Could not read null model file\n");
418 exit(0);
419 }
420 }
421 break;
422 case 3:
423 while(j < null_model.a_size_4) {
424 if (fgets(s, MAX_LINE, nullfile) != NULL) {
425 if(s[0] != '#' && s[0] != '\n') {
426 *(null_model.emissions_4 + j) = atof(s);
427 j++;
428 }
429 }
430 else {
431 printf("Could not read null model file\n");
432 exit(0);
433 }
434 }
435 break;
436 }
437 }
438 while(1) {
439 if(fgets(s, MAX_LINE, nullfile) != NULL) {
440 if(s[0] != '#' && s[0] != '\n') {
441 null_model.trans_prob = atof(s);
442 break;
443 }
444 }
445 else {
446 printf("Could not read null model file\n");
447 exit(0);
448 }
449 }
450 }
451 else {
452 null_model.a_size = -1;
453 null_model.nr_alphabets = -1;
454 }
455 }
456
457
458
459 /*********************score methods******************************************/
460 int get_scores(int use_labels, int scoring_method, int multi_scoring_method, helix_sites* hSites)
461 {
462 struct forward_s *forw_mtx;
463 struct one_best_s *one_best_mtx;
464 struct backward_s *backw_mtx;
465 struct forward_s *rev_forw_mtx;
466 double *forw_scale, *rev_forw_scale, *scaling_factors;
467 char *labels;
468 struct letter_s *reverse_seq, *reverse_seq_2, *reverse_seq_3, *reverse_seq_4;
469 struct helix_site *helixSite;
470
471 int seq_len;
472 double forward_score, backward_score, rev_forward_score, raw_forward_score;
473
474 int a,b;
475 int i,j,k;
476
477 struct hmm_multi_s *hmmp;
478 int IN_HELIX;
479 int helices;
480
481 hmmp = &hmm;
482
483
484 /* get seq_length */
485 seq_len = get_seq_length(seq_info.seqs->seq_1);
486
487
488 /* if needed run forward */
489 forward_multi(hmmp, seq_info.seqs->seq_1, seq_info.seqs->seq_2, seq_info.seqs->seq_3, seq_info.seqs->seq_4,
490 &forw_mtx, &forw_scale, use_labels, multi_scoring_method);
491 raw_forward_score = (forw_mtx + get_mtx_index(seq_len+1, hmmp->nr_v-1, hmmp->nr_v))->prob;
492 forward_score = log10((forw_mtx + get_mtx_index(seq_len+1, hmmp->nr_v-1, hmmp->nr_v))->prob);
493 for (j = seq_len; j > 0; j--) {
494 forward_score = forward_score + log10(*(forw_scale + j));
495 }
496
497 /* if needed run backward */
498 backward_multi(hmmp, seq_info.seqs->seq_1, seq_info.seqs->seq_2, seq_info.seqs->seq_3, seq_info.seqs->seq_4,
499 &backw_mtx, forw_scale, use_labels, multi_scoring_method);
500 backward_score = log10((backw_mtx + get_mtx_index(0, 0, hmmp->nr_v))->prob);
501 for (j = seq_len; j > 0; j--) {
502 backward_score = backward_score + log10(*(forw_scale + j));
503 }
504
505
506 /* if needed run n-best */
507 labels = (char*)malloc_or_die((seq_len + 1) * sizeof(char));
508 one_best_multi(hmmp, seq_info.seqs->seq_1, seq_info.seqs->seq_2, seq_info.seqs->seq_3, seq_info.seqs->seq_4,
509 &one_best_mtx, &scaling_factors, use_labels, labels, multi_scoring_method);
510
511
512 /* count helices + deallocate */
513 j = 0;
514 helices = 0;
515 IN_HELIX = NO;
516 while(1) {
517 if(labels[j] == '\0') {
518 break;
519 }
520 else {
521 if((labels[j] == 'M') && (IN_HELIX == NO)) {
522 IN_HELIX = YES;
523 helices++;
524 } else if((labels[j] != 'M') && (IN_HELIX == YES)) {
525 IN_HELIX = NO;
526 }
527 j++;
528 }
529 }
530
531 /* Check if we care where the helices are, if so, loop
532 through and record their locations */
533
534 if(hSites) {
535 hSites->helix = (helix_sites *) malloc(sizeof (helix_sites) * helices);
536 helixSite = hSites->helix;
537
538 hSites->helix_count = helices;
539
540 j = 0;
541 helices = 0;
542 IN_HELIX = NO;
543 while(1) {
544 if(labels[j] == '\0') {
545 break;
546 }
547 else {
548 if((labels[j] == 'M') && (IN_HELIX == NO)) {
549 IN_HELIX = YES;
550 helixSite->start = (j+1);
551 helices++;
552 } else if((labels[j] != 'M') && (IN_HELIX == YES)) {
553 IN_HELIX = NO;
554 helixSite->end = (j);
555 helixSite++;
556 }
557 j++;
558 }
559 }
560
561 }
562
563 /* deallocate */
564 free(labels);
565
566 /* deallocate result matrix info */
567 free(forw_mtx);
568 free(forw_scale);
569 free(backw_mtx);
570 free(one_best_mtx);
571 free(scaling_factors);
572
573 return helices;
574
575 }
576
577
578 /*********************help functions***************************************/
579 double get_nullmodel_score_multi(struct letter_s *seq, struct letter_s *seq_2, struct letter_s *seq_3,
580 struct letter_s *seq_4, int seq_len, int multi_scoring_method)
581 {
582 int avg_seq_len;
583 double emiss_prob, emiss_prob_2, emiss_prob_3, emiss_prob_4;
584 double e1, e2, e3, e4;
585 double trans_prob;
586 double null_model_score;
587 int letter, letter_2, letter_3, letter_4;
588 int l,i;
589 double t_res;
590
591 /* calculate null model score for seq */
592 if(null_model.nr_alphabets < 0) {
593 /* use default null model */
594 emiss_prob = 1.0 / (double)hmm.a_size;
595 if(hmm.nr_alphabets > 1) {
596 emiss_prob_2 = 1.0 / (double)hmm.a_size_2;
597 }
598 if(hmm.nr_alphabets > 2) {
599 emiss_prob_3 = 1.0 / (double)hmm.a_size_3;
600 }
601 if(hmm.nr_alphabets > 3) {
602 emiss_prob_4 = 1.0 / (double)hmm.a_size_4;
603 }
604 trans_prob = (double)(seq_len)/(double)(seq_len + 1);
605 if(multi_scoring_method == JOINT_PROB) {
606 if(hmm.nr_alphabets == 1) {
607 null_model_score = seq_len * (log10(emiss_prob) + log10(trans_prob));
608 }
609 else if(hmm.nr_alphabets == 2) {
610 null_model_score = seq_len * (log10(emiss_prob) + log10(emiss_prob_2) + log10(trans_prob));
611 }
612 else if(hmm.nr_alphabets == 3) {
613 null_model_score = seq_len * (log10(emiss_prob) + log10(emiss_prob_2) + log10(emiss_prob_3) + log10(trans_prob));
614 }
615 else if(hmm.nr_alphabets == 2) {
616 null_model_score = seq_len * (log10(emiss_prob) + log10(emiss_prob_2) + log10(emiss_prob_3) + log10(emiss_prob_4)
617 + log10(trans_prob));
618 }
619 }
620 else {
621 /* use other multialpha scoring method, not implemented yet */
622 printf("Error: only JOINT_PROB scoring is implemented\n");
623 exit(0);
624 }
625 }
626 else {
627 /* use specified null model */
628 null_model_score = 0.0;
629 for(l = 0; l < seq_len; l++) {
630 letter = get_alphabet_index(&seq[l], hmm.alphabet, hmm.a_size);
631 if(hmm.nr_alphabets > 1) {
632 letter_2 = get_alphabet_index(&seq_2[l], hmm.alphabet_2, hmm.a_size_2);
633 }
634 if(hmm.nr_alphabets > 2) {
635 letter_3 = get_alphabet_index(&seq_3[l], hmm.alphabet_3, hmm.a_size_3);
636 }
637 if(hmm.nr_alphabets > 3) {
638 letter_4 = get_alphabet_index(&seq_4[l], hmm.alphabet_4, hmm.a_size_4);
639 }
640
641 if(letter >= 0) {
642 e1 = null_model.emissions[letter];
643 //null_model_score += log10(null_model.emissions[letter]) + log10(null_model.trans_prob);
644 }
645 else {
646 /* need replacement letters */
647 letter = get_replacement_letter_index_multi(&seq[l], &replacement_letters, 1);
648 if(letter >= 0) {
649 e1 = 0.0;
650 for(i = 0; i < hmm.a_size; i++) {
651 e1 += *(hmm.replacement_letters->probs_1 + get_mtx_index(letter, i, hmm.a_size)) * null_model.emissions[i];
652 }
653 //null_model_score += log10(t_res) + log10(null_model.trans_prob);
654 }
655 else {
656 printf("Could not find letter %s when scoring null model\n", &seq[l]);
657 return DEFAULT;
658 }
659 }
660 if(hmm.nr_alphabets > 1) {
661 if(letter_2 >= 0) {
662 e2 = null_model.emissions_2[letter_2];
663 }
664 else {
665 /* need replacement letters */
666 letter_2 = get_replacement_letter_index_multi(&seq_2[l], &replacement_letters, 2);
667 if(letter_2 >= 0) {
668 e2 = 0.0;
669 for(i = 0; i < hmm.a_size_2; i++) {
670 e2 += *(hmm.replacement_letters->probs_2 + get_mtx_index(letter_2, i, hmm.a_size_2)) * null_model.emissions_2[i];
671 }
672 }
673 else {
674 printf("Could not find letter %s when scoring null model\n", &seq_2[l]);
675 return DEFAULT;
676 }
677 }
678 }
679 if(hmm.nr_alphabets > 2) {
680 if(letter_3 >= 0) {
681 e3 = null_model.emissions_3[letter_3];
682 }
683 else {
684 /* need replacement letters */
685 letter_3 = get_replacement_letter_index_multi(&seq_3[l], &replacement_letters, 3);
686 if(letter_3 >= 0) {
687 e3 = 0.0;
688 for(i = 0; i < hmm.a_size_3; i++) {
689 e3 += *(hmm.replacement_letters->probs_3 + get_mtx_index(letter_3, i, hmm.a_size_3)) * null_model.emissions_3[i];
690 }
691 }
692 else {
693 printf("Could not find letter %s when scoring null model\n", &seq_3[l]);
694 return DEFAULT;
695 }
696 }
697 }
698 if(hmm.nr_alphabets > 3) {
699 if(letter_4 >= 0) {
700 e4 = null_model.emissions_4[letter_4];
701 }
702 else {
703 /* need replacement letters */
704 letter_4 = get_replacement_letter_index_multi(&seq_4[l], &replacement_letters, 4);
705 if(letter_4 >= 0) {
706 e4 = 0.0;
707 for(i = 0; i < hmm.a_size_4; i++) {
708 e4 += *(hmm.replacement_letters->probs_4 + get_mtx_index(letter_4, i, hmm.a_size_4)) * null_model.emissions_4[i];
709 }
710 }
711 else {
712 printf("Could not find letter %s when scoring null model\n", &seq[l]);
713 return DEFAULT;
714 }
715 }
716 }
717
718 if(multi_scoring_method == JOINT_PROB) {
719 null_model_score += log10(e1) + log10(null_model.trans_prob);
720 if(hmm.nr_alphabets > 1) {
721 null_model_score += log10(e2);
722 }
723 if(hmm.nr_alphabets > 2) {
724 null_model_score += log10(e3);
725 }
726 if(hmm.nr_alphabets > 3) {
727 null_model_score += log10(e4);
728 }
729 }
730 else {
731 /* use other multialpha scoring method, not implemented yet */
732 printf("Error: only JOINT_PROB scoring is implemented\n");
733 exit(0);
734 }
735 }
736 }
737
738 return null_model_score;
739 }
740
741
742
743
744
745
746 double get_nullmodel_score_msa_multi(int seq_len, int prf_mode, int use_prior,
747 int use_gap_shares, int normalize,
748 int scoring_method, int multi_scoring_method, double *aa_freqs, double *aa_freqs_2,
749 double *aa_freqs_3, double *aa_freqs_4)
750 {
751 int avg_seq_len;
752 double emiss_prob, emiss_prob_2, emiss_prob_3, emiss_prob_4;
753 double trans_prob;
754 double null_model_score;
755 int letter;
756 int k,l,p,m;
757 double col_score, col_score_2, col_score_3, col_score_4;
758 int using_default_null_model;
759 double seq_normalizer, state_normalizer;
760
761 using_default_null_model = NO;
762 /* calculate null model score for seq */
763 if(null_model.nr_alphabets < 0) {
764 /* use default null model */
765 emiss_prob = 1.0 / (double)hmm.a_size;
766 if(hmm.nr_alphabets > 1) {
767 emiss_prob_2 = 1.0 / (double)hmm.a_size_2;
768 }
769 if(hmm.nr_alphabets > 2) {
770 emiss_prob_3 = 1.0 / (double)hmm.a_size_3;
771 }
772 if(hmm.nr_alphabets > 3) {
773 emiss_prob_4 = 1.0 / (double)hmm.a_size_4;
774 }
775 trans_prob = (double)(seq_len)/(double)(seq_len + 1);
776 null_model.a_size = hmm.a_size;
777 null_model.emissions = (double*)malloc_or_die(null_model.a_size * sizeof(double));
778 if(hmm.nr_alphabets > 1) {
779 null_model.a_size_2 = hmm.a_size_2;
780 null_model.emissions_2 = (double*)malloc_or_die(null_model.a_size_2 * sizeof(double));
781 }
782 if(hmm.nr_alphabets > 2) {
783 null_model.a_size_3 = hmm.a_size_3;
784 null_model.emissions_3 = (double*)malloc_or_die(null_model.a_size_3 * sizeof(double));
785 }
786 if(hmm.nr_alphabets > 3) {
787 null_model.a_size_4 = hmm.a_size_4;
788 null_model.emissions_4 = (double*)malloc_or_die(null_model.a_size_4 * sizeof(double));
789 }
790 for(k = 0; k < hmm.a_size; k++) {
791 null_model.emissions[k] = emiss_prob;
792 if(hmm.nr_alphabets > 1) {
793 null_model.emissions_2[k] = emiss_prob_2;
794 }
795 if(hmm.nr_alphabets > 2) {
796 null_model.emissions_3[k] = emiss_prob_3;
797 }
798 if(hmm.nr_alphabets > 3) {
799 null_model.emissions_4[k] = emiss_prob_4;
800 }
801 }
802
803 null_model.trans_prob = trans_prob;
804 using_default_null_model = YES;
805 }
806
807
808 /* NOTE: must include other scoring methods here as well (copy from core_algorithms) */
809 /* use specified null model */
810 null_model_score = 0.0;
811 if(scoring_method == DOT_PRODUCT) {
812 l = 0;
813 while(1) {
814
815 /* set index for seq pointer */
816 if(prf_mode == ALL_SEQS) {
817 p = l;
818 }
819 else {
820 p = *(msa_seq_infop->lead_columns_start + l);
821 }
822
823 /* get col_score for the different alphabets */
824 col_score = 0.0;
825 col_score_2 = 0.0;
826 col_score_3 = 0.0;
827 col_score_4 = 0.0;
828 col_score = get_dp_statescore(null_model.a_size, use_gap_shares, use_prior, msa_seq_infop->msa_seq_1,
829 p, null_model.emissions,
830 0, normalize, msa_seq_infop->gap_shares);
831 if(hmm.nr_alphabets > 1) {
832 col_score_2 = get_dp_statescore(null_model.a_size_2, use_gap_shares, use_prior, msa_seq_infop->msa_seq_2,
833 p, null_model.emissions_2,
834 0, normalize, msa_seq_infop->gap_shares);
835 }
836 if(hmm.nr_alphabets > 2) {
837 col_score_3 = get_dp_statescore(null_model.a_size_3, use_gap_shares, use_prior, msa_seq_infop->msa_seq_3,
838 p, null_model.emissions_3,
839 0, normalize, msa_seq_infop->gap_shares);
840 }
841 if(hmm.nr_alphabets > 3) {
842 col_score_4 = get_dp_statescore(null_model.a_size_4, use_gap_shares, use_prior, msa_seq_infop->msa_seq_4,
843 p, null_model.emissions_4,
844 0, normalize, msa_seq_infop->gap_shares);
845 }
846
847 /* calculate total column score */
848 if(multi_scoring_method == JOINT_PROB) {
849 null_model_score += log10(col_score) + log10(null_model.trans_prob);
850 if(hmm.nr_alphabets > 1) {
851 null_model_score += log10(col_score_2);
852 }
853 if(hmm.nr_alphabets > 2) {
854 null_model_score += log10(col_score_3);
855 }
856 if(hmm.nr_alphabets > 3) {
857 null_model_score += log10(col_score_4);
858 }
859 }
860 else {
861 /* use other multialpha scoring method, not implemented yet */
862 printf("Error: only JOINT_PROB scoring is implemented\n");
863 exit(0);
864 }
865
866 /* update seq index and check if we are done */
867 l++;
868 if(prf_mode == ALL_SEQS) {
869 if(l >= seq_len) {
870 break;
871 }
872 }
873 else {
874 if(*(msa_seq_infop->lead_columns_start + l) == END) {
875 break;
876 }
877 }
878 }
879 }
880
881 if(scoring_method == DOT_PRODUCT_PICASSO) {
882 l = 0;
883 while(1) {
884
885 /* set index for seq pointer */
886 if(prf_mode == ALL_SEQS) {
887 p = l;
888 }
889 else {
890 p = *(msa_seq_infop->lead_columns_start + l);
891 }
892
893 /* get col_score for the different alphabets */
894 col_score = 0.0;
895 col_score_2 = 0.0;
896 col_score_3 = 0.0;
897 col_score_4 = 0.0;
898 col_score = get_dp_picasso_statescore(null_model.a_size, use_gap_shares, use_prior, msa_seq_infop->msa_seq_1,
899 p, null_model.emissions,
900 0, normalize, msa_seq_infop->gap_shares, aa_freqs);
901 if(hmm.nr_alphabets > 1) {
902 col_score_2 = get_dp_picasso_statescore(null_model.a_size_2, use_gap_shares, use_prior, msa_seq_infop->msa_seq_2,
903 p, null_model.emissions_2,
904 0, normalize, msa_seq_infop->gap_shares, aa_freqs_2);
905 }
906 if(hmm.nr_alphabets > 2) {
907 col_score_3 = get_dp_picasso_statescore(null_model.a_size_3, use_gap_shares, use_prior, msa_seq_infop->msa_seq_3,
908 p, null_model.emissions_3,
909 0, normalize, msa_seq_infop->gap_shares, aa_freqs_3);
910 }
911 if(hmm.nr_alphabets > 3) {
912 col_score_4 = get_dp_picasso_statescore(null_model.a_size_4, use_gap_shares, use_prior, msa_seq_infop->msa_seq_4,
913 p, null_model.emissions_4,
914 0, normalize, msa_seq_infop->gap_shares, aa_freqs_4);
915 }
916
917 /* calculate total column score */
918 if(multi_scoring_method == JOINT_PROB) {
919 null_model_score += log10(col_score) + log10(null_model.trans_prob);
920 if(hmm.nr_alphabets > 1) {
921 null_model_score += log10(col_score_2);
922 }
923 if(hmm.nr_alphabets > 2) {
924 null_model_score += log10(col_score_3);
925 }
926 if(hmm.nr_alphabets > 3) {
927 null_model_score += log10(col_score_4);
928 }
929 }
930 else {
931 /* use other multialpha scoring method, not implemented yet */
932 printf("Error: only JOINT_PROB scoring is implemented\n");
933 exit(0);
934 }
935
936 /* update seq index and check if we are done */
937 l++;
938 if(prf_mode == ALL_SEQS) {
939 if(l >= seq_len) {
940 break;
941 }
942 }
943 else {
944 if(*(msa_seq_infop->lead_columns_start + l) == END) {
945 break;
946 }
947 }
948 }
949 }
950
951 if(scoring_method == PICASSO) {
952 l = 0;
953 while(1) {
954
955 /* set index for seq pointer */
956 if(prf_mode == ALL_SEQS) {
957 p = l;
958 }
959 else {
960 p = *(msa_seq_infop->lead_columns_start + l);
961 }
962
963 /* get col_score for the different alphabets */
964 col_score = 0.0;
965 col_score_2 = 0.0;
966 col_score_3 = 0.0;
967 col_score_4 = 0.0;
968 col_score = get_picasso_statescore(null_model.a_size, use_gap_shares, use_prior, msa_seq_infop->msa_seq_1,
969 p, null_model.emissions,
970 0, normalize, msa_seq_infop->gap_shares, aa_freqs);
971 if(hmm.nr_alphabets > 1) {
972 col_score_2 = get_picasso_statescore(null_model.a_size_2, use_gap_shares, use_prior, msa_seq_infop->msa_seq_2,
973 p, null_model.emissions_2,
974 0, normalize, msa_seq_infop->gap_shares, aa_freqs_2);
975 }
976 if(hmm.nr_alphabets > 2) {
977 col_score_3 = get_picasso_statescore(null_model.a_size_3, use_gap_shares, use_prior, msa_seq_infop->msa_seq_3,
978 p, null_model.emissions_3,
979 0, normalize, msa_seq_infop->gap_shares, aa_freqs_3);
980 }
981 if(hmm.nr_alphabets > 3) {
982 col_score_4 = get_picasso_statescore(null_model.a_size_4, use_gap_shares, use_prior, msa_seq_infop->msa_seq_4,
983 p, null_model.emissions_4,
984 0, normalize, msa_seq_infop->gap_shares, aa_freqs_4);
985 }
986
987 /* calculate total column score */
988 if(multi_scoring_method == JOINT_PROB) {
989 null_model_score += log10(col_score) + log10(null_model.trans_prob);
990 if(hmm.nr_alphabets > 1) {
991 null_model_score += log10(col_score_2);
992 }
993 if(hmm.nr_alphabets > 2) {
994 null_model_score += log10(col_score_3);
995 }
996 if(hmm.nr_alphabets > 3) {
997 null_model_score += log10(col_score_4);
998 }
999 }
1000 else {
1001 /* use other multialpha scoring method, not implemented yet */
1002 printf("Error: only JOINT_PROB scoring is implemented\n");
1003 exit(0);
1004 }
1005
1006 /* update seq index and check if we are done */
1007 l++;
1008 if(prf_mode == ALL_SEQS) {
1009 if(l >= seq_len) {
1010 break;
1011 }
1012 }
1013 else {
1014 if(*(msa_seq_infop->lead_columns_start + l) == END) {
1015 break;
1016 }
1017 }
1018 }
1019 }
1020
1021 if(scoring_method == PICASSO_SYM) {
1022 l = 0;
1023 while(1) {
1024
1025 /* set index for seq pointer */
1026 if(prf_mode == ALL_SEQS) {
1027 p = l;
1028 }
1029 else {
1030 p = *(msa_seq_infop->lead_columns_start + l);
1031 }
1032
1033 /* get col_score for the different alphabets */
1034 col_score = 0.0;
1035 col_score_2 = 0.0;
1036 col_score_3 = 0.0;
1037 col_score_4 = 0.0;
1038 col_score = get_picasso_sym_statescore(null_model.a_size, use_gap_shares, use_prior, msa_seq_infop->msa_seq_1,
1039 p, null_model.emissions,
1040 0, normalize, msa_seq_infop->gap_shares, aa_freqs);
1041 if(hmm.nr_alphabets > 1) {
1042 col_score_2 = get_picasso_sym_statescore(null_model.a_size_2, use_gap_shares, use_prior, msa_seq_infop->msa_seq_2,
1043 p, null_model.emissions_2,
1044 0, normalize, msa_seq_infop->gap_shares, aa_freqs_2);
1045 }
1046 if(hmm.nr_alphabets > 2) {
1047 col_score_3 = get_picasso_sym_statescore(null_model.a_size_3, use_gap_shares, use_prior, msa_seq_infop->msa_seq_3,
1048 p, null_model.emissions_3,
1049 0, normalize, msa_seq_infop->gap_shares, aa_freqs_3);
1050 }
1051 if(hmm.nr_alphabets > 3) {
1052 col_score_4 = get_picasso_sym_statescore(null_model.a_size_4, use_gap_shares, use_prior, msa_seq_infop->msa_seq_4,
1053 p, null_model.emissions_4,
1054 0, normalize, msa_seq_infop->gap_shares, aa_freqs_4);
1055 }
1056
1057 /* calculate total column score */
1058 if(multi_scoring_method == JOINT_PROB) {
1059 null_model_score += log10(col_score) + log10(null_model.trans_prob);
1060 if(hmm.nr_alphabets > 1) {
1061 null_model_score += log10(col_score_2);
1062 }
1063 if(hmm.nr_alphabets > 2) {
1064 null_model_score += log10(col_score_3);
1065 }
1066 if(hmm.nr_alphabets > 3) {
1067 null_model_score += log10(col_score_4);
1068 }
1069 }
1070 else {
1071 /* use other multialpha scoring method, not implemented yet */
1072 printf("Error: only JOINT_PROB scoring is implemented\n");
1073 exit(0);
1074 }
1075
1076 /* update seq index and check if we are done */
1077 l++;
1078 if(prf_mode == ALL_SEQS) {
1079 if(l >= seq_len) {
1080 break;
1081 }
1082 }
1083 else {
1084 if(*(msa_seq_infop->lead_columns_start + l) == END) {
1085 break;
1086 }
1087 }
1088 }
1089 }
1090
1091 if(scoring_method == SJOLANDER) {
1092 l = 0;
1093 while(1) {
1094 /* set index for seq pointer */
1095 if(prf_mode == ALL_SEQS) {
1096 p = l;
1097 }
1098 else {
1099 p = *(msa_seq_infop->lead_columns_start + l);
1100 }
1101
1102 /* get col_score for the different alphabets */
1103 col_score = 0.0;
1104 col_score_2 = 0.0;
1105 col_score_3 = 0.0;
1106 col_score_4 = 0.0;
1107 col_score = get_sjolander_statescore(null_model.a_size, use_gap_shares, use_prior, msa_seq_infop->msa_seq_1,
1108 p, null_model.emissions,
1109 0, normalize, msa_seq_infop->gap_shares);
1110 if(hmm.nr_alphabets > 1) {
1111 col_score_2 = get_sjolander_statescore(null_model.a_size_2, use_gap_shares, use_prior, msa_seq_infop->msa_seq_2,
1112 p, null_model.emissions_2,
1113 0, normalize, msa_seq_infop->gap_shares);
1114 }
1115 if(hmm.nr_alphabets > 2) {
1116 col_score_3 = get_sjolander_statescore(null_model.a_size_3, use_gap_shares, use_prior, msa_seq_infop->msa_seq_3,
1117 p, null_model.emissions_3,
1118 0, normalize, msa_seq_infop->gap_shares);
1119 }
1120 if(hmm.nr_alphabets > 3) {
1121 col_score_4 = get_sjolander_statescore(null_model.a_size_4, use_gap_shares, use_prior, msa_seq_infop->msa_seq_4,
1122 p, null_model.emissions_4,
1123 0, normalize, msa_seq_infop->gap_shares);
1124 }
1125
1126 /* calculate total column score */
1127 if(multi_scoring_method == JOINT_PROB) {
1128 null_model_score += log10(col_score) + log10(null_model.trans_prob);
1129 if(hmm.nr_alphabets > 1) {
1130 null_model_score += log10(col_score_2);
1131 }
1132 if(hmm.nr_alphabets > 2) {
1133 null_model_score += log10(col_score_3);
1134 }
1135 if(hmm.nr_alphabets > 3) {
1136 null_model_score += log10(col_score_4);
1137 }
1138 }
1139 else {
1140 /* use other multialpha scoring method, not implemented yet */
1141 printf("Error: only JOINT_PROB scoring is implemented\n");
1142 exit(0);
1143 }
1144
1145 /* update seq index and check if we are done */
1146 l++;
1147 if(prf_mode == ALL_SEQS) {
1148 if(l >= seq_len) {
1149 break;
1150 }
1151 }
1152 else {
1153 if(*(msa_seq_infop->lead_columns_start + l) == END) {
1154 break;
1155 }
1156 }
1157 }
1158 }
1159
1160 if(scoring_method == SJOLANDER_REVERSED) {
1161 l = 0;
1162 while(1) {
1163 /* set index for seq pointer */
1164 if(prf_mode == ALL_SEQS) {
1165 p = l;
1166 }
1167 else {
1168 p = *(msa_seq_infop->lead_columns_start + l);
1169 }
1170
1171 /* get col_score for the different alphabets */
1172 col_score = 0.0;
1173 col_score_2 = 0.0;
1174 col_score_3 = 0.0;
1175 col_score_4 = 0.0;
1176 col_score = get_sjolander_reversed_statescore(null_model.a_size, use_gap_shares, use_prior, msa_seq_infop->msa_seq_1,
1177 p, null_model.emissions,
1178 0, normalize, msa_seq_infop->gap_shares);
1179 if(hmm.nr_alphabets > 1) {
1180 col_score_2 = get_sjolander_reversed_statescore(null_model.a_size_2, use_gap_shares, use_prior, msa_seq_infop->msa_seq_2,
1181 p, null_model.emissions_2,
1182 0, normalize, msa_seq_infop->gap_shares);
1183 }
1184 if(hmm.nr_alphabets > 2) {
1185 col_score_3 = get_sjolander_reversed_statescore(null_model.a_size_3, use_gap_shares, use_prior, msa_seq_infop->msa_seq_3,
1186 p, null_model.emissions_3,
1187 0, normalize, msa_seq_infop->gap_shares);
1188 }
1189 if(hmm.nr_alphabets > 3) {
1190 col_score_4 = get_sjolander_reversed_statescore(null_model.a_size_4, use_gap_shares, use_prior, msa_seq_infop->msa_seq_4,
1191 p, null_model.emissions_4,
1192 0, normalize, msa_seq_infop->gap_shares);
1193 }
1194
1195 /* calculate total column score */
1196 if(multi_scoring_method == JOINT_PROB) {
1197 null_model_score += log10(col_score) + log10(null_model.trans_prob);
1198 if(hmm.nr_alphabets > 1) {
1199 null_model_score += log10(col_score_2);
1200 }
1201 if(hmm.nr_alphabets > 2) {
1202 null_model_score += log10(col_score_3);
1203 }
1204 if(hmm.nr_alphabets > 3) {
1205 null_model_score += log10(col_score_4);
1206 }
1207 }
1208 else {
1209 /* use other multialpha scoring method, not implemented yet */
1210 printf("Error: only JOINT_PROB scoring is implemented\n");
1211 exit(0);
1212 }
1213
1214 /* update seq index and check if we are done */
1215 l++;
1216 if(prf_mode == ALL_SEQS) {
1217 if(l >= seq_len) {
1218 break;
1219 }
1220 }
1221 else {
1222 if(*(msa_seq_infop->lead_columns_start + l) == END) {
1223 break;
1224 }
1225 }
1226 }
1227 }
1228
1229 else if(scoring_method == SUBST_MTX_PRODUCT) {
1230 l = 0;
1231 while(1) {
1232 /* set index for seq pointer */
1233 if(prf_mode == ALL_SEQS) {
1234 p = l;
1235 }
1236 else {
1237 p = *(msa_seq_infop->lead_columns_start + l);
1238 }
1239
1240 /* get col_score for the different alphabets */
1241 col_score = 0.0;
1242 col_score_2 = 0.0;
1243 col_score_3 = 0.0;
1244 col_score_4 = 0.0;
1245 col_score = get_subst_mtx_product_statescore(null_model.a_size, use_gap_shares, use_prior, msa_seq_infop->msa_seq_1,
1246 p, null_model.emissions,
1247 0, hmm.subst_mtx);
1248 if(hmm.nr_alphabets > 1) {
1249 col_score_2 = get_subst_mtx_product_statescore(null_model.a_size_2, use_gap_shares, use_prior, msa_seq_infop->msa_seq_2,
1250 p, null_model.emissions_2,
1251 0, hmm.subst_mtx_2);
1252 }
1253 if(hmm.nr_alphabets > 2) {
1254 col_score_3 = get_subst_mtx_product_statescore(null_model.a_size_3, use_gap_shares, use_prior, msa_seq_infop->msa_seq_3,
1255 p, null_model.emissions_3,
1256 0, hmm.subst_mtx_3);
1257 }
1258 if(hmm.nr_alphabets > 3) {
1259 col_score_4 = get_subst_mtx_product_statescore(null_model.a_size_4, use_gap_shares, use_prior, msa_seq_infop->msa_seq_4,
1260 p, null_model.emissions_4,
1261 0, hmm.subst_mtx_4);
1262 }
1263
1264 /* calculate total column score */
1265 if(multi_scoring_method == JOINT_PROB) {
1266 null_model_score += log10(col_score) + log10(null_model.trans_prob);
1267 if(hmm.nr_alphabets > 1) {
1268 null_model_score += log10(col_score_2);
1269 }
1270 if(hmm.nr_alphabets > 2) {
1271 null_model_score += log10(col_score_3);
1272 }
1273 if(hmm.nr_alphabets > 3) {
1274 null_model_score += log10(col_score_4);
1275 }
1276 }
1277 else {
1278 /* use other multialpha scoring method, not implemented yet */
1279 printf("Error: only JOINT_PROB scoring is implemented\n");
1280 exit(0);
1281 }
1282
1283 /* update seq index and check if we are done */
1284 l++;
1285 if(prf_mode == ALL_SEQS) {
1286 if(l >= seq_len) {
1287 break;
1288 }
1289 }
1290 else {
1291 if(*(msa_seq_infop->lead_columns_start + l) == END) {
1292 break;
1293 }
1294 }
1295 }
1296 }
1297
1298 else if(scoring_method == SUBST_MTX_DOT_PRODUCT) {
1299 printf("SMDP scoring not implemented yet\n");
1300 exit(0);
1301
1302 }
1303 else if(scoring_method == SUBST_MTX_DOT_PRODUCT_PRIOR) {
1304 printf("SMDPP scoring not implemented yet\n");
1305 exit(0);
1306 }
1307
1308 if(using_default_null_model == YES) {
1309 free(null_model.emissions);
1310 null_model.a_size = -1;
1311 }
1312 return null_model_score;
1313 }
1314
1315
1316
1317 void get_post_prob_matrix(double **ret_post_prob_matrixp, double forw_score, struct forward_s *forw_mtx,
1318 struct backward_s *backw_mtx, int seq_len)
1319 {
1320 int i,j;
1321 double *post_prob_matrix;
1322 double post_prob_score;
1323
1324 /* must be freed by caller */
1325 post_prob_matrix = (double*)(malloc_or_die((seq_len+2) * hmm.nr_v * sizeof(double)));
1326 for(i = 0; i < seq_len + 2; i++) {
1327 for(j = 0; j < hmm.nr_v; j++) {
1328 //printf("forw_score = %f, forw_mtx = %f, backw_mtx = %f\n", forw_score,(forw_mtx + get_mtx_index(i,j,hmm.nr_v))->prob,
1329 // (backw_mtx + get_mtx_index(i,j,hmm.nr_v))->prob);
1330 post_prob_score = (forw_mtx + get_mtx_index(i,j,hmm.nr_v))->prob * (backw_mtx + get_mtx_index(i,j,hmm.nr_v))->prob /
1331 forw_score;
1332 *(post_prob_matrix + get_mtx_index(i,j,hmm.nr_v)) = post_prob_score;
1333 }
1334 }
1335
1336 *ret_post_prob_matrixp = post_prob_matrix;
1337 }