1 /* The MIT License
2 
3    Copyright (c) 2015 Manuel Holtgrewe <manuel.holtgrewe@bihealth.de>
4 
5    Permission is hereby granted, free of charge, to any person obtaining a copy
6    of this software and associated documentation files (the "Software"), to deal
7    in the Software without restriction, including without limitation the rights
8    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9    copies of the Software, and to permit persons to whom the Software is
10    furnished to do so, subject to the following conditions:
11 
12    The above copyright notice and this permission notice shall be included in
13    all copies or substantial portions of the Software.
14 
15    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21    THE SOFTWARE.
22 */
23 
24 #include "needle.h"
25 
26 #include <algorithm>
27 #include <iostream>
28 #include <fstream>
29 #include <string>
30 
NeedlemanWunsch(bool debug)31 NeedlemanWunsch::NeedlemanWunsch(bool debug)
32 {
33     this->debug = debug;
34 }
35 
align(const char * ref,const char * read)36 void NeedlemanWunsch::align(const char* ref, const char* read)
37 {
38     this->ref = ref;
39     this->read = read;
40     this->len_ref = strlen(ref);
41     this->len_read = strlen(read);
42 
43     matrix.clear();
44     matrix.resize((len_ref + 1) * (len_read + 1), EMPTY);
45 
46     // fill first column and row
47     scores.resize(len_read + 1);
48     for (unsigned i = 0; i <= len_ref; ++i)
49         matrix.at(i * (len_read + 1)) = CIGAR_D;
50     for (unsigned i = 0; i <= len_read; ++i)
51     {
52         matrix.at(i) = CIGAR_I;
53         scores.at(i) = i * params.score_gap;
54     }
55     matrix.at(0) = CIGAR_M;
56 
57     // fill remainder of the matrix
58     int offset = 0;
59     int score_n = 0;
60     for (unsigned i = 1; i <= len_ref; ++i)
61     {
62         offset += len_read + 1;
63         score_n += params.score_gap;
64         int best_score = 0;
65         for (unsigned j = 1; j <= len_read; ++j)
66         {
67             int score_nw = scores.at(j - 1);
68             int score_w = scores.at(j);
69 
70             int score_diag = score_nw;
71             Traceback best_dir;
72             if (ref[i - 1] == read[j - 1])
73             {
74                 best_dir = CIGAR_M;
75                 score_diag += params.score_match;
76             }
77             else
78             {
79                 best_dir = CIGAR_X;
80                 score_diag += params.score_mismatch;
81             }
82 
83             best_score = score_diag;
84 
85             if (score_w + params.score_gap > best_score)
86             {
87                 best_score = score_w + params.score_gap;
88                 best_dir = CIGAR_D;
89             }
90             else if (score_n + params.score_gap > best_score)
91             {
92                 best_score = score_n + params.score_gap;
93                 best_dir = CIGAR_I;
94             }
95 
96             scores.at(j - 1) = score_n;
97             score_n = best_score;
98             matrix.at(offset + j) = best_dir;
99         }
100         scores.back() = best_score;
101     }
102 
103 }
104 
trace_path()105 void NeedlemanWunsch::trace_path()
106 {
107     int i = len_ref;
108     int j = len_read;
109     int k = (len_read + 1) * (len_ref + 1) - 1;
110 
111     trace.clear();
112 
113     while (i>0 || j>0)
114     {
115         trace.push_back(matrix.at(k));
116         switch ((int32_t) matrix.at(k))
117         {
118             case CIGAR_X:
119             case CIGAR_M:
120                 --i;
121                 --j;
122                 k -= len_read + 2;
123                 break;
124             case CIGAR_I:
125                 --j;
126                 --k;
127                 break;
128             case CIGAR_D:
129                 --i;
130                 k -= len_read + 1;
131                 break;
132         }
133     }
134 
135     std::reverse(trace.begin(), trace.end());
136 }
137 
print_alignment(std::string const & pad)138 void NeedlemanWunsch::print_alignment(std::string const & pad)
139 {
140     // print reference
141     std::cerr << pad;
142     for (unsigned i = 0, j = 0; i < trace.size(); ++i)
143     {
144         if (trace.at(i) == CIGAR_X || trace[i] == CIGAR_M || trace.at(i) == CIGAR_D)
145             std::cerr << ref[j++];
146         else
147             std::cerr << "-";
148     }
149     std::cerr << "\n";
150 
151     // print trace
152     std::cerr << pad;
153     for (unsigned i = 0, j = 0, k = 0; i < trace.size(); ++i)
154     {
155         if (trace[i] == CIGAR_X || trace[i] == CIGAR_M)
156             std::cerr << ((ref[j++] == read[k++]) ? '|' : ' ');
157         else if (trace[i] == CIGAR_D)
158         {
159             j++;
160             std::cerr << " ";
161         }
162         else  // trace[i] == CIGAR_I
163         {
164             k++;
165             std::cerr << " ";
166         }
167     }
168     std::cerr << "\n";
169 
170     // print read
171     std::cerr << pad;
172     for (unsigned i = 0, j = 0; i < trace.size(); ++i)
173     {
174         if (trace[i] == CIGAR_X || trace[i] == CIGAR_M || trace[i] == CIGAR_I)
175             std::cerr << read[j++];
176         else
177             std::cerr << "-";
178     }
179     std::cerr << "\n";
180 }
181