Ticket #1: functions.h

File functions.h, 139.5 KB (added by Ismail Sadiq, 12 years ago)

header file containing functions

Line 
1/*
2 * functions.h
3 *
4 * Created on: Mar 3, 2014
5 * Author: Ismail
6 */
7
8#include <time.h>
9#include <iostream>
10#include <stdio.h>
11#include <algorithm>
12#include <fstream>
13#include <string>
14#include <vector>
15#include <cstring>
16#include <cmath>
17#include <math.h>
18#include <map>
19#include <stdio.h>
20#include <ctype.h>
21#include <sstream>
22#include <stdlib.h>
23#include <limits>
24#include <float.h>
25using namespace std;
26
27#ifndef FUNCTIONS_H_
28#define FUNCTIONS_H_
29
30class functions {
31public:
32 //functions();
33 //virtual ~functions();
34
35 functions(){}
36
37 ~functions(){}
38
39 string x1, x2, x3;
40
41 // performance measures
42 double ppv;
43 double sensitivity;
44 double score_pred;
45 double score_known;
46 double mea;
47 string pred1, pred2, pred3, known1, known2, known3;
48
49 map<string, double> emission_count_temp;
50 vector<vector<int> > seq_combinations;
51 vector<int> rowvec3;
52 double neg_threshold = -10000;
53 double almost_zero = 10e-10;
54
55 string prev_state;
56 int dim1, dim2, dim3;
57 vector<long> states;
58 vector<string> seqs;
59 string nucleotides = "ACGU.";
60 string trio = "000";
61 string trio0 = "000";
62 string trio1 = "000";
63 string trio2 = "000";
64 string trio_seq = "000";
65 map <string, double> emission_prob;
66 vector<map<string, double> > emission_prob_collection;
67 map <string, double> transition_from_state_count;
68 map <long, int> emission_in_states_count;
69 map <long, int> emission_to_states_count;
70
71 // emission and state trans prob. read
72 vector <vector<string> > state_transition;
73 vector <vector<double> > state_transition_double;
74 vector <vector<string> > emission;
75 vector <vector<double> > emission_double;
76 vector<string> rowvec4;
77 vector<double> rowvec5;
78 vector<string> seq_alpha;
79 map<string, int> alpha_indexes_emissions;
80 map<string, double> emission_indexes;
81 map<string, int> indexes_states;
82
83 string x1_aligned;
84 string x2_aligned;
85 string x3_aligned;
86 string x1_aligned_reverse;
87 string x2_aligned_reverse;
88 string x3_aligned_reverse;
89
90 char gap = '.';
91 //char gap = '-';
92
93 //vector<map<int, bool> > Table; //The vector of maps of ints and booleans
94 //map<int, bool> Temp; //A temporary map to be used below
95 //Table.push_back(Temp); //Add a map<int, bool> to the vector
96 //Table.at(0)[1337/*key for the map*/] = true; //Value for the map
97
98 map <string, double> rowmap;
99 vector<map<string, double> > prob_trans_to_001;
100 vector<map<string, double> > prob_trans_to_010;
101 vector<map<string, double> > prob_trans_to_011;
102 vector<map<string, double> > prob_trans_to_100;
103 vector<map<string, double> > prob_trans_to_101;
104 vector<map<string, double> > prob_trans_to_110;
105 vector<map<string, double> > prob_trans_to_111;
106 map <string, double> prev_state_to_001; // Probability of transitions to So = 001
107 map <string, double> prev_state_to_010; // Probability of transitions to So = 010
108 map <string, double> prev_state_to_011; // Probability of transitions to So = 011
109 map <string, double> prev_state_to_100; // Probability of transitions to So = 100
110 map <string, double> prev_state_to_101; // Probability of transitions to So = 101
111 map <string, double> prev_state_to_110; // Probability of transitions to So = 110
112 map <string, double> prev_state_to_111; // Probability of transitions to So = 111
113
114 vector<vector<double> > rowvec1;
115 vector<double> rowvec2;
116 vector<vector<vector<double> > > V001;
117 vector<vector<vector<double> > > V010;
118 vector<vector<vector<double> > > V011;
119 vector<vector<vector<double> > > V100;
120 vector<vector<vector<double> > > V101;
121 vector<vector<vector<double> > > V110;
122 vector<vector<vector<double> > > V111;
123
124 // forward algo
125 vector<vector<vector<double> > > V001_f;
126 vector<vector<vector<double> > > V010_f;
127 vector<vector<vector<double> > > V011_f;
128 vector<vector<vector<double> > > V100_f;
129 vector<vector<vector<double> > > V101_f;
130 vector<vector<vector<double> > > V110_f;
131 vector<vector<vector<double> > > V111_f;
132
133 // backward algo
134 vector<vector<vector<double> > > V001_b;
135 vector<vector<vector<double> > > V010_b;
136 vector<vector<vector<double> > > V011_b;
137 vector<vector<vector<double> > > V100_b;
138 vector<vector<vector<double> > > V101_b;
139 vector<vector<vector<double> > > V110_b;
140 vector<vector<vector<double> > > V111_b;
141
142 //MEA
143 vector<vector<vector<double> > > BM;
144 string align1_reverse;
145 string align2_reverse;
146 string align3_reverse;
147 string align1;
148 string align2;
149 string align3;
150
151
152
153
154 //
155 double max_imum(double a, double b, double c)
156 {
157 double maxx;
158 maxx = max(a, b);
159 maxx = max(maxx, c);
160
161 return maxx;
162 }
163
164 void vector_assigner()
165 {
166
167 string number;
168 stringstream strstream;
169 //cout << "sizeof state_transition_double: " << state_transition_double.size() << endl;
170 //cout << "sizeof state_transition_double[0]: " << state_transition_double[0].size() << endl;
171
172 // state-transition assigner
173 for (int j = 0; j < state_transition_double[0].size(); j++)
174 {
175 strstream.str("");
176 strstream << states[j];
177 number = strstream.str();
178 //cout << "number: " << number << endl;
179 prev_state_to_001[number] = state_transition_double[0][j];
180 prev_state_to_010[number] = state_transition_double[1][j];
181 prev_state_to_011[number] = state_transition_double[2][j];
182 prev_state_to_100[number] = state_transition_double[3][j];
183 prev_state_to_101[number] = state_transition_double[4][j];
184 prev_state_to_110[number] = state_transition_double[5][j];
185 prev_state_to_111[number] = state_transition_double[6][j];
186 //cout << "001" << prev_state_to_001[number] << endl;
187 //cout << "010" << prev_state_to_010[number] << endl;
188 //cout << "011" << prev_state_to_011[number] << endl;
189 //cout << "100" << prev_state_to_100[number] << endl;
190 //cout << "101" << prev_state_to_101[number] << endl;
191 //cout << "110" << prev_state_to_110[number] << endl;
192 //cout << "111" << prev_state_to_111[number] << endl;
193
194 }
195
196 // emission probability assigner
197 /*
198 for (int j = 0; j < state_transition_double[0].size() - 1; j++)
199 {
200 strstream.str("");
201 strstream << states[j];
202 number = strstream.str();
203 double temp1 = prev_state_to_001[number];
204 double temp2= prev_state_to_010[number];
205 double temp3= prev_state_to_011[number];
206 double temp4 = prev_state_to_100[number];
207 double temp5 = prev_state_to_101[number];
208 double temp6 = prev_state_to_110[number];
209 double temp7 = prev_state_to_111[number];
210 }
211 */
212 }
213
214
215 // reads the prob files
216 int string_parser_prob(char filename[])
217 {
218
219 const int MAX_CHARS_PER_LINE = 512;
220 const int MAX_TOKENS_PER_LINE = 20;
221 const char* const DELIMITER = " \t,";
222
223 int state_trans_row_index = 0;
224 int emission_row_index = 0;
225
226 // create a file-reading object
227 ifstream fin;
228 // fin.open("hmmparam_states.txt"); // open a file
229 // fin.open("hmmparam_emissions.txt"); // open a file
230 fin.open(filename); // open a file
231 if (!fin.good())
232 return 1; // exit if file not found
233
234 // read each line of the file
235 while (!fin.eof())
236 {
237 // read an entire line into memory
238 char buf[MAX_CHARS_PER_LINE];
239 fin.getline(buf, MAX_CHARS_PER_LINE);
240
241 // parse the line into blank-delimited tokens
242 int n = 0; // a for-loop index
243
244 // array to store memory addresses of the tokens in buf
245 const char* token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0
246
247 // parse the line
248 token[0] = strtok(buf, DELIMITER); // first token
249 if (token[0]) // zero if line is blank
250 {
251 for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
252 {
253 token[n] = strtok(0, DELIMITER); // subsequent tokens
254 if (!token[n]) break; // no more tokens
255 }
256 }
257
258 if (filename == "hmmparam_states_3seq.txt")
259 //if (filename == "hmmparam_states.txt")
260 {
261 //state_transition_double.push_back(rowvec2);
262 state_transition.push_back(rowvec4);
263 for (int j = 0; j < n; j++)
264 {
265 state_transition[state_trans_row_index].push_back(token[j]);
266 //cout << "state_transition[" << state_trans_row_index << "][" << j << "]: " << state_transition[state_trans_row_index][j]; // << endl;
267 }
268 //cout << endl;
269 state_trans_row_index++;
270 }
271
272 else if (filename == "hmmparam_emissions_3seq.txt")
273 //else if (filename == "hmmparam_emissions.txt")
274 {
275 emission.push_back(rowvec4);
276 for (int j = 0; j < n; j++)
277 {
278 emission[emission_row_index].push_back(token[j]);
279 //cout << "emission[" << emission_row_index << "][" << j << "]: " << emission[emission_row_index][j]; // << endl;
280 }
281 //cout << endl;
282 emission_row_index++;
283 }
284
285 }
286
287 return 0;
288 }
289
290 // finds the minimum of three values
291 int minimum(int a, int b, int c)
292 {
293 int minm;
294 minm = min(a, b);
295 minm = min(minm, c);
296
297 return minm;
298 }
299
300 double maximum(double a, double b, double c, double d, double e, double f, double g)
301 {
302 double maxx;
303 maxx = max(a, b);
304 maxx = max(maxx, c);
305 maxx = max(maxx, d);
306 maxx = max(maxx, e);
307 maxx = max(maxx, f);
308 maxx = max(maxx, g);
309
310 return maxx;
311 }
312
313 // read text files for prob.'s
314 int string_parser_emission(char filename[])
315 {
316
317 const int MAX_CHARS_PER_LINE = 512;
318 const int MAX_TOKENS_PER_LINE = 5;
319 const char* const DELIMITER = " \t,";
320
321 string emission_symbol;
322 int index_of_emission = 0;
323
324 // create a file-reading object
325 ifstream fin;
326 // fin.open("hmmparam_states.txt"); // open a file
327 // fin.open("hmmparam_emissions.txt"); // open a file
328 fin.open(filename); // open a file
329 if (!fin.good())
330 return 1; // exit if file not found
331
332 // read each line of the file
333 while (!fin.eof())
334 {
335 // read an entire line into memory
336 char buf[MAX_CHARS_PER_LINE];
337 fin.getline(buf, MAX_CHARS_PER_LINE);
338
339 // parse the line into blank-delimited tokens
340 int n = 0; // a for-loop index
341
342 // array to store memory addresses of the tokens in buf
343 const char* token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0
344
345 // parse the line
346 token[0] = strtok(buf, DELIMITER); // first token
347 if (token[0]) // zero if line is blank
348 {
349 for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
350 {
351 token[n] = strtok(0, DELIMITER); // subsequent tokens
352 if (!token[n]) break; // no more tokens
353 }
354 }
355
356 //emission_symbol = token[0];
357 emission_symbol.assign(token[0], token[0] + 3);
358 emission_indexes[emission_symbol] = index_of_emission;
359 index_of_emission++;
360
361
362
363
364 //seq_combinations.push_back(rowvec3);
365 //for (int j = 0; j < n; j++)
366 //{
367 // seq_combinations[seq_combinations_row_index].push_back(atoi(token[j]));
368 //}
369 //seq_combinations_row_index++;
370
371 /*
372 if (filename == "hmmparam_states.txt")
373 {
374 state_transition.push_back(rowvec);
375 for (int j = 0; j < n; j++)
376 {
377 state_transition[state_trans_row_index].push_back(token[j]);
378 }
379 state_trans_row_index++;
380 }
381
382 else if (filename == "hmmparam_emissions.txt")
383 {
384 emission.push_back(rowvec);
385 for (int j = 0; j < n; j++)
386 {
387 emission[emission_row_index].push_back(token[j]);
388 }
389 emission_row_index++;
390 }
391 */
392
393 }
394
395 return 0;
396 }
397
398 // read text files for prob.'s
399 int string_parser(char filename[])
400 {
401
402 const int MAX_CHARS_PER_LINE = 512;
403 const int MAX_TOKENS_PER_LINE = 20;
404 const char* const DELIMITER = " \t,";
405
406 int state_trans_row_index = 0;
407 int emission_row_index = 0;
408 int seq_combinations_row_index = 0;
409
410 // create a file-reading object
411 ifstream fin;
412 // fin.open("hmmparam_states.txt"); // open a file
413 // fin.open("hmmparam_emissions.txt"); // open a file
414 fin.open(filename); // open a file
415 if (!fin.good())
416 return 1; // exit if file not found
417
418 // read each line of the file
419 while (!fin.eof())
420 {
421 // read an entire line into memory
422 char buf[MAX_CHARS_PER_LINE];
423 fin.getline(buf, MAX_CHARS_PER_LINE);
424
425 // parse the line into blank-delimited tokens
426 int n = 0; // a for-loop index
427
428 // array to store memory addresses of the tokens in buf
429 const char* token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0
430
431 // parse the line
432 token[0] = strtok(buf, DELIMITER); // first token
433 if (token[0]) // zero if line is blank
434 {
435 for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
436 {
437 token[n] = strtok(0, DELIMITER); // subsequent tokens
438 if (!token[n]) break; // no more tokens
439 }
440 }
441
442 seq_combinations.push_back(rowvec3);
443 for (int j = 0; j < n; j++)
444 {
445 seq_combinations[seq_combinations_row_index].push_back(atoi(token[j]));
446 }
447 seq_combinations_row_index++;
448
449 /*
450 if (filename == "hmmparam_states.txt")
451 {
452 state_transition.push_back(rowvec);
453 for (int j = 0; j < n; j++)
454 {
455 state_transition[state_trans_row_index].push_back(token[j]);
456 }
457 state_trans_row_index++;
458 }
459
460 else if (filename == "hmmparam_emissions.txt")
461 {
462 emission.push_back(rowvec);
463 for (int j = 0; j < n; j++)
464 {
465 emission[emission_row_index].push_back(token[j]);
466 }
467 emission_row_index++;
468 }
469 */
470
471 }
472
473 return 0;
474 }
475
476 // fill prob matrices
477 void prob_matrix_fill()
478 {
479 string temp;
480 //state_transition_double.push_back(rowvec2);
481
482 //cout << "state_transition.size(): " << state_transition.size() << endl;
483
484 // filling state transition matrix first
485 for (int row = 1; row < state_transition.size(); row++)
486 //for (int row = 1; row < 3; row++)
487 {
488 state_transition_double.push_back(rowvec5);
489 //for (int col = 0; col < 3; col++)
490 for (int col = 0; col < state_transition[0].size(); col++)
491 {
492 temp = state_transition[row][col];
493 char *new_string = new char[temp.length() + 1];
494 strcpy(new_string, temp.c_str());
495 double var = atof(new_string);
496 state_transition_double[row-1].push_back(var);
497 //cout << "row: " << row-1 << " col: " << col << " " << state_transition_double[row-1][col] << " ";
498 }
499 //cout << endl;
500 }
501
502 //cout << "sizeof state_transition_double: " << state_transition_double.size() << endl;
503
504
505 // filling emission matrix second
506 for (int row = 1; row < emission.size(); row++)
507 {
508 emission_double.push_back(rowvec5);
509 for (int col = 0; col < emission[0].size(); col++)
510 {
511 temp = emission[row][col];
512 char *new_string = new char[temp.length() + 1];
513 strcpy(new_string, temp.c_str());
514 double var = atof(new_string);
515 emission_double[row-1].push_back(var);
516 //cout << emission_double[row-1][col] << " ";
517 }
518 //cout << endl;
519 }
520 }
521
522
523 // fill in map of indexes for state transition probabilities and emission probability pairs
524 void map_fill()
525 {
526 string temp;
527
528 // filling map for states
529 for (int i = 0; i < state_transition[0].size(); i++)
530 {
531 indexes_states[state_transition[0][i]] = i + 1;
532 }
533
534 // filling map for emission
535 for (int i = 1; i < emission.size(); i++)
536 {
537 temp = emission[i][emission[1].size() - 1];
538 alpha_indexes_emissions[temp] = i - 1;
539 }
540 }
541
542 int readfasta(char filename[])
543 {
544 std::ifstream input(filename);
545 if(!input.good()){
546 std::cerr << "Error opening '"<<filename<<"'. Bailing out." << std::endl;
547 return -1;
548 }
549
550 int vec_index = 0;
551 std::string line, name, content;
552 while( std::getline( input, line ).good() )
553 {
554 if( line.empty() || line[0] == '>' ) // Identifier marker
555 {
556 if( !name.empty() )
557 {
558 // Print out what we read from the last entry
559 // std::cout << name << " : " << content << std::endl;
560 name.clear();
561 seqs.push_back(content);
562 }
563 if( !line.empty() )
564 {
565 name = line.substr(1);
566 }
567 content.clear();
568 }
569 else if( !name.empty() )
570 {
571 if( line.find(' ') != std::string::npos ) // Invalid sequence--no spaces allowed
572 {
573 name.clear();
574 content.clear();
575 }
576 else
577 {
578 content += line;
579 }
580 }
581 }
582
583 if( !name.empty() ) // Print out what we read from the last entry
584 {
585 // std::cout << name << " : " << content << std::endl;
586 seqs.push_back(content);
587 }
588
589 //cout << "seq1: " << seqs[0] << endl;
590 //cout << "seq2: " << seqs[1] << endl;
591 //cout << "seq3: " << seqs[2] << endl;
592
593 return 0;
594 }
595
596 long dec_2_bin(long dec)
597 {
598 long rem,i=1,sum=0;
599 do
600 {
601 rem=dec%2;
602 sum=sum + (i*rem);
603 dec=dec/2;
604 i=i*10;
605 }while(dec>0);
606 return sum;
607 }
608
609
610 void standardiser()
611 {
612
613 char DUMMYCHAR = 'Y';
614 for (int j = 0; j < seqs.size(); j++)
615 {
616 for (int i = 0; i < seqs[j].length(); i++)
617 {
618 seqs[j][i] = toupper(seqs[j][i]);
619 if (seqs[j][i] != 'A' && seqs[j][i] != 'G' && seqs[j][i] != 'U' && seqs[j][i] != 'C' && seqs[j][i] != '.' && seqs[j][i] != '-')
620 seqs[j][i] = 'X';
621 }
622 }
623 }
624
625
626 void standardiser2()
627 {
628 char DUMMYCHAR = 'Y';
629
630 for (int i = 0; x1[i] != NULL; i++)
631 {
632 trio[0] = x1[i];
633 trio[1] = x2[i];
634 trio[2] = x3[i];
635
636 if (x1[i] == gap && x2[i] == gap && x3[i] == gap)
637 {
638 //seqs[j].erase(seqs[j].begin()); // (str.begin()+9);
639
640 x1[i] = DUMMYCHAR;
641 x2[i] = DUMMYCHAR;
642 x3[i] = DUMMYCHAR;
643 }
644 }
645
646 // you need include <algorithm> to use general algorithms like std::remove()
647 x1.erase (std::remove(x1.begin(), x1.end(), DUMMYCHAR), x1.end());
648 x2.erase (std::remove(x2.begin(), x2.end(), DUMMYCHAR), x2.end());
649 x3.erase (std::remove(x3.begin(), x3.end(), DUMMYCHAR), x3.end());
650 }
651
652 void standardiser3 ()
653 {
654 x1.erase (std::remove(x1.begin(), x1.end(), gap), x1.end());
655 x2.erase (std::remove(x2.begin(), x2.end(), gap), x2.end());
656 x3.erase (std::remove(x3.begin(), x3.end(), gap), x3.end());
657 }
658
659 void emission_prob_calc()
660 {
661 cout << "size of seq_comb: " << seq_combinations.size() << endl;
662 string seq1, seq2, seq3;
663 string number;
664 stringstream strstream;
665 string prev_state = "111";
666
667 //for (int seq_count = 0; seq_count < seqs.size(); seq_count+=3)
668 for (int seq_count = 0; seq_count < (seq_combinations.size() - 4); seq_count++)
669 {
670 //cout << "index: " << seq_count << " " << seq_combinations[seq_count][0] << " "<< seq_combinations[seq_count][1] << " " << seq_combinations[seq_count][2] << endl;
671
672 //string prev_state = "111";
673
674 /*
675 //clearing state transition variables
676 transition_from_state_count.clear();
677 prev_state_to_001.clear();
678 prev_state_to_010.clear();
679 prev_state_to_011.clear();
680 prev_state_to_100.clear();
681 prev_state_to_101.clear();
682 prev_state_to_110.clear();
683 prev_state_to_111.clear();
684
685 //clearing state emission variables
686 emission_in_states_count.clear();
687 emission_count_temp.clear();
688 emission_prob.clear();
689 */
690
691 //seq1 = seqs[seq_count];
692 //seq2 = seqs[seq_count + 1];
693 //seq3 = seqs[seq_count + 2];
694
695 seq1 = seqs[seq_combinations[seq_count][0] - 1];
696 seq2 = seqs[seq_combinations[seq_count][1] - 1];
697 seq3 = seqs[seq_combinations[seq_count][2] - 1];
698 //cout << seq_combinations[seq_count][0] << endl;
699 //cout << seq_combinations[seq_count][1] << endl;
700 //cout << seq_combinations[seq_count][2] << endl;
701 //cout << "sequence1: " << seq1 << endl;
702 //cout << "sequence2: " << seq2 << endl;
703 //cout << "sequence3: " << seq3 << endl;
704
705 char DUMMYCHAR = 'Y';
706
707
708 // removing the triple gap state
709 for (int i = 0; i < seq1.length(); i++)
710 {
711 trio[0] = seq1[i];
712 trio[1] = seq2[i];
713 trio[2] = seq3[i];
714
715 if (seq1[i] == gap && seq2[i] == gap && seq3[i] == gap)
716 {
717 //seqs[j].erase(seqs[j].begin()); // (str.begin()+9);
718
719 seq1[i] = DUMMYCHAR;
720 seq2[i] = DUMMYCHAR;
721 seq3[i] = DUMMYCHAR;
722 //seqs[j][i+1] = '.';
723 //seqs[j+1][i+1] = '.';
724 //seqs[j+2][i+1] = '.';
725 //i--;
726 }
727 }
728
729 // you need include <algorithm> to use general algorithms like std::remove()
730 seq1.erase(std::remove(seq1.begin(), seq1.end(), DUMMYCHAR), seq1.end());
731 seq2.erase(std::remove(seq2.begin(), seq2.end(), DUMMYCHAR), seq2.end());
732 seq3.erase(std::remove(seq3.begin(), seq3.end(), DUMMYCHAR), seq3.end());
733
734
735
736 /*
737 cout << "string being processed:" << endl;
738 cout << "seq1: " << seq1 << endl;
739 cout << "seq2: " << seq2 << endl;
740 cout << "seq3: " << seq3 << endl;
741 cout << "seq1.length: " << seq1.length() << endl;
742 cout << "seq2.length: " << seq2.length() << endl;
743 cout << "seq3.length: " << seq3.length() << endl;
744 */
745
746 int seq_size = minimum(seq1.length(), seq2.length(), seq3.length());
747 //cout << "minimum_seq_size: " << seq_size << endl;
748
749 //for (int i = 0; i < seqs[seq_count].length(); i++)
750 for (int i = 0; i < seq_size; i++)
751 {
752
753
754
755 trio[0] = seq1[i]; //[seq_combinations[seq_count][0]][i];
756 trio[1] = seq2[i]; //[seq_combinations[seq_count][1]][i];
757 trio[2] = seq3[i]; //[seq_combinations[seq_count][2]][i];
758
759 if (trio == "XCC")
760 int wait = 1;
761
762
763 //if (trio[0] != gap && trio[1] != gap && trio[2] != gap)
764 //{
765 // calculating the counts for each emission
766 emission_prob[trio] += 1;
767
768 if (trio == "..Y")
769 {
770 int wait = 0;
771 }
772
773 // calculating emissions count in states
774 if (trio[0] == gap && trio[1] == gap && trio[2] != gap) //INS 3
775 {
776 emission_in_states_count[states[0]]++;
777 prev_state_to_001[prev_state]++;
778 emission_count_temp[trio]++;
779
780
781 //strstream << 111;
782 //number = strstream.str();
783 //cout << number << endl;
784 //strstream.str("");
785 //strstream << 110;
786 //number = strstream.str();
787 //cout << number << endl;
788
789
790 strstream.str("");
791 strstream << states[0];
792 number = strstream.str();
793 //cout << "new state: " << states[0] << endl;
794 //cout << "number: " << states[0] << endl;
795 prev_state = number;
796 transition_from_state_count[number]++;
797 }
798 else if (trio[0] == gap && trio[1] != gap && trio[2] == gap) //INS 2
799 {
800 emission_count_temp[trio]++;
801 emission_in_states_count[states[1]]++;
802 prev_state_to_010[prev_state]++;
803 strstream.str("");
804 strstream << states[1];
805 number = strstream.str();
806 //cout << "new state: " << states[1] << endl;
807 //cout << "number: " << states[1] << endl;
808 prev_state = number;
809 transition_from_state_count[number]++;
810 }
811 else if (trio[0] == gap && trio[1] != gap && trio[2] != gap) //INS 2&3
812 {
813 emission_count_temp[trio]++;
814 emission_in_states_count[states[2]]++;
815 prev_state_to_011[prev_state]++;
816 strstream.str("");
817 strstream << states[2];
818 number = strstream.str();
819 //cout << "new state: " << states[2] << endl;
820 //cout << "number: " << states[2] << endl;
821 prev_state = number;
822 transition_from_state_count[number]++;
823 }
824 else if (trio[0] != gap && trio[1] == gap && trio[2] == gap) //INS 1
825 {
826 emission_count_temp[trio]++;
827 emission_in_states_count[states[3]]++;
828 prev_state_to_100[prev_state]++;
829 strstream.str("");
830 strstream << states[3];
831 number = strstream.str();
832 //cout << "new state: " << states[3] << endl;
833 //cout << "number: " << states[3] << endl;
834 prev_state = number;
835 transition_from_state_count[number]++;
836 }
837 else if (trio[0] != gap && trio[1] == gap && trio[2] != gap) //INS 1&3
838 {
839 emission_count_temp[trio]++;
840 emission_in_states_count[states[4]]++;
841 prev_state_to_101[prev_state]++;
842 strstream.str("");
843 strstream << states[4];
844 number = strstream.str();
845 //cout << "new state: " << states[4] << endl;
846 //cout << "number: " << states[4] << endl;
847 prev_state = number;
848 transition_from_state_count[number]++;
849 }
850 else if (trio[0] != gap && trio[1] != gap && trio[2] == gap) //INS 1&2
851 {
852 emission_count_temp[trio]++;
853 emission_in_states_count[states[5]]++;
854 prev_state_to_110[prev_state]++;
855 //cout << "prev_state_to_110 count: " << prev_state_to_110["110"] << endl;
856 strstream.str("");
857 strstream << states[5];
858 number = strstream.str();
859 //cout << "new state: " << states[5] << endl;
860 //cout << "number: " << states[5] << endl;
861 prev_state = number;
862 transition_from_state_count[number]++;
863 }
864 else if (trio[0] != gap && trio[1] != gap && trio[2] != gap) // transitions from aligned or mismatch state
865 {
866 emission_count_temp[trio]++;
867 emission_in_states_count[states[6]]++;
868 prev_state_to_111[prev_state]++;
869 //cout << "prev_state_to_111 count: " << prev_state_to_111["110"] << endl;
870 strstream.str("");
871 strstream << states[6];
872 number = strstream.str();
873 //cout << "new state: " << states[6] << endl;
874 //cout << "number: " << states[6] << endl;
875 prev_state = number;
876 transition_from_state_count[number]++;
877 }
878 //}
879 }
880
881
882 /*
883 for (int i = 0; i < seqs[seq_count].length(); i++)
884 {
885 trio[0] = seqs[seq_count][i];
886 trio[1] = seqs[seq_count + 1][i];
887 trio[2] = seqs[seq_count + 2][i];
888
889 if (trio[0] == gap && trio[1] == gap && trio[2] != gap)
890 emission_count_temp[trio]++;
891 }
892 */
893
894
895 /*
896 cout << "110_to_001 count: " << prev_state_to_001["110"] << endl;
897 cout << "110_to_010 count: " << prev_state_to_010["110"] << endl;
898 cout << "110_to_011 count: " << prev_state_to_011["110"] << endl;
899 cout << "110_to_100 count: " << prev_state_to_100["110"] << endl;
900 cout << "110_to_101 count: " << prev_state_to_101["110"] << endl;
901 cout << "110_to_110 count: " << prev_state_to_110["110"] << endl;
902 cout << "110_to_111 count: " << prev_state_to_111["110"] << endl;
903 */
904
905 /*
906 // calculating probabilities
907 for( map <string, double>::const_iterator it = emission_prob.begin(); it != emission_prob.end(); ++it )
908 {
909 trio = it->first;
910 double value = it->second;
911
912 if (trio[0] == gap && trio[1] == gap && trio[2] != gap) //INS 3
913 {
914 //cout << "emission in state 001 of with total count " << emission_in_states_count[states[0]] << endl;
915 //cout << "emission in state 001 of: " << trio << " with count " << emission_prob[trio] << endl;
916 emission_prob[trio] = emission_prob[trio] / emission_in_states_count[states[0]];
917 //transitions_from_001++;
918 }
919 else if (trio[0] == gap && trio[1] != gap && trio[2] == gap) //INS 2
920 {
921 emission_prob[trio] = emission_prob[trio] / emission_in_states_count[states[1]];
922 //transitions_from_010++;
923 }
924 else if (trio[0] == gap && trio[1] != gap && trio[2] != gap) //INS 2&3
925 {
926 emission_prob[trio] = emission_prob[trio] / emission_in_states_count[states[2]];
927 //transitions_from_011++;
928 }
929 else if (trio[0] != gap && trio[1] == gap && trio[2] == gap) //INS 1
930 {
931 emission_prob[trio] = emission_prob[trio] / emission_in_states_count[states[3]];
932 //transitions_from_100++;
933 }
934 else if (trio[0] != gap && trio[1] == gap && trio[2] != gap) //INS 1&3
935 {
936 emission_prob[trio] = emission_prob[trio] / emission_in_states_count[states[4]];
937 //transitions_from_101++;
938 }
939 else if (trio[0] != gap && trio[1] != gap && trio[2] == gap) //INS 1&2
940 {
941 emission_prob[trio] = emission_prob[trio] / emission_in_states_count[states[5]];
942 //transitions_from_110++;
943 }
944 else if (trio[0] != gap && trio[1] != gap && trio[2] != gap) // transitions from aligned or mismatch state
945 {
946 emission_prob[trio] = emission_prob[trio] / emission_in_states_count[states[6]];
947 //transitions_from_111++;
948 }
949 }
950
951 //cout << "emission prob for: " << "..U" << " in state 001: " << emission_prob["..U"] << endl;
952 //cout << "emission prob for: " << "..A" << " in state 001: " << emission_prob["..A"] << endl;
953 //cout << "emission prob for: " << "..C" << " in state 001: " << emission_prob["..C"] << endl;
954 //cout << "emission prob for: " << "..G" << " in state 001: " << emission_prob["..G"] << endl;
955
956
957 //cout << "emission probability for '-C-': " << emission_prob[".C."] << endl;
958
959 emission_prob_collection.push_back(emission_prob);
960
961 //int count = 0;
962
963 // calculating the probabilities for state transitions
964 for(int count = 0; count < states.size(); count++)
965 {
966 strstream.str("");
967 strstream << states[count];
968 number = strstream.str();
969 //cout << "state: " << number << endl;
970 //cout << "state: " << states[count] << endl;
971
972 if (transition_from_state_count[number] == 0)
973 transition_from_state_count[number] = 1;
974
975 //cout << "prev_state_to_001[number]: " << prev_state_to_001[number] << endl;
976 //cout << "transition_from_state_count[number]: " << transition_from_state_count[number] << endl;
977 prev_state_to_001[number] = prev_state_to_001[number] / transition_from_state_count[number];
978 //cout << "prev_state_to_001[number]: " << prev_state_to_001[number] << endl;
979 //if (prev_state_to_001[number] < 0)
980 // prev_state_to_001[number] = 0;
981
982 //cout << "prev_state_to_001[number]: " << prev_state_to_001[number] << endl;
983 //cout << "transition_from_state_count[number]: " << transition_from_state_count[number] << endl;
984 //cout << "prev_state_to_010[number]: " << prev_state_to_010[number] << endl;
985 prev_state_to_010[number] = prev_state_to_010[number] / transition_from_state_count[number];
986 //cout << "prev_state_to_010[number]: " << prev_state_to_010[number] << endl;
987 //if (prev_state_to_010[number] < 0)
988 // prev_state_to_010[number] = 0;
989
990 //cout << "prev_state_to_011[number]: " << prev_state_to_011[number] << endl;
991 //cout << "prev_state_to_001[number]: " << prev_state_to_001[number] << endl;
992 //cout << "transition_from_state_count[number]: " << transition_from_state_count[number] << endl;
993 prev_state_to_011[number] = prev_state_to_011[number] / transition_from_state_count[number];
994 //cout << "prev_state_to_011[number]: " << prev_state_to_011[number] << endl;
995 //if (prev_state_to_011[number] < 0)
996 // prev_state_to_011[number] = 0;
997
998 //cout << "prev_state_to_100[number]: " << prev_state_to_100[number] << endl;
999 //cout << "prev_state_to_001[number]: " << prev_state_to_001[number] << endl;
1000 //cout << "transition_from_state_count[number]: " << transition_from_state_count[number] << endl;
1001 prev_state_to_100[number] = prev_state_to_100[number] / transition_from_state_count[number];
1002 //cout << "prev_state_to_100[number]: " << prev_state_to_100[number] << endl;
1003 //if (prev_state_to_100[number] < 0)
1004 // prev_state_to_100[number] = 0;
1005
1006 //cout << "prev_state_to_101[number]: " << prev_state_to_101[number] << endl;
1007 //cout << "transition_from_state_count[number]: " << transition_from_state_count[number] << endl;
1008 prev_state_to_101[number] = prev_state_to_101[number] / transition_from_state_count[number];
1009 //cout << "prev_state_to_101[number]: " << prev_state_to_101[number] << endl;
1010 //if (prev_state_to_101[number] < 0)
1011 // prev_state_to_101[number] = 0;
1012
1013 //cout << "prev_state_to_110[number]: " << prev_state_to_110[number] << endl;
1014 //cout << "transition_from_state_count[number]: " << transition_from_state_count[number] << endl;
1015 prev_state_to_110[number] = prev_state_to_110[number] / transition_from_state_count[number];
1016 //cout << "prev_state_to_110[number]: " << prev_state_to_110[number] << endl;
1017 //if (prev_state_to_110[number] < 0)
1018 // prev_state_to_110[number] = 0;
1019
1020 //cout << "prev_state_to_111[number]: " << prev_state_to_111[number] << endl;
1021 //cout << "transition_from_state_count[number]: " << transition_from_state_count[number] << endl;
1022 prev_state_to_111[number] = prev_state_to_111[number] / transition_from_state_count[number];
1023 //cout << "prev_state_to_111[number]: " << prev_state_to_111[number] << endl;
1024 //if (prev_state_to_111[number] < 0)
1025 // prev_state_to_111[number] = 0;
1026 }
1027
1028
1029 prob_trans_to_001.push_back(prev_state_to_001);
1030 prob_trans_to_010.push_back(prev_state_to_010);
1031 prob_trans_to_011.push_back(prev_state_to_011);
1032 prob_trans_to_100.push_back(prev_state_to_100);
1033 prob_trans_to_101.push_back(prev_state_to_101);
1034 prob_trans_to_110.push_back(prev_state_to_110);
1035 prob_trans_to_111.push_back(prev_state_to_111);
1036
1037
1038 //cout << "prob. for transition (110->001): " << prev_state_to_001["110"] << endl;
1039 //cout << "prob. for transition (110->010): " << prev_state_to_010["110"] << endl;
1040 //cout << "prob. for transition (110->011): " << prev_state_to_011["110"] << endl;
1041 //cout << "prob. for transition (110->100): " << prev_state_to_100["110"] << endl;
1042 //cout << "prob. for transition (110->101): " << prev_state_to_101["110"] << endl;
1043 //cout << "prob. for transition (110->110): " << prev_state_to_110["110"] << endl;
1044 //cout << "prob. for transition (110->111): " << prev_state_to_111["110"] << endl;
1045
1046 }
1047 */
1048
1049
1050
1051
1052 /*
1053 prob_trans_to_001.push_back(prev_state_to_001);
1054 prob_trans_to_010.push_back(prev_state_to_010);
1055 prob_trans_to_011.push_back(prev_state_to_011);
1056 prob_trans_to_100.push_back(prev_state_to_100);
1057 prob_trans_to_101.push_back(prev_state_to_101);
1058 prob_trans_to_110.push_back(prev_state_to_110);
1059 prob_trans_to_111.push_back(prev_state_to_111);
1060 */
1061 }
1062
1063 cout << "end of iterations reached" << endl;
1064
1065
1066 // calculating probabilities
1067 for( map <string, double>::const_iterator it = emission_prob.begin(); it != emission_prob.end(); ++it )
1068 {
1069 trio = it->first;
1070 double value = it->second;
1071
1072 if (trio[0] == gap && trio[1] == gap && trio[2] != gap) //INS 3
1073 {
1074 cout << "emission in state 001 of with total count " << emission_in_states_count[states[0]] << endl;
1075 cout << "emission in state 001 of: " << trio << " with count " << emission_prob[trio] << endl;
1076 emission_prob[trio] = emission_prob[trio] / emission_in_states_count[states[0]];
1077 //transitions_from_001++;
1078 }
1079 else if (trio[0] == gap && trio[1] != gap && trio[2] == gap) //INS 2
1080 {
1081 emission_prob[trio] = emission_prob[trio] / emission_in_states_count[states[1]];
1082 //transitions_from_010++;
1083 }
1084 else if (trio[0] == gap && trio[1] != gap && trio[2] != gap) //INS 2&3
1085 {
1086 emission_prob[trio] = emission_prob[trio] / emission_in_states_count[states[2]];
1087 //transitions_from_011++;
1088 }
1089 else if (trio[0] != gap && trio[1] == gap && trio[2] == gap) //INS 1
1090 {
1091 emission_prob[trio] = emission_prob[trio] / emission_in_states_count[states[3]];
1092 //transitions_from_100++;
1093 }
1094 else if (trio[0] != gap && trio[1] == gap && trio[2] != gap) //INS 1&3
1095 {
1096 emission_prob[trio] = emission_prob[trio] / emission_in_states_count[states[4]];
1097 //transitions_from_101++;
1098 }
1099 else if (trio[0] != gap && trio[1] != gap && trio[2] == gap) //INS 1&2
1100 {
1101 emission_prob[trio] = emission_prob[trio] / emission_in_states_count[states[5]];
1102 //transitions_from_110++;
1103 }
1104 else if (trio[0] != gap && trio[1] != gap && trio[2] != gap) // transitions from aligned or mismatch state
1105 {
1106 emission_prob[trio] = emission_prob[trio] / emission_in_states_count[states[6]];
1107 //transitions_from_111++;
1108 }
1109 }
1110
1111
1112 cout << "emission prob for: " << "..U" << " in state 001: " << emission_prob["..U"] << endl;
1113 cout << "emission prob for: " << "..A" << " in state 001: " << emission_prob["..A"] << endl;
1114 cout << "emission prob for: " << "..C" << " in state 001: " << emission_prob["..C"] << endl;
1115 cout << "emission prob for: " << "..G" << " in state 001: " << emission_prob["..G"] << endl;
1116
1117
1118 //cout << "emission probability for '-C-': " << emission_prob[".C."] << endl;
1119
1120 //emission_prob_collection.push_back(emission_prob);
1121
1122 //int count = 0;
1123
1124
1125 // calculating the probabilities for state transitions
1126 for(int count = 0; count < states.size(); count++)
1127 {
1128 strstream.str("");
1129 strstream << states[count];
1130 number = strstream.str();
1131 //cout << "state: " << number << endl;
1132 //cout << "state: " << states[count] << endl;
1133
1134 if (transition_from_state_count[number] == 0)
1135 transition_from_state_count[number] = 1;
1136
1137 cout << "from state: " << number << endl;
1138 cout << "transition_from_state_count[number]: " << transition_from_state_count[number] << endl;
1139 cout << "prev_state_to_001[number]: " << prev_state_to_001[number] << endl;
1140 prev_state_to_001[number] = prev_state_to_001[number] / transition_from_state_count[number];
1141 //cout << "prev_state_to_001[number]: " << prev_state_to_001[number] << endl;
1142 //if (prev_state_to_001[number] < 0)
1143 // prev_state_to_001[number] = 0;
1144
1145 //cout << "prev_state_to_001[number]: " << prev_state_to_001[number] << endl;
1146 //cout << "transition_from_state_count[number]: " << transition_from_state_count[number] << endl;
1147 cout << "prev_state_to_010[number]: " << prev_state_to_010[number] << endl;
1148 prev_state_to_010[number] = prev_state_to_010[number] / transition_from_state_count[number];
1149 //cout << "prev_state_to_010[number]: " << prev_state_to_010[number] << endl;
1150 //if (prev_state_to_010[number] < 0)
1151 // prev_state_to_010[number] = 0;
1152
1153 cout << "prev_state_to_011[number]: " << prev_state_to_011[number] << endl;
1154 //cout << "prev_state_to_001[number]: " << prev_state_to_001[number] << endl;
1155 //cout << "transition_from_state_count[number]: " << transition_from_state_count[number] << endl;
1156 prev_state_to_011[number] = prev_state_to_011[number] / transition_from_state_count[number];
1157 //cout << "prev_state_to_011[number]: " << prev_state_to_011[number] << endl;
1158 //if (prev_state_to_011[number] < 0)
1159 // prev_state_to_011[number] = 0;
1160
1161 cout << "prev_state_to_100[number]: " << prev_state_to_100[number] << endl;
1162 //cout << "prev_state_to_001[number]: " << prev_state_to_001[number] << endl;
1163 //cout << "transition_from_state_count[number]: " << transition_from_state_count[number] << endl;
1164 prev_state_to_100[number] = prev_state_to_100[number] / transition_from_state_count[number];
1165 //cout << "prev_state_to_100[number]: " << prev_state_to_100[number] << endl;
1166 //if (prev_state_to_100[number] < 0)
1167 // prev_state_to_100[number] = 0;
1168
1169 cout << "prev_state_to_101[number]: " << prev_state_to_101[number] << endl;
1170 //cout << "transition_from_state_count[number]: " << transition_from_state_count[number] << endl;
1171 prev_state_to_101[number] = prev_state_to_101[number] / transition_from_state_count[number];
1172 //cout << "prev_state_to_101[number]: " << prev_state_to_101[number] << endl;
1173 //if (prev_state_to_101[number] < 0)
1174 // prev_state_to_101[number] = 0;
1175
1176 cout << "prev_state_to_110[number]: " << prev_state_to_110[number] << endl;
1177 //cout << "transition_from_state_count[number]: " << transition_from_state_count[number] << endl;
1178 prev_state_to_110[number] = prev_state_to_110[number] / transition_from_state_count[number];
1179 //cout << "prev_state_to_110[number]: " << prev_state_to_110[number] << endl;
1180 //if (prev_state_to_110[number] < 0)
1181 // prev_state_to_110[number] = 0;
1182
1183 cout << "prev_state_to_111[number]: " << prev_state_to_111[number] << endl;
1184 //cout << "transition_from_state_count[number]: " << transition_from_state_count[number] << endl;
1185 prev_state_to_111[number] = prev_state_to_111[number] / transition_from_state_count[number];
1186 //cout << "prev_state_to_111[number]: " << prev_state_to_111[number] << endl;
1187 //if (prev_state_to_111[number] < 0)
1188 // prev_state_to_111[number] = 0;
1189 }
1190
1191 }
1192
1193 void viterbi_matrix_initialisation()
1194 {
1195 for (int count1 = 0; count1 <= x1.length(); count1++)
1196 {
1197 V001.push_back(rowvec1);
1198 V010.push_back(rowvec1);
1199 V011.push_back(rowvec1);
1200 V100.push_back(rowvec1);
1201 V101.push_back(rowvec1);
1202 V110.push_back(rowvec1);
1203 V111.push_back(rowvec1);
1204
1205 for (int count2 = 0; count2 <= x2.length(); count2++)
1206 {
1207 V001[count1].push_back(rowvec2);
1208 V010[count1].push_back(rowvec2);
1209 V011[count1].push_back(rowvec2);
1210 V100[count1].push_back(rowvec2);
1211 V101[count1].push_back(rowvec2);
1212 V110[count1].push_back(rowvec2);
1213 V111[count1].push_back(rowvec2);
1214
1215 for (int count3 = 0; count3 <= x3.length(); count3++)
1216 {
1217 if (count1 == 0 || count2 == 0 || count3 == 0)
1218 {
1219 V001[count1][count2].push_back(-DBL_MAX);
1220 V010[count1][count2].push_back(-DBL_MAX);
1221 V011[count1][count2].push_back(-DBL_MAX);
1222 V100[count1][count2].push_back(-DBL_MAX);
1223 V101[count1][count2].push_back(-DBL_MAX);
1224 V110[count1][count2].push_back(-DBL_MAX);
1225 V111[count1][count2].push_back(-DBL_MAX);
1226 }
1227 else
1228 {
1229 V001[count1][count2].push_back(0);
1230 V010[count1][count2].push_back(0);
1231 V011[count1][count2].push_back(0);
1232 V100[count1][count2].push_back(0);
1233 V101[count1][count2].push_back(0);
1234 V110[count1][count2].push_back(0);
1235 V111[count1][count2].push_back(0);
1236 }
1237 }
1238 }
1239 }
1240
1241 V001[0][0][0] = -DBL_MAX;
1242 V010[0][0][0] = -DBL_MAX;
1243 V011[0][0][0] = -DBL_MAX;
1244 V100[0][0][0] = -DBL_MAX;
1245 V101[0][0][0] = -DBL_MAX;
1246 V110[0][0][0] = -DBL_MAX;
1247 V111[0][0][0] = 0;
1248
1249 /*cout << "printing matrices before filling: " << endl;
1250 cout << "printing V001 matrix: " << endl;
1251 for (dim3 = 0; dim3 <= x3.length(); dim3++)
1252 {
1253 cout << "dim3: " << dim3 << endl;
1254 for (dim1 = 0; dim1 <= x1.length(); dim1++)
1255 {
1256 cout << "dim1: " << dim1 << endl;
1257 for (dim2 = 0; dim2 <= x2.length(); dim2++)
1258 {
1259 cout << V001[dim1][dim2][dim3] << " ";
1260 }
1261 cout << endl;
1262 }
1263 }
1264
1265 cout << "printing V010 matrix: " << endl;
1266 for (dim3 = 0; dim3 <= x3.length(); dim3++)
1267 {
1268 cout << "dim3: " << dim3 << endl;
1269 for (dim1 = 0; dim1 <= x1.length(); dim1++)
1270 {
1271 cout << "dim1: " << dim1 << endl;
1272 for (dim2 = 0; dim2 <= x2.length(); dim2++)
1273 {
1274 cout << V010[dim1][dim2][dim3] << " ";
1275 }
1276 cout << endl;
1277 }
1278 }
1279
1280 cout << "printing V011 matrix: " << endl;
1281 for (dim3 = 0; dim3 <= x3.length(); dim3++)
1282 {
1283 cout << "dim3: " << dim3 << endl;
1284 for (dim1 = 0; dim1 <= x1.length(); dim1++)
1285 {
1286 cout << "dim1: " << dim1 << endl;
1287 for (dim2 = 0; dim2 <= x2.length(); dim2++)
1288 {
1289 cout << V011[dim1][dim2][dim3] << " ";
1290 }
1291 cout << endl;
1292 }
1293 }
1294
1295 cout << "printing V100 matrix: " << endl;
1296 for (dim3 = 0; dim3 <= x3.length(); dim3++)
1297 {
1298 cout << "dim3: " << dim3 << endl;
1299 for (dim1 = 0; dim1 <= x1.length(); dim1++)
1300 {
1301 cout << "dim1: " << dim1 << endl;
1302 for (dim2 = 0; dim2 <= x2.length(); dim2++)
1303 {
1304 cout << V100[dim1][dim2][dim3] << " ";
1305 }
1306 cout << endl;
1307 }
1308 }
1309
1310 cout << "printing V101 matrix: " << endl;
1311 for (dim3 = 0; dim3 <= x3.length(); dim3++)
1312 {
1313 cout << "dim3: " << dim3 << endl;
1314 for (dim1 = 0; dim1 <= x1.length(); dim1++)
1315 {
1316 cout << "dim1: " << dim1 << endl;
1317 for (dim2 = 0; dim2 <= x2.length(); dim2++)
1318 {
1319 cout << V101[dim1][dim2][dim3] << " ";
1320 }
1321 cout << endl;
1322 }
1323 }
1324
1325 cout << "printing V110 matrix: " << endl;
1326 for (dim3 = 0; dim3 <= x3.length(); dim3++)
1327 {
1328 cout << "dim3: " << dim3 << endl;
1329 for (dim1 = 0; dim1 <= x1.length(); dim1++)
1330 {
1331 cout << "dim1: " << dim1 << endl;
1332 for (dim2 = 0; dim2 <= x2.length(); dim2++)
1333 {
1334 cout << V110[dim1][dim2][dim3] << " ";
1335 }
1336 cout << endl;
1337 }
1338 }
1339
1340 cout << "printing V111 matrix: " << endl;
1341 for (dim3 = 0; dim3 <= x3.length(); dim3++)
1342 {
1343 cout << "dim3: " << dim3 << endl;
1344 for (dim1 = 0; dim1 <= x1.length(); dim1++)
1345 {
1346 cout << "dim1: " << dim1 << endl;
1347 for (dim2 = 0; dim2 <= x2.length(); dim2++)
1348 {
1349 cout << V111[dim1][dim2][dim3] << " ";
1350 }
1351 cout << endl;
1352 }
1353 }*/
1354 }
1355
1356 void viterbi_3seq()
1357 {
1358 double v001_1, v001_2, v001_3, v001_4, v001_5, v001_6, v001_7;
1359 double v010_1, v010_2, v010_3, v010_4, v010_5, v010_6, v010_7;
1360 double v011_1, v011_2, v011_3, v011_4, v011_5, v011_6, v011_7;
1361 double v100_1, v100_2, v100_3, v100_4, v100_5, v100_6, v100_7;
1362 double v101_1, v101_2, v101_3, v101_4, v101_5, v101_6, v101_7;
1363 double v110_1, v110_2, v110_3, v110_4, v110_5, v110_6, v110_7;
1364 double v111_1, v111_2, v111_3, v111_4, v111_5, v111_6, v111_7;
1365
1366 double temp_state_111, temp_state_110, temp_state_101, temp_state_100, temp_state_011, temp_state_010, temp_state_001, tempp_state;
1367 double matrix_111, matrix_110, matrix_101, matrix_100, matrix_011, matrix_010, matrix_001;
1368 double temp_emission;
1369 double temp_total;
1370
1371
1372 // filling matrices
1373 for (dim1 = 1; dim1 <= x1.length(); dim1++)
1374 {
1375 for (dim2 = 1; dim2 <= x2.length(); dim2++)
1376 {
1377 for (dim3 = 1; dim3 <= x3.length(); dim3++)
1378 {
1379
1380 // ALIGNMENT matrix
1381 trio[0] = x1[dim1-1];
1382 trio[1] = x2[dim2-1];
1383 trio[2] = x3[dim3-1];
1384
1385 //cout << "trio: " << trio << endl;
1386
1387 temp_state_111 = log(prev_state_to_111["111"]);
1388 temp_state_110 = log(prev_state_to_111["110"]);
1389 temp_state_101 = log(prev_state_to_111["101"]);
1390 temp_state_100 = log(prev_state_to_111["100"]);
1391 temp_state_011 = log(prev_state_to_111["11"]);
1392 temp_state_010 = log(prev_state_to_111["10"]);
1393 temp_state_001 = log(prev_state_to_111["1"]);
1394 //tempp_state = log(prev_state_to_111["001"]);
1395
1396 matrix_111 = V111[dim1 - 1][dim2 - 1][dim3 - 1];
1397 matrix_111 = V110[dim1 - 1][dim2 - 1][dim3 - 1];
1398 matrix_111 = V101[dim1 - 1][dim2 - 1][dim3 - 1];
1399 matrix_111 = V100[dim1 - 1][dim2 - 1][dim3 - 1];
1400 matrix_111 = V011[dim1 - 1][dim2 - 1][dim3 - 1];
1401 matrix_111 = V010[dim1 - 1][dim2 - 1][dim3 - 1];
1402 matrix_111 = V001[dim1 - 1][dim2 - 1][dim3 - 1];
1403
1404 temp_emission = log(emission_prob[trio]);
1405 temp_emission = log(emission_double[alpha_indexes_emissions[trio]][6]);
1406
1407 v111_1 = log(prev_state_to_111["111"]) + V111[dim1 - 1][dim2 - 1][dim3 - 1];
1408 v111_2 = log(prev_state_to_111["110"]) + V110[dim1 - 1][dim2 - 1][dim3 - 1];
1409 v111_3 = log(prev_state_to_111["101"]) + V101[dim1 - 1][dim2 - 1][dim3 - 1];
1410 v111_4 = log(prev_state_to_111["100"]) + V100[dim1 - 1][dim2 - 1][dim3 - 1];
1411 v111_5 = log(prev_state_to_111["11"]) + V011[dim1 - 1][dim2 - 1][dim3 - 1];
1412 v111_6 = log(prev_state_to_111["10"]) + V010[dim1 - 1][dim2 - 1][dim3 - 1];
1413 v111_7 = log(prev_state_to_111["1"]) + V001[dim1 - 1][dim2 - 1][dim3 - 1];
1414
1415 // temp_total = log(emission_prob[trio]) + maximum(v111_1, v111_2, v111_3, v111_4, v111_5, v111_6, v111_7);
1416
1417 /*cout << "dim1: " << dim1 << " dim2: " << dim2 << " dim3: " << dim3 << endl;
1418 cout << "trio: " << trio << endl;
1419 //cout << "emission prob:" << log(emission_prob[trio]) << endl;
1420 cout << "state transition: " << log(prev_state_to_111["111"]) << endl;
1421 cout << "emission prob log: " << log(emission_double[alpha_indexes_emissions[trio]][6]) << endl;
1422 cout << "emission prob: " << emission_double[alpha_indexes_emissions[trio]][6] << endl;
1423 cout << "elements: " << v111_1 << " " << v111_2 << " "<< v111_3 << " "<< v111_4 << " "<< v111_5 << " "<< v111_6 << " "<< v111_7 << endl;*/
1424
1425
1426
1427 //V111[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v111_1, v111_2, v111_3, v111_4, v111_5, v111_6, v111_7);
1428 //V111[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][6]) + maximum(v111_1, v111_2, v111_3, v111_4, v111_5, v111_6, v111_7);
1429 V111[dim1][dim2][dim3] = log(emission_double[emission_indexes[trio]][6]) + maximum(v111_1, v111_2, v111_3, v111_4, v111_5, v111_6, v111_7);
1430
1431 // INS3 matrix
1432
1433 trio[0] = gap;
1434 trio[1] = gap;
1435 trio[2] = x3[dim3-1];
1436
1437 temp_state_111 = log(prev_state_to_001["111"]);
1438 temp_state_110 = log(prev_state_to_001["110"]);
1439 temp_state_101 = log(prev_state_to_001["101"]);
1440 temp_state_100 = log(prev_state_to_001["100"]);
1441 temp_state_011 = log(prev_state_to_001["11"]);
1442 temp_state_010 = log(prev_state_to_001["10"]);
1443 temp_state_001 = log(prev_state_to_001["1"]);
1444
1445 matrix_111 = V111[dim1][dim2][dim3 - 1];
1446 matrix_110 = V110[dim1][dim2][dim3 - 1];
1447 matrix_101 = V101[dim1][dim2][dim3 - 1];
1448 matrix_100 = V100[dim1][dim2][dim3 - 1];
1449 matrix_011 = V011[dim1][dim2][dim3 - 1];
1450 matrix_010 = V010[dim1][dim2][dim3 - 1];
1451 matrix_001 = V001[dim1][dim2][dim3 - 1];
1452
1453 temp_emission = log(emission_prob[trio]);
1454 temp_emission = log(emission_double[alpha_indexes_emissions[trio]][0]);
1455
1456 v001_1 = log(prev_state_to_001["111"]) + V111[dim1][dim2][dim3 - 1];
1457 v001_2 = log(prev_state_to_001["110"]) + V110[dim1][dim2][dim3 - 1];
1458 v001_3 = log(prev_state_to_001["101"]) + V101[dim1][dim2][dim3 - 1];
1459 v001_4 = log(prev_state_to_001["100"]) + V100[dim1][dim2][dim3 - 1];
1460 v001_5 = log(prev_state_to_001["11"]) + V011[dim1][dim2][dim3 - 1];
1461 v001_6 = log(prev_state_to_001["10"]) + V010[dim1][dim2][dim3 - 1];
1462 v001_7 = log(prev_state_to_001["1"]) + V001[dim1][dim2][dim3 - 1];
1463
1464 temp_total = log(emission_prob[trio]) + maximum(v001_1, v001_2, v001_3, v001_4, v001_5, v001_6, v001_7);
1465
1466 //V001[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v001_1, v001_2, v001_3, v001_4, v001_5, v001_6, v001_7);
1467 //V001[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][0]) + maximum(v001_1, v001_2, v001_3, v001_4, v001_5, v001_6, v001_7);
1468 V001[dim1][dim2][dim3] = log(emission_double[emission_indexes[trio]][0]) + maximum(v001_1, v001_2, v001_3, v001_4, v001_5, v001_6, v001_7);
1469
1470 // INS2 matrix
1471
1472 trio[0] = gap;
1473 trio[1] = x2[dim2-1];
1474 trio[2] = gap;
1475
1476 temp_state_111 = log(prev_state_to_010["111"]);
1477 temp_state_110 = log(prev_state_to_010["110"]);
1478 temp_state_101 = log(prev_state_to_010["101"]);
1479 temp_state_100 = log(prev_state_to_010["100"]);
1480 temp_state_011 = log(prev_state_to_010["11"]);
1481 temp_state_010 = log(prev_state_to_010["10"]);
1482 temp_state_001 = log(prev_state_to_010["1"]);
1483
1484 matrix_111 = V111[dim1][dim2 - 1][dim3];
1485 matrix_110 = V110[dim1][dim2 - 1][dim3];
1486 matrix_101 = V101[dim1][dim2 - 1][dim3];
1487 matrix_100 = V100[dim1][dim2 - 1][dim3];
1488 matrix_011 = V011[dim1][dim2 - 1][dim3];
1489 matrix_010 = V010[dim1][dim2 - 1][dim3];
1490 matrix_001 = V001[dim1][dim2 - 1][dim3];
1491
1492 temp_emission = log(emission_prob[trio]);
1493 temp_emission = log(emission_double[alpha_indexes_emissions[trio]][1]);
1494
1495 v010_1 = log(prev_state_to_010["111"]) + V111[dim1][dim2 - 1][dim3];
1496 v010_2 = log(prev_state_to_010["110"]) + V110[dim1][dim2 - 1][dim3];
1497 v010_3 = log(prev_state_to_010["101"]) + V101[dim1][dim2 - 1][dim3];
1498 v010_4 = log(prev_state_to_010["100"]) + V100[dim1][dim2 - 1][dim3];
1499 v010_5 = log(prev_state_to_010["11"]) + V011[dim1][dim2 - 1][dim3];
1500 v010_6 = log(prev_state_to_010["10"]) + V010[dim1][dim2 - 1][dim3];
1501 v010_7 = log(prev_state_to_010["1"]) + V001[dim1][dim2 - 1][dim3];
1502
1503 temp_total = log(emission_prob[trio]) + maximum(v010_1, v010_2, v010_3, v010_4, v010_5, v010_6, v010_7);
1504
1505 //V010[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v010_1, v010_2, v010_3, v010_4, v010_5, v010_6, v010_7);
1506 //V010[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][1]) + maximum(v010_1, v010_2, v010_3, v010_4, v010_5, v010_6, v010_7);
1507 V010[dim1][dim2][dim3] = log(emission_double[emission_indexes[trio]][1]) + maximum(v010_1, v010_2, v010_3, v010_4, v010_5, v010_6, v010_7);
1508
1509 // INS23 matrix
1510
1511 trio[0] = gap;
1512 trio[1] = x2[dim2-1];
1513 trio[2] = x3[dim3-1];
1514
1515 temp_state_111 = log(prev_state_to_011["111"]);
1516 temp_state_110 = log(prev_state_to_011["110"]);
1517 temp_state_101 = log(prev_state_to_011["101"]);
1518 temp_state_100 = log(prev_state_to_011["100"]);
1519 temp_state_011 = log(prev_state_to_011["11"]);
1520 temp_state_010 = log(prev_state_to_011["10"]);
1521 temp_state_001 = log(prev_state_to_011["1"]);
1522
1523 matrix_111 = V111[dim1][dim2 - 1][dim3 - 1];
1524 matrix_110 = V110[dim1][dim2 - 1][dim3 - 1];
1525 matrix_101 = V101[dim1][dim2 - 1][dim3 - 1];
1526 matrix_100 = V100[dim1][dim2 - 1][dim3 - 1];
1527 matrix_011 = V011[dim1][dim2 - 1][dim3 - 1];
1528 matrix_010 = V010[dim1][dim2 - 1][dim3 - 1];
1529 matrix_001 = V001[dim1][dim2 - 1][dim3 - 1];
1530
1531 temp_emission = log(emission_prob[trio]);
1532 temp_emission = log(emission_double[alpha_indexes_emissions[trio]][2]);
1533
1534 v011_1 = log(prev_state_to_011["111"]) + V111[dim1][dim2 - 1][dim3 - 1];
1535 v011_2 = log(prev_state_to_011["110"]) + V110[dim1][dim2 - 1][dim3 - 1];
1536 v011_3 = log(prev_state_to_011["101"]) + V101[dim1][dim2 - 1][dim3 - 1];
1537 v011_4 = log(prev_state_to_011["100"]) + V100[dim1][dim2 - 1][dim3 - 1];
1538 v011_5 = log(prev_state_to_011["11"]) + V011[dim1][dim2 - 1][dim3 - 1];
1539 v011_6 = log(prev_state_to_011["10"]) + V010[dim1][dim2 - 1][dim3 - 1];
1540 v011_7 = log(prev_state_to_011["1"]) + V001[dim1][dim2 - 1][dim3 - 1];
1541
1542 temp_total = log(emission_prob[trio]) + maximum(v011_1, v011_2, v011_3, v011_4, v011_5, v011_6, v011_7);
1543
1544 //V011[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v011_1, v011_2, v011_3, v011_4, v011_5, v011_6, v011_7);
1545 //V011[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][2]) + maximum(v011_1, v011_2, v011_3, v011_4, v011_5, v011_6, v011_7);
1546 V011[dim1][dim2][dim3] = log(emission_double[emission_indexes[trio]][2]) + maximum(v011_1, v011_2, v011_3, v011_4, v011_5, v011_6, v011_7);
1547
1548 // INS1 matrix
1549
1550 trio[0] = x1[dim1-1];
1551 trio[1] = gap;
1552 trio[2] = gap;
1553
1554 temp_state_111 = log(prev_state_to_100["111"]);
1555 temp_state_110 = log(prev_state_to_100["110"]);
1556 temp_state_101 = log(prev_state_to_100["101"]);
1557 temp_state_100 = log(prev_state_to_100["100"]);
1558 temp_state_011 = log(prev_state_to_100["11"]);
1559 temp_state_010 = log(prev_state_to_100["10"]);
1560 temp_state_001 = log(prev_state_to_100["1"]);
1561
1562 matrix_111 = V111[dim1 - 1][dim2][dim3];
1563 matrix_110 = V110[dim1 - 1][dim2][dim3];
1564 matrix_101 = V101[dim1 - 1][dim2][dim3];
1565 matrix_100 = V100[dim1 - 1][dim2][dim3];
1566 matrix_011 = V011[dim1 - 1][dim2][dim3];
1567 matrix_010 = V010[dim1 - 1][dim2][dim3];
1568 matrix_001 = V001[dim1 - 1][dim2][dim3];
1569
1570 temp_emission = log(emission_prob[trio]);
1571 temp_emission = log(emission_double[alpha_indexes_emissions[trio]][3]);
1572
1573 v100_1 = log(prev_state_to_100["111"]) + V111[dim1 - 1][dim2][dim3];
1574 v100_2 = log(prev_state_to_100["110"]) + V110[dim1 - 1][dim2][dim3];
1575 v100_3 = log(prev_state_to_100["101"]) + V101[dim1 - 1][dim2][dim3];
1576 v100_4 = log(prev_state_to_100["100"]) + V100[dim1 - 1][dim2][dim3];
1577 v100_5 = log(prev_state_to_100["11"]) + V011[dim1 - 1][dim2][dim3];
1578 v100_6 = log(prev_state_to_100["10"]) + V010[dim1 - 1][dim2][dim3];
1579 v100_7 = log(prev_state_to_100["1"]) + V001[dim1 - 1][dim2][dim3];
1580
1581 temp_total = log(emission_prob[trio]) + maximum(v100_1, v100_2, v100_3, v100_4, v100_5, v100_6, v100_7);
1582
1583 //V100[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v100_1, v100_2, v100_3, v100_4, v100_5, v100_6, v100_7);
1584 //V100[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][3]) + maximum(v100_1, v100_2, v100_3, v100_4, v100_5, v100_6, v100_7);
1585 V100[dim1][dim2][dim3] = log(emission_double[emission_indexes[trio]][3]) + maximum(v100_1, v100_2, v100_3, v100_4, v100_5, v100_6, v100_7);
1586
1587 // INS13 matrix
1588
1589 trio[0] = x1[dim1-1];
1590 trio[1] = gap;
1591 trio[2] = x3[dim3-1];
1592
1593 temp_state_111 = log(prev_state_to_101["111"]);
1594 temp_state_110 = log(prev_state_to_101["110"]);
1595 temp_state_101 = log(prev_state_to_101["101"]);
1596 temp_state_100 = log(prev_state_to_101["100"]);
1597 temp_state_011 = log(prev_state_to_101["11"]);
1598 temp_state_010 = log(prev_state_to_101["10"]);
1599 temp_state_001 = log(prev_state_to_101["1"]);
1600
1601 matrix_111 = V111[dim1 - 1][dim2][dim3 - 1];
1602 matrix_110 = V110[dim1 - 1][dim2][dim3 - 1];
1603 matrix_101 = V101[dim1 - 1][dim2][dim3 - 1];
1604 matrix_100 = V100[dim1 - 1][dim2][dim3 - 1];
1605 matrix_011 = V011[dim1 - 1][dim2][dim3 - 1];
1606 matrix_010 = V010[dim1 - 1][dim2][dim3 - 1];
1607 matrix_001 = V001[dim1 - 1][dim2][dim3 - 1];
1608
1609 temp_emission = log(emission_prob[trio]);
1610 temp_emission = log(emission_double[alpha_indexes_emissions[trio]][4]);
1611
1612 v101_1 = log(prev_state_to_101["111"]) + V111[dim1 - 1][dim2][dim3 - 1];
1613 v101_2 = log(prev_state_to_101["110"]) + V110[dim1 - 1][dim2][dim3 - 1];
1614 v101_3 = log(prev_state_to_101["101"]) + V101[dim1 - 1][dim2][dim3 - 1];
1615 v101_4 = log(prev_state_to_101["100"]) + V100[dim1 - 1][dim2][dim3 - 1];
1616 v101_5 = log(prev_state_to_101["11"]) + V011[dim1 - 1][dim2][dim3 - 1];
1617 v101_6 = log(prev_state_to_101["10"]) + V010[dim1 - 1][dim2][dim3 - 1];
1618 v101_7 = log(prev_state_to_101["1"]) + V001[dim1 - 1][dim2][dim3 - 1];
1619
1620 temp_total = log(emission_prob[trio]) + maximum(v101_1, v101_2, v101_3, v101_4, v101_5, v101_6, v101_7);
1621
1622 //V101[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v101_1, v101_2, v101_3, v101_4, v101_5, v101_6, v101_7);
1623 //V101[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][4]) + maximum(v101_1, v101_2, v101_3, v101_4, v101_5, v101_6, v101_7);
1624 V101[dim1][dim2][dim3] = log(emission_double[emission_indexes[trio]][4]) + maximum(v101_1, v101_2, v101_3, v101_4, v101_5, v101_6, v101_7);
1625
1626 // INS12 matrix
1627
1628 trio[0] = x1[dim1-1];
1629 trio[1] = x2[dim2-1];
1630 trio[2] = gap;
1631
1632 temp_state_111 = log(prev_state_to_110["111"]);
1633 temp_state_110 = log(prev_state_to_110["110"]);
1634 temp_state_101 = log(prev_state_to_110["101"]);
1635 temp_state_100 = log(prev_state_to_110["100"]);
1636 temp_state_011 = log(prev_state_to_110["11"]);
1637 temp_state_010 = log(prev_state_to_110["10"]);
1638 temp_state_001 = log(prev_state_to_110["1"]);
1639
1640 matrix_111 = V111[dim1 - 1][dim2 - 1][dim3];
1641 matrix_110 = V110[dim1 - 1][dim2 - 1][dim3];
1642 matrix_101 = V101[dim1 - 1][dim2 - 1][dim3];
1643 matrix_100 = V100[dim1 - 1][dim2 - 1][dim3];
1644 matrix_011 = V011[dim1 - 1][dim2 - 1][dim3];
1645 matrix_010 = V010[dim1 - 1][dim2 - 1][dim3];
1646 matrix_001 = V001[dim1 - 1][dim2 - 1][dim3];
1647
1648 temp_emission = log(emission_prob[trio]);
1649 temp_emission = log(emission_double[alpha_indexes_emissions[trio]][5]);
1650
1651 v110_1 = log(prev_state_to_110["111"]) + V111[dim1 - 1][dim2 - 1][dim3];
1652 v110_2 = log(prev_state_to_110["110"]) + V110[dim1 - 1][dim2 - 1][dim3];
1653 v110_3 = log(prev_state_to_110["101"]) + V101[dim1 - 1][dim2 - 1][dim3];
1654 v110_4 = log(prev_state_to_110["100"]) + V100[dim1 - 1][dim2 - 1][dim3];
1655 v110_5 = log(prev_state_to_110["11"]) + V011[dim1 - 1][dim2 - 1][dim3];
1656 v110_6 = log(prev_state_to_110["10"]) + V010[dim1 - 1][dim2 - 1][dim3];
1657 v110_7 = log(prev_state_to_110["1"]) + V001[dim1 - 1][dim2 - 1][dim3];
1658
1659 temp_total = log(emission_prob[trio]) + maximum(v110_1, v110_2, v110_3, v110_4, v110_5, v110_6, v110_7);
1660
1661 //V110[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v110_1, v110_2, v110_3, v110_4, v110_5, v110_6, v110_7);
1662 //V110[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][5]) + maximum(v110_1, v110_2, v110_3, v110_4, v110_5, v110_6, v110_7);
1663 V110[dim1][dim2][dim3] = log(emission_double[emission_indexes[trio]][5]) + maximum(v110_1, v110_2, v110_3, v110_4, v110_5, v110_6, v110_7);
1664
1665 }
1666 }
1667 }
1668
1669 /*
1670 // printing matrices
1671 cout << "printing matrices after filling: " << endl;
1672 cout << "printing V001 matrix: " << endl;
1673 for (dim3 = 0; dim3 <= x3.length(); dim3++)
1674 {
1675 cout << "dim3: " << dim3 << endl;
1676 for (dim1 = 0; dim1 <= x1.length(); dim1++)
1677 {
1678 cout << "dim1: " << dim1 << endl;
1679 for (dim2 = 0; dim2 <= x2.length(); dim2++)
1680 {
1681 cout << V001[dim1][dim2][dim3] << " ";
1682 }
1683 cout << endl;
1684 }
1685 }
1686
1687 cout << "printing V010 matrix: " << endl;
1688 for (dim3 = 0; dim3 <= x3.length(); dim3++)
1689 {
1690 cout << "dim3: " << dim3 << endl;
1691 for (dim1 = 0; dim1 <= x1.length(); dim1++)
1692 {
1693 cout << "dim1: " << dim1 << endl;
1694 for (dim2 = 0; dim2 <= x2.length(); dim2++)
1695 {
1696 cout << V010[dim1][dim2][dim3] << " ";
1697 }
1698 cout << endl;
1699 }
1700 }
1701
1702 cout << "printing V011 matrix: " << endl;
1703 for (dim3 = 0; dim3 <= x3.length(); dim3++)
1704 {
1705 cout << "dim3: " << dim3 << endl;
1706 for (dim1 = 0; dim1 <= x1.length(); dim1++)
1707 {
1708 cout << "dim1: " << dim1 << endl;
1709 for (dim2 = 0; dim2 <= x2.length(); dim2++)
1710 {
1711 cout << V011[dim1][dim2][dim3] << " ";
1712 }
1713 cout << endl;
1714 }
1715 }
1716
1717 cout << "printing V100 matrix: " << endl;
1718 for (dim3 = 0; dim3 <= x3.length(); dim3++)
1719 {
1720 cout << "dim3: " << dim3 << endl;
1721 for (dim1 = 0; dim1 <= x1.length(); dim1++)
1722 {
1723 cout << "dim1: " << dim1 << endl;
1724 for (dim2 = 0; dim2 <= x2.length(); dim2++)
1725 {
1726 cout << V100[dim1][dim2][dim3] << " ";
1727 }
1728 cout << endl;
1729 }
1730 }
1731
1732 cout << "printing V101 matrix: " << endl;
1733 for (dim3 = 0; dim3 <= x3.length(); dim3++)
1734 {
1735 cout << "dim3: " << dim3 << endl;
1736 for (dim1 = 0; dim1 <= x1.length(); dim1++)
1737 {
1738 cout << "dim1: " << dim1 << endl;
1739 for (dim2 = 0; dim2 <= x2.length(); dim2++)
1740 {
1741 cout << V101[dim1][dim2][dim3] << " ";
1742 }
1743 cout << endl;
1744 }
1745 }
1746
1747 cout << "printing V110 matrix: " << endl;
1748 for (dim3 = 0; dim3 <= x3.length(); dim3++)
1749 {
1750 cout << "dim3: " << dim3 << endl;
1751 for (dim1 = 0; dim1 <= x1.length(); dim1++)
1752 {
1753 cout << "dim1: " << dim1 << endl;
1754 for (dim2 = 0; dim2 <= x2.length(); dim2++)
1755 {
1756 cout << V110[dim1][dim2][dim3] << " ";
1757 }
1758 cout << endl;
1759 }
1760 }
1761
1762 cout << "printing V111 matrix: " << endl;
1763 for (dim3 = 0; dim3 <= x3.length(); dim3++)
1764 {
1765 cout << "dim3: " << dim3 << endl;
1766 for (dim1 = 0; dim1 <= x1.length(); dim1++)
1767 {
1768 cout << "dim1: " << dim1 << endl;
1769 for (dim2 = 0; dim2 <= x2.length(); dim2++)
1770 {
1771 cout << V111[dim1][dim2][dim3] << " ";
1772 }
1773 cout << endl;
1774 }
1775 }
1776 */
1777
1778
1779 //cout << "value: " << V100[4][3][2];
1780
1781
1782 // traceback
1783 dim1 = x1.length();
1784 dim2 = x2.length();
1785 dim3 = x3.length();
1786
1787 prev_state = " ";
1788 //double tempp = maximum(V001[dim1][dim2][dim3], V010[dim1][dim2][dim3], V011[dim1][dim2][dim3], V100[dim1][dim2][dim3], V101[dim1][dim2][dim3], V110[dim1][dim2][dim3], V111[dim1][dim2][dim3]);
1789 //double tempp001 = V001[dim1][dim2][dim3];
1790 //double tempp100 = V100[dim1][dim2][dim3];
1791 if (V001[dim1][dim2][dim3] == maximum(V001[dim1][dim2][dim3], V010[dim1][dim2][dim3], V011[dim1][dim2][dim3], V100[dim1][dim2][dim3], V101[dim1][dim2][dim3], V110[dim1][dim2][dim3], V111[dim1][dim2][dim3]))
1792 prev_state = "001";
1793 if (V010[dim1][dim2][dim3] == maximum(V001[dim1][dim2][dim3], V010[dim1][dim2][dim3], V011[dim1][dim2][dim3], V100[dim1][dim2][dim3], V101[dim1][dim2][dim3], V110[dim1][dim2][dim3], V111[dim1][dim2][dim3]))
1794 prev_state = "010";
1795 if (V011[dim1][dim2][dim3] == maximum(V001[dim1][dim2][dim3], V010[dim1][dim2][dim3], V011[dim1][dim2][dim3], V100[dim1][dim2][dim3], V101[dim1][dim2][dim3], V110[dim1][dim2][dim3], V111[dim1][dim2][dim3]))
1796 prev_state = "011";
1797 if (V100[dim1][dim2][dim3] == maximum(V001[dim1][dim2][dim3], V010[dim1][dim2][dim3], V011[dim1][dim2][dim3], V100[dim1][dim2][dim3], V101[dim1][dim2][dim3], V110[dim1][dim2][dim3], V111[dim1][dim2][dim3]))
1798 prev_state = "100";
1799 if (V101[dim1][dim2][dim3] == maximum(V001[dim1][dim2][dim3], V010[dim1][dim2][dim3], V011[dim1][dim2][dim3], V100[dim1][dim2][dim3], V101[dim1][dim2][dim3], V110[dim1][dim2][dim3], V111[dim1][dim2][dim3]))
1800 prev_state = "101";
1801 if (V110[dim1][dim2][dim3] == maximum(V001[dim1][dim2][dim3], V010[dim1][dim2][dim3], V011[dim1][dim2][dim3], V100[dim1][dim2][dim3], V101[dim1][dim2][dim3], V110[dim1][dim2][dim3], V111[dim1][dim2][dim3]))
1802 prev_state = "110";
1803 if (V111[dim1][dim2][dim3] == maximum(V001[dim1][dim2][dim3], V010[dim1][dim2][dim3], V011[dim1][dim2][dim3], V100[dim1][dim2][dim3], V101[dim1][dim2][dim3], V110[dim1][dim2][dim3], V111[dim1][dim2][dim3]))
1804 prev_state = "111";
1805
1806 while(dim1 >= 1 && dim2 >= 1 && dim3 >= 1)
1807 {
1808
1809 double temp_max = 0;
1810
1811 if (prev_state == "001") // Ins3
1812 {
1813 v001_1 = log(prev_state_to_001["111"]) + V111[dim1][dim2][dim3 - 1];
1814 v001_2 = log(prev_state_to_001["110"]) + V110[dim1][dim2][dim3 - 1];
1815 v001_3 = log(prev_state_to_001["101"]) + V101[dim1][dim2][dim3 - 1];
1816 v001_4 = log(prev_state_to_001["100"]) + V100[dim1][dim2][dim3 - 1];
1817 v001_5 = log(prev_state_to_001["11"]) + V011[dim1][dim2][dim3 - 1];
1818 v001_6 = log(prev_state_to_001["10"]) + V010[dim1][dim2][dim3 - 1];
1819 v001_7 = log(prev_state_to_001["1"]) + V001[dim1][dim2][dim3 - 1];
1820
1821 temp_max = maximum(v001_1, v001_2, v001_3, v001_4, v001_5, v001_6, v001_7);
1822
1823 if (fabs(v001_1 - temp_max) < almost_zero)
1824 {
1825 prev_state = "111";
1826 }
1827 else if (fabs(v001_2 - temp_max) < almost_zero)
1828 {
1829 prev_state = "110";
1830 }
1831 else if (fabs(v001_3 - temp_max) < almost_zero)
1832 {
1833 prev_state = "101";
1834 }
1835 else if (fabs(v001_4 - temp_max) < almost_zero)
1836 {
1837 prev_state = "100";
1838 }
1839 else if (fabs(v001_5 - temp_max) < almost_zero)
1840 {
1841 prev_state = "011";
1842 }
1843 else if (fabs(v001_6 - temp_max) < almost_zero)
1844 {
1845 prev_state = "010";
1846 }
1847 else if (fabs(v001_7 - temp_max) < almost_zero)
1848 {
1849 prev_state = "001";
1850 }
1851 else
1852 {
1853 cout << "No transition possible." << endl;
1854 }
1855
1856 x1_aligned.push_back(gap);
1857 x2_aligned.push_back(gap);
1858 x3_aligned.push_back(x3[dim3-1]);
1859 dim3--;
1860 }
1861 else if (prev_state == "010") //Ins2
1862 {
1863 v010_1 = log(prev_state_to_010["111"]) + V111[dim1][dim2 - 1][dim3];
1864 v010_2 = log(prev_state_to_010["110"]) + V110[dim1][dim2 - 1][dim3];
1865 v010_3 = log(prev_state_to_010["101"]) + V101[dim1][dim2 - 1][dim3];
1866 v010_4 = log(prev_state_to_010["100"]) + V100[dim1][dim2 - 1][dim3];
1867 v010_5 = log(prev_state_to_010["11"]) + V011[dim1][dim2 - 1][dim3];
1868 v010_6 = log(prev_state_to_010["10"]) + V010[dim1][dim2 - 1][dim3];
1869 v010_7 = log(prev_state_to_010["1"]) + V001[dim1][dim2 - 1][dim3];
1870
1871 temp_max = maximum(v010_1, v010_2, v010_3, v010_4, v010_5, v010_6, v010_7);
1872
1873 if (fabs(v010_1 - temp_max) < almost_zero)
1874 {
1875 prev_state = "111";
1876 }
1877 else if (fabs(v010_2 - temp_max) < almost_zero)
1878 {
1879 prev_state = "110";
1880 }
1881 else if (fabs(v010_3 - temp_max) < almost_zero)
1882 {
1883 prev_state = "101";
1884 }
1885 else if (fabs(v010_4 - temp_max) < almost_zero)
1886 {
1887 prev_state = "100";
1888 }
1889 else if (fabs(v010_5 - temp_max) < almost_zero)
1890 {
1891 prev_state = "011";
1892 }
1893 else if (fabs(v010_6 - temp_max) < almost_zero)
1894 {
1895 prev_state = "010";
1896 }
1897 else if (fabs(v010_7 - temp_max) < almost_zero)
1898 {
1899 prev_state = "001";
1900 }
1901 else
1902 {
1903 cout << "No transition possible." << endl;
1904 }
1905
1906 x1_aligned.push_back(gap);
1907 x2_aligned.push_back(x2[dim2-1]);
1908 x3_aligned.push_back(gap);
1909 dim2--;
1910 }
1911 else if (prev_state == "011") //Ins23
1912 {
1913 v011_1 = log(prev_state_to_011["111"]) + V111[dim1][dim2 - 1][dim3 - 1];
1914 v011_2 = log(prev_state_to_011["110"]) + V110[dim1][dim2 - 1][dim3 - 1];
1915 v011_3 = log(prev_state_to_011["101"]) + V101[dim1][dim2 - 1][dim3 - 1];
1916 v011_4 = log(prev_state_to_011["100"]) + V100[dim1][dim2 - 1][dim3 - 1];
1917 v011_5 = log(prev_state_to_011["11"]) + V011[dim1][dim2 - 1][dim3 - 1];
1918 v011_6 = log(prev_state_to_011["10"]) + V010[dim1][dim2 - 1][dim3 - 1];
1919 v011_7 = log(prev_state_to_011["1"]) + V001[dim1][dim2 - 1][dim3 - 1];
1920
1921 temp_max = maximum(v011_1, v011_2, v011_3, v011_4, v011_5, v011_6, v011_7);
1922
1923 if (fabs(v011_1 - temp_max) < almost_zero)
1924 {
1925 prev_state = "111";
1926 }
1927 else if (fabs(v011_2 - temp_max) < almost_zero)
1928 {
1929 prev_state = "110";
1930 }
1931 else if (fabs(v011_3 - temp_max) < almost_zero)
1932 {
1933 prev_state = "101";
1934 }
1935 else if (fabs(v011_4 - temp_max) < almost_zero)
1936 {
1937 prev_state = "100";
1938 }
1939 else if (fabs(v011_5 - temp_max) < almost_zero)
1940 {
1941 prev_state = "011";
1942 }
1943 else if (fabs(v011_6 - temp_max) < almost_zero)
1944 {
1945 prev_state = "010";
1946 }
1947 else if (fabs(v011_7 - temp_max) - almost_zero)
1948 {
1949 prev_state = "001";
1950 }
1951 else
1952 {
1953 cout << "No transition possible." << endl;
1954 }
1955
1956 x1_aligned.push_back(gap);
1957 x2_aligned.push_back(x2[dim2-1]);
1958 x3_aligned.push_back(x3[dim3-1]);
1959 dim2--; dim3--;
1960 }
1961 else if (prev_state == "100") //Ins1
1962 {
1963 v100_1 = log(prev_state_to_100["111"]) + V111[dim1 - 1][dim2][dim3];
1964 v100_2 = log(prev_state_to_100["110"]) + V110[dim1 - 1][dim2][dim3];
1965 v100_3 = log(prev_state_to_100["101"]) + V101[dim1 - 1][dim2][dim3];
1966 v100_4 = log(prev_state_to_100["100"]) + V100[dim1 - 1][dim2][dim3];
1967 double tempp1 = V011[dim1 - 1][dim2][dim3];
1968 double tempp2 = V010[dim1 - 1][dim2][dim3];
1969 double tempp3 = V001[dim1 - 1][dim2][dim3];
1970 v100_5 = log(prev_state_to_100["11"]) + V011[dim1 - 1][dim2][dim3];
1971 v100_6 = log(prev_state_to_100["10"]) + V010[dim1 - 1][dim2][dim3];
1972 v100_7 = log(prev_state_to_100["1"]) + V001[dim1 - 1][dim2][dim3];
1973
1974 temp_max = maximum(v100_1, v100_2, v100_3, v100_4, v100_5, v100_6, v100_7);
1975
1976 if (fabs(v100_1 - temp_max) < almost_zero)
1977 {
1978 prev_state = "111";
1979 }
1980 else if (fabs(v100_2 - temp_max) - almost_zero)
1981 {
1982 prev_state = "110";
1983 }
1984 else if (fabs(v100_3 - temp_max) < almost_zero)
1985 {
1986 prev_state = "101";
1987 }
1988 else if (fabs(v100_4 - temp_max) < almost_zero)
1989 {
1990 prev_state = "100";
1991 }
1992 else if (fabs(v100_5 - temp_max) < almost_zero)
1993 {
1994 prev_state = "011";
1995 }
1996 else if (fabs(v100_6 - temp_max) < almost_zero)
1997 {
1998 prev_state = "010";
1999 }
2000 else if (fabs(v100_7 - temp_max) < almost_zero)
2001 {
2002 prev_state = "001";
2003 }
2004 else
2005 {
2006 cout << "No transition possible." << endl;
2007 }
2008
2009 x1_aligned.push_back(x1[dim1-1]);
2010 x2_aligned.push_back(gap);
2011 x3_aligned.push_back(gap);
2012 dim1--;
2013 }
2014 else if (prev_state == "101") //Ins13
2015 {
2016 v101_1 = log(prev_state_to_101["111"]) + V111[dim1 - 1][dim2][dim3 - 1];
2017 v101_2 = log(prev_state_to_101["110"]) + V110[dim1 - 1][dim2][dim3 - 1];
2018 v101_3 = log(prev_state_to_101["101"]) + V101[dim1 - 1][dim2][dim3 - 1];
2019 v101_4 = log(prev_state_to_101["100"]) + V100[dim1 - 1][dim2][dim3 - 1];
2020 v101_5 = log(prev_state_to_101["11"]) + V011[dim1 - 1][dim2][dim3 - 1];
2021 v101_6 = log(prev_state_to_101["10"]) + V010[dim1 - 1][dim2][dim3 - 1];
2022 v101_7 = log(prev_state_to_101["1"]) + V001[dim1 - 1][dim2][dim3 - 1];
2023
2024 temp_max = maximum(v101_1, v101_2, v101_3, v101_4, v101_5, v101_6, v101_7);
2025
2026 if (fabs(v101_1 - temp_max) < almost_zero)
2027 {
2028 prev_state = "111";
2029 }
2030 else if (fabs(v101_2 - temp_max) < almost_zero)
2031 {
2032 prev_state = "110";
2033 }
2034 else if (fabs(v101_3 - temp_max) < almost_zero)
2035 {
2036 prev_state = "101";
2037 }
2038 else if (fabs(v101_4 - temp_max) < almost_zero)
2039 {
2040 prev_state = "100";
2041 }
2042 else if (fabs(v101_5 - temp_max) < almost_zero)
2043 {
2044 prev_state = "011";
2045 }
2046 else if (fabs(v101_6 - temp_max) < almost_zero)
2047 {
2048 prev_state = "010";
2049 }
2050 else if (fabs(v101_7 - temp_max) < almost_zero)
2051 {
2052 prev_state = "001";
2053 }
2054 else
2055 {
2056 cout << "No transition possible." << endl;
2057 }
2058
2059 x1_aligned.push_back(x1[dim1-1]);
2060 x2_aligned.push_back(gap);
2061 x3_aligned.push_back(x3[dim3-1]);
2062 dim1--; dim3--;
2063 }
2064 else if (prev_state == "110") //Ins12
2065 {
2066 v110_1 = log(prev_state_to_110["111"]) + V111[dim1 - 1][dim2 - 1][dim3];
2067 v110_2 = log(prev_state_to_110["110"]) + V110[dim1 - 1][dim2 - 1][dim3];
2068 v110_3 = log(prev_state_to_110["101"]) + V101[dim1 - 1][dim2 - 1][dim3];
2069 v110_4 = log(prev_state_to_110["100"]) + V100[dim1 - 1][dim2 - 1][dim3];
2070 v110_5 = log(prev_state_to_110["11"]) + V011[dim1 - 1][dim2 - 1][dim3];
2071 v110_6 = log(prev_state_to_110["10"]) + V010[dim1 - 1][dim2 - 1][dim3];
2072 v110_7 = log(prev_state_to_110["1"]) + V001[dim1 - 1][dim2 - 1][dim3];
2073
2074 temp_max = maximum(v110_1, v110_2, v110_3, v110_4, v110_5, v110_6, v110_7);
2075
2076 if (fabs(v110_1 - temp_max) < almost_zero)
2077 {
2078 prev_state = "111";
2079 }
2080 else if (fabs(v110_2 - temp_max) < almost_zero)
2081 {
2082 prev_state = "110";
2083 }
2084 else if (fabs(v110_3 - temp_max) < almost_zero)
2085 {
2086 prev_state = "101";
2087 }
2088 else if (fabs(v110_4 - temp_max) < almost_zero)
2089 {
2090 prev_state = "100";
2091 }
2092 else if (fabs(v110_5 - temp_max) < almost_zero)
2093 {
2094 prev_state = "011";
2095 }
2096 else if (fabs(v110_6 - temp_max) < almost_zero)
2097 {
2098 prev_state = "010";
2099 }
2100 else if (fabs(v110_7 - temp_max) < almost_zero)
2101 {
2102 prev_state = "001";
2103 }
2104 else
2105 {
2106 cout << "No transition possible." << endl;
2107 }
2108
2109 x1_aligned.push_back(x1[dim1-1]);
2110 x2_aligned.push_back(x2[dim2-1]);
2111 x3_aligned.push_back(gap);
2112 dim1--; dim2--;
2113 }
2114 else // align/mismatch
2115 {
2116 v111_1 = log(prev_state_to_111["111"]) + V111[dim1 - 1][dim2 - 1][dim3 - 1];
2117 v111_2 = log(prev_state_to_111["110"]) + V110[dim1 - 1][dim2 - 1][dim3 - 1];
2118 v111_3 = log(prev_state_to_111["101"]) + V101[dim1 - 1][dim2 - 1][dim3 - 1];
2119 v111_4 = log(prev_state_to_111["100"]) + V100[dim1 - 1][dim2 - 1][dim3 - 1];
2120 v111_5 = log(prev_state_to_111["11"]) + V011[dim1 - 1][dim2 - 1][dim3 - 1];
2121 v111_6 = log(prev_state_to_111["10"]) + V010[dim1 - 1][dim2 - 1][dim3 - 1];
2122 v111_7 = log(prev_state_to_111["1"]) + V001[dim1 - 1][dim2 - 1][dim3 - 1];
2123
2124 temp_max = maximum(v111_1, v111_2, v111_3, v111_4, v111_5, v111_6, v111_7);
2125
2126 if (fabs(v111_1 - temp_max) < almost_zero)
2127 {
2128 prev_state = "111";
2129 }
2130 else if (fabs(v111_2 - temp_max) < almost_zero)
2131 {
2132 prev_state = "110";
2133 }
2134 else if (fabs(v111_3 - temp_max) < almost_zero)
2135 {
2136 prev_state = "101";
2137 }
2138 else if (fabs(v111_4 - temp_max) < almost_zero)
2139 {
2140 prev_state = "100";
2141 }
2142 else if (fabs(v111_5 - temp_max) < almost_zero)
2143 {
2144 prev_state = "011";
2145 }
2146 else if (fabs(v111_6 - temp_max) < almost_zero)
2147 {
2148 prev_state = "010";
2149 }
2150 else if (fabs(v111_7 - temp_max) < almost_zero)
2151 {
2152 prev_state = "001";
2153 }
2154 else
2155 {
2156 cout << "No transition possible." << endl;
2157 }
2158
2159 x1_aligned.push_back(x1[dim1-1]);
2160 x2_aligned.push_back(x2[dim2-1]);
2161 x3_aligned.push_back(x3[dim3-1]);
2162 dim1--; dim2--; dim3--;
2163 }
2164 }
2165
2166 for (int i = (x1_aligned.length() - 1); i >= 0; i--)
2167 {
2168 x1_aligned_reverse.push_back(x1_aligned.at(i));
2169 x2_aligned_reverse.push_back(x2_aligned.at(i));
2170 x3_aligned_reverse.push_back(x3_aligned.at(i));
2171 }
2172 }
2173
2174/*
2175 void final_probability_calc()
2176 {
2177 string temp1;
2178 double temp2;
2179 // clearing all maps
2180 emission_prob.clear();
2181 prev_state_to_001.clear();
2182 prev_state_to_010.clear();
2183 prev_state_to_011.clear();
2184 prev_state_to_100.clear();
2185 prev_state_to_101.clear();
2186 prev_state_to_110.clear();
2187 prev_state_to_111.clear();
2188
2189 //map<string, int> emission_prob_counter;
2190
2191 // calculating emission probs from all sequences
2192 for (int count = 0; count < emission_prob_collection.size(); count++)
2193 {
2194 for ( map<string, double>::const_iterator iter = emission_prob_collection[count].begin(); iter != emission_prob_collection[count].end(); ++iter)
2195 {
2196 temp1 = iter->first;
2197 temp2 = iter->second;
2198 if (temp2 < 0)
2199 temp2 = 0;
2200 emission_prob[iter->first] += temp2; // adding probs. of the emissions for each set of sequences
2201 //emission_prob_counter[iter->first]++; // adding the count of each set of emissions
2202 }
2203 }
2204
2205 for ( map<string, double>::const_iterator iter = emission_prob.begin(); iter != emission_prob.end(); ++iter)
2206 {
2207 temp1 = iter->first;
2208 temp2 = iter->second;
2209 emission_prob[iter->first] = iter->second / emission_prob_collection.size(); // adding probs. of the emissions for each set of sequences
2210 //emission_prob[iter->first] = emission_prob[iter->first]/emission_prob_counter[iter->first]; // adding probs. of the emissions for each set of sequences
2211 }
2212
2213
2214 // calculating state transition probs for '->001' from all sequences
2215 for (int count = 0; count < prob_trans_to_001.size(); count++)
2216 {
2217 for ( map<string, double>::const_iterator iter = prob_trans_to_001[count].begin(); iter != prob_trans_to_001[count].end(); ++iter)
2218 {
2219 prev_state_to_001[iter->first] += iter->second; // adding probs. of the emissions for each set of sequences
2220 //emission_prob_counter[iter->first]++; // adding the count of each set of emissions
2221 }
2222
2223 for ( map<string, double>::const_iterator iter = prob_trans_to_010[count].begin(); iter != prob_trans_to_010[count].end(); ++iter)
2224 {
2225 prev_state_to_010[iter->first] += iter->second; // adding probs. of the emissions for each set of sequences
2226 //emission_prob_counter[iter->first]++; // adding the count of each set of emissions
2227 }
2228
2229 for ( map<string, double>::const_iterator iter = prob_trans_to_011[count].begin(); iter != prob_trans_to_011[count].end(); ++iter)
2230 {
2231 prev_state_to_011[iter->first] += iter->second; // adding probs. of the emissions for each set of sequences
2232 //emission_prob_counter[iter->first]++; // adding the count of each set of emissions
2233 }
2234
2235 for ( map<string, double>::const_iterator iter = prob_trans_to_100[count].begin(); iter != prob_trans_to_100[count].end(); ++iter)
2236 {
2237 prev_state_to_100[iter->first] += iter->second; // adding probs. of the emissions for each set of sequences
2238 //emission_prob_counter[iter->first]++; // adding the count of each set of emissions
2239 }
2240
2241 for ( map<string, double>::const_iterator iter = prob_trans_to_101[count].begin(); iter != prob_trans_to_101[count].end(); ++iter)
2242 {
2243 prev_state_to_101[iter->first] += iter->second; // adding probs. of the emissions for each set of sequences
2244 //emission_prob_counter[iter->first]++; // adding the count of each set of emissions
2245 }
2246
2247 for ( map<string, double>::const_iterator iter = prob_trans_to_110[count].begin(); iter != prob_trans_to_110[count].end(); ++iter)
2248 {
2249 prev_state_to_110[iter->first] += iter->second; // adding probs. of the emissions for each set of sequences
2250 //emission_prob_counter[iter->first]++; // adding the count of each set of emissions
2251 }
2252
2253 for ( map<string, double>::const_iterator iter = prob_trans_to_111[count].begin(); iter != prob_trans_to_111[count].end(); ++iter)
2254 {
2255 prev_state_to_111[iter->first] += iter->second; // adding probs. of the emissions for each set of sequences
2256 //emission_prob_counter[iter->first]++; // adding the count of each set of emissions
2257 }
2258 }
2259
2260
2261 // dividing and finding probabilities
2262 for ( map<string, double>::const_iterator iter = prev_state_to_001.begin(); iter != prev_state_to_001.end(); ++iter)
2263 {
2264 //prev_state_to_001[iter->first] = prev_state_to_001[iter->first] / prev_state_to_001.size(); // adding probs. of the emissions for each set of sequences
2265 prev_state_to_001[iter->first] = prev_state_to_001[iter->first] / prob_trans_to_001.size();
2266 prev_state_to_010[iter->first] = prev_state_to_010[iter->first] / prob_trans_to_010.size(); // normalising probs. of the emissions for each set of sequences
2267 prev_state_to_011[iter->first] = prev_state_to_011[iter->first] / prob_trans_to_011.size(); // normalising probs. of the emissions for each set of sequences
2268 prev_state_to_100[iter->first] = prev_state_to_100[iter->first] / prob_trans_to_100.size(); // normalising probs. of the emissions for each set of sequences
2269 prev_state_to_101[iter->first] = prev_state_to_101[iter->first] / prob_trans_to_101.size(); // normalising probs. of the emissions for each set of sequences
2270 prev_state_to_110[iter->first] = prev_state_to_110[iter->first] / prob_trans_to_110.size(); // normalising probs. of the emissions for each set of sequences
2271 prev_state_to_111[iter->first] = prev_state_to_111[iter->first] / prob_trans_to_111.size(); // normalising probs. of the emissions for each set of sequences
2272 }
2273
2274
2275 }
2276 */
2277
2278 void sensitivity_and_ppv_calculator(string one_known, string two_known, string three_known)
2279 {
2280
2281 int ppv_count = 0;
2282 int sensitivity_count = 0;
2283 for (int i = 0; i < one_known.length(); i++)
2284 {
2285 trio1[0] = one_known[i];
2286 trio1[1] = two_known[i];
2287 trio1[2] = three_known[i];
2288
2289 if (i > 0)
2290 {
2291 trio0[0] = one_known[i-1];
2292 trio0[1] = two_known[i-1];
2293 trio0[2] = three_known[i-1];
2294 }
2295 else
2296 trio0 = "XXX";
2297
2298 if (i < one_known.length() - 1)
2299 {
2300 trio2[0] = one_known[i+1];
2301 trio2[1] = two_known[i+1];
2302 trio2[2] = three_known[i+1];
2303 }
2304 else
2305 trio2 = "XXX";
2306
2307 trio_seq[0] = x1_aligned_reverse[i];
2308 trio_seq[1] = x2_aligned_reverse[i];
2309 trio_seq[2] = x3_aligned_reverse[i];
2310
2311 if (trio_seq == trio0 || trio_seq == trio1 || trio_seq == trio2)
2312 {
2313 ppv_count++;
2314 sensitivity_count++;
2315 }
2316 }
2317
2318 //int a1 = x1_aligned_reverse.length();
2319 //int a2 = one_known.length();
2320 ppv = (double)ppv_count/x1_aligned_reverse.length();
2321 sensitivity = (double)sensitivity_count/one_known.length();
2322
2323 //cout << "ppv_count: " << ppv_count << " pred_length: " << x1_aligned_reverse.length() << endl;
2324 //cout << "sens_count: " << sensitivity_count << " known_length: " << one_known.length() << endl;
2325 //cout << "ppv: " << ppv << endl;
2326 //cout << "sensitivity: " << sensitivity << endl;
2327 ppv_count = 0;
2328 sensitivity_count = 0;
2329
2330 }
2331
2332 void alignment_scorer()
2333 {
2334 score_pred = 0;
2335 score_known = 0;
2336 int match = 2;
2337 int ins_del = -7;
2338 int mismatch = -7;
2339 for (int i = 0; i < x1_aligned_reverse.length(); i++)
2340 {
2341 if ((x1_aligned_reverse[i] == x2_aligned_reverse[i]) && (x1_aligned_reverse[i] == x3_aligned_reverse[i]) && (x2_aligned_reverse[i] == x3_aligned_reverse[i])) // match
2342 {
2343 score_pred = score_pred + match;
2344 }
2345 else if (x1_aligned_reverse[i] == gap || x2_aligned_reverse[i] == gap || x3_aligned_reverse[i] == gap) //
2346 {
2347 score_pred = score_pred + ins_del;
2348 }
2349 else
2350 {
2351 score_pred = score_pred + mismatch;
2352 }
2353 }
2354
2355 for (int i = 0; i < known1.length(); i++)
2356 {
2357 if ((known1[i] == known2[i]) && (known1[i] == known3[i]) && (known2[i] == known3[i])) // match
2358 {
2359 score_known = score_known + match;
2360 }
2361 else if (x1_aligned_reverse[i] == gap || x2_aligned_reverse[i] == gap || x3_aligned_reverse[i] == gap) //
2362 {
2363 score_known = score_known + ins_del;
2364 }
2365 else
2366 {
2367 score_known = score_known + mismatch;
2368 }
2369 }
2370 }
2371
2372 void forward_algo_3seq()
2373 {
2374 // forward algo array initializer
2375 for (int count1 = 0; count1 <= x1.length(); count1++)
2376 {
2377 V001_f.push_back(rowvec1);
2378 V010_f.push_back(rowvec1);
2379 V011_f.push_back(rowvec1);
2380 V100_f.push_back(rowvec1);
2381 V101_f.push_back(rowvec1);
2382 V110_f.push_back(rowvec1);
2383 V111_f.push_back(rowvec1);
2384
2385 for (int count2 = 0; count2 <= x2.length(); count2++)
2386 {
2387 V001_f[count1].push_back(rowvec2);
2388 V010_f[count1].push_back(rowvec2);
2389 V011_f[count1].push_back(rowvec2);
2390 V100_f[count1].push_back(rowvec2);
2391 V101_f[count1].push_back(rowvec2);
2392 V110_f[count1].push_back(rowvec2);
2393 V111_f[count1].push_back(rowvec2);
2394
2395 for (int count3 = 0; count3 <= x3.length(); count3++)
2396 {
2397 if (count1 == 0 || count2 == 0 || count3 == 0)
2398 {
2399 V001_f[count1][count2].push_back(neg_threshold);
2400 V010_f[count1][count2].push_back(neg_threshold);
2401 V011_f[count1][count2].push_back(neg_threshold);
2402 V100_f[count1][count2].push_back(neg_threshold);
2403 V101_f[count1][count2].push_back(neg_threshold);
2404 V110_f[count1][count2].push_back(neg_threshold);
2405 V111_f[count1][count2].push_back(neg_threshold);
2406 }
2407 else
2408 {
2409 V001_f[count1][count2].push_back(0);
2410 V010_f[count1][count2].push_back(0);
2411 V011_f[count1][count2].push_back(0);
2412 V100_f[count1][count2].push_back(0);
2413 V101_f[count1][count2].push_back(0);
2414 V110_f[count1][count2].push_back(0);
2415 V111_f[count1][count2].push_back(0);
2416 }
2417 }
2418 }
2419 }
2420
2421 V001_f[0][0][0] = neg_threshold;
2422 V010_f[0][0][0] = neg_threshold;
2423 V011_f[0][0][0] = neg_threshold;
2424 V100_f[0][0][0] = neg_threshold;
2425 V101_f[0][0][0] = neg_threshold;
2426 V110_f[0][0][0] = neg_threshold;
2427 V111_f[0][0][0] = 0;
2428
2429 float a,b,c,d,e,f,g,temp3;
2430
2431 double v001_1, v001_2, v001_3, v001_4, v001_5, v001_6, v001_7;
2432 double v010_1, v010_2, v010_3, v010_4, v010_5, v010_6, v010_7;
2433 double v011_1, v011_2, v011_3, v011_4, v011_5, v011_6, v011_7;
2434 double v100_1, v100_2, v100_3, v100_4, v100_5, v100_6, v100_7;
2435 double v101_1, v101_2, v101_3, v101_4, v101_5, v101_6, v101_7;
2436 double v110_1, v110_2, v110_3, v110_4, v110_5, v110_6, v110_7;
2437 double v111_1, v111_2, v111_3, v111_4, v111_5, v111_6, v111_7;
2438
2439 double temp_state_111, temp_state_110, temp_state_101, temp_state_100, temp_state_011, temp_state_010, temp_state_001, tempp_state;
2440 double matrix_111, matrix_110, matrix_101, matrix_100, matrix_011, matrix_010, matrix_001;
2441 double temp_emission;
2442 double temp_total;
2443
2444
2445 // filling matrices
2446 for (dim1 = 1; dim1 <= x1.length(); dim1++)
2447 {
2448 for (dim2 = 1; dim2 <= x2.length(); dim2++)
2449 {
2450 for (dim3 = 1; dim3 <= x3.length(); dim3++)
2451 {
2452
2453 // ALIGNMENT matrix
2454 trio[0] = x1[dim1-1];
2455 trio[1] = x2[dim2-1];
2456 trio[2] = x3[dim3-1];
2457
2458 temp_state_111 = log(prev_state_to_111["111"]);
2459 temp_state_110 = log(prev_state_to_111["110"]);
2460 temp_state_101 = log(prev_state_to_111["101"]);
2461 temp_state_100 = log(prev_state_to_111["100"]);
2462 temp_state_011 = log(prev_state_to_111["11"]);
2463 temp_state_010 = log(prev_state_to_111["10"]);
2464 temp_state_001 = log(prev_state_to_111["1"]);
2465 //tempp_state = log(prev_state_to_111["001"]);
2466
2467
2468 matrix_111 = V111_f[dim1 - 1][dim2 - 1][dim3 - 1];
2469 matrix_111 = V110_f[dim1 - 1][dim2 - 1][dim3 - 1];
2470 matrix_111 = V101_f[dim1 - 1][dim2 - 1][dim3 - 1];
2471 matrix_111 = V100_f[dim1 - 1][dim2 - 1][dim3 - 1];
2472 matrix_111 = V011_f[dim1 - 1][dim2 - 1][dim3 - 1];
2473 matrix_111 = V010_f[dim1 - 1][dim2 - 1][dim3 - 1];
2474 matrix_111 = V001_f[dim1 - 1][dim2 - 1][dim3 - 1];
2475
2476 temp_emission = log(emission_prob[trio]);
2477 temp_emission = log(emission_double[alpha_indexes_emissions[trio]][6]);
2478
2479 v111_1 = log(prev_state_to_111["111"]) + V111_f[dim1 - 1][dim2 - 1][dim3 - 1];
2480 v111_2 = log(prev_state_to_111["110"]) + V110_f[dim1 - 1][dim2 - 1][dim3 - 1];
2481 v111_3 = log(prev_state_to_111["101"]) + V101_f[dim1 - 1][dim2 - 1][dim3 - 1];
2482 v111_4 = log(prev_state_to_111["100"]) + V100_f[dim1 - 1][dim2 - 1][dim3 - 1];
2483 v111_5 = log(prev_state_to_111["11"]) + V011_f[dim1 - 1][dim2 - 1][dim3 - 1];
2484 v111_6 = log(prev_state_to_111["10"]) + V010_f[dim1 - 1][dim2 - 1][dim3 - 1];
2485 v111_7 = log(prev_state_to_111["1"]) + V001_f[dim1 - 1][dim2 - 1][dim3 - 1];
2486
2487 // a = v111_1 v111_2, v111_3, v111_4, v111_5, v111_6, v111_7;
2488 // temp3 = maximum(a, b, -DBL_MAX);
2489 // b = a + b - temp3;
2490 // a = temp3;
2491 // temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
2492
2493 a = max_imum(v111_1, v111_2, -DBL_MAX); // evaluating sum of logs for 'ALIGN' state.
2494 b = v111_1 + v111_2 - a;
2495 temp3 = a + log(1 + exp(b-a));
2496 a = max_imum(v111_3, temp3, -DBL_MAX);
2497 b = v111_3 + temp3 - a;
2498 temp3 = a + log(1 + exp(b-a)); // sum of v111_1, v111_2 and v111_3 = temp3
2499 a = max_imum(v111_4, temp3, -DBL_MAX);
2500 b = v111_4 + temp3 - a;
2501 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_4
2502 a = max_imum(v111_5, temp3, -DBL_MAX);
2503 b = v111_5 + temp3 - a;
2504 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_5
2505 a = max_imum(v111_6, temp3, -DBL_MAX);
2506 b = v111_6 + temp3 - a;
2507 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_6
2508 a = max_imum(v111_7, temp3, -DBL_MAX);
2509 b = v111_7 + temp3 - a;
2510 temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
2511
2512
2513 //temp_total = log(emission_prob[trio]) + maximum(v111_1, v111_2, v111_3, v111_4, v111_5, v111_6, v111_7);
2514
2515
2516 //V111[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v111_1, v111_2, v111_3, v111_4, v111_5, v111_6, v111_7);
2517 //V111_f[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][6]) + maximum(v111_1, v111_2, v111_3, v111_4, v111_5, v111_6, v111_7);
2518 V111_f[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][6]) + temp3;
2519
2520 // INS3 matrix
2521
2522 trio[0] = gap;
2523 trio[1] = gap;
2524 trio[2] = x3[dim3-1];
2525
2526 temp_state_111 = log(prev_state_to_001["111"]);
2527 temp_state_110 = log(prev_state_to_001["110"]);
2528 temp_state_101 = log(prev_state_to_001["101"]);
2529 temp_state_100 = log(prev_state_to_001["100"]);
2530 temp_state_011 = log(prev_state_to_001["11"]);
2531 temp_state_010 = log(prev_state_to_001["10"]);
2532 temp_state_001 = log(prev_state_to_001["1"]);
2533
2534 matrix_111 = V111_f[dim1][dim2][dim3 - 1];
2535 matrix_110 = V110_f[dim1][dim2][dim3 - 1];
2536 matrix_101 = V101_f[dim1][dim2][dim3 - 1];
2537 matrix_100 = V100_f[dim1][dim2][dim3 - 1];
2538 matrix_011 = V011_f[dim1][dim2][dim3 - 1];
2539 matrix_010 = V010_f[dim1][dim2][dim3 - 1];
2540 matrix_001 = V001_f[dim1][dim2][dim3 - 1];
2541
2542 temp_emission = log(emission_prob[trio]);
2543 temp_emission = log(emission_double[alpha_indexes_emissions[trio]][0]);
2544
2545 v001_1 = log(prev_state_to_001["111"]) + V111_f[dim1][dim2][dim3 - 1];
2546 v001_2 = log(prev_state_to_001["110"]) + V110_f[dim1][dim2][dim3 - 1];
2547 v001_3 = log(prev_state_to_001["101"]) + V101_f[dim1][dim2][dim3 - 1];
2548 v001_4 = log(prev_state_to_001["100"]) + V100_f[dim1][dim2][dim3 - 1];
2549 v001_5 = log(prev_state_to_001["11"]) + V011_f[dim1][dim2][dim3 - 1];
2550 v001_6 = log(prev_state_to_001["10"]) + V010_f[dim1][dim2][dim3 - 1];
2551 v001_7 = log(prev_state_to_001["1"]) + V001_f[dim1][dim2][dim3 - 1];
2552
2553
2554 a = max_imum(v001_1, v001_2, -DBL_MAX); // evaluating sum of logs for 'ALIGN' state.
2555 b = v001_1 + v001_2 - a;
2556 temp3 = a + log(1 + exp(b-a));
2557 a = max_imum(v001_3, temp3, -DBL_MAX);
2558 b = v001_3 + temp3 - a;
2559 temp3 = a + log(1 + exp(b-a)); // sum of v111_1, v111_2 and v111_3 = temp3
2560 a = max_imum(v001_4, temp3, -DBL_MAX);
2561 b = v001_4 + temp3 - a;
2562 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_4
2563 a = max_imum(v001_5, temp3, -DBL_MAX);
2564 b = v001_5 + temp3 - a;
2565 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_5
2566 a = max_imum(v001_6, temp3, -DBL_MAX);
2567 b = v001_6 + temp3 - a;
2568 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_6
2569 a = max_imum(v001_7, temp3, -DBL_MAX);
2570 b = v001_7 + temp3 - a;
2571 temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
2572
2573
2574 temp_total = log(emission_prob[trio]) + maximum(v001_1, v001_2, v001_3, v001_4, v001_5, v001_6, v001_7);
2575
2576 //V001_f[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v001_1, v001_2, v001_3, v001_4, v001_5, v001_6, v001_7);
2577 V001_f[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][0]) + temp3;
2578
2579 // INS2 matrix
2580
2581 trio[0] = gap;
2582 trio[1] = x2[dim2-1];
2583 trio[2] = gap;
2584
2585 temp_state_111 = log(prev_state_to_010["111"]);
2586 temp_state_110 = log(prev_state_to_010["110"]);
2587 temp_state_101 = log(prev_state_to_010["101"]);
2588 temp_state_100 = log(prev_state_to_010["100"]);
2589 temp_state_011 = log(prev_state_to_010["11"]);
2590 temp_state_010 = log(prev_state_to_010["10"]);
2591 temp_state_001 = log(prev_state_to_010["1"]);
2592
2593 matrix_111 = V111_f[dim1][dim2 - 1][dim3];
2594 matrix_110 = V110_f[dim1][dim2 - 1][dim3];
2595 matrix_101 = V101_f[dim1][dim2 - 1][dim3];
2596 matrix_100 = V100_f[dim1][dim2 - 1][dim3];
2597 matrix_011 = V011_f[dim1][dim2 - 1][dim3];
2598 matrix_010 = V010_f[dim1][dim2 - 1][dim3];
2599 matrix_001 = V001_f[dim1][dim2 - 1][dim3];
2600
2601 temp_emission = log(emission_prob[trio]);
2602 temp_emission = log(emission_double[alpha_indexes_emissions[trio]][1]);
2603
2604 v010_1 = log(prev_state_to_010["111"]) + V111_f[dim1][dim2 - 1][dim3];
2605 v010_2 = log(prev_state_to_010["110"]) + V110_f[dim1][dim2 - 1][dim3];
2606 v010_3 = log(prev_state_to_010["101"]) + V101_f[dim1][dim2 - 1][dim3];
2607 v010_4 = log(prev_state_to_010["100"]) + V100_f[dim1][dim2 - 1][dim3];
2608 v010_5 = log(prev_state_to_010["11"]) + V011_f[dim1][dim2 - 1][dim3];
2609 v010_6 = log(prev_state_to_010["10"]) + V010_f[dim1][dim2 - 1][dim3];
2610 v010_7 = log(prev_state_to_010["1"]) + V001_f[dim1][dim2 - 1][dim3];
2611
2612 a = max_imum(v010_1, v010_2, -DBL_MAX); // evaluating sum of logs for 'ALIGN' state.
2613 b = v010_1 + v010_2 - a;
2614 temp3 = a + log(1 + exp(b-a));
2615 a = max_imum(v010_3, temp3, -DBL_MAX);
2616 b = v010_3 + temp3 - a;
2617 temp3 = a + log(1 + exp(b-a)); // sum of v111_1, v111_2 and v111_3 = temp3
2618 a = max_imum(v010_4, temp3, -DBL_MAX);
2619 b = v010_4 + temp3 - a;
2620 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_4
2621 a = max_imum(v010_5, temp3, -DBL_MAX);
2622 b = v010_5 + temp3 - a;
2623 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_5
2624 a = max_imum(v010_6, temp3, -DBL_MAX);
2625 b = v010_6 + temp3 - a;
2626 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_6
2627 a = max_imum(v010_7, temp3, -DBL_MAX);
2628 b = v010_7 + temp3 - a;
2629 temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
2630
2631 temp_total = log(emission_prob[trio]) + maximum(v010_1, v010_2, v010_3, v010_4, v010_5, v010_6, v010_7);
2632
2633 //V010[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v010_1, v010_2, v010_3, v010_4, v010_5, v010_6, v010_7);
2634 //V010_f[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][1]) + maximum(v010_1, v010_2, v010_3, v010_4, v010_5, v010_6, v010_7);
2635 V010_f[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][1]) + temp3;
2636
2637 // INS23 matrix
2638
2639 trio[0] = gap;
2640 trio[1] = x2[dim2-1];
2641 trio[2] = x3[dim3-1];
2642
2643 temp_state_111 = log(prev_state_to_011["111"]);
2644 temp_state_110 = log(prev_state_to_011["110"]);
2645 temp_state_101 = log(prev_state_to_011["101"]);
2646 temp_state_100 = log(prev_state_to_011["100"]);
2647 temp_state_011 = log(prev_state_to_011["11"]);
2648 temp_state_010 = log(prev_state_to_011["10"]);
2649 temp_state_001 = log(prev_state_to_011["1"]);
2650
2651 matrix_111 = V111_f[dim1][dim2 - 1][dim3 - 1];
2652 matrix_110 = V110_f[dim1][dim2 - 1][dim3 - 1];
2653 matrix_101 = V101_f[dim1][dim2 - 1][dim3 - 1];
2654 matrix_100 = V100_f[dim1][dim2 - 1][dim3 - 1];
2655 matrix_011 = V011_f[dim1][dim2 - 1][dim3 - 1];
2656 matrix_010 = V010_f[dim1][dim2 - 1][dim3 - 1];
2657 matrix_001 = V001_f[dim1][dim2 - 1][dim3 - 1];
2658
2659 temp_emission = log(emission_prob[trio]);
2660 temp_emission = log(emission_double[alpha_indexes_emissions[trio]][2]);
2661
2662 v011_1 = log(prev_state_to_011["111"]) + V111_f[dim1][dim2 - 1][dim3 - 1];
2663 v011_2 = log(prev_state_to_011["110"]) + V110_f[dim1][dim2 - 1][dim3 - 1];
2664 v011_3 = log(prev_state_to_011["101"]) + V101_f[dim1][dim2 - 1][dim3 - 1];
2665 v011_4 = log(prev_state_to_011["100"]) + V100_f[dim1][dim2 - 1][dim3 - 1];
2666 v011_5 = log(prev_state_to_011["11"]) + V011_f[dim1][dim2 - 1][dim3 - 1];
2667 v011_6 = log(prev_state_to_011["10"]) + V010_f[dim1][dim2 - 1][dim3 - 1];
2668 v011_7 = log(prev_state_to_011["1"]) + V001_f[dim1][dim2 - 1][dim3 - 1];
2669
2670 temp_total = log(emission_prob[trio]) + maximum(v011_1, v011_2, v011_3, v011_4, v011_5, v011_6, v011_7);
2671
2672 a = max_imum(v011_1, v011_2, -DBL_MAX); // evaluating sum of logs for 'ALIGN' state.
2673 b = v011_1 + v011_2 - a;
2674 temp3 = a + log(1 + exp(b-a));
2675 a = max_imum(v011_3, temp3, -DBL_MAX);
2676 b = v011_3 + temp3 - a;
2677 temp3 = a + log(1 + exp(b-a)); // sum of v111_1, v111_2 and v111_3 = temp3
2678 a = max_imum(v011_4, temp3, -DBL_MAX);
2679 b = v011_4 + temp3 - a;
2680 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_4
2681 a = max_imum(v011_5, temp3, -DBL_MAX);
2682 b = v011_5 + temp3 - a;
2683 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_5
2684 a = max_imum(v011_6, temp3, -DBL_MAX);
2685 b = v011_6 + temp3 - a;
2686 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_6
2687 a = max_imum(v011_7, temp3, -DBL_MAX);
2688 b = v011_7 + temp3 - a;
2689 temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
2690
2691 //V011[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v011_1, v011_2, v011_3, v011_4, v011_5, v011_6, v011_7);
2692 //V011_f[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][2]) + maximum(v011_1, v011_2, v011_3, v011_4, v011_5, v011_6, v011_7);
2693 V011_f[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][2]) + temp3;
2694
2695 // INS1 matrix
2696
2697 trio[0] = x1[dim1-1];
2698 trio[1] = gap;
2699 trio[2] = gap;
2700
2701 temp_state_111 = log(prev_state_to_100["111"]);
2702 temp_state_110 = log(prev_state_to_100["110"]);
2703 temp_state_101 = log(prev_state_to_100["101"]);
2704 temp_state_100 = log(prev_state_to_100["100"]);
2705 temp_state_011 = log(prev_state_to_100["11"]);
2706 temp_state_010 = log(prev_state_to_100["10"]);
2707 temp_state_001 = log(prev_state_to_100["1"]);
2708
2709 matrix_111 = V111_f[dim1 - 1][dim2][dim3];
2710 matrix_110 = V110_f[dim1 - 1][dim2][dim3];
2711 matrix_101 = V101_f[dim1 - 1][dim2][dim3];
2712 matrix_100 = V100_f[dim1 - 1][dim2][dim3];
2713 matrix_011 = V011_f[dim1 - 1][dim2][dim3];
2714 matrix_010 = V010_f[dim1 - 1][dim2][dim3];
2715 matrix_001 = V001_f[dim1 - 1][dim2][dim3];
2716
2717 temp_emission = log(emission_prob[trio]);
2718 temp_emission = log(emission_double[alpha_indexes_emissions[trio]][3]);
2719
2720 v100_1 = log(prev_state_to_100["111"]) + V111_f[dim1 - 1][dim2][dim3];
2721 v100_2 = log(prev_state_to_100["110"]) + V110_f[dim1 - 1][dim2][dim3];
2722 v100_3 = log(prev_state_to_100["101"]) + V101_f[dim1 - 1][dim2][dim3];
2723 v100_4 = log(prev_state_to_100["100"]) + V100_f[dim1 - 1][dim2][dim3];
2724 v100_5 = log(prev_state_to_100["11"]) + V011_f[dim1 - 1][dim2][dim3];
2725 v100_6 = log(prev_state_to_100["10"]) + V010_f[dim1 - 1][dim2][dim3];
2726 v100_7 = log(prev_state_to_100["1"]) + V001_f[dim1 - 1][dim2][dim3];
2727
2728 a = max_imum(v100_1, v100_2, -DBL_MAX); // evaluating sum of logs for 'ALIGN' state.
2729 b = v100_1 + v100_2 - a;
2730 temp3 = a + log(1 + exp(b-a));
2731 a = max_imum(v100_3, temp3, -DBL_MAX);
2732 b = v100_3 + temp3 - a;
2733 temp3 = a + log(1 + exp(b-a)); // sum of v111_1, v111_2 and v111_3 = temp3
2734 a = max_imum(v100_4, temp3, -DBL_MAX);
2735 b = v100_4 + temp3 - a;
2736 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_4
2737 a = max_imum(v100_5, temp3, -DBL_MAX);
2738 b = v100_5 + temp3 - a;
2739 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_5
2740 a = max_imum(v100_6, temp3, -DBL_MAX);
2741 b = v100_6 + temp3 - a;
2742 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_6
2743 a = max_imum(v100_7, temp3, -DBL_MAX);
2744 b = v100_7 + temp3 - a;
2745 temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
2746
2747 temp_total = log(emission_prob[trio]) + maximum(v100_1, v100_2, v100_3, v100_4, v100_5, v100_6, v100_7);
2748
2749 //V100[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v100_1, v100_2, v100_3, v100_4, v100_5, v100_6, v100_7);
2750 //V100_f[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][3]) + maximum(v100_1, v100_2, v100_3, v100_4, v100_5, v100_6, v100_7);
2751 V100_f[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][3]) + temp3;
2752
2753 // INS13 matrix
2754
2755 trio[0] = x1[dim1-1];
2756 trio[1] = gap;
2757 trio[2] = x3[dim3-1];
2758
2759 temp_state_111 = log(prev_state_to_101["111"]);
2760 temp_state_110 = log(prev_state_to_101["110"]);
2761 temp_state_101 = log(prev_state_to_101["101"]);
2762 temp_state_100 = log(prev_state_to_101["100"]);
2763 temp_state_011 = log(prev_state_to_101["11"]);
2764 temp_state_010 = log(prev_state_to_101["10"]);
2765 temp_state_001 = log(prev_state_to_101["1"]);
2766
2767 matrix_111 = V111_f[dim1 - 1][dim2][dim3 - 1];
2768 matrix_110 = V110_f[dim1 - 1][dim2][dim3 - 1];
2769 matrix_101 = V101_f[dim1 - 1][dim2][dim3 - 1];
2770 matrix_100 = V100_f[dim1 - 1][dim2][dim3 - 1];
2771 matrix_011 = V011_f[dim1 - 1][dim2][dim3 - 1];
2772 matrix_010 = V010_f[dim1 - 1][dim2][dim3 - 1];
2773 matrix_001 = V001_f[dim1 - 1][dim2][dim3 - 1];
2774
2775 temp_emission = log(emission_prob[trio]);
2776 temp_emission = log(emission_double[alpha_indexes_emissions[trio]][4]);
2777
2778 v101_1 = log(prev_state_to_101["111"]) + V111_f[dim1 - 1][dim2][dim3 - 1];
2779 v101_2 = log(prev_state_to_101["110"]) + V110_f[dim1 - 1][dim2][dim3 - 1];
2780 v101_3 = log(prev_state_to_101["101"]) + V101_f[dim1 - 1][dim2][dim3 - 1];
2781 v101_4 = log(prev_state_to_101["100"]) + V100_f[dim1 - 1][dim2][dim3 - 1];
2782 v101_5 = log(prev_state_to_101["11"]) + V011_f[dim1 - 1][dim2][dim3 - 1];
2783 v101_6 = log(prev_state_to_101["10"]) + V010_f[dim1 - 1][dim2][dim3 - 1];
2784 v101_7 = log(prev_state_to_101["1"]) + V001_f[dim1 - 1][dim2][dim3 - 1];
2785
2786 a = max_imum(v101_1, v101_2, -DBL_MAX); // evaluating sum of logs for 'ALIGN' state.
2787 b = v101_1 + v101_2 - a;
2788 temp3 = a + log(1 + exp(b-a));
2789 a = max_imum(v101_3, temp3, -DBL_MAX);
2790 b = v101_3 + temp3 - a;
2791 temp3 = a + log(1 + exp(b-a)); // sum of v111_1, v111_2 and v111_3 = temp3
2792 a = max_imum(v101_4, temp3, -DBL_MAX);
2793 b = v101_4 + temp3 - a;
2794 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_4
2795 a = max_imum(v101_5, temp3, -DBL_MAX);
2796 b = v101_5 + temp3 - a;
2797 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_5
2798 a = max_imum(v101_6, temp3, -DBL_MAX);
2799 b = v101_6 + temp3 - a;
2800 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_6
2801 a = max_imum(v101_7, temp3, -DBL_MAX);
2802 b = v101_7 + temp3 - a;
2803 temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
2804
2805 temp_total = log(emission_prob[trio]) + maximum(v101_1, v101_2, v101_3, v101_4, v101_5, v101_6, v101_7);
2806
2807 //V101[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v101_1, v101_2, v101_3, v101_4, v101_5, v101_6, v101_7);
2808 //V101_f[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][4]) + maximum(v101_1, v101_2, v101_3, v101_4, v101_5, v101_6, v101_7);
2809 V101_f[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][4]) + temp3;
2810
2811 // INS12 matrix
2812
2813 trio[0] = x1[dim1-1];
2814 trio[1] = x2[dim2-1];
2815 trio[2] = gap;
2816
2817 temp_state_111 = log(prev_state_to_110["111"]);
2818 temp_state_110 = log(prev_state_to_110["110"]);
2819 temp_state_101 = log(prev_state_to_110["101"]);
2820 temp_state_100 = log(prev_state_to_110["100"]);
2821 temp_state_011 = log(prev_state_to_110["11"]);
2822 temp_state_010 = log(prev_state_to_110["10"]);
2823 temp_state_001 = log(prev_state_to_110["1"]);
2824
2825 matrix_111 = V111_f[dim1 - 1][dim2 - 1][dim3];
2826 matrix_110 = V110_f[dim1 - 1][dim2 - 1][dim3];
2827 matrix_101 = V101_f[dim1 - 1][dim2 - 1][dim3];
2828 matrix_100 = V100_f[dim1 - 1][dim2 - 1][dim3];
2829 matrix_011 = V011_f[dim1 - 1][dim2 - 1][dim3];
2830 matrix_010 = V010_f[dim1 - 1][dim2 - 1][dim3];
2831 matrix_001 = V001_f[dim1 - 1][dim2 - 1][dim3];
2832
2833 temp_emission = log(emission_prob[trio]);
2834 temp_emission = log(emission_double[alpha_indexes_emissions[trio]][5]);
2835
2836 v110_1 = log(prev_state_to_110["111"]) + V111_f[dim1 - 1][dim2 - 1][dim3];
2837 v110_2 = log(prev_state_to_110["110"]) + V110_f[dim1 - 1][dim2 - 1][dim3];
2838 v110_3 = log(prev_state_to_110["101"]) + V101_f[dim1 - 1][dim2 - 1][dim3];
2839 v110_4 = log(prev_state_to_110["100"]) + V100_f[dim1 - 1][dim2 - 1][dim3];
2840 v110_5 = log(prev_state_to_110["11"]) + V011_f[dim1 - 1][dim2 - 1][dim3];
2841 v110_6 = log(prev_state_to_110["10"]) + V010_f[dim1 - 1][dim2 - 1][dim3];
2842 v110_7 = log(prev_state_to_110["1"]) + V001_f[dim1 - 1][dim2 - 1][dim3];
2843
2844 a = max_imum(v110_1, v110_2, -DBL_MAX); // evaluating sum of logs for 'ALIGN' state.
2845 b = v110_1 + v110_2 - a;
2846 temp3 = a + log(1 + exp(b-a));
2847 a = max_imum(v110_3, temp3, -DBL_MAX);
2848 b = v110_3 + temp3 - a;
2849 temp3 = a + log(1 + exp(b-a)); // sum of v111_1, v111_2 and v111_3 = temp3
2850 a = max_imum(v110_4, temp3, -DBL_MAX);
2851 b = v110_4 + temp3 - a;
2852 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_4
2853 a = max_imum(v110_5, temp3, -DBL_MAX);
2854 b = v110_5 + temp3 - a;
2855 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_5
2856 a = max_imum(v110_6, temp3, -DBL_MAX);
2857 b = v110_6 + temp3 - a;
2858 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_6
2859 a = max_imum(v110_7, temp3, -DBL_MAX);
2860 b = v110_7 + temp3 - a;
2861 temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
2862
2863 temp_total = log(emission_prob[trio]) + maximum(v110_1, v110_2, v110_3, v110_4, v110_5, v110_6, v110_7);
2864
2865 //V110_f[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][5]) + maximum(v110_1, v110_2, v110_3, v110_4, v110_5, v110_6, v110_7);
2866 V110_f[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][5]) + temp3;
2867
2868 }
2869 }
2870 }
2871
2872 /*//printing arrays
2873 cout << "V111_f: " << endl;
2874 for (dim1 = 1; dim1 <= x1.length(); dim1++)
2875 {
2876 cout << "Dim1: " << dim1 << endl;
2877 for (dim2 = 1; dim2 <= x2.length(); dim2++)
2878 {
2879 cout << "Dim2: " << dim2 << endl;
2880 for (dim3 = 1; dim3 <= x3.length(); dim3++)
2881 {
2882 cout << V111_f[dim1][dim2][dim3] << " ";
2883 }
2884 cout << endl;
2885 }
2886 }
2887
2888 cout << "V100_f: " << endl;
2889 for (dim1 = 1; dim1 <= x1.length(); dim1++)
2890 {
2891 cout << "Dim1: " << dim1 << endl;
2892 for (dim2 = 1; dim2 <= x2.length(); dim2++)
2893 {
2894 cout << "Dim2: " << dim2 << endl;
2895 for (dim3 = 1; dim3 <= x3.length(); dim3++)
2896 {
2897 cout << V100_f[dim1][dim2][dim3] << " ";
2898 }
2899 cout << endl;
2900 }
2901 }
2902
2903 cout << "V110_f: " << endl;
2904 for (dim1 = 1; dim1 <= x1.length(); dim1++)
2905 {
2906 cout << "Dim1: " << dim1 << endl;
2907 for (dim2 = 1; dim2 <= x2.length(); dim2++)
2908 {
2909 cout << "Dim2: " << dim2 << endl;
2910 for (dim3 = 1; dim3 <= x3.length(); dim3++)
2911 {
2912 cout << V110_f[dim1][dim2][dim3] << " ";
2913 }
2914 cout << endl;
2915 }
2916 }*/
2917 }
2918
2919
2920 void backward_algo_3seq()
2921 {
2922 // backward algo array initializer
2923 for (int count1 = 0; count1 <= x1.length(); count1++)
2924 {
2925 V001_b.push_back(rowvec1);
2926 V010_b.push_back(rowvec1);
2927 V011_b.push_back(rowvec1);
2928 V100_b.push_back(rowvec1);
2929 V101_b.push_back(rowvec1);
2930 V110_b.push_back(rowvec1);
2931 V111_b.push_back(rowvec1);
2932
2933 for (int count2 = 0; count2 <= x2.length(); count2++)
2934 {
2935 V001_b[count1].push_back(rowvec2);
2936 V010_b[count1].push_back(rowvec2);
2937 V011_b[count1].push_back(rowvec2);
2938 V100_b[count1].push_back(rowvec2);
2939 V101_b[count1].push_back(rowvec2);
2940 V110_b[count1].push_back(rowvec2);
2941 V111_b[count1].push_back(rowvec2);
2942
2943 for (int count3 = 0; count3 <= x3.length(); count3++)
2944 {
2945 if (count1 == x1.length() || count2 == x2.length() || count3 == x3.length())
2946 {
2947 V001_b[count1][count2].push_back(neg_threshold);
2948 V010_b[count1][count2].push_back(neg_threshold);
2949 V011_b[count1][count2].push_back(neg_threshold);
2950 V100_b[count1][count2].push_back(neg_threshold);
2951 V101_b[count1][count2].push_back(neg_threshold);
2952 V110_b[count1][count2].push_back(neg_threshold);
2953 V111_b[count1][count2].push_back(neg_threshold);
2954 }
2955 else
2956 {
2957 V001_b[count1][count2].push_back(0);
2958 V010_b[count1][count2].push_back(0);
2959 V011_b[count1][count2].push_back(0);
2960 V100_b[count1][count2].push_back(0);
2961 V101_b[count1][count2].push_back(0);
2962 V110_b[count1][count2].push_back(0);
2963 V111_b[count1][count2].push_back(0);
2964 }
2965 }
2966 }
2967 }
2968
2969 V001_b[x1.length() - 1][x2.length() - 1][x3.length() - 1] = 0;
2970 V010_b[x1.length() - 1][x2.length() - 1][x3.length() - 1] = 0;
2971 V011_b[x1.length() - 1][x2.length() - 1][x3.length() - 1] = 0;
2972 V100_b[x1.length() - 1][x2.length() - 1][x3.length() - 1] = 0;
2973 V101_b[x1.length() - 1][x2.length() - 1][x3.length() - 1] = 0;
2974 V110_b[x1.length() - 1][x2.length() - 1][x3.length() - 1] = 0;
2975 V111_b[x1.length() - 1][x2.length() - 1][x3.length() - 1] = 0;
2976
2977 float a,b,c,d,e,f,g,temp3;
2978
2979 double v001_1, v001_2, v001_3, v001_4, v001_5, v001_6, v001_7;
2980 double v010_1, v010_2, v010_3, v010_4, v010_5, v010_6, v010_7;
2981 double v011_1, v011_2, v011_3, v011_4, v011_5, v011_6, v011_7;
2982 double v100_1, v100_2, v100_3, v100_4, v100_5, v100_6, v100_7;
2983 double v101_1, v101_2, v101_3, v101_4, v101_5, v101_6, v101_7;
2984 double v110_1, v110_2, v110_3, v110_4, v110_5, v110_6, v110_7;
2985 double v111_1, v111_2, v111_3, v111_4, v111_5, v111_6, v111_7;
2986
2987 double emm_111, emm_110, emm_101, emm_100, emm_011, emm_010, emm_001;
2988 double temp_state_111, temp_state_110, temp_state_101, temp_state_100, temp_state_011, temp_state_010, temp_state_001, tempp_state;
2989 double matrix_111, matrix_110, matrix_101, matrix_100, matrix_011, matrix_010, matrix_001;
2990 double temp_emission1, temp_emission2, temp_emission3, temp_emission4, temp_emission5, temp_emission6, temp_emission7;
2991 double temp_total;
2992
2993 char triplet_111[4] = {0};
2994 char triplet_110[4] = {0};
2995 char triplet_101[4] = {0};
2996 char triplet_100[4] = {0};
2997 char triplet_011[4] = {0};
2998 char triplet_010[4] = {0};
2999 char triplet_001[4] = {0};
3000
3001 // filling matrices
3002 for (dim1 = x1.length()-1; dim1 >= 0; dim1--)
3003 {
3004 for (dim2 = x2.length()-1; dim2 >= 0; dim2--)
3005 {
3006 for (dim3 = x3.length()-1; dim3 >= 0; dim3--)
3007 {
3008 if (dim3 < (x3.length() - 1)) // 001
3009 {
3010 triplet_001[0] = gap;
3011 triplet_001[1] = gap;
3012 triplet_001[2] = x3[dim3+1];
3013 emm_001 = log(emission_double[alpha_indexes_emissions[triplet_001]][0]);
3014 }
3015 else
3016 {
3017 triplet_001[0] = gap;
3018 triplet_001[1] = gap;
3019 triplet_001[2] = 'A';
3020 emm_001 = neg_threshold;
3021 }
3022
3023 if (dim2 < (x2.length() - 1)) // 010
3024 {
3025 triplet_010[0] = gap;
3026 triplet_010[1] = x2[dim2+1];
3027 triplet_010[2] = gap;
3028 emm_010 = log(emission_double[alpha_indexes_emissions[triplet_010]][1]);
3029 }
3030 else
3031 {
3032 triplet_010[0] = gap;
3033 triplet_010[1] = 'A';
3034 triplet_010[2] = gap;
3035 emm_010 = neg_threshold;
3036 }
3037
3038 if (dim2 < (x2.length() - 1) && dim3 < (x3.length() - 1)) // 011
3039 {
3040 triplet_011[0] = gap;
3041 triplet_011[1] = x2[dim2+1];
3042 triplet_011[2] = x3[dim3+1];
3043 emm_011 = log(emission_double[alpha_indexes_emissions[triplet_011]][2]);
3044 }
3045 else
3046 {
3047 triplet_011[0] = gap;
3048 triplet_011[1] = 'A';
3049 triplet_011[2] = 'A';
3050 emm_011 = neg_threshold;
3051 }
3052
3053 if (dim1 < (x1.length() - 1)) // 100
3054 {
3055 triplet_100[0] = x1[dim1+1];
3056 triplet_100[1] = gap;
3057 triplet_100[2] = gap;
3058 emm_100 = log(emission_double[alpha_indexes_emissions[triplet_100]][3]);
3059 }
3060 else
3061 {
3062 triplet_100[0] = 'A';
3063 triplet_100[1] = gap;
3064 triplet_100[2] = gap;
3065 emm_100 = neg_threshold;
3066 }
3067
3068 if ((dim1 < (x1.length() - 1)) && (dim3 < (x3.length() - 1))) // 101
3069 {
3070 triplet_101[0] = x1[dim1+1];
3071 triplet_101[1] = gap;
3072 triplet_101[2] = x3[dim3+1];
3073 emm_101 = log(emission_double[alpha_indexes_emissions[triplet_101]][4]);
3074 }
3075 else
3076 {
3077 triplet_101[0] = 'A';
3078 triplet_101[1] = gap;
3079 triplet_101[2] = 'A';
3080 emm_101 = neg_threshold;
3081 }
3082
3083 if ((dim1 < (x1.length() - 1)) && (dim2 < (x2.length() - 1))) // 110
3084 {
3085 triplet_110[0] = x1[dim1+1];
3086 triplet_110[1] = x2[dim2+1];
3087 triplet_110[2] = gap;
3088 emm_110 = log(emission_double[alpha_indexes_emissions[triplet_110]][5]);
3089 }
3090 else
3091 {
3092 triplet_110[0] = 'A';
3093 triplet_110[1] = 'A';
3094 triplet_110[2] = gap;
3095 emm_110 = neg_threshold;
3096 }
3097
3098 if (dim1 < (x1.length() - 1) && dim2 < (x2.length() - 1) && dim3 < (x3.length() - 1)) // 111
3099 {
3100 triplet_111[0] = x1[dim1+1];
3101 triplet_111[1] = x2[dim2+1];
3102 triplet_111[2] = x3[dim3+1];
3103 emm_111 = log(emission_double[alpha_indexes_emissions[triplet_111]][6]);
3104 }
3105 else
3106 {
3107 triplet_111[0] = 'A';
3108 triplet_111[1] = 'A';
3109 triplet_111[2] = 'A';
3110 emm_111 = neg_threshold;
3111 }
3112
3113 if (!((dim1 == x1.length()-1) && (dim2 == x2.length()-1) && (dim3 == x3.length()-1)))
3114 {
3115 // aligned
3116 temp_state_111 = log(prev_state_to_111["111"]);
3117 temp_state_110 = log(prev_state_to_110["111"]);
3118 temp_state_101 = log(prev_state_to_101["111"]);
3119 temp_state_100 = log(prev_state_to_100["111"]);
3120 temp_state_011 = log(prev_state_to_011["111"]);
3121 temp_state_010 = log(prev_state_to_010["111"]);
3122 temp_state_001 = log(prev_state_to_001["111"]);
3123
3124 temp_emission1 = log(emission_double[alpha_indexes_emissions[triplet_111]][6]);
3125 temp_emission2 = log(emission_double[alpha_indexes_emissions[triplet_110]][5]);
3126 temp_emission3 = log(emission_double[alpha_indexes_emissions[triplet_101]][4]);
3127 temp_emission4 = log(emission_double[alpha_indexes_emissions[triplet_100]][3]);
3128 temp_emission5 = log(emission_double[alpha_indexes_emissions[triplet_011]][2]);
3129 temp_emission6 = log(emission_double[alpha_indexes_emissions[triplet_010]][1]);
3130 temp_emission7 = log(emission_double[alpha_indexes_emissions[triplet_001]][0]);
3131
3132 matrix_111 = V111_b[dim1 + 1][dim2 + 1][dim3 + 1];
3133 matrix_110 = V110_b[dim1 + 1][dim2 + 1][dim3];
3134 matrix_101 = V101_b[dim1 + 1][dim2][dim3 + 1];
3135 matrix_100 = V100_b[dim1 + 1][dim2][dim3];
3136 matrix_011 = V011_b[dim1][dim2 + 1][dim3 + 1];
3137 matrix_010 = V010_b[dim1][dim2 + 1][dim3];
3138 matrix_001 = V001_b[dim1][dim2][dim3 + 1];
3139
3140 temp_emission1 = log(emission_prob[trio]);
3141 //temp_emission = log(emission_double[alpha_indexes_emissions[trio]][6]);
3142
3143 v111_1 = log(prev_state_to_111["111"]) + V111_b[dim1 + 1][dim2 + 1][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_111]][6]);
3144 v111_2 = log(prev_state_to_110["111"]) + V110_b[dim1 + 1][dim2 + 1][dim3] + log(emission_double[alpha_indexes_emissions[triplet_110]][5]);
3145 v111_3 = log(prev_state_to_101["111"]) + V101_b[dim1 + 1][dim2][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_101]][4]);
3146 v111_4 = log(prev_state_to_100["111"]) + V100_b[dim1 + 1][dim2][dim3] + log(emission_double[alpha_indexes_emissions[triplet_100]][3]);
3147 v111_5 = log(prev_state_to_011["111"]) + V011_b[dim1][dim2 + 1][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_011]][2]);
3148 v111_6 = log(prev_state_to_010["111"]) + V010_b[dim1][dim2 + 1][dim3] + log(emission_double[alpha_indexes_emissions[triplet_010]][1]);
3149 v111_7 = log(prev_state_to_001["111"]) + V001_b[dim1][dim2][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_001]][0]);
3150
3151 a = max_imum(v111_1, v111_2, -DBL_MAX); // evaluating sum of logs for 'ALIGN' state.
3152 b = v111_1 + v111_2 - a;
3153 temp3 = a + log(1 + exp(b-a));
3154 a = max_imum(v111_3, temp3, -DBL_MAX);
3155 b = v111_3 + temp3 - a;
3156 temp3 = a + log(1 + exp(b-a)); // sum of v111_1, v111_2 and v111_3 = temp3
3157 a = max_imum(v111_4, temp3, -DBL_MAX);
3158 b = v111_4 + temp3 - a;
3159 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_4
3160 a = max_imum(v111_5, temp3, -DBL_MAX);
3161 b = v111_5 + temp3 - a;
3162 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_5
3163 a = max_imum(v111_6, temp3, -DBL_MAX);
3164 b = v111_6 + temp3 - a;
3165 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_6
3166 a = max_imum(v111_7, temp3, -DBL_MAX);
3167 b = v111_7 + temp3 - a;
3168 temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
3169
3170 V111_b[dim1][dim2][dim3] = temp3;
3171
3172 // INS3 matrix
3173 trio[0] = gap;
3174 trio[1] = gap;
3175 trio[2] = x3[dim3-1];
3176
3177 temp_state_111 = log(prev_state_to_111["1"]);
3178 temp_state_110 = log(prev_state_to_110["1"]);
3179 temp_state_101 = log(prev_state_to_101["1"]);
3180 temp_state_100 = log(prev_state_to_100["1"]);
3181 temp_state_011 = log(prev_state_to_011["1"]);
3182 temp_state_010 = log(prev_state_to_010["1"]);
3183 temp_state_001 = log(prev_state_to_001["1"]);
3184
3185 temp_emission1 = log(emission_prob[trio]);
3186 temp_emission1 = log(emission_double[alpha_indexes_emissions[trio]][0]);
3187
3188 v001_1 = log(prev_state_to_111["1"]) + V111_b[dim1 + 1][dim2 + 1][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_111]][6]);
3189 v001_2 = log(prev_state_to_110["1"]) + V110_b[dim1 + 1][dim2 + 1][dim3] + log(emission_double[alpha_indexes_emissions[triplet_110]][5]);
3190 v001_3 = log(prev_state_to_101["1"]) + V101_b[dim1 + 1][dim2][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_101]][4]);
3191 v001_4 = log(prev_state_to_100["1"]) + V100_b[dim1 + 1][dim2][dim3] + log(emission_double[alpha_indexes_emissions[triplet_100]][3]);
3192 v001_5 = log(prev_state_to_011["1"]) + V011_b[dim1][dim2 + 1][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_011]][2]);
3193 v001_6 = log(prev_state_to_010["1"]) + V010_b[dim1][dim2 + 1][dim3] + log(emission_double[alpha_indexes_emissions[triplet_010]][1]);
3194 v001_7 = log(prev_state_to_001["1"]) + V001_b[dim1][dim2][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_001]][0]);
3195
3196 a = max_imum(v001_1, v001_2, -DBL_MAX); // evaluating sum of logs for 'ALIGN' state.
3197 b = v001_1 + v001_2 - a;
3198 temp3 = a + log(1 + exp(b-a));
3199 a = max_imum(v001_3, temp3, -DBL_MAX);
3200 b = v001_3 + temp3 - a;
3201 temp3 = a + log(1 + exp(b-a)); // sum of v111_1, v111_2 and v111_3 = temp3
3202 a = max_imum(v001_4, temp3, -DBL_MAX);
3203 b = v001_4 + temp3 - a;
3204 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_4
3205 a = max_imum(v001_5, temp3, -DBL_MAX);
3206 b = v001_5 + temp3 - a;
3207 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_5
3208 a = max_imum(v001_6, temp3, -DBL_MAX);
3209 b = v001_6 + temp3 - a;
3210 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_6
3211 a = max_imum(v001_7, temp3, -DBL_MAX);
3212 b = v001_7 + temp3 - a;
3213 temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
3214
3215 temp_total = log(emission_prob[trio]) + maximum(v001_1, v001_2, v001_3, v001_4, v001_5, v001_6, v001_7);
3216
3217 //V001[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v001_1, v001_2, v001_3, v001_4, v001_5, v001_6, v001_7);
3218 V001_b[dim1][dim2][dim3] = temp3;
3219
3220 // INS2 matrix
3221
3222 //trio[0] = gap;
3223 //trio[1] = x2[dim2-1];
3224 //trio[2] = gap;
3225
3226 temp_state_111 = log(prev_state_to_010["111"]);
3227 temp_state_110 = log(prev_state_to_010["110"]);
3228 temp_state_101 = log(prev_state_to_010["101"]);
3229 temp_state_100 = log(prev_state_to_010["100"]);
3230 temp_state_011 = log(prev_state_to_010["11"]);
3231 temp_state_010 = log(prev_state_to_010["10"]);
3232 temp_state_001 = log(prev_state_to_010["1"]);
3233
3234 //temp_emission1 = log(emission_prob[trio]);
3235 //temp_emission1 = log(emission_double[alpha_indexes_emissions[trio]][1]);
3236
3237 v010_1 = log(prev_state_to_111["10"]) + V111_b[dim1 + 1][dim2 + 1][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_111]][6]);
3238 v010_2 = log(prev_state_to_110["10"]) + V110_b[dim1 + 1][dim2 + 1][dim3] + log(emission_double[alpha_indexes_emissions[triplet_110]][5]);
3239 v010_3 = log(prev_state_to_101["10"]) + V101_b[dim1 + 1][dim2][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_101]][4]);
3240 v010_4 = log(prev_state_to_100["10"]) + V100_b[dim1 + 1][dim2][dim3] + log(emission_double[alpha_indexes_emissions[triplet_100]][3]);
3241 v010_5 = log(prev_state_to_011["10"]) + V011_b[dim1][dim2 + 1][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_011]][2]);
3242 v010_6 = log(prev_state_to_010["10"]) + V010_b[dim1][dim2 + 1][dim3] + log(emission_double[alpha_indexes_emissions[triplet_010]][1]);
3243 v010_7 = log(prev_state_to_001["10"]) + V001_b[dim1][dim2][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_001]][0]);
3244
3245 a = max_imum(v010_1, v010_2, -DBL_MAX); // evaluating sum of logs for 'ALIGN' state.
3246 b = v010_1 + v010_2 - a;
3247 temp3 = a + log(1 + exp(b-a));
3248 a = max_imum(v010_3, temp3, -DBL_MAX);
3249 b = v010_3 + temp3 - a;
3250 temp3 = a + log(1 + exp(b-a)); // sum of v111_1, v111_2 and v111_3 = temp3
3251 a = max_imum(v010_4, temp3, -DBL_MAX);
3252 b = v010_4 + temp3 - a;
3253 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_4
3254 a = max_imum(v010_5, temp3, -DBL_MAX);
3255 b = v010_5 + temp3 - a;
3256 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_5
3257 a = max_imum(v010_6, temp3, -DBL_MAX);
3258 b = v010_6 + temp3 - a;
3259 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_6
3260 a = max_imum(v010_7, temp3, -DBL_MAX);
3261 b = v010_7 + temp3 - a;
3262 temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
3263
3264 temp_total = log(emission_prob[trio]) + maximum(v010_1, v010_2, v010_3, v010_4, v010_5, v010_6, v010_7);
3265
3266 //V010[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v010_1, v010_2, v010_3, v010_4, v010_5, v010_6, v010_7);
3267 //V010[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][1]) + maximum(v010_1, v010_2, v010_3, v010_4, v010_5, v010_6, v010_7);
3268 V010_b[dim1][dim2][dim3] = temp3;
3269
3270 // INS23 matrix
3271
3272 trio[0] = gap;
3273 trio[1] = x2[dim2-1];
3274 trio[2] = x3[dim3-1];
3275
3276 temp_state_111 = log(prev_state_to_011["111"]);
3277 temp_state_110 = log(prev_state_to_011["110"]);
3278 temp_state_101 = log(prev_state_to_011["101"]);
3279 temp_state_100 = log(prev_state_to_011["100"]);
3280 temp_state_011 = log(prev_state_to_011["11"]);
3281 temp_state_010 = log(prev_state_to_011["10"]);
3282 temp_state_001 = log(prev_state_to_011["1"]);
3283
3284 //temp_emission1 = log(emission_prob[trio]);
3285 //temp_emission1 = log(emission_double[alpha_indexes_emissions[trio]][2]);
3286
3287 v011_1 = log(prev_state_to_111["11"]) + V111_b[dim1 + 1][dim2 + 1][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_111]][6]);
3288 v011_2 = log(prev_state_to_110["11"]) + V110_b[dim1 + 1][dim2 + 1][dim3] + log(emission_double[alpha_indexes_emissions[triplet_110]][5]);
3289 v011_3 = log(prev_state_to_101["11"]) + V101_b[dim1 + 1][dim2][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_101]][4]);
3290 v011_4 = log(prev_state_to_100["11"]) + V100_b[dim1 + 1][dim2][dim3] + log(emission_double[alpha_indexes_emissions[triplet_100]][3]);
3291 v011_5 = log(prev_state_to_011["11"]) + V011_b[dim1][dim2 + 1][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_011]][2]);
3292 v011_6 = log(prev_state_to_010["11"]) + V010_b[dim1][dim2 + 1][dim3] + log(emission_double[alpha_indexes_emissions[triplet_010]][1]);
3293 v011_7 = log(prev_state_to_001["11"]) + V001_b[dim1][dim2][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_001]][0]);
3294
3295 //v011_1 = log(prev_state_to_111["011"]) + V111[dim1][dim2 - 1][dim3 - 1];
3296 //v011_2 = log(prev_state_to_111["011"]) + V110[dim1][dim2 - 1][dim3 - 1];
3297 //v011_3 = log(prev_state_to_011["011"]) + V101[dim1][dim2 - 1][dim3 - 1];
3298 //v011_4 = log(prev_state_to_011["011"]) + V100[dim1][dim2 - 1][dim3 - 1];
3299 //v011_5 = log(prev_state_to_011["011"]) + V011[dim1][dim2 - 1][dim3 - 1];
3300 //v011_6 = log(prev_state_to_011["011"]) + V010[dim1][dim2 - 1][dim3 - 1];
3301 //v011_7 = log(prev_state_to_011["011"]) + V001[dim1][dim2 - 1][dim3 - 1];
3302
3303 //temp_total = log(emission_prob[trio]) + maximum(v011_1, v011_2, v011_3, v011_4, v011_5, v011_6, v011_7);
3304
3305 a = max_imum(v011_1, v010_2, -DBL_MAX); // evaluating sum of logs for 'ALIGN' state.
3306 b = v011_1 + v011_2 - a;
3307 temp3 = a + log(1 + exp(b-a));
3308 a = max_imum(v011_3, temp3, -DBL_MAX);
3309 b = v011_3 + temp3 - a;
3310 temp3 = a + log(1 + exp(b-a)); // sum of v111_1, v111_2 and v111_3 = temp3
3311 a = max_imum(v011_4, temp3, -DBL_MAX);
3312 b = v011_4 + temp3 - a;
3313 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_4
3314 a = max_imum(v011_5, temp3, -DBL_MAX);
3315 b = v011_5 + temp3 - a;
3316 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_5
3317 a = max_imum(v011_6, temp3, -DBL_MAX);
3318 b = v011_6 + temp3 - a;
3319 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_6
3320 a = max_imum(v011_7, temp3, -DBL_MAX);
3321 b = v011_7 + temp3 - a;
3322 temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
3323
3324 //V011[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v011_1, v011_2, v011_3, v011_4, v011_5, v011_6, v011_7);
3325 //V011[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][2]) + maximum(v011_1, v011_2, v011_3, v011_4, v011_5, v011_6, v011_7);
3326 //V011[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][2]) + temp3;
3327 V011_b[dim1][dim2][dim3] = temp3;
3328
3329 // INS1 matrix
3330
3331 trio[0] = x1[dim1-1];
3332 trio[1] = gap;
3333 trio[2] = gap;
3334
3335 temp_state_111 = log(prev_state_to_100["111"]);
3336 temp_state_110 = log(prev_state_to_100["110"]);
3337 temp_state_101 = log(prev_state_to_100["101"]);
3338 temp_state_100 = log(prev_state_to_100["100"]);
3339 temp_state_011 = log(prev_state_to_100["11"]);
3340 temp_state_010 = log(prev_state_to_100["10"]);
3341 temp_state_001 = log(prev_state_to_100["1"]);
3342
3343 temp_emission1 = log(emission_prob[trio]);
3344 temp_emission1 = log(emission_double[alpha_indexes_emissions[trio]][3]);
3345
3346 v100_1 = log(prev_state_to_111["100"]) + V111_b[dim1 + 1][dim2 + 1][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_111]][6]);
3347 v100_2 = log(prev_state_to_110["100"]) + V110_b[dim1 + 1][dim2 + 1][dim3] + log(emission_double[alpha_indexes_emissions[triplet_110]][5]);
3348 v100_3 = log(prev_state_to_101["100"]) + V101_b[dim1 + 1][dim2][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_101]][4]);
3349 v100_4 = log(prev_state_to_100["100"]) + V100_b[dim1 + 1][dim2][dim3] + log(emission_double[alpha_indexes_emissions[triplet_100]][3]);
3350 v100_5 = log(prev_state_to_011["100"]) + V011_b[dim1][dim2 + 1][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_011]][2]);
3351 v100_6 = log(prev_state_to_010["100"]) + V010_b[dim1][dim2 + 1][dim3] + log(emission_double[alpha_indexes_emissions[triplet_010]][1]);
3352 v100_7 = log(prev_state_to_001["100"]) + V001_b[dim1][dim2][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_001]][0]);
3353
3354 a = max_imum(v100_1, v100_2, -DBL_MAX); // evaluating sum of logs for 'ALIGN' state.
3355 b = v100_1 + v100_2 - a;
3356 temp3 = a + log(1 + exp(b-a));
3357 a = max_imum(v100_3, temp3, -DBL_MAX);
3358 b = v100_3 + temp3 - a;
3359 temp3 = a + log(1 + exp(b-a)); // sum of v111_1, v111_2 and v111_3 = temp3
3360 a = max_imum(v100_4, temp3, -DBL_MAX);
3361 b = v100_4 + temp3 - a;
3362 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_4
3363 a = max_imum(v100_5, temp3, -DBL_MAX);
3364 b = v100_5 + temp3 - a;
3365 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_5
3366 a = max_imum(v100_6, temp3, -DBL_MAX);
3367 b = v100_6 + temp3 - a;
3368 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_6
3369 a = max_imum(v100_7, temp3, -DBL_MAX);
3370 b = v100_7 + temp3 - a;
3371 temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
3372
3373 temp_total = log(emission_prob[trio]) + maximum(v100_1, v100_2, v100_3, v100_4, v100_5, v100_6, v100_7);
3374
3375 //V100[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v100_1, v100_2, v100_3, v100_4, v100_5, v100_6, v100_7);
3376 //V100[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][3]) + maximum(v100_1, v100_2, v100_3, v100_4, v100_5, v100_6, v100_7);
3377 //V100[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][3]) + temp3;
3378 V100_b[dim1][dim2][dim3] = temp3;
3379
3380 // INS13 matrix
3381
3382 trio[0] = x1[dim1-1];
3383 trio[1] = gap;
3384 trio[2] = x3[dim3-1];
3385
3386 temp_state_111 = log(prev_state_to_101["111"]);
3387 temp_state_110 = log(prev_state_to_101["110"]);
3388 temp_state_101 = log(prev_state_to_101["101"]);
3389 temp_state_100 = log(prev_state_to_101["100"]);
3390 temp_state_011 = log(prev_state_to_101["11"]);
3391 temp_state_010 = log(prev_state_to_101["10"]);
3392 temp_state_001 = log(prev_state_to_101["1"]);
3393
3394 temp_emission1 = log(emission_prob[trio]);
3395 temp_emission1 = log(emission_double[alpha_indexes_emissions[trio]][4]);
3396
3397 v101_1 = log(prev_state_to_111["101"]) + V111_b[dim1 + 1][dim2 + 1][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_111]][6]);
3398 v101_2 = log(prev_state_to_110["101"]) + V110_b[dim1 + 1][dim2 + 1][dim3] + log(emission_double[alpha_indexes_emissions[triplet_110]][5]);
3399 v101_3 = log(prev_state_to_101["101"]) + V101_b[dim1 + 1][dim2][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_101]][4]);
3400 v101_4 = log(prev_state_to_100["101"]) + V100_b[dim1 + 1][dim2][dim3] + log(emission_double[alpha_indexes_emissions[triplet_100]][3]);
3401 v101_5 = log(prev_state_to_011["101"]) + V011_b[dim1][dim2 + 1][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_011]][2]);
3402 v101_6 = log(prev_state_to_010["101"]) + V010_b[dim1][dim2 + 1][dim3] + log(emission_double[alpha_indexes_emissions[triplet_010]][1]);
3403 v101_7 = log(prev_state_to_001["101"]) + V001_b[dim1][dim2][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_001]][0]);
3404
3405 a = max_imum(v101_1, v101_2, -DBL_MAX); // evaluating sum of logs for 'ALIGN' state.
3406 b = v101_1 + v101_2 - a;
3407 temp3 = a + log(1 + exp(b-a));
3408 a = max_imum(v101_3, temp3, -DBL_MAX);
3409 b = v101_3 + temp3 - a;
3410 temp3 = a + log(1 + exp(b-a)); // sum of v111_1, v111_2 and v111_3 = temp3
3411 a = max_imum(v101_4, temp3, -DBL_MAX);
3412 b = v101_4 + temp3 - a;
3413 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_4
3414 a = max_imum(v101_5, temp3, -DBL_MAX);
3415 b = v101_5 + temp3 - a;
3416 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_5
3417 a = max_imum(v101_6, temp3, -DBL_MAX);
3418 b = v101_6 + temp3 - a;
3419 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_6
3420 a = max_imum(v101_7, temp3, -DBL_MAX);
3421 b = v101_7 + temp3 - a;
3422 temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
3423
3424 temp_total = log(emission_prob[trio]) + maximum(v101_1, v101_2, v101_3, v101_4, v101_5, v101_6, v101_7);
3425
3426 //V101[dim1][dim2][dim3] = log(emission_prob[trio]) + maximum(v101_1, v101_2, v101_3, v101_4, v101_5, v101_6, v101_7);
3427 //V101[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][4]) + maximum(v101_1, v101_2, v101_3, v101_4, v101_5, v101_6, v101_7);
3428 //V101[dim1][dim2][dim3] = log(emission_double[alpha_indexes_emissions[trio]][4]) + temp3;
3429 V101_b[dim1][dim2][dim3] = temp3;
3430
3431 // INS12 matrix
3432
3433 trio[0] = x1[dim1-1];
3434 trio[1] = x2[dim2-1];
3435 trio[2] = gap;
3436
3437 temp_state_111 = log(prev_state_to_110["111"]);
3438 temp_state_110 = log(prev_state_to_110["110"]);
3439 temp_state_101 = log(prev_state_to_110["101"]);
3440 temp_state_100 = log(prev_state_to_110["100"]);
3441 temp_state_011 = log(prev_state_to_110["11"]);
3442 temp_state_010 = log(prev_state_to_110["10"]);
3443 temp_state_001 = log(prev_state_to_110["1"]);
3444
3445 temp_emission1 = log(emission_prob[trio]);
3446 temp_emission1 = log(emission_double[alpha_indexes_emissions[trio]][5]);
3447
3448 v110_1 = log(prev_state_to_111["110"]) + V111_b[dim1 + 1][dim2 + 1][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_111]][6]);
3449 v110_2 = log(prev_state_to_110["110"]) + V110_b[dim1 + 1][dim2 + 1][dim3] + log(emission_double[alpha_indexes_emissions[triplet_110]][5]);
3450 v110_3 = log(prev_state_to_101["110"]) + V101_b[dim1 + 1][dim2][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_101]][4]);
3451 v110_4 = log(prev_state_to_100["110"]) + V100_b[dim1 + 1][dim2][dim3] + log(emission_double[alpha_indexes_emissions[triplet_100]][3]);
3452 v110_5 = log(prev_state_to_011["110"]) + V011_b[dim1][dim2 + 1][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_011]][2]);
3453 v110_6 = log(prev_state_to_010["110"]) + V010_b[dim1][dim2 + 1][dim3] + log(emission_double[alpha_indexes_emissions[triplet_010]][1]);
3454 v110_7 = log(prev_state_to_001["110"]) + V001_b[dim1][dim2][dim3 + 1] + log(emission_double[alpha_indexes_emissions[triplet_001]][0]);
3455
3456 a = max_imum(v110_1, v110_2, -DBL_MAX); // evaluating sum of logs for 'ALIGN' state.
3457 b = v110_1 + v110_2 - a;
3458 temp3 = a + log(1 + exp(b-a));
3459 a = max_imum(v110_3, temp3, -DBL_MAX);
3460 b = v110_3 + temp3 - a;
3461 temp3 = a + log(1 + exp(b-a)); // sum of v111_1, v111_2 and v111_3 = temp3
3462 a = max_imum(v110_4, temp3, -DBL_MAX);
3463 b = v110_4 + temp3 - a;
3464 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_4
3465 a = max_imum(v110_5, temp3, -DBL_MAX);
3466 b = v110_5 + temp3 - a;
3467 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_5
3468 a = max_imum(v110_6, temp3, -DBL_MAX);
3469 b = v110_6 + temp3 - a;
3470 temp3 = a + log(1 + exp(b-a)); // sum of temp3 and v111_6
3471 a = max_imum(v110_7, temp3, -DBL_MAX);
3472 b = v110_7 + temp3 - a;
3473 temp3 = a + log(1 + exp(b-a)); // sum of all state transitions
3474
3475 temp_total = log(emission_prob[trio]) + maximum(v110_1, v110_2, v110_3, v110_4, v110_5, v110_6, v110_7);
3476
3477 V110_b[dim1][dim2][dim3] = temp3;
3478 }
3479
3480
3481 //}
3482 }
3483 }
3484 }
3485
3486 /*// printing arrays
3487 cout << "V111_b: " << endl;
3488 for (dim1 = 0; dim1 <= x1.length()-1; dim1++)
3489 {
3490 cout << "Dim1: " << dim1 << endl;
3491 for (dim2 = 0; dim2 <= x2.length()-1; dim2++)
3492 {
3493 cout << "Dim2: " << dim2 << endl;
3494 for (dim3 = 0; dim3 <= x3.length()-1; dim3++)
3495 {
3496 cout << V111_b[dim1][dim2][dim3] << " ";
3497 }
3498 cout << endl;
3499 }
3500 }
3501
3502 cout << "V100_b: " << endl;
3503 for (dim1 = 0; dim1 <= x1.length()-1; dim1++)
3504 {
3505 cout << "Dim1: " << dim1 << endl;
3506 for (dim2 = 0; dim2 <= x2.length()-1; dim2++)
3507 {
3508 cout << "Dim2: " << dim2 << endl;
3509 for (dim3 = 0; dim3 <= x3.length()-1; dim3++)
3510 {
3511 cout << V100_b[dim1][dim2][dim3] << " ";
3512 }
3513 cout << endl;
3514 }
3515 }
3516
3517 cout << "V110_b: " << endl;
3518 for (dim1 = 0; dim1 <= x1.length()-1; dim1++)
3519 {
3520 cout << "Dim1: " << dim1 << endl;
3521 for (dim2 = 0; dim2 <= x2.length()-1; dim2++)
3522 {
3523 cout << "Dim2: " << dim2 << endl;
3524 for (dim3 = 0; dim3 <= x3.length()-1; dim3++)
3525 {
3526 cout << V110_b[dim1][dim2][dim3] << " ";
3527 }
3528 cout << endl;
3529 }
3530 }*/
3531 }
3532
3533
3534 void max_expected_accuracy()
3535 {
3536
3537 double trans_001, trans_010, trans_011, trans_100, trans_101, trans_110, trans_111, cost, temp, temp1, temp2;
3538
3539 // BM array initializer
3540 for (int count1 = 0; count1 <= x1.length(); count1++)
3541 {
3542 BM.push_back(rowvec1);
3543 BM.push_back(rowvec1);
3544 BM.push_back(rowvec1);
3545 BM.push_back(rowvec1);
3546 BM.push_back(rowvec1);
3547 BM.push_back(rowvec1);
3548 BM.push_back(rowvec1);
3549
3550 for (int count2 = 0; count2 <= x2.length(); count2++)
3551 {
3552 BM[count1].push_back(rowvec2);
3553 BM[count1].push_back(rowvec2);
3554 BM[count1].push_back(rowvec2);
3555 BM[count1].push_back(rowvec2);
3556 BM[count1].push_back(rowvec2);
3557 BM[count1].push_back(rowvec2);
3558 BM[count1].push_back(rowvec2);
3559
3560 for (int count3 = 0; count3 <= x3.length(); count3++)
3561 {
3562 BM[count1][count2].push_back(0);
3563 BM[count1][count2].push_back(0);
3564 BM[count1][count2].push_back(0);
3565 BM[count1][count2].push_back(0);
3566 BM[count1][count2].push_back(0);
3567 BM[count1][count2].push_back(0);
3568 BM[count1][count2].push_back(0);
3569 }
3570 }
3571 }
3572
3573 temp1 = exp(V111_f[1][1][1] + V111_b[0][0][0]);
3574 temp2 = exp(V111_f[2][2][2] + V111_b[1][1][1]);
3575
3576 // matrix filling
3577 for (dim1 = 1; dim1 <= x1.length(); dim1++)
3578 {
3579 for (dim2 = 1; dim2 <= x2.length(); dim2++)
3580 {
3581 for (dim3 = 1; dim3 <= x3.length(); dim3++)
3582 {
3583 cost = exp(V111_f[dim1][dim2][dim3] + V111_b[dim1-1][dim2-1][dim3-1]);
3584 trans_111 = BM[dim1 - 1][dim2 - 1][dim3 - 1] + cost;
3585 trans_110 = BM[dim1 - 1][dim2 - 1][dim3];
3586 trans_101 = BM[dim1 - 1][dim2][dim3 - 1];
3587 trans_100 = BM[dim1 - 1][dim2][dim3];
3588 trans_011 = BM[dim1][dim2 - 1][dim3 - 1];
3589 trans_010 = BM[dim1][dim2 - 1][dim3];
3590 trans_001 = BM[dim1][dim2][dim3 - 1];
3591 temp = maximum(trans_001, trans_010, trans_011, trans_100, trans_101, trans_110, trans_111);
3592
3593 BM[dim1][dim2][dim3] = maximum(trans_001, trans_010, trans_011, trans_100, trans_101, trans_110, trans_111);
3594 }
3595 }
3596 }
3597
3598 // printing arrays
3599 for (dim1 = 1; dim1 <= x1.length(); dim1++)
3600 {
3601 cout << "dim1: " << dim1 << endl;
3602 for (dim2 = 1; dim2 <= x2.length(); dim2++)
3603 {
3604 cout << "dim2: " << dim2 << endl;
3605 for (dim3 = 1; dim3 <= x3.length(); dim3++)
3606 {
3607 //cout << BM[dim1][dim2][dim3] << " ";
3608 }
3609 cout << endl;
3610 }
3611 }
3612
3613 int wait = 1;
3614
3615 // trace-back
3616 dim1 = x1.length();
3617 dim2 = x2.length();
3618 dim3 = x3.length();
3619
3620 while (dim1 >= 1 && dim2 >= 1 && dim3 >= 1)
3621 {
3622
3623 cost = exp(V111_f[dim1][dim2][dim3] + V111_b[dim1-1][dim2-1][dim3-1]);
3624
3625 // aligned state
3626 if (dim1 >= 1 && dim2 >= 1 && dim3 >= 1 && ((BM[dim1 - 1][dim2 - 1][dim3 - 1] + cost == BM[dim1][dim2][dim3])))
3627 {
3628 align1_reverse.push_back(x1[dim1-1]);
3629 align2_reverse.push_back(x2[dim2-1]);
3630 align3_reverse.push_back(x3[dim3-1]);
3631 dim1--;
3632 dim2--;
3633 dim3--;
3634 }
3635
3636 // insertion in seq 1,2 110
3637 else if (dim1 >= 1 && dim2 >= 1 && ((BM[dim1 - 1][dim2 - 1][dim3] == BM[dim1][dim2][dim3])))
3638 {
3639 align1_reverse.push_back(x1[dim1-1]);
3640 align2_reverse.push_back(x2[dim2-1]);
3641 align3_reverse.push_back(gap);
3642 dim1--;
3643 dim2--;
3644
3645 }
3646
3647 // insertion in seq 1,3 101
3648 else if (dim1 >= 1 && dim3 >= 1 && ((BM[dim1 - 1][dim2][dim3 - 1] == BM[dim1][dim2][dim3])))
3649 {
3650 align1_reverse.push_back(x1[dim1-1]);
3651 align2_reverse.push_back(gap);
3652 align3_reverse.push_back(x3[dim3-1]);
3653 dim1--;
3654 dim3--;
3655 }
3656
3657 // insertion in seq 1, 100
3658 else if (dim1 >= 1 && ((BM[dim1 - 1][dim2][dim3] == BM[dim1][dim2][dim3])))
3659 {
3660 align1_reverse.push_back(x1[dim1-1]);
3661 align2_reverse.push_back(gap);
3662 align3_reverse.push_back(gap);
3663 dim1--;
3664 }
3665
3666 // insertion in seq 2,3, 011
3667 else if (dim2 >= 1 && dim3 >= 1 && ((BM[dim1][dim2 - 1][dim3 - 1] == BM[dim1][dim2][dim3])))
3668 {
3669 align1_reverse.push_back(gap);
3670 align2_reverse.push_back(x2[dim2-1]);
3671 align3_reverse.push_back(x3[dim3-1]);
3672 dim2--;
3673 dim3--;
3674 }
3675
3676 // insertion in seq 2, 010
3677 else if (dim2 >= 1 && ((BM[dim1][dim2 - 1][dim3] == BM[dim1][dim2][dim3])))
3678 {
3679 align1_reverse.push_back(gap);
3680 align2_reverse.push_back(x2[dim2-1]);
3681 align3_reverse.push_back(gap);
3682 dim2--;
3683 }
3684
3685 // insertion in seq 3, 001
3686 else //if (dim3 >= 1 && ((BM[dim1][dim2][dim3 - 1] == BM[dim1][dim2][dim3])))
3687 {
3688 align1_reverse.push_back(gap);
3689 align2_reverse.push_back(gap);
3690 align3_reverse.push_back(x3[dim3-1]);
3691 //dim1--;
3692 //dim2--;
3693 dim3--;
3694 }
3695 }
3696
3697 // reversing arrays
3698 for (int i = align1_reverse.length(); i > 0; i--)
3699 {
3700 align1.push_back(align1_reverse[i-1]);
3701 align2.push_back(align2_reverse[i-1]);
3702 align3.push_back(align3_reverse[i-1]);
3703 }
3704
3705
3706 }
3707
3708};
3709
3710#endif /* FUNCTIONS_H_ */