Debian Codegraph @

src/primer_bed.cpp

raw symbols SCIP
1 #include "primer_bed.h"
2
3 std::string primer::get_name() { return name; }
4
5 std::string primer::get_region() { return region; }
6
7 int primer::get_score() { return score; }
8
9 uint32_t primer::get_start() const { return start; }
10
11 uint32_t primer::get_end() const { return end; }
12
13 char primer::get_strand() { return strand; }
14
15 int primer::get_length() { return end - start + 1; }
16
17 int16_t primer::get_pair_indice() { return pair_indice; }
18
19 int16_t primer::get_indice() const { return indice; }
20
21 uint32_t primer::get_read_count() const { return read_count; }
22
23 void primer::set_start(uint32_t s) { start = s; }
24
25 void primer::set_end(uint32_t e) { end = e; }
26
27 void primer::set_strand(char s) { strand = s; }
28
29 void primer::set_region(std::string r) { region = r; }
30
31 void primer::set_name(std::string n) { name = n; }
32
33 void primer::set_score(int s) { score = s; }
34
35 void primer::set_pair_indice(int16_t i) { pair_indice = i; }
36
37 void primer::set_indice(int16_t i) { indice = i; }
38
39 void primer::set_read_count(uint32_t rc) { read_count = rc; }
40
41 void primer::add_read_count(uint32_t rc) { read_count += rc; }
42
43 void print_bed_format() {
44 std::cerr << "iVar uses the standard 6 column BED format as defined here - "
45 "https://genome.ucsc.edu/FAQ/FAQformat.html#format1."
46 << std::endl;
47 std::cerr << "It requires the following columns delimited by a tab: chrom, "
48 "chromStart, chromEnd, name, score, strand"
49 << std::endl;
50 }
51
52 std::vector<primer> populate_from_file(std::string path, int32_t offset = 0) {
53 std::ifstream data(path.c_str());
54 std::string line;
55 std::vector<primer> primers;
56 int16_t indice = 0;
57 while (std::getline(data, line)) { // Remove extra lineStream
58 std::stringstream lineStream(line);
59 std::string cell;
60 int ctr = 0;
61 primer p;
62 p.set_strand(0); // Set strand to NULL
63 while (std::getline(lineStream, cell, '\t')) {
64 switch (ctr) {
65 case 0:
66 p.set_region(cell);
67 break;
68 case 1:
69 if (std::all_of(cell.begin(), cell.end(), ::isdigit)) {
70 p.set_start(std::stoul(cell) - offset);
71 } else {
72 print_bed_format();
73 primers.clear();
74 return primers;
75 }
76 break;
77 case 2:
78 if (std::all_of(cell.begin(), cell.end(), ::isdigit)) {
79 p.set_end(std::stoul(cell) - 1 +
80 offset); // Bed format - End is not 0 based
81 } else {
82 print_bed_format();
83 primers.clear();
84 return primers;
85 }
86 break;
87 case 3:
88 p.set_name(cell);
89 break;
90 case 4:
91 if (std::all_of(cell.begin(), cell.end(), ::isdigit)) {
92 p.set_score(stoi(cell));
93 } else {
94 print_bed_format(); // score is missing, send warning but continue
95 // populating
96 std::cerr
97 << "\nWARNING: The BED file provided did not have the expected "
98 "score column, but iVar will continue trimming\n"
99 << std::endl;
100 p.set_score(-1);
101 }
102 break;
103 case 5:
104 if (cell[0] == '+' || cell[0] == '-')
105 p.set_strand(cell[0]);
106 else {
107 print_bed_format();
108 primers.clear();
109 return primers;
110 }
111 }
112 ctr++;
113 }
114 if (indice == 0 && ctr < 6)
115 std::cerr << "Strand not found in primer BED file so strand will not be "
116 "considered for trimming"
117 << std::endl;
118 p.set_indice(indice);
119 p.set_pair_indice(-1);
120 p.set_read_count(0);
121 primers.push_back(p);
122 indice++;
123 }
124 std::cerr << "Found " << primers.size() << " primers in BED file"
125 << std::endl;
126 return primers;
127 }
128
129 std::vector<primer> populate_from_file(std::string path) {
130 std::ifstream data(path.c_str());
131 std::string line;
132 std::vector<primer> primers;
133 int16_t indice = 0;
134 while (std::getline(data, line)) { // Remove extra lineStream
135 std::stringstream lineStream(line);
136 std::string cell;
137 int ctr = 0;
138 primer p;
139 p.set_strand(0); // Set strand to NULL
140 while (std::getline(lineStream, cell, '\t')) {
141 switch (ctr) {
142 case 0:
143 p.set_region(cell);
144 break;
145 case 1:
146 if (std::all_of(cell.begin(), cell.end(), ::isdigit)) {
147 p.set_start(std::stoul(cell));
148 } else {
149 print_bed_format();
150 primers.clear();
151 return primers;
152 }
153 break;
154 case 2:
155 if (std::all_of(cell.begin(), cell.end(), ::isdigit)) {
156 p.set_end(std::stoul(cell) - 1); // Bed format - End is not 0 based
157 } else {
158 print_bed_format();
159 primers.clear();
160 return primers;
161 }
162 break;
163 case 3:
164 p.set_name(cell);
165 break;
166 case 4:
167 if (std::all_of(cell.begin(), cell.end(), ::isdigit)) {
168 p.set_score(stoi(cell));
169 } else {
170 print_bed_format(); // score is missing, send warning but continue
171 // populating
172 std::cerr
173 << "\nWARNING: The BED file provided did not have the expected "
174 "score column, but iVar will continue trimming\n"
175 << std::endl;
176 p.set_score(-1);
177 }
178 break;
179 case 5:
180 if (cell[0] == '+' || cell[0] == '-')
181 p.set_strand(cell[0]);
182 else {
183 print_bed_format();
184 primers.clear();
185 return primers;
186 }
187 }
188 ctr++;
189 }
190 if (indice == 0 && ctr < 6)
191 std::cerr << "Strand not found in primer BED file so strand will not be "
192 "considered for trimming"
193 << std::endl;
194 p.set_indice(indice);
195 p.set_pair_indice(-1);
196 p.set_read_count(0);
197 primers.push_back(p);
198 indice++;
199 }
200 std::cerr << "Found " << primers.size() << " primers in BED file"
201 << std::endl;
202 return primers;
203 }
204
205 std::vector<primer> get_primers(std::vector<primer> p, unsigned int pos) {
206 std::vector<primer> primers_with_mismatches;
207 for (std::vector<primer>::iterator it = p.begin(); it != p.end(); ++it) {
208 if (it->get_start() <= pos && it->get_end() >= pos) {
209 primers_with_mismatches.push_back(*it);
210 }
211 }
212 return primers_with_mismatches;
213 }
214
215 // function to trim trailing spaces
216 std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ") {
217 str.erase(str.find_last_not_of(chars) + 1);
218 return str;
219 }
220
221 // Assumes unique primer names in BED file
222 // returns the index of a primer by name
223 int get_primer_indice(std::vector<primer> p, std::string name) {
224 // iterate through the primers from the bed file
225 for (std::vector<primer>::iterator it = p.begin(); it != p.end(); ++it) {
226 // check if the two strings are the same
227 if (it->get_name().compare(rtrim(name)) == 0) {
228 return it - p.begin();
229 }
230 }
231 return -1;
232 }
233
234 // function using the tab seperated primer pair file
235 int populate_pair_indices(std::vector<primer>& primers, std::string path) {
236 /*
237 * @param primers: the primer vector to add pair info to
238 * @param path: the path to the primer pair file
239 */
240
241 // load primer pair file
242 std::ifstream fin(path.c_str());
243 std::string line, cell, p1, p2;
244 std::stringstream line_stream;
245 std::vector<primer>::iterator it;
246 int32_t indice;
247 // iterate the primer pair file line by line
248 while (std::getline(fin, line)) {
249 line_stream << line;
250 std::getline(line_stream, cell, '\t');
251 p1 = cell;
252 line_stream.clear();
253 std::getline(line_stream, cell, '\t');
254 p2 = cell;
255 line_stream.clear();
256
257 p1 = rtrim(p1);
258 p2 = rtrim(p2);
259
260 if (!p1.empty() && !p2.empty()) {
261 for (it = primers.begin(); it != primers.end(); ++it) {
262 // search for primer name in pair file
263 if (it->get_name() == p1) {
264 // make sure it's pair exists
265 indice = get_primer_indice(primers, p2);
266 if (indice != -1) {
267 it->set_pair_indice(indice);
268 } else {
269 std::cerr << "Primer pair for " << p1 << " not found in BED file."
270 << std::endl;
271 }
272 } else if (it->get_name() == p2) {
273 indice = get_primer_indice(primers, p1);
274 if (indice != -1)
275 it->set_pair_indice(indice);
276 else
277 std::cerr << "Primer pair for " << p2 << " not found in BED file."
278 << std::endl;
279 }
280 }
281 } else {
282 std::cerr << "Primer pair is empty." << std::endl;
283 }
284 }
285 return 0;
286 }
287
288 primer get_min_start(std::vector<primer> primers) {
289 std::vector<primer>::iterator it;
290 auto minmax_start = std::minmax_element(
291 primers.begin(), primers.end(),
292 [](primer lhs, primer rhs) { return lhs.get_start() < rhs.get_start(); });
293 return *(minmax_start.first);
294 }
295
296 primer get_max_end(std::vector<primer> primers) {
297 auto minmax_start = std::minmax_element(
298 primers.begin(), primers.end(),
299 [](primer lhs, primer rhs) { return lhs.get_end() < rhs.get_end(); });
300 return *(minmax_start.second);
301 }
302
303 void print_all_primer_info(std::vector<primer> primers) {
304 std::vector<primer>::iterator it;
305 for (it = primers.begin(); it != primers.end(); ++it) {
306 std::cerr << "Primer name: " << it->get_name() << std::endl;
307 std::cerr << "Primer start: " << it->get_start() << std::endl;
308 std::cerr << "Primer end: " << it->get_end() << std::endl;
309 std::cerr << "Indice: " << it->get_indice() << std::endl;
310 std::cerr << "Pair indice: " << it->get_pair_indice() << std::endl;
311 }
312 }
313
314 void print_primer_info(primer primer) {
315 std::cerr << "Primer name: " << primer.get_name() << std::endl;
316 std::cerr << "Primer start: " << primer.get_start() << std::endl;
317 std::cerr << "Primer end: " << primer.get_end() << std::endl;
318 std::cerr << "Indice: " << primer.get_indice() << std::endl;
319 std::cerr << "Pair indice: " << primer.get_pair_indice() << std::endl;
320 }