Debian Codegraph @

src/allele_functions.cpp

raw symbols SCIP
1 #include "allele_functions.h"
2
3 #include <stdint.h>
4
5 #include <algorithm>
6 #include <iostream>
7 #include <string>
8 #include <vector>
9
10 void print_allele_depths(std::vector<allele> ad) {
11 std::cout << "AD Size: " << ad.size() << " " << std::endl;
12 for (std::vector<allele>::iterator it = ad.begin(); it != ad.end(); ++it) {
13 std::cout << "Nuc: " << it->nuc << std::endl;
14 ;
15 std::cout << "Depth: " << it->depth << std::endl;
16 std::cout << "Reverse: " << it->reverse << std::endl;
17 std::cout << "Qual: " << (uint16_t)it->mean_qual << std::endl;
18 std::cout << "Beg: " << it->beg << std::endl;
19 std::cout << "End: " << it->end << std::endl;
20 }
21 }
22
23 int check_allele_exists(std::string n, std::vector<allele> ad) {
24 for (std::vector<allele>::iterator it = ad.begin(); it != ad.end(); ++it) {
25 if (it->nuc.compare(n) == 0) {
26 return it - ad.begin();
27 }
28 }
29 // allele does not exist
30 return -1;
31 }
32
33 int find_ref_in_allele(std::vector<allele> ad, char ref) {
34 std::string ref_s(1, ref);
35 std::vector<allele>::iterator it = ad.begin();
36 while (it < ad.end()) {
37 if (it->nuc.compare(ref_s) == 0) return (it - ad.begin());
38 it++;
39 }
40 return -1;
41 }
42
43 // ad means the number of reads that support the reported alleles
44 std::vector<allele> update_allele_depth(char ref, std::string bases,
45 std::string qualities,
46 uint8_t min_qual) {
47 // ref always starts as 'N' ?
48 // bases seems to print all bases found at a position but is both lower and
49 // upper case qualities is phred quality min qual is blank
50 std::vector<allele> ad;
51 // print_allele_depths(ad);
52 std::string indel;
53 uint32_t i = 0, n = 0, j = 0, q_ind = 0;
54 bool beg, end;
55 uint8_t q;
56
57 // iterate the bases
58 while (i < bases.length()) {
59 beg = false;
60 end = false;
61
62 // not sure why either of these would be true?
63 if (bases[i] == '^') {
64 i += 2; // Skip mapping quality as well (i+1) - 33
65 continue;
66 }
67 if (bases[i] == '$') {
68 i++;
69 continue;
70 }
71 // ascii conversion 33
72 q = qualities[q_ind] - 33;
73 std::string b;
74 allele tmp;
75 bool forward = true;
76
77 // in case the bases aren't a number for some reason?
78 switch (bases[i]) {
79 case '.':
80 b = ref;
81 end = (bases[i + 1] == '$');
82 beg = (bases[i + 1] == '^');
83 break;
84 case ',':
85 b = ref;
86 forward = false;
87 end = (bases[i + 1] == '$');
88 beg = (bases[i + 1] == '^');
89 break;
90 case '*':
91 b = bases[i];
92 q = min_qual;
93 break;
94 // we have an insertion or deletion
95 case '+':
96 case '-':
97 j = i + 1;
98 // think j and i are coordinates for where insertion/deletion begins
99 // 'N' means deletion 'any other letter' is insertion in bases
100 // syntax -5nnnnn
101 while (isdigit(bases[j])) {
102 j++;
103 }
104
105 j = j - (i + 1);
106 // this slices like in python
107 // n = how many bases
108 // indel = the substring
109 n = stoi(bases.substr(i + 1, j));
110 indel = bases.substr(i + 1 + j, n);
111
112 // make it upper case
113 transform(indel.begin(), indel.end(), indel.begin(), ::toupper);
114
115 b = bases[i] + indel; // + for Insertion and - for Deletion
116 i += n + j;
117
118 // making sure it's an alphabetic char
119 if (indel[0] >= 97 && indel[0] <= 122) forward = false;
120 q = min_qual; // For insertions and deletion ust use minimum quality.
121 break;
122
123 // the default assumption is no insertion or deletion
124 default:
125 int asc_val = bases[i];
126 if (asc_val >= 65 && asc_val <= 90) {
127 b = bases[i];
128 } else if (bases[i] >= 97 && bases[i] <= 122) {
129 b = bases[i] - ('a' - 'A');
130 forward = false;
131 }
132 end = (bases[i + 1] == '$');
133 beg = (bases[i + 1] == '^');
134 }
135
136 // end base switch
137 int ind = check_allele_exists(b, ad);
138 // print_allele_depths(ad);
139 // meet our quality min
140 if (q >= min_qual) {
141 // allele does not exist making it an insertion or deletion (and the first
142 // one seen)
143 if (ind == -1) {
144 tmp.nuc = b;
145 tmp.depth = 1;
146 tmp.tmp_mean_qual = q;
147 if (!forward)
148 tmp.reverse = 1;
149 else
150 tmp.reverse = 0;
151 if (beg)
152 tmp.beg = 1;
153 else
154 tmp.beg = 0;
155 if (end)
156 tmp.end = 1;
157 else
158 tmp.end = 0;
159 ad.push_back(tmp);
160 } else {
161 // this executes when this allele has been seen at this position before
162 ad.at(ind).tmp_mean_qual =
163 (ad.at(ind).tmp_mean_qual * ad.at(ind).depth + q) /
164 (ad.at(ind).depth + 1);
165 ad.at(ind).depth += 1;
166 if (beg) ad.at(ind).beg += 1;
167 if (end) ad.at(ind).end += 1;
168 if (!forward) ad.at(ind).reverse += 1;
169 }
170 }
171 i++;
172 if (b[0] != '+' && b[0] != '-') q_ind++;
173 }
174 for (std::vector<allele>::iterator it = ad.begin(); it != ad.end(); ++it) {
175 it->mean_qual = (uint8_t)it->tmp_mean_qual;
176 }
177 if (ad.size() > 0) std::sort(ad.begin(), ad.end());
178 return ad;
179 }
180
181 int get_index(char a) {
182 switch (a) {
183 case 'Y':
184 a = 0;
185 break;
186 case 'R':
187 a = 1;
188 break;
189 case 'W':
190 a = 2;
191 break;
192 case 'S':
193 a = 3;
194 break;
195 case 'K':
196 a = 4;
197 break;
198 case 'M':
199 a = 5;
200 break;
201 case 'A':
202 a = 6;
203 break;
204 case 'T':
205 a = 7;
206 break;
207 case 'G':
208 a = 8;
209 break;
210 case 'C':
211 a = 9;
212 break;
213 case 'D':
214 a = 10;
215 break;
216 case 'V':
217 a = 11;
218 break;
219 case 'H':
220 a = 12;
221 break;
222 case 'B':
223 a = 13;
224 break;
225 default:
226 a = -1;
227 }
228 return (int)a;
229 }
230
231 // Modified gt2iupac from bcftools.h. Expanded iupac matrix to include all
232 // ambigious nucleotides(added Y, R, W, S, K, M, A, T, G, C, D, V, H, B) -
233 // https://github.com/samtools/bcftools/blob/b0376dff1ed70603c9490802f37883b9009215d2/bcftools.h#L48
234 /*
235 Expanded IUPAC matrix:
236
237 Y N H B B H H Y B Y N N H B
238 N R D V D V R D R V D V N N
239 H D W N D H W W D H D N H N
240 B V N S B V V B S S N V N B
241 B D D B K N D K K B D N N B
242 H V H V N M M H V M N V H N
243 H R W V D M A W R M D V H N
244 Y D W B K H W T K Y D N H B
245 B R D S K V R K G S D V N B
246 Y V H S B M M Y S C N V H B
247 N D D N D N D D D N D N N N
248 N V N V N V V N V V N V N N
249 H N H N N H H H N H N N H N
250 B N N B B N N B B B N N N B
251
252 */
253 char gt2iupac(char a, char b) {
254 if (a == '*' || b == '*') return 'N';
255 static const char iupac[14][14] = {
256 {'Y', 'N', 'H', 'B', 'B', 'H', 'H', 'Y', 'B', 'Y', 'N', 'N', 'H', 'B'},
257 {'N', 'R', 'D', 'V', 'D', 'V', 'R', 'D', 'R', 'V', 'D', 'V', 'N', 'N'},
258 {'H', 'D', 'W', 'N', 'D', 'H', 'W', 'W', 'D', 'H', 'D', 'N', 'H', 'N'},
259 {'B', 'V', 'N', 'S', 'B', 'V', 'V', 'B', 'S', 'S', 'N', 'V', 'N', 'B'},
260 {'B', 'D', 'D', 'B', 'K', 'N', 'D', 'K', 'K', 'B', 'D', 'N', 'N', 'B'},
261 {'H', 'V', 'H', 'V', 'N', 'M', 'M', 'H', 'V', 'M', 'N', 'V', 'H', 'N'},
262 {'H', 'R', 'W', 'V', 'D', 'M', 'A', 'W', 'R', 'M', 'D', 'V', 'H', 'N'},
263 {'Y', 'D', 'W', 'B', 'K', 'H', 'W', 'T', 'K', 'Y', 'D', 'N', 'H', 'B'},
264 {'B', 'R', 'D', 'S', 'K', 'V', 'R', 'K', 'G', 'S', 'D', 'V', 'N', 'B'},
265 {'Y', 'V', 'H', 'S', 'B', 'M', 'M', 'Y', 'S', 'C', 'N', 'V', 'H', 'B'},
266 {'N', 'D', 'D', 'N', 'D', 'N', 'D', 'D', 'D', 'N', 'D', 'N', 'N', 'N'},
267 {'N', 'V', 'N', 'V', 'N', 'V', 'V', 'N', 'V', 'V', 'N', 'V', 'N', 'N'},
268 {'H', 'N', 'H', 'N', 'N', 'H', 'H', 'H', 'N', 'H', 'N', 'N', 'H', 'N'},
269 {'B', 'N', 'N', 'B', 'B', 'N', 'N', 'B', 'B', 'B', 'N', 'N', 'N', 'B'}};
270 if (a >= 'a') a -= 'a' - 'A';
271 if (b >= 'a') b -= 'a' - 'A';
272 int _a, _b;
273 _a = get_index(a);
274 _b = get_index(b);
275 if (_a == -1 || _b == -1) return 'N';
276 return iupac[_a][_b];
277 }
278
279 /*
280 ATGC: 6,7,8,9 indices set up with get_index()
281 Stop codon: *
282 Unknown codon: X
283 [[['AAA', 'AAT', 'AAG', 'AAC'],
284 ['ATA', 'ATT', 'ATG', 'ATC'],
285 ['AGA', 'AGT', 'AGG', 'AGC'],
286 ['ACA', 'ACT', 'ACG', 'ACC']],
287 [['TAA', 'TAT', 'TAG', 'TAC'],
288 ['TTA', 'TTT', 'TTG', 'TTC'],
289 ['TGA', 'TGT', 'TGG', 'TGC'],
290 ['TCA', 'TCT', 'TCG', 'TCC']],
291 [['GAA', 'GAT', 'GAG', 'GAC'],
292 ['GTA', 'GTT', 'GTG', 'GTC'],
293 ['GGA', 'GGT', 'GGG', 'GGC'],
294 ['GCA', 'GCT', 'GCG', 'GCC']],
295 [['CAA', 'CAT', 'CAG', 'CAC'],
296 ['CTA', 'CTT', 'CTG', 'CTC'],
297 ['CGA', 'CGT', 'CGG', 'CGC'],
298 ['CCA', 'CCT', 'CCG', 'CCC']]]
299 */
300
301 char codon2aa(char n1, char n2, char n3) {
302 if (n1 >= 'a') n1 -= 'a' - 'A';
303 if (n2 >= 'a') n2 -= 'a' - 'A';
304 if (n3 >= 'a') n3 -= 'a' - 'A';
305 int _n1 = get_index(n1), _n2 = get_index(n2), _n3 = get_index(n3);
306 static const char iupac_aa[4][4][4] = {
307 {
308 {'K', 'N', 'K', 'N'}, // 'AAA', 'AAT', 'AAG', 'AAC'
309 {'I', 'I', 'M', 'I'}, // 'ATA', 'ATT', 'ATG', 'ATC'
310 {'R', 'S', 'R', 'S'}, // 'AGA', 'AGT', 'AGG', 'AGC'
311 {'T', 'T', 'T', 'T'} // 'ACA', 'ACT', 'ACG', 'ACC'
312 },
313 {
314 {'*', 'Y', '*', 'Y'}, // 'TAA', 'TAT', 'TAG', 'TAC'
315 {'L', 'F', 'L', 'F'}, // 'TTA', 'TTT', 'TTG', 'TTC'
316 {'*', 'C', 'W', 'C'}, // 'TGA', 'TGT', 'TGG', 'TGC'
317 {'S', 'S', 'S', 'S'} // 'TCA', 'TCT', 'TCG', 'TCC'
318 },
319 {
320 {'E', 'D', 'E', 'D'}, // 'GAA', 'GAT', 'GAG', 'GAC'
321 {'V', 'V', 'V', 'V'}, // 'GTA', 'GTT', 'GTG', 'GTC'
322 {'G', 'G', 'G', 'G'}, // 'GGA', 'GGT', 'GGG', 'GGC'
323 {'A', 'A', 'A', 'A'} // 'GCA', 'GCT', 'GCG', 'GCC'
324 },
325 {
326 {'Q', 'H', 'Q', 'H'}, // 'CAA', 'CAT', 'CAG', 'CAC'
327 {'L', 'L', 'L', 'L'}, // 'CTA', 'CTT', 'CTG', 'CTC'
328 {'R', 'R', 'R', 'R'}, // 'CGA', 'CGT', 'CGG', 'CGC'
329 {'P', 'P', 'P', 'P'} // 'CCA', 'CCT', 'CCG', 'CCC'
330 }};
331 if ((_n1 < 6) || (_n1 > 9) || (_n2 < 6) || (_n2 > 9) || (_n3 < 6) ||
332 (_n3 > 9))
333 return 'X';
334 return iupac_aa[_n1 - 6][_n2 - 6][_n3 - 6];
335 }