iVar: src/call_variants.cpp Source File

iVar

call_variants.cpp
1 #include<iostream>
2 #include<fstream>
3 #include<sstream>
4 #include<vector>
5 #include<algorithm>
6 #include<string>
7 #include<regex>
8 #include<cmath>
9 #include<htslib/kfunc.h>
10 #include "htslib/kfunc.h"
11
12 #include "allele_functions.h"
13
14 const char gap='N';
15 const float sig_level = 0.01;
16
17 std::vector<allele>::iterator get_ref_allele(std::vector<allele> &ad, char ref){
18 for(std::vector<allele>::iterator it = ad.begin(); it != ad.end(); ++it) {
19 if(it->nuc[0] == ref)
20 return it;
21 }
22 // print_allele_depths(ad);
23 return ad.end();
24 }
25
26 int call_variants_from_plup(std::istream &cin, std::string out_file, uint8_t min_qual, double min_threshold){
27 std::string line, cell, bases, qualities, region;
28 std::ofstream fout(out_file+".tsv");
29 fout << "REGION\tPOS\tREF\tALT\tAD\tRAD\tDP\tFREQ\tQUAL\tPVAL\tPASS"<<std::endl;
30 int ctr = 0, pos = 0, mdepth = 0;
31 double pval_left, pval_right, pval_twotailed, freq, err;
32 std::stringstream lineStream;
33 char ref;
34 std::vector<allele> ad;
35 std::vector<allele>::iterator ref_it;
36 while (std::getline(cin, line)){
37 lineStream << line;
38 ctr = 0;
39 while(std::getline(lineStream,cell,'\t')){
40 switch(ctr){
41 case 0:
42 region = cell;
43 break;
44 case 1:
45 pos = stoi(cell);
46 break;
47 case 2:
48 ref = cell[0];
49 break;
50 case 3:
51 mdepth = stoi(cell);
52 break;
53 case 4:
54 bases = cell;
55 break;
56 case 5:
57 qualities = cell;
58 break;
59 case 6:
60 break;
61 }
62 ctr++;
63 }
64 ad = update_allele_depth(ref, bases, qualities, min_qual);
65 if(ad.size() == 0){
66 lineStream.clear();
67 continue;
68 }
69 ref_it = get_ref_allele(ad, ref);
70 // Get ungapped coverage
71 mdepth = 0;
72 for(std::vector<allele>::iterator it = ad.begin(); it != ad.end(); ++it) {
73 if(it->nuc[0]=='*' || it->nuc[0] == '+' || it->nuc[0] == '-')
74 continue;
75 mdepth += it->depth;
76 }
77 for(std::vector<allele>::iterator it = ad.begin(); it != ad.end(); ++it) {
78 if((*it == *ref_it) || it->nuc[0]=='*')
79 continue;
80 freq = it->depth/(double)mdepth;
81 if(freq < min_threshold)
82 continue;
83 fout << region << "\t";
84 fout << pos << "\t";
85 fout << ref << "\t";
86 fout << it->nuc << "\t";
87 fout << it->depth << "\t";
88 fout << it->reverse << "\t";
89 fout << mdepth << "\t";
90 fout << freq << "\t";
91 fout << (uint16_t)it->mean_qual << "\t";
92 /*
93 | Var | Ref |
94 Exp | Error | Err free |
95 Obs | AD | RD |
96 */
97 err = pow(10, ( -1 * (it->mean_qual)/10));
98 kt_fisher_exact((err * mdepth), (1-err) * mdepth, it->depth, ref_it->depth, &pval_left, &pval_right, &pval_twotailed);
99 fout << pval_left << "\t";
100 if(pval_left <= sig_level){
101 fout << "TRUE";
102 } else {
103 fout << "FALSE";
104 }
105 fout << std::endl;
106 }
107 lineStream.clear();
108 }
109 fout.close();
110 return 0;
111 }
112
113 // int main_old(int argc, char* argv[]) {
114 // std::string line, cell;
115 // std::string out_file = argv[1];
116 // uint8_t min_qual = 20;
117 // if(argc > 2)
118 // min_qual = atoi(argv[2]);
119 // std::cout << "Min Qual: " << (uint16_t)min_qual << std::endl;
120 // std::ofstream fout(out_file+".tsv");
121 // fout << "POS\tREF\tALT\tAD\tRAD\tDP\tQUAL"<<std::endl;
122 // int ctr = 0, pos = 0, mdepth = 0;
123 // std::stringstream lineStream;
124 // char ref;
125 // std::string bases;
126 // std::string qualities;
127 // std::vector<allele> ad;
128 // while (std::getline(std::cin, line)){
129 // lineStream << line;
130 // ctr = 0;
131 // while(std::getline(lineStream,cell,'\t')){
132 // switch(ctr){
133 // case 0:
134 // break;
135 // case 1:
136 // pos = stoi(cell);
137 // break;
138 // case 2:
139 // ref = cell[0];
140 // break;
141 // case 3:
142 // mdepth = stoi(cell);
143 // break;
144 // case 4:
145 // bases = cell;
146 // break;
147 // case 5:
148 // qualities = cell;
149 // break;
150 // case 6:
151 // break;
152 // }
153 // ctr++;
154 // }
155 // ad = update_allele_depth(ref, bases, qualities, min_qual);
156 // for(std::vector<allele>::iterator it = ad.begin(); it != ad.end(); ++it) {
157 // if((it->nuc[0] == ref && it->nuc.length() == 1) || it->nuc[0]=='*')
158 // continue;
159 // fout << pos << "\t";
160 // fout << ref << "\t";
161 // fout << it->nuc << "\t";
162 // fout << it->depth << "\t";
163 // fout << it->reverse << "\t";
164 // fout << mdepth << "\t";
165 // fout << (uint16_t)it->mean_qual << std::endl;
166 // }
167 // lineStream.clear();
168 // }
169 // fout.close();
170 // return 0;
171 // }