| 1 | #include "call_consensus_pileup.h" |
| 2 | |
| 3 | void format_alleles(std::vector<allele> &ad) { |
| 4 | std::vector<allele>::iterator it = ad.begin(); |
| 5 | while (it < ad.end()) { |
| 6 | if (it->nuc[0] == '-') { // Remove deletions |
| 7 | it = ad.erase(it); |
| 8 | } else { |
| 9 | ++it; |
| 10 | } |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | bool compare_allele_depth(const allele &a, const allele &b) { |
| 15 | return b.depth < a.depth; |
| 16 | } |
| 17 | |
| 18 | ret_t get_consensus_allele(std::vector<allele> ad, uint8_t min_qual, |
| 19 | double threshold, char gap, |
| 20 | double min_insert_threshold) { |
| 21 | // print_allele_depths(ad); |
| 22 | ret_t t; |
| 23 | t.nuc = gap; |
| 24 | t.q = min_qual + 33; |
| 25 | if (ad.size() == 0) return t; |
| 26 | format_alleles(ad); |
| 27 | // if(ad.size() == 1){ |
| 28 | // t.nuc = (ad.at(0).nuc.compare("*") == 0) ? "" : ad.at(0).nuc; |
| 29 | // t.q = (ad.at(0).nuc.compare("*") == 0) ? min_qual + 33 : |
| 30 | // ad.at(0).mean_qual + 33; return t; |
| 31 | // } |
| 32 | std::string cnuc = ""; |
| 33 | std::vector<allele> nuc_pos; |
| 34 | allele tmp_a; |
| 35 | char n; |
| 36 | uint32_t max_l = 0, max_depth = 0, tmp_depth = 0, |
| 37 | total_max_depth = 0, gap_depth = 0, total_indel_depth = 0; |
| 38 | // uint32_t cur_depth = 0; |
| 39 | uint8_t ambg_n = 1, ctr = 0; |
| 40 | double q = 0, tq = 0, cur_threshold = 0; |
| 41 | std::vector<allele>::iterator it; |
| 42 | for (std::vector<allele>::iterator it = ad.begin(); it != ad.end(); ++it) { |
| 43 | if (it->nuc.length() > max_l) { |
| 44 | max_l = it->nuc.length(); |
| 45 | } |
| 46 | if (it->nuc.length() == 1) { |
| 47 | total_max_depth += it->depth; |
| 48 | total_indel_depth += it->depth; |
| 49 | total_indel_depth = total_indel_depth - it->end; // For indels |
| 50 | } |
| 51 | } |
| 52 | t.nuc = ""; |
| 53 | t.q = ""; |
| 54 | for (uint16_t i = 0; i < max_l; ++i) { |
| 55 | n = '*'; |
| 56 | q = 0; |
| 57 | tq = 0; |
| 58 | max_depth = 0; |
| 59 | tmp_depth = 0; |
| 60 | // cur_depth = 0; |
| 61 | // prev_depth = 0; |
| 62 | ctr = 1; |
| 63 | it = ad.begin(); |
| 64 | gap_depth = 0; |
| 65 | nuc_pos.clear(); |
| 66 | while (it != ad.end()) { |
| 67 | if (!(i < it->nuc.length())) { |
| 68 | it++; |
| 69 | continue; |
| 70 | } |
| 71 | if (it->nuc[i] == '+' || it->nuc[i] == '-') { |
| 72 | it++; |
| 73 | continue; |
| 74 | } |
| 75 | tmp_depth = it->depth; |
| 76 | tq = it->mean_qual; |
| 77 | ctr = 1; |
| 78 | while (it != ad.end() - 1 && i < (it + 1)->nuc.length() && |
| 79 | it->nuc[i] == |
| 80 | (it + 1)->nuc[i]) { // Third condition for cases with more |
| 81 | // than 1 base in insertion && (i+1) < |
| 82 | // (it+1)->nuc.length() |
| 83 | tmp_depth += (it + 1)->depth; |
| 84 | tq = ((tq * (ctr)) + (it + 1)->mean_qual) / (ctr + 1); |
| 85 | it++; |
| 86 | ctr++; |
| 87 | } |
| 88 | // cur_depth += tmp_depth; |
| 89 | tmp_a.nuc = it->nuc[i]; |
| 90 | tmp_a.mean_qual = tq; |
| 91 | tmp_a.reverse = 0; // Reverse reads not important for consensus |
| 92 | tmp_a.depth = tmp_depth; |
| 93 | nuc_pos.push_back(tmp_a); |
| 94 | // if(tmp_depth > max_depth){ |
| 95 | // n = it->nuc[i]; |
| 96 | // max_depth = tmp_depth; |
| 97 | // q = tq; |
| 98 | // ambg_n = 1; |
| 99 | // } else if(tmp_depth == max_depth){ |
| 100 | // n = gt2iupac(n, it->nuc[i]); |
| 101 | // q = ((q * ambg_n) + tq)/(ambg_n+1); |
| 102 | // ambg_n += 1; |
| 103 | // } |
| 104 | it++; |
| 105 | } |
| 106 | |
| 107 | // Sort nuc_pos by depth of alleles |
| 108 | std::sort(nuc_pos.begin(), nuc_pos.end(), compare_allele_depth); |
| 109 | it = nuc_pos.begin(); |
| 110 | if (nuc_pos.size() == 0) { // For char at first position with only |
| 111 | // insertions or deletions (+/-) |
| 112 | n = 'N'; |
| 113 | } else { |
| 114 | n = it->nuc[0]; |
| 115 | q = it->mean_qual; |
| 116 | max_depth = it->depth; |
| 117 | it++; |
| 118 | ambg_n = 1; |
| 119 | if (i > 0) { |
| 120 | cur_threshold = threshold * (double)total_indel_depth; |
| 121 | } else { |
| 122 | cur_threshold = threshold * (double)total_max_depth; |
| 123 | } |
| 124 | while ( |
| 125 | it != nuc_pos.end() && |
| 126 | (max_depth < cur_threshold || |
| 127 | it->depth == |
| 128 | (it - 1)->depth)) { // Iterate till end or till cross threshold |
| 129 | // and no more allele with same depth |
| 130 | n = gt2iupac(n, it->nuc[0]); |
| 131 | q = ((q * ambg_n) + it->mean_qual) / (ambg_n + 1); |
| 132 | ambg_n += 1; |
| 133 | max_depth += it->depth; |
| 134 | it++; |
| 135 | } |
| 136 | if (max_depth < cur_threshold) // If depth still less than threshold |
| 137 | n = 'N'; |
| 138 | } |
| 139 | |
| 140 | if (i > 0) { |
| 141 | // print_allele_depths(ad); |
| 142 | // std::cout << "\n"; |
| 143 | // gap_depth = (total_indel_depth > cur_depth) ? total_indel_depth - |
| 144 | // cur_depth : 0; |
| 145 | gap_depth = min_insert_threshold * total_max_depth; |
| 146 | // std::cout << max_depth << " " << gap_depth << " " << |
| 147 | // min_insert_threshold << " " << total_max_depth<<" \n"; |
| 148 | } else |
| 149 | gap_depth = 0; // For first position of allele |
| 150 | if (n != '*' && |
| 151 | max_depth >= gap_depth) { // TODO: Check what to do when equal.{ |
| 152 | t.nuc += n; |
| 153 | q += 0.5; // For rounding before converting to int |
| 154 | t.q += (((uint8_t)q) + 33); |
| 155 | } |
| 156 | } |
| 157 | return t; |
| 158 | } |
| 159 | |
| 160 | int call_consensus_from_plup(std::istream &cin, std::string seq_id, |
| 161 | std::string out_file, uint8_t min_qual, |
| 162 | double threshold, uint32_t min_depth, char gap, |
| 163 | bool min_coverage_flag, |
| 164 | double min_insert_threshold) { |
| 165 | std::string line, cell; |
| 166 | std::ofstream fout((out_file + ".fa").c_str()); |
| 167 | std::ofstream tmp_qout((out_file + ".qual.txt").c_str()); |
| 168 | char *o = new char[out_file.length() + 1]; |
| 169 | strcpy(o, out_file.c_str()); |
| 170 | if (seq_id.empty()) { |
| 171 | fout << ">Consensus_" << basename(o) << "_threshold_" << threshold |
| 172 | << "_quality_" << (uint16_t)min_qual << std::endl; |
| 173 | } else { |
| 174 | fout << ">" << seq_id << std::endl; |
| 175 | } |
| 176 | delete[] o; |
| 177 | int ctr = 0; |
| 178 | uint32_t prev_pos = 0, pos = 0, mdepth = 0; |
| 179 | std::stringstream lineStream; |
| 180 | char ref; |
| 181 | std::string bases; |
| 182 | std::string qualities; |
| 183 | std::vector<allele> ad; |
| 184 | uint32_t bases_zero_depth = 0, bases_min_depth = 0, total_bases = 0; |
| 185 | while (std::getline(cin, line)) { |
| 186 | lineStream << line; |
| 187 | ctr = 0; |
| 188 | ref = 'N'; |
| 189 | while (std::getline(lineStream, cell, '\t')) { |
| 190 | switch (ctr) { |
| 191 | case 0: |
| 192 | break; |
| 193 | case 1: |
| 194 | pos = stoi(cell); |
| 195 | break; |
| 196 | case 2: |
| 197 | ref = cell[0]; |
| 198 | break; |
| 199 | case 3: |
| 200 | mdepth = stoi(cell); |
| 201 | break; |
| 202 | case 4: |
| 203 | bases = cell; |
| 204 | break; |
| 205 | case 5: |
| 206 | qualities = cell; |
| 207 | break; |
| 208 | case 6: |
| 209 | break; |
| 210 | } |
| 211 | ctr++; |
| 212 | } |
| 213 | total_bases++; |
| 214 | if (prev_pos == 0) // No -/N before alignment starts |
| 215 | prev_pos = pos; |
| 216 | if ((pos > prev_pos && min_coverage_flag)) { |
| 217 | fout << std::string((pos - prev_pos) - 1, gap); |
| 218 | tmp_qout << std::string((pos - prev_pos) - 1, |
| 219 | '!'); // ! represents 0 quality score. |
| 220 | } |
| 221 | ret_t t; |
| 222 | if (mdepth >= min_depth) { |
| 223 | ad = update_allele_depth(ref, bases, qualities, min_qual); |
| 224 | t = get_consensus_allele(ad, min_qual, threshold, gap, |
| 225 | min_insert_threshold); |
| 226 | fout << t.nuc; |
| 227 | tmp_qout << t.q; |
| 228 | } else { |
| 229 | bases_min_depth += 1; |
| 230 | if (mdepth == 0) bases_zero_depth += 1; |
| 231 | if (min_coverage_flag) { |
| 232 | fout << gap; |
| 233 | tmp_qout << '!'; |
| 234 | } |
| 235 | } |
| 236 | lineStream.clear(); |
| 237 | ad.clear(); |
| 238 | prev_pos = pos; |
| 239 | } |
| 240 | fout << "\n"; // Add new line character after end of sequence |
| 241 | tmp_qout << "\n"; |
| 242 | tmp_qout.close(); |
| 243 | fout.close(); |
| 244 | std::cout << "Reference length: " << total_bases << std::endl; |
| 245 | std::cout << "Positions with 0 depth: " << bases_zero_depth << std::endl; |
| 246 | std::cout << "Positions with depth below " << (unsigned)min_depth << ": " |
| 247 | << bases_min_depth << std::endl; |
| 248 | return 0; |
| 249 | } |