Debian Codegraph @

src/modvcf.h

raw symbols SCIP
1 #ifndef MODVCF_H
2 #define MODVCF_H
3
4 #include <htslib/sam.h>
5 #include <htslib/vcf.h>
6
7 #include "bolog.h"
8
9
10
11 namespace torali
12 {
13
14
15 void _remove_info_tag(bcf_hdr_t const* hdr, bcf1_t* rec, std::string const& tag) {
16 bcf_update_info(hdr, rec, tag.c_str(), NULL, 0, BCF_HT_INT); // Type does not matter for n = 0
17 }
18
19 void _remove_format_tag(bcf_hdr_t const* hdr, bcf1_t* rec, std::string const& tag) {
20 bcf_update_format(hdr, rec, tag.c_str(), NULL, 0, BCF_HT_INT); // Type does not matter for n = 0
21 }
22
23 void _remove_info(bcf_hdr_t* hdr, bcf1_t* rec) {
24 std::string tmp[] = {"CT", "PRECISE", "IMPRECISE", "SVTYPE", "SVMETHOD", "CIEND", "CIPOS", "CHR2", "POS2", "END", "PE", "MAPQ", "SRMAPQ", "SR", "SRQ", "CONSENSUS"};
25 std::set<std::string> keepInfo(tmp, tmp + sizeof(tmp)/sizeof(tmp[0]));
26
27 if (!(rec->unpacked & BCF_UN_INFO)) bcf_unpack(rec, BCF_UN_INFO);
28
29 for (uint32_t i = 0; i < rec->n_info; ++i){
30 bcf_info_t* inf = &rec->d.info[i];
31 const char* key = bcf_hdr_int2id(hdr, BCF_DT_ID, inf->key);
32 if (keepInfo.find(std::string(key)) != keepInfo.end()) continue;
33
34 if (inf->vptr_free) {
35 free(inf->vptr - inf->vptr_off);
36 inf->vptr_free = 0;
37 }
38 rec->d.shared_dirty |= BCF1_DIRTY_INF;
39 inf->vptr = NULL;
40 }
41 }
42
43 void _remove_format(bcf_hdr_t* hdr, bcf1_t* rec) {
44 if (!(rec->unpacked & BCF_UN_FMT)) bcf_unpack(rec, BCF_UN_FMT);
45
46 for(uint32_t i = 0; i<rec->n_fmt; ++i) {
47 bcf_fmt_t* fmt = &rec->d.fmt[i];
48 const char* key = bcf_hdr_int2id(hdr, BCF_DT_ID, fmt->id);
49 bcf_update_format(hdr, rec, key, NULL, 0, BCF_HT_INT); // the type is irrelevant for n = 0
50 // Keep GT
51 //if ((key[0]=='G') && key[1]=='T' && (!key[2])) continue;
52
53 if (fmt->p_free) {
54 free(fmt->p - fmt->p_off);
55 fmt->p_free = 0;
56 }
57 rec->d.indiv_dirty = 1;
58 fmt->p = NULL;
59 }
60 }
61
62 inline int
63 _getInfoType(bcf_hdr_t const* hdr, std::string const& key) {
64 return bcf_hdr_id2type(hdr, BCF_HL_INFO, bcf_hdr_id2int(hdr, BCF_DT_ID, key.c_str()));
65 }
66
67 inline int
68 _getFormatType(bcf_hdr_t const* hdr, std::string const& key) {
69 return bcf_hdr_id2type(hdr, BCF_HL_FMT, bcf_hdr_id2int(hdr, BCF_DT_ID, key.c_str()));
70 }
71
72 inline bool _missing(bool const value) {
73 return !value;
74 }
75
76 inline bool _missing(float const value) {
77 return bcf_float_is_missing(value);
78 }
79
80 inline bool _missing(int8_t const value) {
81 return (value == bcf_int8_missing);
82 }
83
84 inline bool _missing(int16_t const value) {
85 return (value == bcf_int16_missing);
86 }
87
88 inline bool _missing(int32_t const value) {
89 return (value == bcf_int32_missing);
90 }
91
92 inline bool _missing(std::string const& value) {
93 return ((value.empty()) || (value == "."));
94 }
95
96 inline bool
97 _isKeyPresent(bcf_hdr_t const* hdr, std::string const& key) {
98 return (bcf_hdr_id2int(hdr, BCF_DT_ID, key.c_str())>=0);
99 }
100
101 inline std::string
102 _replaceIUPAC(std::string const& alleles) {
103 std::vector<char> out(alleles.size());
104 int32_t inTag = 0;
105 for(uint32_t i = 0; i<alleles.size(); ++i) {
106 if ((inTag) || (alleles[i] == 'A') || (alleles[i] == 'C') || (alleles[i] == 'G') || (alleles[i] == 'T') || (alleles[i] == 'N') || (alleles[i] == 'a') || (alleles[i] == 'c') || (alleles[i] == 'g') || (alleles[i] == 't') || (alleles[i] == 'n') || (alleles[i] == '<') || (alleles[i] == '>') || (alleles[i] == ']') || (alleles[i] == '[') || (alleles[i] == ',')) {
107 out[i] = alleles[i];
108 if (alleles[i] == '<') inTag = 1;
109 else if (alleles[i] == ']') inTag = 2;
110 else if (alleles[i] == '[') inTag = 3;
111 else if ((alleles[i] == '>') && (inTag == 1)) inTag = 0;
112 else if ((alleles[i] == ']') && (inTag == 2)) inTag = 0;
113 else if ((alleles[i] == '[') && (inTag == 3)) inTag = 0;
114 } else {
115 // Replace IUPAC
116 if ((alleles[i] == 'U') || (alleles[i] == 'u')) out[i] = 'T';
117 else if ((alleles[i] == 'R') || (alleles[i] == 'r')) out[i] = 'A';
118 else if ((alleles[i] == 'Y') || (alleles[i] == 'y')) out[i] = 'C';
119 else if ((alleles[i] == 'S') || (alleles[i] == 's')) out[i] = 'C';
120 else if ((alleles[i] == 'W') || (alleles[i] == 'w')) out[i] = 'A';
121 else if ((alleles[i] == 'K') || (alleles[i] == 'k')) out[i] = 'G';
122 else if ((alleles[i] == 'M') || (alleles[i] == 'm')) out[i] = 'A';
123 else if ((alleles[i] == 'B') || (alleles[i] == 'b')) out[i] = 'C';
124 else if ((alleles[i] == 'D') || (alleles[i] == 'd')) out[i] = 'A';
125 else if ((alleles[i] == 'H') || (alleles[i] == 'h')) out[i] = 'A';
126 else if ((alleles[i] == 'V') || (alleles[i] == 'v')) out[i] = 'A';
127 else out[i] = 'N';
128 }
129 }
130 return std::string(out.begin(), out.end());
131 }
132
133
134 // Convert string to char*
135 struct cstyle_str {
136 const char* operator ()(const std::string& s) {
137 return s.c_str();
138 }
139 };
140
141 // Parse Delly vcf file
142 template<typename TConfig, typename TStructuralVariantRecord>
143 inline void
144 vcfParse(TConfig const& c, bam_hdr_t* hd, std::vector<TStructuralVariantRecord>& svs) {
145 // Load bcf file
146 htsFile* ifile = bcf_open(c.vcffile.string().c_str(), "r");
147 bcf_hdr_t* hdr = bcf_hdr_read(ifile);
148 bcf1_t* rec = bcf_init();
149
150 // Parse bcf
151 int32_t nsvend = 0;
152 int32_t* svend = NULL;
153 int32_t npos2 = 0;
154 int32_t* pos2 = NULL;
155 int32_t nconsbp = 0;
156 int32_t* consbp = NULL;
157 int32_t nsvlen = 0;
158 int32_t* svlen = NULL;
159 int32_t npe = 0;
160 int32_t* pe = NULL;
161 int32_t ninslen = 0;
162 int32_t* inslen = NULL;
163 int32_t nhomlen = 0;
164 int32_t* homlen = NULL;
165 int32_t nsr = 0;
166 int32_t* sr = NULL;
167 int32_t ncipos = 0;
168 int32_t* cipos = NULL;
169 int32_t nmapq = 0;
170 int32_t* mapq = NULL;
171 int32_t nct = 0;
172 char* ct = NULL;
173 int32_t nsrq = 0;
174 float* srq = NULL;
175 int32_t nsvt = 0;
176 char* svt = NULL;
177 int32_t nmethod = 0;
178 char* method = NULL;
179 int32_t ncons = 0;
180 char* cons = NULL;
181 int32_t nchr2 = 0;
182 char* chr2 = NULL;
183 bool dellyVCF = false;
184 while (bcf_read(ifile, hdr, rec) == 0) {
185 bcf_unpack(rec, BCF_UN_INFO);
186
187 // Delly BCF file?
188 if (!dellyVCF) {
189 if (bcf_get_info_string(hdr, rec, "SVMETHOD", &method, &nmethod) > 0) {
190 std::string mstr = std::string(method);
191 if ((mstr.size() >= 10) && (mstr.substr(0, 10) == "EMBL.DELLY")) {
192 if (_isKeyPresent(hdr, "CONSBP")) dellyVCF = true;
193 }
194 }
195 }
196
197 // Delly
198 if (dellyVCF) {
199 // Fill SV record
200 StructuralVariantRecord svRec;
201 std::string chrName = bcf_hdr_id2name(hdr, rec->rid);
202 int32_t tid = bam_name2id(hd, chrName.c_str());
203 svRec.chr = tid;
204 svRec.svStart = rec->pos + 1;
205 svRec.id = svs.size();
206 svRec.mapq = rec->qual;
207 std::string refAllele = rec->d.allele[0];
208 if ((!refAllele.empty()) && (refAllele != ".")) {
209 std::string altAllele = rec->d.allele[1];
210 if ((!altAllele.empty()) && (altAllele != ".")) {
211 svRec.alleles = refAllele + "," + altAllele;
212 }
213 }
214
215 // Parse SV type
216 if ((bcf_get_info_string(hdr, rec, "SVTYPE", &svt, &nsvt) > 0) && (bcf_get_info_string(hdr, rec, "CT", &ct, &nct) > 0)) svRec.svt = _decodeOrientation(std::string(ct), std::string(svt));
217 else continue;
218
219 // Parse INFO
220 if (bcf_get_info_flag(hdr, rec, "PRECISE", 0, 0) > 0) svRec.precise=true;
221 else svRec.precise = false;
222 if (bcf_get_info_int32(hdr, rec, "PE", &pe, &npe) > 0) svRec.peSupport = *pe;
223 else {
224 if (svRec.precise) svRec.peSupport = 0;
225 else svRec.peSupport = 2;
226 }
227 if (svRec.svt != 4) {
228 if (bcf_get_info_int32(hdr, rec, "INSLEN", &inslen, &ninslen) > 0) svRec.insLen = *inslen;
229 else svRec.insLen = 0;
230 } else {
231 // Insertions must have INFO/SVLEN
232 if (bcf_get_info_int32(hdr, rec, "SVLEN", &svlen, &nsvlen) > 0) svRec.insLen = *svlen;
233 else continue;
234 }
235 if (bcf_get_info_int32(hdr, rec, "HOMLEN", &homlen, &nhomlen) > 0) svRec.homLen = *homlen;
236 else svRec.homLen = 0;
237 if (bcf_get_info_int32(hdr, rec, "SR", &sr, &nsr) > 0) svRec.srSupport = *sr;
238 else svRec.srSupport = 0;
239
240 // SV end assignment
241 svRec.chr2 = tid;
242 svRec.svEnd = rec->pos + 2;
243 if (svRec.svt < DELLY_SVT_TRANS) {
244 // Intra-chromosomal SV
245 if (bcf_get_info_int32(hdr, rec, "END", &svend, &nsvend) > 0) svRec.svEnd = *svend;
246 svRec.svEnd += 1;
247 } else {
248 // Inter-chromosomal SV
249 if (bcf_get_info_string(hdr, rec, "CHR2", &chr2, &nchr2) > 0) {
250 std::string chr2Name = std::string(chr2);
251 svRec.chr2 = bam_name2id(hd, chr2Name.c_str());
252 }
253 if (bcf_get_info_int32(hdr, rec, "POS2", &pos2, &npos2) > 0) svRec.svEnd = *pos2;
254 }
255 if (bcf_get_info_string(hdr, rec, "CONSENSUS", &cons, &ncons) > 0) {
256 svRec.consensus = std::string(cons);
257 if (bcf_get_info_int32(hdr, rec, "CONSBP", &consbp, &nconsbp) > 0) svRec.consBp = *consbp;
258 }
259 else svRec.precise = false;
260 if (bcf_get_info_int32(hdr, rec, "CIPOS", &cipos, &ncipos) > 0) {
261 svRec.ciposlow = cipos[0];
262 svRec.ciposhigh = cipos[1];
263 } else {
264 svRec.ciposlow = -50;
265 svRec.ciposhigh = 50;
266 }
267 if (bcf_get_info_int32(hdr, rec, "CIEND", &cipos, &ncipos) > 0) {
268 svRec.ciendlow = cipos[0];
269 svRec.ciendhigh = cipos[1];
270 } else {
271 svRec.ciendlow = -50;
272 svRec.ciendhigh = 50;
273 }
274 if (bcf_get_info_int32(hdr, rec, "MAPQ", &mapq, &nmapq) > 0) svRec.peMapQuality = (uint8_t) *mapq;
275 else svRec.peMapQuality = 0;
276 if (bcf_get_info_int32(hdr, rec, "SRMAPQ", &mapq, &nmapq) > 0) svRec.srMapQuality = (uint8_t) *mapq;
277 else svRec.srMapQuality = 0;
278 if (bcf_get_info_float(hdr, rec, "SRQ", &srq, &nsrq) > 0) svRec.srAlignQuality = (double) *srq;
279 else svRec.srAlignQuality = 0;
280
281 svs.push_back(svRec);
282 } else {
283 std::cerr << "Error: Delly genotyping requires local SV assembly (INFO/CONSENSUS) and breakpoint (INFO/CONSBP) introduced in delly v1.1.7!" << std::endl;
284 break;
285 }
286 }
287 // Clean-up
288 free(svend);
289 free(pos2);
290 free(consbp);
291 free(svlen);
292 free(svt);
293 free(method);
294 free(pe);
295 free(inslen);
296 free(homlen);
297 free(sr);
298 free(cons);
299 free(cipos);
300 free(mapq);
301 free(srq);
302 free(ct);
303 free(chr2);
304
305 // Close VCF
306 bcf_hdr_destroy(hdr);
307 bcf_close(ifile);
308 bcf_destroy(rec);
309 }
310
311
312 template<typename TConfig, typename TStructuralVariantRecord, typename TJunctionCountMap, typename TReadCountMap, typename TCountMap>
313 inline void
314 vcfOutput(TConfig const& c, std::vector<TStructuralVariantRecord> const& svs, TJunctionCountMap const& jctCountMap, TReadCountMap const& readCountMap, TCountMap const& spanCountMap)
315 {
316 // BoLog class
317 BoLog<double> bl;
318
319 // Open one bam file header
320 samFile* samfile = sam_open(c.files[0].string().c_str(), "r");
321 hts_set_fai_filename(samfile, c.genome.string().c_str());
322 bam_hdr_t* bamhd = sam_hdr_read(samfile);
323
324 // Output all structural variants
325 std::string fmtout = "wb";
326 if (c.outfile.string() == "-") fmtout = "w";
327 htsFile *fp = hts_open(c.outfile.string().c_str(), fmtout.c_str());
328 bcf_hdr_t *hdr = bcf_hdr_init("w");
329
330 // Print vcf header
331 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
332 boost::gregorian::date today = now.date();
333 std::string datestr("##fileDate=");
334 datestr += boost::gregorian::to_iso_string(today);
335 bcf_hdr_append(hdr, datestr.c_str());
336 bcf_hdr_append(hdr, "##ALT=<ID=DEL,Description=\"Deletion\">");
337 bcf_hdr_append(hdr, "##ALT=<ID=DUP,Description=\"Duplication\">");
338 bcf_hdr_append(hdr, "##ALT=<ID=INV,Description=\"Inversion\">");
339 bcf_hdr_append(hdr, "##ALT=<ID=BND,Description=\"Translocation\">");
340 bcf_hdr_append(hdr, "##ALT=<ID=INS,Description=\"Insertion\">");
341 bcf_hdr_append(hdr, "##FILTER=<ID=LowQual,Description=\"Poor quality and insufficient number of PEs and SRs.\">");
342 bcf_hdr_append(hdr, "##INFO=<ID=CIEND,Number=2,Type=Integer,Description=\"PE confidence interval around END\">");
343 bcf_hdr_append(hdr, "##INFO=<ID=CIPOS,Number=2,Type=Integer,Description=\"PE confidence interval around POS\">");
344 bcf_hdr_append(hdr, "##INFO=<ID=CHR2,Number=1,Type=String,Description=\"Chromosome for POS2 coordinate in case of an inter-chromosomal translocation\">");
345 bcf_hdr_append(hdr, "##INFO=<ID=POS2,Number=1,Type=Integer,Description=\"Genomic position for CHR2 in case of an inter-chromosomal translocation\">");
346 bcf_hdr_append(hdr, "##INFO=<ID=END,Number=1,Type=Integer,Description=\"End position of the structural variant\">");
347 bcf_hdr_append(hdr, "##INFO=<ID=PE,Number=1,Type=Integer,Description=\"Paired-end support of the structural variant\">");
348 bcf_hdr_append(hdr, "##INFO=<ID=MAPQ,Number=1,Type=Integer,Description=\"Median mapping quality of paired-ends\">");
349 bcf_hdr_append(hdr, "##INFO=<ID=SRMAPQ,Number=1,Type=Integer,Description=\"Median mapping quality of split-reads\">");
350 bcf_hdr_append(hdr, "##INFO=<ID=SR,Number=1,Type=Integer,Description=\"Split-read support\">");
351 bcf_hdr_append(hdr, "##INFO=<ID=SRQ,Number=1,Type=Float,Description=\"Split-read consensus alignment quality\">");
352 bcf_hdr_append(hdr, "##INFO=<ID=CONSENSUS,Number=1,Type=String,Description=\"Split-read consensus sequence\">");
353 bcf_hdr_append(hdr, "##INFO=<ID=CONSBP,Number=1,Type=Integer,Description=\"Consensus SV breakpoint position\">");
354 bcf_hdr_append(hdr, "##INFO=<ID=CE,Number=1,Type=Float,Description=\"Consensus sequence entropy\">");
355 bcf_hdr_append(hdr, "##INFO=<ID=CT,Number=1,Type=String,Description=\"Paired-end signature induced connection type\">");
356 bcf_hdr_append(hdr, "##INFO=<ID=SVLEN,Number=1,Type=Integer,Description=\"Insertion length for SVTYPE=INS.\">");
357 bcf_hdr_append(hdr, "##INFO=<ID=IMPRECISE,Number=0,Type=Flag,Description=\"Imprecise structural variation\">");
358 bcf_hdr_append(hdr, "##INFO=<ID=PRECISE,Number=0,Type=Flag,Description=\"Precise structural variation\">");
359 bcf_hdr_append(hdr, "##INFO=<ID=SVTYPE,Number=1,Type=String,Description=\"Type of structural variant\">");
360 bcf_hdr_append(hdr, "##INFO=<ID=SVMETHOD,Number=1,Type=String,Description=\"Type of approach used to detect SV\">");
361 bcf_hdr_append(hdr, "##INFO=<ID=INSLEN,Number=1,Type=Integer,Description=\"Predicted length of the insertion\">");
362 bcf_hdr_append(hdr, "##INFO=<ID=HOMLEN,Number=1,Type=Integer,Description=\"Predicted microhomology length using a max. edit distance of 2\">");
363 bcf_hdr_append(hdr, "##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">");
364 bcf_hdr_append(hdr, "##FORMAT=<ID=GL,Number=G,Type=Float,Description=\"Log10-scaled genotype likelihoods for RR,RA,AA genotypes\">");
365 bcf_hdr_append(hdr, "##FORMAT=<ID=GQ,Number=1,Type=Integer,Description=\"Genotype Quality\">");
366 bcf_hdr_append(hdr, "##FORMAT=<ID=FT,Number=1,Type=String,Description=\"Per-sample genotype filter\">");
367 bcf_hdr_append(hdr, "##FORMAT=<ID=RC,Number=1,Type=Integer,Description=\"Raw high-quality read counts or base counts for the SV\">");
368 bcf_hdr_append(hdr, "##FORMAT=<ID=RCL,Number=1,Type=Integer,Description=\"Raw high-quality read counts or base counts for the left control region\">");
369 bcf_hdr_append(hdr, "##FORMAT=<ID=RCR,Number=1,Type=Integer,Description=\"Raw high-quality read counts or base counts for the right control region\">");
370 bcf_hdr_append(hdr, "##FORMAT=<ID=RDCN,Number=1,Type=Integer,Description=\"Read-depth based copy-number estimate for autosomal sites\">");
371 bcf_hdr_append(hdr, "##FORMAT=<ID=DR,Number=1,Type=Integer,Description=\"# high-quality reference pairs\">");
372 bcf_hdr_append(hdr, "##FORMAT=<ID=DV,Number=1,Type=Integer,Description=\"# high-quality variant pairs\">");
373 bcf_hdr_append(hdr, "##FORMAT=<ID=RR,Number=1,Type=Integer,Description=\"# high-quality reference junction reads\">");
374 bcf_hdr_append(hdr, "##FORMAT=<ID=RV,Number=1,Type=Integer,Description=\"# high-quality variant junction reads\">");
375
376 // Add reference
377 std::string refloc("##reference=");
378 refloc += c.genome.string();
379 bcf_hdr_append(hdr, refloc.c_str());
380 for (int i = 0; i<bamhd->n_targets; ++i) {
381 std::string refname("##contig=<ID=");
382 refname += std::string(bamhd->target_name[i]) + ",length=" + boost::lexical_cast<std::string>(bamhd->target_len[i]) + ">";
383 bcf_hdr_append(hdr, refname.c_str());
384 }
385 // Add samples
386 for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) bcf_hdr_add_sample(hdr, c.sampleName[file_c].c_str());
387 bcf_hdr_add_sample(hdr, NULL);
388 if (bcf_hdr_write(fp, hdr) != 0) std::cerr << "Error: Failed to write BCF header!" << std::endl;
389
390 if (!svs.empty()) {
391 // Genotype arrays
392 int32_t *gts = (int*) malloc(bcf_hdr_nsamples(hdr) * 2 * sizeof(int));
393 float *gls = (float*) malloc(bcf_hdr_nsamples(hdr) * 3 * sizeof(float));
394 int32_t *rcl = (int*) malloc(bcf_hdr_nsamples(hdr) * sizeof(int));
395 int32_t *rc = (int*) malloc(bcf_hdr_nsamples(hdr) * sizeof(int));
396 int32_t *rcr = (int*) malloc(bcf_hdr_nsamples(hdr) * sizeof(int));
397 int32_t *cnest = (int*) malloc(bcf_hdr_nsamples(hdr) * sizeof(int));
398 int32_t *drcount = (int*) malloc(bcf_hdr_nsamples(hdr) * sizeof(int));
399 int32_t *dvcount = (int*) malloc(bcf_hdr_nsamples(hdr) * sizeof(int));
400 int32_t *rrcount = (int*) malloc(bcf_hdr_nsamples(hdr) * sizeof(int));
401 int32_t *rvcount = (int*) malloc(bcf_hdr_nsamples(hdr) * sizeof(int));
402 int32_t *gqval = (int*) malloc(bcf_hdr_nsamples(hdr) * sizeof(int));
403 std::vector<std::string> ftarr;
404 ftarr.resize(bcf_hdr_nsamples(hdr));
405
406 // Iterate all structural variants
407 typedef std::vector<TStructuralVariantRecord> TSVs;
408 now = boost::posix_time::second_clock::local_time();
409 std::cerr << '[' << boost::posix_time::to_simple_string(now) << "] " << "Genotyping" << std::endl;
410 bcf1_t *rec = bcf_init();
411 for(typename TSVs::const_iterator svIter = svs.begin(); svIter!=svs.end(); ++svIter) {
412 if ((svIter->srSupport == 0) && (svIter->peSupport == 0)) continue;
413 // In discovery mode, skip SVs that have less than 2 reads support after genotyping
414 if (!c.hasVcfFile) {
415 uint32_t totalGtSup = 0;
416 for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) {
417 totalGtSup += spanCountMap[file_c][svIter->id].alt.size() + jctCountMap[file_c][svIter->id].alt.size();
418 }
419 if (totalGtSup < 2) continue;
420 }
421
422 // Output main vcf fields
423 int32_t tmpi = bcf_hdr_id2int(hdr, BCF_DT_ID, "PASS");
424 if (svIter->chr == svIter->chr2) {
425 // Intra-chromosomal
426 if (((svIter->peSupport < 3) || (svIter->peMapQuality < 20)) && ((svIter->srSupport < 3) || (svIter->srMapQuality < 20))) tmpi = bcf_hdr_id2int(hdr, BCF_DT_ID, "LowQual");
427 } else {
428 // Inter-chromosomal
429 if (((svIter->peSupport < 5) || (svIter->peMapQuality < 20)) && ((svIter->srSupport < 5) || (svIter->srMapQuality < 20))) tmpi = bcf_hdr_id2int(hdr, BCF_DT_ID, "LowQual");
430 }
431 rec->rid = bcf_hdr_name2id(hdr, bamhd->target_name[svIter->chr]);
432 int32_t svStartPos = svIter->svStart - 1;
433 if (svStartPos < 1) svStartPos = 1;
434 int32_t svEndPos = svIter->svEnd;
435 if (svIter->chr == svIter->chr2) --svEndPos; // To match VCF spec
436 if (svEndPos < 1) svEndPos = 1;
437 if (svEndPos >= (int32_t) bamhd->target_len[svIter->chr2]) svEndPos = bamhd->target_len[svIter->chr2] - 1;
438 rec->pos = svStartPos;
439 std::string id(_addID(svIter->svt));
440 std::string padNumber = boost::lexical_cast<std::string>(svIter->id);
441 padNumber.insert(padNumber.begin(), 8 - padNumber.length(), '0');
442 id += padNumber;
443 bcf_update_id(hdr, rec, id.c_str());
444 std::string alleles = _replaceIUPAC(svIter->alleles);
445 bcf_update_alleles_str(hdr, rec, alleles.c_str());
446 bcf_update_filter(hdr, rec, &tmpi, 1);
447
448 // Add INFO fields
449 if (svIter->precise) bcf_update_info_flag(hdr, rec, "PRECISE", NULL, 1);
450 else bcf_update_info_flag(hdr, rec, "IMPRECISE", NULL, 1);
451 bcf_update_info_string(hdr, rec, "SVTYPE", _addID(svIter->svt).c_str());
452 std::string dellyVersion("EMBL.DELLYv");
453 dellyVersion += dellyVersionNumber;
454 bcf_update_info_string(hdr,rec, "SVMETHOD", dellyVersion.c_str());
455 if (svIter->svt < DELLY_SVT_TRANS) {
456 if (svEndPos <= svStartPos) svEndPos = svStartPos + 1;
457 tmpi = svEndPos;
458 bcf_update_info_int32(hdr, rec, "END", &tmpi, 1);
459 } else {
460 tmpi = svStartPos + 2;
461 bcf_update_info_int32(hdr, rec, "END", &tmpi, 1);
462 bcf_update_info_string(hdr,rec, "CHR2", bamhd->target_name[svIter->chr2]);
463 tmpi = svEndPos;
464 bcf_update_info_int32(hdr, rec, "POS2", &tmpi, 1);
465 }
466 if (svIter->svt == 4) {
467 tmpi = svIter->insLen;
468 bcf_update_info_int32(hdr, rec, "SVLEN", &tmpi, 1);
469 }
470 tmpi = svIter->peSupport;
471 bcf_update_info_int32(hdr, rec, "PE", &tmpi, 1);
472 tmpi = svIter->peMapQuality;
473 bcf_update_info_int32(hdr, rec, "MAPQ", &tmpi, 1);
474 bcf_update_info_string(hdr, rec, "CT", _addOrientation(svIter->svt).c_str());
475 int32_t ciend[2];
476 ciend[0] = svIter->ciendlow;
477 ciend[1] = svIter->ciendhigh;
478 int32_t cipos[2];
479 cipos[0] = svIter->ciposlow;
480 cipos[1] = svIter->ciposhigh;
481 bcf_update_info_int32(hdr, rec, "CIPOS", cipos, 2);
482 bcf_update_info_int32(hdr, rec, "CIEND", ciend, 2);
483
484 if (svIter->precise) {
485 tmpi = svIter->srMapQuality;
486 bcf_update_info_int32(hdr, rec, "SRMAPQ", &tmpi, 1);
487 tmpi = svIter->insLen;
488 bcf_update_info_int32(hdr, rec, "INSLEN", &tmpi, 1);
489 tmpi = svIter->homLen;
490 bcf_update_info_int32(hdr, rec, "HOMLEN", &tmpi, 1);
491 tmpi = svIter->srSupport;
492 bcf_update_info_int32(hdr, rec, "SR", &tmpi, 1);
493 float tmpf = svIter->srAlignQuality;
494 bcf_update_info_float(hdr, rec, "SRQ", &tmpf, 1);
495 if (svIter->consensus.size()) {
496 bcf_update_info_string(hdr, rec, "CONSENSUS", svIter->consensus.c_str());
497 tmpf = entropy(svIter->consensus);
498 bcf_update_info_float(hdr, rec, "CE", &tmpf, 1);
499 tmpi = svIter->consBp;
500 bcf_update_info_int32(hdr, rec, "CONSBP", &tmpi, 1);
501 }
502 }
503
504 // Add genotype columns
505 for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) {
506 // Counters
507 rcl[file_c] = 0;
508 rc[file_c] = 0;
509 rcr[file_c] = 0;
510 cnest[file_c] = 0;
511 drcount[file_c] = 0;
512 dvcount[file_c] = 0;
513 rrcount[file_c] = 0;
514 rvcount[file_c] = 0;
515 drcount[file_c] = spanCountMap[file_c][svIter->id].ref.size();
516 dvcount[file_c] = spanCountMap[file_c][svIter->id].alt.size();
517 rrcount[file_c] = jctCountMap[file_c][svIter->id].ref.size();
518 rvcount[file_c] = jctCountMap[file_c][svIter->id].alt.size();
519
520 // Compute GLs
521 if (svIter->precise) _computeGLs(bl, jctCountMap[file_c][svIter->id].ref, jctCountMap[file_c][svIter->id].alt, gls, gqval, gts, file_c);
522 else _computeGLs(bl, spanCountMap[file_c][svIter->id].ref, spanCountMap[file_c][svIter->id].alt, gls, gqval, gts, file_c);
523
524 // Compute RCs
525 rcl[file_c] = readCountMap[file_c][svIter->id].leftRC;
526 rc[file_c] = readCountMap[file_c][svIter->id].rc;
527 rcr[file_c] = readCountMap[file_c][svIter->id].rightRC;
528 cnest[file_c] = -1;
529 if ((rcl[file_c] + rcr[file_c]) > 0) cnest[file_c] = boost::math::iround( 2.0 * (double) rc[file_c] / (double) (rcl[file_c] + rcr[file_c]) );
530
531 // Genotype filter
532 if (gqval[file_c] < 15) ftarr[file_c] = "LowQual";
533 else ftarr[file_c] = "PASS";
534 }
535 int32_t qvalout = svIter->mapq;
536 if (qvalout < 0) qvalout = 0;
537 if (qvalout > 10000) qvalout = 10000;
538 rec->qual = qvalout;
539
540 bcf_update_genotypes(hdr, rec, gts, bcf_hdr_nsamples(hdr) * 2);
541 bcf_update_format_float(hdr, rec, "GL", gls, bcf_hdr_nsamples(hdr) * 3);
542 bcf_update_format_int32(hdr, rec, "GQ", gqval, bcf_hdr_nsamples(hdr));
543 std::vector<const char*> strp(bcf_hdr_nsamples(hdr));
544 std::transform(ftarr.begin(), ftarr.end(), strp.begin(), cstyle_str());
545 bcf_update_format_string(hdr, rec, "FT", &strp[0], bcf_hdr_nsamples(hdr));
546 bcf_update_format_int32(hdr, rec, "RCL", rcl, bcf_hdr_nsamples(hdr));
547 bcf_update_format_int32(hdr, rec, "RC", rc, bcf_hdr_nsamples(hdr));
548 bcf_update_format_int32(hdr, rec, "RCR", rcr, bcf_hdr_nsamples(hdr));
549 bcf_update_format_int32(hdr, rec, "RDCN", cnest, bcf_hdr_nsamples(hdr));
550 bcf_update_format_int32(hdr, rec, "DR", drcount, bcf_hdr_nsamples(hdr));
551 bcf_update_format_int32(hdr, rec, "DV", dvcount, bcf_hdr_nsamples(hdr));
552 bcf_update_format_int32(hdr, rec, "RR", rrcount, bcf_hdr_nsamples(hdr));
553 bcf_update_format_int32(hdr, rec, "RV", rvcount, bcf_hdr_nsamples(hdr));
554 bcf_write1(fp, hdr, rec);
555 bcf_clear1(rec);
556 }
557 bcf_destroy1(rec);
558
559 // Clean-up
560 free(gts);
561 free(gls);
562 free(rcl);
563 free(rc);
564 free(rcr);
565 free(cnest);
566 free(drcount);
567 free(dvcount);
568 free(rrcount);
569 free(rvcount);
570 free(gqval);
571 }
572
573 // Close BAM file
574 bam_hdr_destroy(bamhd);
575 sam_close(samfile);
576
577 // Close VCF file
578 bcf_hdr_destroy(hdr);
579 hts_close(fp);
580
581 // Build index
582 if (c.outfile.string() != "-") bcf_index_build(c.outfile.string().c_str(), 14);
583 }
584
585
586 }
587
588 #endif