| 1 | #include "suffix_tree.h" |
| 2 | |
| 3 | #include "algorithm" |
| 4 | #include "alignment.h" |
| 5 | #include "fstream" |
| 6 | #include "iostream" |
| 7 | #include "vector" |
| 8 | |
| 9 | suffix_node::suffix_node(int b, int *e, suffix_node *p, suffix_node *l) { |
| 10 | this->children = new suffix_node *[MAX_CHAR]; |
| 11 | for (unsigned int i = 0; i < MAX_CHAR; i++) this->children[i] = 0; |
| 12 | this->begin = b; |
| 13 | this->end = e; |
| 14 | this->nchildren = 0; |
| 15 | this->parent = p; |
| 16 | this->suffix_link = l; |
| 17 | } |
| 18 | |
| 19 | bool suffix_node::is_leaf_node() { return (nchildren == 0); } |
| 20 | |
| 21 | bool suffix_node::contains_child(int ext) { |
| 22 | return !(this->children[ext] == 0); |
| 23 | } |
| 24 | |
| 25 | int suffix_node::get_length() { |
| 26 | if (this->end == 0) return 0; // For root |
| 27 | return *(this->end) - this->begin + 1; |
| 28 | } |
| 29 | |
| 30 | int suffix_node::get_depth() { |
| 31 | int ctr = 0; |
| 32 | suffix_node *n = this; |
| 33 | while (n->begin != -1) { |
| 34 | n = n->parent; |
| 35 | ctr++; |
| 36 | } |
| 37 | return ctr; |
| 38 | } |
| 39 | |
| 40 | int get_hamming_distance(std::string s1, std::string s2, int k) { |
| 41 | int n = 0; |
| 42 | for (unsigned int i = 0; i < s1.length(); i++) { |
| 43 | if (s1[i] != s2[i]) { |
| 44 | n++; |
| 45 | } |
| 46 | if (n > k) return -1; |
| 47 | } |
| 48 | return n; |
| 49 | } |
| 50 | |
| 51 | std::string suffix_node::get_longest_common_substring(std::string s1, |
| 52 | std::string s2) { |
| 53 | std::string str = s1 + "#" + s2 + "@"; |
| 54 | std::string p = "", tmp, longest_trvsl = ""; |
| 55 | unsigned int max = 0, i; |
| 56 | suffix_node *node = this; |
| 57 | if (node->begin == -1) |
| 58 | p = ""; |
| 59 | else { |
| 60 | tmp = node->get_path(str); |
| 61 | if (s1.find(p + tmp) != std::string::npos && |
| 62 | s2.find(p + tmp) != std::string::npos) |
| 63 | p += tmp; |
| 64 | } |
| 65 | tmp = ""; |
| 66 | for (i = 0; i < MAX_CHAR; i++) { |
| 67 | if (node->children[i] == 0) continue; |
| 68 | longest_trvsl = node->children[i]->get_longest_common_substring(s1, s2); |
| 69 | if (longest_trvsl.length() > max) { |
| 70 | max = longest_trvsl.length(); |
| 71 | tmp = longest_trvsl; |
| 72 | } |
| 73 | } |
| 74 | p += tmp; |
| 75 | return p; |
| 76 | } |
| 77 | |
| 78 | std::string suffix_node::get_path(std::string s) { |
| 79 | if (this->begin == -1) return "R"; |
| 80 | return s.substr(this->begin, *(this->end) - this->begin + 1); |
| 81 | } |
| 82 | |
| 83 | void suffix_node::extend_path(int *e) { this->end = e; } |
| 84 | |
| 85 | suffix_node *suffix_node::add_child(int ext, int b, int *e, suffix_node *l) { |
| 86 | suffix_node *n = new suffix_node(b, e, this, l); |
| 87 | this->children[ext] = n; |
| 88 | this->nchildren++; |
| 89 | return n; |
| 90 | } |
| 91 | |
| 92 | void suffix_node::add_child(suffix_node *c, int ext) { |
| 93 | this->children[ext] = c; |
| 94 | c->parent = this; |
| 95 | this->nchildren++; |
| 96 | } |
| 97 | |
| 98 | suffix_node *suffix_node::get_child(int ext) { return this->children[ext]; } |
| 99 | |
| 100 | bool suffix_node::contains_depth(int depth) { |
| 101 | return this->get_depth() <= depth && |
| 102 | depth <= (this->get_length() + this->get_depth()); |
| 103 | } |
| 104 | |
| 105 | void suffix_node::print(std::string s) { |
| 106 | for (int i = 0; i < this->get_depth(); i++) { |
| 107 | std::cout << " "; |
| 108 | } |
| 109 | std::string t = |
| 110 | (this->begin == -1) |
| 111 | ? "R" |
| 112 | : " " + s.substr(this->begin, *(this->end) - this->begin + 1); |
| 113 | std::cout << t; |
| 114 | if (this->suffix_link != 0) |
| 115 | std::cout << " --- ( " << this->suffix_link->parent->get_path(s) << " " |
| 116 | << this->suffix_link->get_path(s) << ")"; |
| 117 | if (this->begin != -1) std::cout << " - " << this->parent->get_path(s); |
| 118 | std::cout << std::endl; |
| 119 | for (unsigned int i = 0; i < alphabet.length(); i++) { |
| 120 | if (this->children[i] != 0) this->children[i]->print(s); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | bool suffix_node::walk_next(int &beg, int &suffix_length) { |
| 125 | suffix_node *node = this; |
| 126 | if (suffix_length >= node->get_length()) { |
| 127 | beg += node->get_length(); |
| 128 | suffix_length -= node->get_length(); |
| 129 | return true; |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | suffix_node *build_suffix_tree(std::string s) { |
| 135 | suffix_node *root = new suffix_node(-1, 0, 0, 0); |
| 136 | root->suffix_link = root; |
| 137 | root->parent = root; |
| 138 | suffix_node *cur_node = root, *new_node = 0, *new_cur_node = 0; |
| 139 | int str_ind[MAX_SIZE], suffix_length = 0, *leaf_end = new int, |
| 140 | suffix_count = 0, beg = -1, *end; |
| 141 | unsigned int i = 0, n_str_ind = 0; |
| 142 | for (i = 0; i < s.length(); i++) { |
| 143 | str_ind[i] = alphabet.find(s[i]); |
| 144 | n_str_ind++; |
| 145 | } |
| 146 | *leaf_end = 0; |
| 147 | // root->add_child(str_ind[0], 0, leaf_end, root); |
| 148 | for (i = 0; i < n_str_ind; i++) { |
| 149 | *leaf_end = i; // Handle Extension Rule 1 |
| 150 | suffix_count++; |
| 151 | new_node = 0; |
| 152 | while (suffix_count > 0) { |
| 153 | if (suffix_length == 0) beg = i; |
| 154 | if (cur_node->children[str_ind[beg]] == 0) { // Rule 2 |
| 155 | cur_node->add_child(str_ind[beg], beg, leaf_end, 0); // Trick 3 |
| 156 | if (new_node != 0) { |
| 157 | new_node->suffix_link = cur_node; |
| 158 | new_node = 0; // No internal node created in Rule 2 here. |
| 159 | } |
| 160 | } else { |
| 161 | new_cur_node = cur_node->children[str_ind[beg]]; |
| 162 | if (new_cur_node->walk_next(beg, suffix_length)) { |
| 163 | cur_node = new_cur_node; |
| 164 | continue; |
| 165 | } |
| 166 | if (str_ind[new_cur_node->begin + suffix_length] == |
| 167 | str_ind[i]) { // Rule 3 |
| 168 | if (new_node != 0 && cur_node->end != 0) { |
| 169 | new_node->suffix_link = cur_node; |
| 170 | new_node = 0; // No internal node created in Rule 2 here. |
| 171 | } |
| 172 | suffix_length++; |
| 173 | break; |
| 174 | } |
| 175 | // Rule 2 - New internal node created |
| 176 | suffix_node *new_int_node; |
| 177 | end = new int; |
| 178 | *end = new_cur_node->begin + suffix_length - 1; |
| 179 | // New internal node |
| 180 | new_int_node = |
| 181 | cur_node->add_child(str_ind[beg], new_cur_node->begin, end, 0); |
| 182 | new_int_node->add_child(str_ind[i], i, leaf_end, 0); |
| 183 | new_cur_node->begin += suffix_length; |
| 184 | new_int_node->add_child(new_cur_node, str_ind[new_cur_node->begin]); |
| 185 | if (new_node != 0) new_node->suffix_link = new_int_node; |
| 186 | new_node = new_int_node; |
| 187 | } |
| 188 | suffix_count--; |
| 189 | if (cur_node->end == 0 && suffix_length > 0) { |
| 190 | suffix_length--; |
| 191 | beg = i - suffix_count + 1; |
| 192 | } else if (cur_node->end != 0) { |
| 193 | if (cur_node->suffix_link != 0) { |
| 194 | cur_node = cur_node->suffix_link; |
| 195 | } else { |
| 196 | cur_node = cur_node->parent->suffix_link; |
| 197 | } |
| 198 | } else if (cur_node->get_length() == 1) { |
| 199 | cur_node = root; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | return root; |
| 204 | } |
| 205 | |
| 206 | std::string get_reverse_complement(std::string rev_read) { |
| 207 | char t = 'N'; |
| 208 | for (unsigned int i = 0; i < rev_read.length(); i++) { |
| 209 | switch (rev_read[i]) { |
| 210 | case 'A': |
| 211 | t = 'T'; |
| 212 | break; |
| 213 | case 'G': |
| 214 | t = 'C'; |
| 215 | break; |
| 216 | case 'C': |
| 217 | t = 'G'; |
| 218 | break; |
| 219 | case 'T': |
| 220 | t = 'A'; |
| 221 | break; |
| 222 | } |
| 223 | rev_read[i] = t; |
| 224 | } |
| 225 | std::reverse(rev_read.begin(), rev_read.end()); |
| 226 | return rev_read; |
| 227 | } |
| 228 | |
| 229 | std::vector<std::string> read_adapters_from_fasta(std::string p) { |
| 230 | std::vector<std::string> adp; |
| 231 | std::ifstream fin(p.c_str()); |
| 232 | std::string line; |
| 233 | while (std::getline(fin, line)) { |
| 234 | if (line[0] == '>') continue; |
| 235 | adp.push_back(line); |
| 236 | } |
| 237 | return adp; |
| 238 | } |
| 239 | |
| 240 | std::string extend_till_mismatches(std::string cmn, std::string l, |
| 241 | std::string adp) { |
| 242 | if (cmn.length() < MIN_LENGTH) { |
| 243 | return cmn; |
| 244 | } |
| 245 | int pos = adp.find(cmn); |
| 246 | int r_pos = l.find(cmn); |
| 247 | if (pos == 0) // Beginning of adapter match |
| 248 | return cmn; |
| 249 | int k = 0; |
| 250 | pos--; |
| 251 | r_pos--; |
| 252 | while (r_pos >= 0 && pos >= 0 && k <= MAX_MISMATCHES) { |
| 253 | cmn = l[r_pos] + cmn; |
| 254 | if (l[r_pos] != adp[pos]) k++; |
| 255 | r_pos--; |
| 256 | pos--; |
| 257 | } |
| 258 | return cmn; |
| 259 | } |
| 260 | |
| 261 | int trim_adapter(std::string f1, std::string f2, std::string adp_path, |
| 262 | std::string p) { |
| 263 | std::string l1, l2, l1_rc, l2_rc, s, cmn, trmd_read; |
| 264 | int *t = new int[2], beg; |
| 265 | unsigned int pos; |
| 266 | std::ifstream ff1(f1.c_str()), ff2, pf(p.c_str()); |
| 267 | std::ofstream out((p + ".trimmed.fastq").c_str()); |
| 268 | std::vector<std::string>::iterator it; |
| 269 | suffix_node *root = (suffix_node *)malloc(sizeof(suffix_node)); |
| 270 | std::vector<std::string> adp = read_adapters_from_fasta(adp_path); |
| 271 | int i = -1, n = 0, adp_pos; |
| 272 | if (!f2.empty()) { |
| 273 | ff2.open(f2.c_str()); |
| 274 | while (std::getline(ff1, l1) && std::getline(ff2, l2)) { |
| 275 | i++; |
| 276 | if ((i - 1) % 4 != 0) continue; |
| 277 | } |
| 278 | } else { |
| 279 | while (std::getline(ff1, l1)) { |
| 280 | i++; |
| 281 | if ((i - 1) % 4 != 0) { |
| 282 | out << l1 << std::endl; |
| 283 | continue; |
| 284 | } |
| 285 | beg = 0; |
| 286 | pos = l1.length(); |
| 287 | trmd_read = l1; |
| 288 | for (it = adp.begin(); it != adp.end(); ++it) { |
| 289 | s = l1 + "#" + *it + "@"; |
| 290 | root = build_suffix_tree(s); |
| 291 | cmn = root->get_longest_common_substring(l1, *it); |
| 292 | cmn = extend_till_mismatches(cmn, l1, *it); |
| 293 | if (cmn.empty()) { |
| 294 | l1_rc = get_reverse_complement(l1); |
| 295 | s = l1_rc + "#" + *it + "@"; |
| 296 | root = build_suffix_tree(s); |
| 297 | cmn = root->get_longest_common_substring(l1_rc, *it); |
| 298 | cmn = extend_till_mismatches(cmn, l1_rc, *it); |
| 299 | pos = l1_rc.find(cmn); |
| 300 | beg = l1_rc.length() - pos; |
| 301 | adp_pos = (*it).find(cmn); |
| 302 | } else { |
| 303 | beg = 0; |
| 304 | pos = l1.find(cmn); |
| 305 | adp_pos = (*it).find(cmn); |
| 306 | } |
| 307 | if (cmn.length() >= MIN_LENGTH && adp_pos != 0) { |
| 308 | beg = 0; |
| 309 | pos = l1.length(); |
| 310 | t = align_seqs(l1, *it); |
| 311 | if (*t != -1) { |
| 312 | beg = 0; |
| 313 | pos = *(t + 1); |
| 314 | } else { |
| 315 | std::cout << "Reverse!" << std::endl; |
| 316 | l1_rc = get_reverse_complement(l1); |
| 317 | t = align_seqs(l1_rc, *it); |
| 318 | if (*t != -1) { |
| 319 | pos = l1.length() - *(t + 1); |
| 320 | beg = *(t + 1); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | std::cout << beg << " " << pos << std::endl; |
| 325 | if (beg != 0 || pos != l1.length()) { |
| 326 | trmd_read = l1.substr(beg, pos); |
| 327 | n++; |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | out << trmd_read << std::endl; |
| 332 | } |
| 333 | } |
| 334 | std::cout << "Number of Adapter Trimmed: :" << n << std::endl; |
| 335 | delete root; |
| 336 | delete[] t; |
| 337 | ff1.close(); |
| 338 | ff2.close(); |
| 339 | pf.close(); |
| 340 | return 0; |
| 341 | } |