forked from pkrusche/vt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discover2.cpp
314 lines (265 loc) · 11 KB
/
discover2.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
/* The MIT License
Copyright (c) 2014 Adrian Tan <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "discover2.h"
namespace
{
typedef struct
{
int32_t start1, end1;
} interval_t;
KHASH_MAP_INIT_STR(rdict, interval_t)
class Igor : Program
{
public:
///////////
//options//
///////////
std::vector<GenomeInterval> intervals;
std::string output_vcf_file;
std::string input_bam_file;
std::string ref_fasta_file;
std::string sample_id;
uint32_t mapq_cutoff;
uint32_t baseq_cutoff;
std::string variant_type;
uint32_t evidence_allele_count_cutoff;
double fractional_evidence_allele_count_cutoff;
uint16_t exclude_flag;
///////
//i/o//
///////
BAMOrderedReader *odr;
bam1_t *s;
BCFOrderedWriter *odw;
bcf1_t *v;
/////////
//stats//
/////////
uint32_t no_reads;
uint32_t no_overlapping_reads;
uint32_t no_passed_reads;
uint32_t no_exclude_flag_reads;
uint32_t no_low_mapq_reads;
/////////
//tools//
/////////
BAMVariantExtractor *bve;
Igor(int argc, char **argv)
{
version = "0.5";
//////////////////////////
//options initialization//
//////////////////////////
try
{
std::string desc = "Discovers variants from reads in a BAM file.";
TCLAP::CmdLine cmd(desc, ' ', version);
VTOutput my; cmd.setOutput(&my);
TCLAP::ValueArg<std::string> arg_intervals("i", "i", "intervals []", false, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_interval_list("I", "I", "file containing list of intervals []", false, "", "file", cmd);
TCLAP::ValueArg<std::string> arg_output_vcf_file("o", "o", "output VCF file [-]", false, "-", "str", cmd);
TCLAP::ValueArg<std::string> arg_ref_fasta_file("r", "r", "reference sequence fasta file []", true, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_sample_id("s", "s", "sample ID", true, "", "str", cmd);
TCLAP::ValueArg<uint32_t> arg_mapq_cutoff("m", "m", "MAPQ cutoff for alignments [20]", false, 20, "int", cmd);
TCLAP::ValueArg<uint32_t> arg_baseq_cutoff("q", "q", "base quality cutoff for bases [13]", false, 13, "int", cmd);
TCLAP::ValueArg<uint32_t> arg_evidence_allele_count_cutoff("e", "e", "evidence count cutoff for candidate allele [2]", false, 2, "int", cmd);
TCLAP::ValueArg<double> arg_fractional_evidence_allele_count_cutoff("f", "f", "fractional evidence cutoff for candidate allele [0.1]", false, 0.1, "float", cmd);
TCLAP::ValueArg<std::string> arg_variant_type("v", "v", "variant types [snps,mnps,indels]", false, "snps,mnps,indels", "str", cmd);
TCLAP::ValueArg<std::string> arg_input_bam_file("b", "b", "input BAM file", true, "", "string", cmd);
cmd.parse(argc, argv);
input_bam_file = arg_input_bam_file.getValue();
parse_intervals(intervals, arg_interval_list.getValue(), arg_intervals.getValue());
output_vcf_file = arg_output_vcf_file.getValue();
sample_id = arg_sample_id.getValue();
ref_fasta_file = arg_ref_fasta_file.getValue();
mapq_cutoff = arg_mapq_cutoff.getValue();
baseq_cutoff = arg_baseq_cutoff.getValue();
variant_type = arg_variant_type.getValue();
evidence_allele_count_cutoff = arg_evidence_allele_count_cutoff.getValue();
fractional_evidence_allele_count_cutoff = arg_fractional_evidence_allele_count_cutoff.getValue();
}
catch (TCLAP::ArgException &e)
{
std::cerr << "error: " << e.error() << " for arg " << e.argId() << "\n";
abort();
}
};
void initialize()
{
//////////////////////
//i/o initialization//
//////////////////////
exclude_flag = 0x0704;
odr = new BAMOrderedReader(input_bam_file, intervals);
s = bam_init1();
odw = new BCFOrderedWriter(output_vcf_file, 0);
bam_hdr_transfer_contigs_to_bcf_hdr(odr->hdr, odw->hdr);
bcf_hdr_append(odw->hdr, "##FORMAT=<ID=E,Number=1,Type=Integer,Description=\"Number of reads containing evidence of the alternate allele\">");
bcf_hdr_append(odw->hdr, "##FORMAT=<ID=N,Number=1,Type=Integer,Description=\"Total number of reads at a candidate locus with reads that contain evidence of the alternate allele\">");
bcf_hdr_add_sample(odw->hdr, sample_id.c_str());
bcf_hdr_add_sample(odw->hdr, NULL);
v = NULL;
std::vector<std::string> variant_types;
split(variant_types, ",", variant_type);
int32_t vtype = 0;
for (size_t i = 0; i<variant_types.size(); ++i)
{
if (variant_types[i] == "snps")
{
vtype |= VT_SNP;
}
else if (variant_types[i] == "indels")
{
vtype |= VT_INDEL;
}
else if (variant_types[i] == "all")
{
vtype = VT_SNP|VT_INDEL;
}
}
////////////////////////
//stats initialization//
////////////////////////
no_reads = 0;
no_overlapping_reads = 0;
no_passed_reads = 0;
no_exclude_flag_reads = 0;
no_low_mapq_reads = 0;
////////////////////////
//tools initialization//
////////////////////////
bve = new BAMVariantExtractor(vtype,
evidence_allele_count_cutoff,
fractional_evidence_allele_count_cutoff,
baseq_cutoff,
ref_fasta_file);
}
void discover()
{
odw->write_hdr();
//for tracking overlapping reads
khash_t(rdict) *reads = kh_init(rdict);
khiter_t k;
int32_t ret;
bcf1_t *v = bcf_init();;
while (odr->read(s))
{
++no_reads;
//this read is the first of the pair
if (bam_get_mpos1(s) && (bam_get_tid(s)==bam_get_mtid(s)))
{
//first mate
if (bam_get_mpos1(s)>bam_get_pos1(s))
{
//overlapping
if (bam_get_mpos1(s)<=(bam_get_pos1(s) + bam_get_l_qseq(s) - 1))
{
//add read that has overlapping
//duplicate the record and perform the stitching later
char* qname = strdup(bam_get_qname(s));
k = kh_put(rdict, reads, qname, &ret);
if (!ret)
{
//already present
free(qname);
}
kh_val(reads, k) = {bam_get_pos1(s), bam_get_pos1(s)+bam_get_l_qseq(s)-1};
}
}
else
{
//check overlap
//todo: perform stitching in future
if((k = kh_get(rdict, reads, bam_get_qname(s)))!=kh_end(reads))
{
if (kh_exist(reads, k))
{
free((char*)kh_key(reads, k));
kh_del(rdict, reads, k);
++no_overlapping_reads;
}
//continue;
}
}
}
if(bam_get_flag(s) & exclude_flag)
{
//1. unmapped
//2. secondary alignment
//3. not passing QC
//4. PCR or optical duplicate
++no_exclude_flag_reads;
continue;
}
if (bam_get_mapq(s) < mapq_cutoff)
{
//filter short aligments and those with too many indels (?)
++no_low_mapq_reads;
continue;
}
bve->process_read(odr->hdr, s);
while(bve->next_variant(v))
{
odw->write(v);
}
++no_passed_reads;
}
while(bve->next_variant(v))
{
odw->write(v);
}
odw->close();
};
void print_options()
{
std::clog << "discover v" << version << "\n\n";
std::clog << "options: [b] input BAM File " << input_bam_file << "\n";
std::clog << " [o] output VCF File " << output_vcf_file << "\n";
std::clog << " [s] sample ID " << sample_id << "\n";
std::clog << " [r] reference FASTA File " << ref_fasta_file << "\n";
std::clog << " [m] MAPQ cutoff " << mapq_cutoff << "\n";
std::clog << " [q] base quality cutoff " << baseq_cutoff << "\n";
std::clog << " [v] variant type(s) " << variant_type << "\n";
std::clog << " [e] evidence cutoff " << evidence_allele_count_cutoff << "\n";
std::clog << " [f] fractional evidence cutoff " << fractional_evidence_allele_count_cutoff<< "\n";
print_int_op(" [i] intervals ", intervals);
std::clog << "\n";
}
void print_stats()
{
std::clog << "\n";
std::clog << "stats: no. reads : " << no_reads << "\n";
std::clog << " no. overlapping reads : " << no_overlapping_reads << "\n";
std::clog << " no. low mapq reads : " << no_low_mapq_reads << "\n";
std::clog << " no. passed reads : " << no_passed_reads << "\n";
std::clog << " no. exclude flag reads : " << no_exclude_flag_reads << "\n";
std::clog << "\n";
};
~Igor() {};
private:
};
}
void discover2(int argc, char ** argv)
{
Igor igor(argc, argv);
igor.print_options();
igor.initialize();
igor.discover();
igor.print_stats();
};