| 1 | #include "alignment.h" |
| 2 | |
| 3 | #include <algorithm> |
| 4 | #include <fstream> |
| 5 | #include <iostream> |
| 6 | #include <vector> |
| 7 | |
| 8 | /* Substitution Matrix |
| 9 | |
| 10 | A T G C N |
| 11 | A 1 -1 -1 -1 0 |
| 12 | T -1 1 -1 -1 0 |
| 13 | G -1 -1 1 -1 0 |
| 14 | C -1 -1 -1 1 0 |
| 15 | N 0 0 0 0 0 |
| 16 | */ |
| 17 | |
| 18 | int get_sub_score(char a, char b) { |
| 19 | int i = 4, j = 4; |
| 20 | switch (a) { |
| 21 | case 'A': |
| 22 | i = 0; |
| 23 | break; |
| 24 | case 'T': |
| 25 | i = 1; |
| 26 | break; |
| 27 | case 'G': |
| 28 | i = 2; |
| 29 | break; |
| 30 | case 'C': |
| 31 | i = 3; |
| 32 | break; |
| 33 | case 'N': |
| 34 | i = 4; |
| 35 | break; |
| 36 | } |
| 37 | switch (b) { |
| 38 | case 'A': |
| 39 | j = 0; |
| 40 | break; |
| 41 | case 'T': |
| 42 | j = 1; |
| 43 | break; |
| 44 | case 'G': |
| 45 | j = 2; |
| 46 | break; |
| 47 | case 'C': |
| 48 | j = 3; |
| 49 | break; |
| 50 | case 'N': |
| 51 | j = 4; |
| 52 | break; |
| 53 | } |
| 54 | return substitution[i][j]; |
| 55 | } |
| 56 | |
| 57 | int get_gap_penalty(int k, char a) { |
| 58 | if (a == 'N') // Allow for i7 and i5 indexesto be specified as NNNNNNNN |
| 59 | return 0; |
| 60 | return k * gap_open; // Linear Gap Penalty |
| 61 | } |
| 62 | |
| 63 | void print_matrix(int h[][max_adapter_size], int r, int c, std::string read, |
| 64 | std::string adap) { |
| 65 | for (int i = 0; i < r; i++) { |
| 66 | if (i == 0) { |
| 67 | std::cout << " "; |
| 68 | for (int k = 0; k < c - 1; k++) std::cout << adap[k] << " "; |
| 69 | std::cout << "\n"; |
| 70 | } |
| 71 | for (int j = 0; j < c; j++) { |
| 72 | if (j == 0 && i > 0) { |
| 73 | if (i - 1 < (int)read.length()) std::cout << read[i - 1] << " "; |
| 74 | } |
| 75 | if (i == 0 && j == 0) |
| 76 | std::cout << " " |
| 77 | << " "; |
| 78 | std::cout << h[i][j] << " "; |
| 79 | } |
| 80 | std::cout << "\n"; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Return Value 1 - Diag, 2->Right, 3->Down, 0-> 0 |
| 85 | int* get_score_cell(int h[][max_adapter_size], int i, int j, std::string read, |
| 86 | std::string adap) { |
| 87 | int s[] = {0, 0, 0}; |
| 88 | int* tmp = new int[2]; |
| 89 | tmp[0] = 0; |
| 90 | tmp[1] = 0; |
| 91 | s[0] = h[i - 1][j - 1] + get_sub_score(read[i - 1], adap[j - 1]); |
| 92 | s[1] = h[i - 1][j] - get_gap_penalty(1, adap[j - 1]); |
| 93 | s[2] = h[i][j - 1] - get_gap_penalty(1, read[i - 1]); |
| 94 | int max = (s[0] >= s[1]) ? s[0] : s[1]; |
| 95 | max = (max >= s[2]) ? max : s[2]; |
| 96 | max = (max > 0) ? max : 0; |
| 97 | tmp[0] = max; |
| 98 | if (max == s[0]) { |
| 99 | tmp[1] = 1; |
| 100 | } else if (max == s[1]) { |
| 101 | tmp[1] = 3; |
| 102 | } else if (max == s[2]) { |
| 103 | tmp[1] = 2; |
| 104 | } |
| 105 | return tmp; |
| 106 | } |
| 107 | |
| 108 | void print_alignment(char a[2][max_read_size], int n) { |
| 109 | std::cout << "Alignment: " << std::endl << std::endl; |
| 110 | for (int j = 0; j < 2; j++) { |
| 111 | if (j == 0) |
| 112 | std::cout << "Read: "; |
| 113 | else if (j == 1) |
| 114 | std::cout << "Adap: "; |
| 115 | for (int i = n - 1; i >= 0; i--) { |
| 116 | std::cout << a[j][i] << " "; |
| 117 | } |
| 118 | std::cout << std::endl; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | int* align_seqs(std::string read, std::string adap) { |
| 123 | int h[max_read_size][max_adapter_size], t[max_read_size][max_adapter_size], |
| 124 | m = read.length() + 1, n = adap.length() + 1, max_i = 0, max_j = 0, |
| 125 | max_v = -1, max_score, *rt = new int[2], *tmp = new int[2]; |
| 126 | rt[0] = -1; |
| 127 | rt[1] = read.length(); |
| 128 | // Initialize first column to 0 |
| 129 | for (int i = 0; i < n; i++) h[0][i] = 0; |
| 130 | // Initialize first row to 0 |
| 131 | for (int i = 0; i < m; i++) h[i][0] = 0; |
| 132 | // print_matrix(h, m, n, read, adap); |
| 133 | for (int i = 1; i < m; i++) { |
| 134 | for (int j = 1; j < n; j++) { |
| 135 | tmp = get_score_cell(h, i, j, read, adap); |
| 136 | h[i][j] = *tmp; |
| 137 | t[i][j] = *(tmp + 1); |
| 138 | if (*tmp >= max_v) { |
| 139 | max_i = i; |
| 140 | max_j = j; |
| 141 | max_v = *tmp; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | max_score = max_v; |
| 146 | // if(max_i != read.length()) // Ensure alignment to end of read. |
| 147 | // return rt; |
| 148 | // std::cout << "Score: " << max_v << " "; |
| 149 | // print_matrix(h, m, n, read, adap); |
| 150 | // print_matrix(t, m, n, read, adap); |
| 151 | // Traceback |
| 152 | int _t, _l, _d, _align_n = 0; |
| 153 | char _align[2][max_read_size]; |
| 154 | // 1 - Diag, 2->Right, 3->Down |
| 155 | while (max_v != 0) { |
| 156 | _d = h[max_i - 1][max_j - 1]; |
| 157 | _l = h[max_i][max_j - 1]; |
| 158 | _t = h[max_i - 1][max_j]; |
| 159 | switch (t[max_i][max_j]) { |
| 160 | case 1: |
| 161 | max_v = _d; |
| 162 | max_i = max_i - 1; |
| 163 | max_j = max_j - 1; |
| 164 | _align[0][_align_n] = read[max_i]; |
| 165 | _align[1][_align_n] = adap[max_j]; |
| 166 | _align_n++; |
| 167 | break; |
| 168 | case 2: |
| 169 | max_v = _l; |
| 170 | max_j = max_j - 1; |
| 171 | _align[0][_align_n] = '-'; |
| 172 | _align[1][_align_n] = adap[max_j]; |
| 173 | _align_n++; |
| 174 | break; |
| 175 | case 3: |
| 176 | max_v = _t; |
| 177 | max_i = max_i - 1; |
| 178 | _align[0][_align_n] = read[max_i]; |
| 179 | _align[1][_align_n] = '-'; |
| 180 | _align_n++; |
| 181 | break; |
| 182 | case 0: |
| 183 | max_v = 0; |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | rt[1] = max_i; |
| 188 | print_alignment(_align, _align_n); |
| 189 | if (max_score <= ((_align_n - 2) * unit_score) - get_gap_penalty(2, 'A') || |
| 190 | max_j != 0) // If alignment does not start at beginning of adapter |
| 191 | return rt; |
| 192 | rt[0] = max_score; |
| 193 | // print_alignment(_align, _align_n); |
| 194 | return rt; |
| 195 | } |
| 196 | |
| 197 | // int find_adapters_contaminants(std::istream &cin, std::string |
| 198 | // adp_cntms_file){ |
| 199 | // std::vector<std::string> adp = read_adapters_from_fasta(adp_cntms_file, |
| 200 | // "Read1"); std::string line, bases, qual, b; int ctr = 0, score, *t = new |
| 201 | // int[2]; while (std::getline(cin, line)){ |
| 202 | // if(ctr % 400000 == 0) |
| 203 | // std::cout << "Processed " << (ctr/4) << " reads ..." << std::endl; |
| 204 | // if(ctr % 2 == 0){ |
| 205 | // ctr++; |
| 206 | // continue; |
| 207 | // } |
| 208 | // if((ctr+1) % 2 == 0 && (ctr + 1) % 4 == 0 ){ |
| 209 | // qual = line; |
| 210 | // // std::cout << bases << std::endl; |
| 211 | // // std::cout << qual << std::endl; |
| 212 | // score = -1; |
| 213 | // for(std::vector<std::string>::iterator it = adp.begin(); it != |
| 214 | // adp.end() && score == -1; ++it) { |
| 215 | // b = ((*it).length() > bases.length()) ? bases : |
| 216 | // bases.substr(bases.length() - (*it).length(), (*it).length()); t = |
| 217 | // align_seqs(b, *it); score = *t; if(score != -1) std::cout << "Trimmed: " << |
| 218 | // bases.substr(0, bases.length() - *(t+1)) << std::endl; |
| 219 | // } |
| 220 | // } else { |
| 221 | // bases = line; |
| 222 | // } |
| 223 | // ctr++; |
| 224 | // } |
| 225 | // return 0; |
| 226 | // } |
| 227 | |
| 228 | // int main(int argc, char* argv[]){ |
| 229 | // std::string adp = "../data/adapters/NexteraPR.fa"; |
| 230 | // // clock_t begin = clock(); |
| 231 | // find_adapters_contaminants(std::cin, adp); |
| 232 | // // clock_t end = clock(); |
| 233 | // // double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; |
| 234 | // // std::cout << "Time Taken: " << elapsed_secs << std::endl; |
| 235 | // return 0; |
| 236 | // } |