Debian Codegraph @

src/get_common_variants.cpp

raw symbols SCIP
1 #include "get_common_variants.h"
2
3 const int NUM_FIELDS = 19;
4 const std::string fields[NUM_FIELDS] = {
5 "REGION", "POS", "REF", "ALT", "REF_DP",
6 "REF_RV", "REF_QUAL", "ALT_DP", "ALT_RV", "ALT_QUAL",
7 "ALT_FREQ", "TOTAL_DP", "PVAL", "PASS", "GFF_FEATURE",
8 "REF_CODON", "REF_AA", "ALT_CODON", "ALT_AA"};
9
10 const std::string na_tab_delimited_str =
11 "NA\tNA\tNA\tNA\tNA\tNA\tNA\tNA\tNA\tNA";
12
13 int read_variant_file(
14 std::ifstream &fin, unsigned int file_number,
15 std::map<std::string, unsigned int> &counts,
16 std::map<std::string, std::string> &file_tab_delimited_str) {
17 unsigned int ctr;
18 std::string line, cell, tab_delimited_key, tab_delimited_val;
19 std::stringstream line_stream;
20 std::string region, ref, alt;
21 // Make sure format of header matches
22 std::getline(fin, line);
23 line_stream << line;
24 ctr = 0;
25 while (std::getline(line_stream, cell, '\t')) {
26 if (cell.compare(fields[ctr]) != 0) {
27 return -1;
28 }
29 ctr++;
30 }
31 line_stream.clear();
32 while (std::getline(fin, line)) {
33 line_stream << line;
34 ctr = 0;
35 tab_delimited_key = "";
36 tab_delimited_val = "";
37 while (std::getline(line_stream, cell, '\t')) {
38 switch (ctr) {
39 case 0: // REGION
40 case 1: // POS
41 case 2: // REF
42 case 3: // ALT
43 case 14: // GFF_FEATURE
44 case 15: // REF_CODON
45 case 16: // REF_AA
46 case 17: // ALT_CODON
47 case 18: // ALT_AA
48 tab_delimited_key += cell + "\t";
49 break;
50 case 4:
51 case 5:
52 case 6:
53 case 7:
54 case 8:
55 case 9:
56 case 10:
57 case 11:
58 case 12:
59 case 13:
60 default:
61 tab_delimited_val += cell;
62 if (ctr < 13) {
63 tab_delimited_val += "\t";
64 }
65 break;
66 }
67 ctr++;
68 }
69 if (counts.find(tab_delimited_key) == counts.end()) {
70 counts[tab_delimited_key] = 1;
71 } else {
72 counts[tab_delimited_key] += 1;
73 }
74 file_tab_delimited_str[tab_delimited_key + std::to_string(file_number)] =
75 tab_delimited_val;
76 line_stream.clear();
77 }
78 return 0;
79 }
80
81 int common_variants(std::string out, double min_threshold, char *files[],
82 unsigned int nfiles) {
83 out += ".tsv";
84 std::ofstream fout(out.c_str());
85 unsigned int i, j;
86 std::ifstream fin;
87 std::map<std::string, unsigned int> counts =
88 std::map<std::string, unsigned int>();
89 std::map<std::string, std::string> file_tab_delimited_str =
90 std::map<std::string, std::string>();
91 for (i = 0; i < nfiles; ++i) {
92 fin.open(files[i]);
93 if (read_variant_file(fin, i, counts, file_tab_delimited_str) != 0) {
94 std::cout << "Header format of " << files[i] << " did not match!";
95 std::cout
96 << " Please use files generated using \"ivar variants\" command."
97 << std::endl;
98 }
99 fin.close();
100 }
101 std::map<std::string, unsigned int>::iterator it = counts.begin();
102 // Write header
103 for (i = 0; i < 4; ++i) {
104 fout << fields[i] << "\t";
105 }
106 for (i = 14; i < 19; ++i) {
107 fout << fields[i] << "\t";
108 }
109 for (i = 0; i < nfiles; ++i) {
110 for (j = 4; j < 14; ++j) {
111 fout << fields[j] << "_" << files[i] << "\t";
112 }
113 }
114 fout << "\n";
115 // Write rows
116 while (it != counts.end()) {
117 if (((float)it->second) / (float)nfiles >=
118 min_threshold) { // Check if variant occurs in more 'min_threshold'
119 // fraction of files
120 fout << it->first;
121 for (j = 0; j < nfiles; ++j) {
122 if (file_tab_delimited_str.find(it->first + std::to_string(j)) ==
123 file_tab_delimited_str.end()) {
124 fout << na_tab_delimited_str;
125 } else {
126 fout << file_tab_delimited_str[it->first + std::to_string(j)];
127 }
128 if (j < nfiles - 1) fout << "\t";
129 }
130 fout << "\n";
131 }
132 it++;
133 }
134 return 0;
135 }