Debian Codegraph @

src/call_variants.cpp

raw symbols SCIP
1 #include "call_variants.h"
2
3 const float sig_level = 0.01;
4
5 std::vector<allele>::iterator get_ref_allele(std::vector<allele> &ad,
6 char ref) {
7 for (std::vector<allele>::iterator it = ad.begin(); it != ad.end(); ++it) {
8 if (it->nuc[0] == ref) return it;
9 }
10 // print_allele_depths(ad);
11 return ad.end();
12 }
13
14 double *get_frequency_depth(
15 allele a, uint32_t pos_depth,
16 uint32_t total_depth) { // pos_depth is ungapped depth after passing
17 // quality. total_depth is depth without quality
18 // filtering for indels.
19 double *val = new double[2];
20 if (a.nuc[0] == '+' ||
21 a.nuc[0] ==
22 '-') { // For insertions and deletions use depth discarding quality
23 val[0] = a.depth / (double)total_depth;
24 val[1] = total_depth;
25 return val;
26 }
27 val[0] = a.depth / (double)pos_depth;
28 val[1] = pos_depth;
29 return val;
30 }
31
32 int call_variants_from_plup(std::istream &cin, std::string out_file,
33 uint8_t min_qual, double min_threshold,
34 uint32_t min_depth, std::string ref_path,
35 std::string gff_path, bool gapped_depth) {
36 std::string line, cell, bases, qualities, region;
37 ref_antd refantd(ref_path, gff_path);
38 std::ostringstream out_str;
39 std::ofstream fout((out_file + ".tsv").c_str());
40 // make columns in the file
41 fout << "REGION"
42 "\tPOS"
43 "\tREF"
44 "\tALT"
45 "\tREF_DP"
46 "\tREF_RV"
47 "\tREF_QUAL"
48 "\tALT_DP"
49 "\tALT_RV"
50 "\tALT_QUAL"
51 "\tALT_FREQ"
52 "\tTOTAL_DP"
53 "\tPVAL"
54 "\tPASS"
55 "\tGFF_FEATURE"
56 "\tREF_CODON"
57 "\tREF_AA"
58 "\tALT_CODON"
59 "\tALT_AA"
60 "\tPOS_AA"
61 << std::endl;
62 int ctr = 0;
63 int64_t pos = 0;
64 uint32_t mdepth = 0, pdepth = 0; // mpdepth for mpileup depth and pdeth for
65 // ungapped depth at position
66 double pval_left, pval_right, pval_twotailed, *freq_depth, err;
67 std::stringstream line_stream;
68 char ref;
69 std::vector<allele> ad;
70 std::vector<allele>::iterator ref_it;
71 while (std::getline(cin, line)) {
72 line_stream << line;
73 ctr = 0;
74 ref = 'N';
75 while (std::getline(line_stream, cell, '\t')) {
76 switch (ctr) {
77 case 0:
78 region = cell;
79 break;
80 case 1:
81 pos = stoi(cell);
82 break;
83 case 2:
84 ref = refantd.get_base(pos, region);
85 ref = (ref == 0)
86 ? cell[0]
87 : ref; // If ref does not exist then use from mpileup
88 break;
89 case 3:
90 mdepth = stoi(cell);
91 break;
92 case 4:
93 bases = cell;
94 break;
95 case 5:
96 qualities = cell;
97 break;
98 case 6:
99 break;
100 }
101 ctr++;
102 }
103 ad = update_allele_depth(ref, bases, qualities, min_qual);
104 if (ad.size() == 0) {
105 line_stream.clear();
106 continue;
107 }
108 // Get ungapped depth
109 pdepth = 0;
110 for (std::vector<allele>::iterator it = ad.begin(); it != ad.end(); ++it) {
111 // if ((gapped_depth && (it->nuc[0] == '+' || it->nuc[0] == '-')) || (!gapped_depth && (it->nuc[0] == '+' || it->nuc[0] == '*' || it->nuc[0] == '-'))) continue;
112 // Split into two if statements for readability.
113 if (gapped_depth) {
114 if (it->nuc[0] == '+' || it->nuc[0] == '-') continue; // Count gaps ('*') towards depth
115 } else {
116 if (it->nuc[0] == '+' || it->nuc[0] == '*' || it->nuc[0] == '-') continue;
117 }
118 pdepth += it->depth;
119 }
120 if (pdepth < min_depth) { // Check for minimum depth
121 line_stream.clear();
122 continue;
123 }
124 ref_it = get_ref_allele(ad, ref);
125 if (ref_it == ad.end()) { // If ref not present in reads.
126 allele a;
127 a.nuc = ref;
128 a.depth = 0;
129 a.reverse = 0;
130 a.mean_qual = 0;
131 ad.push_back(a);
132 ref_it = ad.end() - 1;
133 }
134 for (std::vector<allele>::iterator it = ad.begin(); it != ad.end(); ++it) {
135 if ((*it == *ref_it) || it->nuc[0] == '*') continue;
136 freq_depth = get_frequency_depth(*it, pdepth, mdepth);
137 if (freq_depth[0] < min_threshold) continue;
138 // this only adds the first bit of the information to the tsv
139 out_str << region << "\t";
140 out_str << pos << "\t";
141 out_str << ref << "\t";
142 out_str << it->nuc << "\t";
143 out_str << ref_it->depth << "\t";
144 out_str << ref_it->reverse << "\t";
145 out_str << (uint16_t)ref_it->mean_qual << "\t";
146 out_str << it->depth << "\t";
147 out_str << it->reverse << "\t";
148 out_str << (uint16_t)it->mean_qual << "\t";
149 out_str << freq_depth[0] << "\t";
150 out_str << freq_depth[1] << "\t";
151 /*
152 | Var | Ref |
153 Exp | Error | Err free |
154 Obs | AD | RD |
155 */
156 // adding pvalue related info
157 err = pow(10, (-1 * (it->mean_qual) / 10));
158 kt_fisher_exact((err * mdepth), (1 - err) * mdepth, it->depth,
159 ref_it->depth, &pval_left, &pval_right, &pval_twotailed);
160 out_str << pval_left << "\t";
161 if (pval_left <= sig_level) {
162 out_str << "TRUE"
163 << "\t";
164 } else {
165 out_str << "FALSE"
166 << "\t";
167 }
168 // adding codon related info, can add gene info here too
169 if (it->nuc[0] != '+' && it->nuc[0] != '-') {
170 refantd.codon_aa_stream(region, out_str, fout, pos, it->nuc[0]);
171 } else {
172 fout << out_str.str() << "NA\tNA\tNA\tNA\tNA\tNA" << std::endl;
173 }
174 out_str.str("");
175 out_str.clear();
176 delete[] freq_depth;
177 }
178 line_stream.clear();
179 }
180 fout.close();
181 return 0;
182 }