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