Debian Codegraph @

src/tegua.h

raw symbols SCIP
1 #ifndef TEGUA_H
2 #define TEGUA_H
3
4 #include <boost/unordered_map.hpp>
5 #include <boost/date_time/posix_time/posix_time.hpp>
6 #include <boost/date_time/gregorian/gregorian.hpp>
7 #include <boost/program_options/cmdline.hpp>
8 #include <boost/program_options/options_description.hpp>
9 #include <boost/program_options/parsers.hpp>
10 #include <boost/program_options/variables_map.hpp>
11 #include <boost/filesystem.hpp>
12 #include <boost/tokenizer.hpp>
13 #include <iostream>
14 #include <vector>
15 #include <htslib/vcf.h>
16 #include <htslib/sam.h>
17 #include <math.h>
18 #include <stdio.h>
19
20 #include <htslib/sam.h>
21 #include <htslib/vcf.h>
22 #include <htslib/faidx.h>
23
24 #include "edlib.h"
25 #include "delly.h"
26 #include "coverage.h"
27 #include "genotype.h"
28 #include "util.h"
29 #include "junction.h"
30 #include "cluster.h"
31 #include "assemble.h"
32 #include "modvcf.h"
33
34 namespace torali {
35
36
37 struct TeguaConfig {
38 bool hasDumpFile;
39 bool hasExcludeFile;
40 bool hasVcfFile;
41 bool hasAltFile;
42 uint16_t minMapQual;
43 uint16_t minGenoQual;
44 uint32_t minClip;
45 uint32_t minRefSep;
46 uint32_t maxReadSep;
47 uint32_t graphPruning;
48 uint32_t minCliqueSize;
49 uint32_t maxReadPerSV;
50 uint32_t maxGenoReadCount;
51 int32_t minConsWindow;
52 int32_t nchr;
53 int32_t minimumFlankSize;
54 int32_t indelsize;
55 float indelExtension;
56 float flankQuality;
57 std::set<int32_t> svtset;
58 DnaScore<int> aliscore;
59 boost::filesystem::path dumpfile;
60 boost::filesystem::path outfile;
61 boost::filesystem::path vcffile;
62 std::vector<boost::filesystem::path> files;
63 boost::filesystem::path altfile;
64 boost::filesystem::path genome;
65 boost::filesystem::path exclude;
66 std::vector<std::string> sampleName;
67 };
68
69
70 template<typename TConfig>
71 inline int32_t
72 runTegua(TConfig& c) {
73
74 #ifdef PROFILE
75 ProfilerStart("delly.prof");
76 #endif
77
78 // Structural Variants
79 typedef std::vector<StructuralVariantRecord> TVariants;
80 TVariants svs;
81
82 // Open header
83 samFile* samfile = sam_open(c.files[0].string().c_str(), "r");
84 bam_hdr_t* hdr = sam_hdr_read(samfile);
85
86 // Exclude intervals
87 typedef boost::icl::interval_set<uint32_t> TChrIntervals;
88 typedef std::vector<TChrIntervals> TRegionsGenome;
89 TRegionsGenome validRegions;
90 if (!_parseExcludeIntervals(c, hdr, validRegions)) {
91 std::cerr << "Delly couldn't parse exclude intervals!" << std::endl;
92 bam_hdr_destroy(hdr);
93 sam_close(samfile);
94 return 1;
95 }
96
97 // SV Discovery
98 if (!c.hasVcfFile) {
99
100 // Structural Variant Candidates
101 typedef std::vector<StructuralVariantRecord> TVariants;
102 TVariants svc;
103
104 // Split-read store
105 typedef std::pair<int32_t, std::size_t> TPosRead;
106 typedef std::vector<SeqSlice> TSvPosVector;
107 typedef boost::unordered_map<TPosRead, TSvPosVector> TPosReadSV;
108 typedef std::vector<TPosReadSV> TGenomicPosReadSV;
109 TGenomicPosReadSV srStore(c.nchr, TPosReadSV());
110
111 // SV Discovery
112 _clusterSRReads(c, validRegions, svc, srStore);
113
114 // Assemble
115 assemble(c, validRegions, svc, srStore);
116
117 // Sort SVs
118 sort(svc.begin(), svc.end());
119
120 // Remove duplicates
121 StructuralVariantRecord lastSV;
122 for(typename TVariants::iterator svIter = svc.begin(); svIter != svc.end(); ++svIter) {
123 if ((svIter->srSupport == 0) && (svIter->peSupport == 0)) continue;
124 // Duplicate?
125 if (!svs.empty()) {
126 if ((lastSV.chr == svIter->chr) && (lastSV.chr2 == svIter->chr2) && (lastSV.svt == svIter->svt) && (std::abs(svIter->svStart - lastSV.svStart) < c.minRefSep) && (std::abs(svIter->svEnd - lastSV.svEnd) < c.minRefSep)) {
127 // Check length
128 int32_t lengthvar = std::min(0.1 * (svIter->svEnd - svIter->svStart), 0.1 * (lastSV.svEnd - lastSV.svStart));
129 int32_t lengthdiff = std::min(svIter->svEnd - svIter->svStart, lastSV.svEnd - lastSV.svStart);
130 if (lengthvar < 15) lengthvar = 15;
131 if (lengthdiff < lengthvar) continue;
132 }
133 }
134 lastSV = *svIter;
135 svs.push_back(*svIter);
136 }
137
138 // Sort
139 sort(svs.begin(), svs.end());
140
141 // Re-number SVs
142 uint32_t cliqueCount = 0;
143 for(typename TVariants::iterator svIt = svs.begin(); svIt != svs.end(); ++svIt, ++cliqueCount) svIt->id = cliqueCount;
144 } else vcfParse(c, hdr, svs);
145 // Clean-up
146 bam_hdr_destroy(hdr);
147 sam_close(samfile);
148
149 // Annotate junction reads
150 typedef std::vector<JunctionCount> TSVJunctionMap;
151 typedef std::vector<TSVJunctionMap> TSampleSVJunctionMap;
152 TSampleSVJunctionMap jctMap(c.files.size());
153
154 // Annotate spanning coverage
155 typedef std::vector<SpanningCount> TSVSpanningMap;
156 typedef std::vector<TSVSpanningMap> TSampleSVSpanningMap;
157 TSampleSVSpanningMap spanMap(c.files.size());
158
159 // Annotate coverage
160 typedef std::vector<ReadCount> TSVReadCount;
161 typedef std::vector<TSVReadCount> TSampleSVReadCount;
162 TSampleSVReadCount rcMap(c.files.size());
163
164 // Initialize count maps
165 for(uint32_t file_c = 0; file_c < c.files.size(); ++file_c) {
166 jctMap[file_c].resize(svs.size(), JunctionCount());
167 spanMap[file_c].resize(svs.size(), SpanningCount());
168 rcMap[file_c].resize(svs.size());
169 }
170
171 // SV Genotyping
172 genotypeLR(c, svs, jctMap, rcMap);
173
174 // VCF Output
175 vcfOutput(c, svs, jctMap, rcMap, spanMap);
176
177 #ifdef PROFILE
178 ProfilerStop();
179 #endif
180
181 // End
182 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
183 std::cerr << '[' << boost::posix_time::to_simple_string(now) << "] Done." << std::endl;;
184
185 return 0;
186 }
187
188 int tegua(int argc, char **argv) {
189 TeguaConfig c;
190
191 // Parameter
192 std::string svtype;
193 std::string scoring;
194 std::string mode;
195 boost::program_options::options_description generic("Generic options");
196 generic.add_options()
197 ("help,?", "show help message")
198 ("svtype,t", boost::program_options::value<std::string>(&svtype)->default_value("ALL"), "SV type to compute [DEL, INS, DUP, INV, BND, ALL]")
199 ("technology,y", boost::program_options::value<std::string>(&mode)->default_value("ont"), "seq. technology [pb, ont]")
200 ("genome,g", boost::program_options::value<boost::filesystem::path>(&c.genome), "genome fasta file")
201 ("exclude,x", boost::program_options::value<boost::filesystem::path>(&c.exclude), "file with regions to exclude")
202 ("outfile,o", boost::program_options::value<boost::filesystem::path>(&c.outfile), "BCF output file")
203 ;
204
205 boost::program_options::options_description disc("Discovery options");
206 disc.add_options()
207 ("alt-align,l", boost::program_options::value<boost::filesystem::path>(&c.altfile), "alternate alignments config file")
208 ("map-qual,q", boost::program_options::value<uint16_t>(&c.minMapQual)->default_value(1), "min. mapping quality")
209 ("minclip,c", boost::program_options::value<uint32_t>(&c.minClip)->default_value(25), "min. clipping length")
210 ("min-clique-size,z", boost::program_options::value<uint32_t>(&c.minCliqueSize)->default_value(3), "min. clique size")
211 ("minrefsep,m", boost::program_options::value<uint32_t>(&c.minRefSep)->default_value(30), "min. reference separation")
212 ("maxreadsep,n", boost::program_options::value<uint32_t>(&c.maxReadSep)->default_value(100), "max. read separation")
213 ;
214
215 boost::program_options::options_description cons("Consensus options");
216 cons.add_options()
217 ("max-reads,p", boost::program_options::value<uint32_t>(&c.maxReadPerSV)->default_value(15), "max. reads for consensus computation")
218 ("cons-window,w", boost::program_options::value<int32_t>(&c.minConsWindow)->default_value(1000), "consensus window")
219 ("flank-size,f", boost::program_options::value<int32_t>(&c.minimumFlankSize)->default_value(100), "min. flank size")
220 ("flank-quality,a", boost::program_options::value<float>(&c.flankQuality)->default_value(0.9), "min. flank quality")
221 ("indel-size,i", boost::program_options::value<int32_t>(&c.indelsize)->default_value(10000), "use exact alleles for InDels <10kbp")
222 ;
223
224 boost::program_options::options_description geno("Genotyping options");
225 geno.add_options()
226 ("vcffile,v", boost::program_options::value<boost::filesystem::path>(&c.vcffile), "input VCF/BCF file for genotyping")
227 ("geno-qual,u", boost::program_options::value<uint16_t>(&c.minGenoQual)->default_value(5), "min. mapping quality for genotyping")
228 ("max-geno-count,b", boost::program_options::value<uint32_t>(&c.maxGenoReadCount)->default_value(250), "max. reads aligned for SR genotyping")
229 ("dump,d", boost::program_options::value<boost::filesystem::path>(&c.dumpfile), "gzipped output file for SV-reads")
230 ;
231
232 boost::program_options::options_description hidden("Hidden options");
233 hidden.add_options()
234 ("input-file", boost::program_options::value< std::vector<boost::filesystem::path> >(&c.files), "input file")
235 ("pruning,j", boost::program_options::value<uint32_t>(&c.graphPruning)->default_value(1000), "graph pruning cutoff")
236 ("extension,e", boost::program_options::value<float>(&c.indelExtension)->default_value(0.5), "enforce indel extension")
237 ("scoring,s", boost::program_options::value<std::string>(&scoring)->default_value("3,-2,-3,-1"), "alignment scoring")
238 ;
239
240 boost::program_options::positional_options_description pos_args;
241 pos_args.add("input-file", -1);
242
243 boost::program_options::options_description cmdline_options;
244 cmdline_options.add(generic).add(disc).add(cons).add(geno).add(hidden);
245 boost::program_options::options_description visible_options;
246 visible_options.add(generic).add(disc).add(cons).add(geno);
247 boost::program_options::variables_map vm;
248 boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(cmdline_options).positional(pos_args).run(), vm);
249 boost::program_options::notify(vm);
250
251 // Check command line arguments
252 if ((vm.count("help")) || (!vm.count("input-file")) || (!vm.count("genome"))) {
253 std::cerr << std::endl;
254 std::cerr << "Usage: delly " << argv[0] << " [OPTIONS] -g <ref.fa> <sample1.sort.bam> <sample2.sort.bam> ..." << std::endl;
255 std::cerr << visible_options << "\n";
256 return 0;
257 }
258
259 // Set alignment score
260 _alignmentScore(c, scoring);
261
262 // SV types to compute?
263 if (!_svTypesToCompute(c, svtype)) {
264 std::cerr << "Please specify a valid SV type, i.e., -t INV or -t DEL,INV without spaces." << std::endl;
265 return 1;
266 }
267
268 // Dump reads
269 if (vm.count("dump")) c.hasDumpFile = true;
270 else c.hasDumpFile = false;
271
272 // Clique size
273 if (c.minCliqueSize < 2) c.minCliqueSize = 2;
274
275 // Alternate alignments
276 if (vm.count("alt-align")) {
277 if (!_checkAlternateAlignments(c)) {
278 std::cerr << "Invalid alternate alignments config file!" << std::endl;
279 return 1;
280 }
281 c.hasAltFile = true;
282 } else c.hasAltFile = false;
283
284 // Check reference
285 if (!(boost::filesystem::exists(c.genome) && boost::filesystem::is_regular_file(c.genome) && boost::filesystem::file_size(c.genome))) {
286 std::cerr << "Reference file is missing: " << c.genome.string() << std::endl;
287 return 1;
288 } else {
289 faidx_t* fai = fai_load(c.genome.string().c_str());
290 if (fai == NULL) {
291 if (fai_build(c.genome.string().c_str()) == -1) {
292 std::cerr << "Fail to open genome fai index for " << c.genome.string() << std::endl;
293 return 1;
294 } else fai = fai_load(c.genome.string().c_str());
295 }
296 fai_destroy(fai);
297 }
298
299 // Check input files
300 c.sampleName.resize(c.files.size());
301 c.nchr = 0;
302 for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) {
303 if (!(boost::filesystem::exists(c.files[file_c]) && boost::filesystem::is_regular_file(c.files[file_c]) && boost::filesystem::file_size(c.files[file_c]))) {
304 std::cerr << "Alignment file is missing: " << c.files[file_c].string() << std::endl;
305 return 1;
306 }
307 samFile* samfile = sam_open(c.files[file_c].string().c_str(), "r");
308 if (samfile == NULL) {
309 std::cerr << "Fail to open file " << c.files[file_c].string() << std::endl;
310 return 1;
311 }
312 hts_idx_t* idx = sam_index_load(samfile, c.files[file_c].string().c_str());
313 if (idx == NULL) {
314 std::cerr << "Fail to open index for " << c.files[file_c].string() << std::endl;
315 return 1;
316 }
317 bam_hdr_t* hdr = sam_hdr_read(samfile);
318 if (hdr == NULL) {
319 std::cerr << "Fail to open header for " << c.files[file_c].string() << std::endl;
320 return 1;
321 }
322 if (!c.nchr) c.nchr = hdr->n_targets;
323 else {
324 if (c.nchr != hdr->n_targets) {
325 std::cerr << "BAM files have different number of chromosomes!" << std::endl;
326 return 1;
327 }
328 }
329 faidx_t* fai = fai_load(c.genome.string().c_str());
330 for(int32_t refIndex=0; refIndex < hdr->n_targets; ++refIndex) {
331 std::string tname(hdr->target_name[refIndex]);
332 if (!faidx_has_seq(fai, tname.c_str())) {
333 std::cerr << "BAM file chromosome " << hdr->target_name[refIndex] << " is NOT present in your reference file " << c.genome.string() << std::endl;
334 return 1;
335 }
336 }
337 fai_destroy(fai);
338 std::string sampleName = "unknown";
339 getSMTag(std::string(hdr->text), c.files[file_c].stem().string(), sampleName);
340 c.sampleName[file_c] = sampleName;
341 bam_hdr_destroy(hdr);
342 hts_idx_destroy(idx);
343 sam_close(samfile);
344 }
345 checkSampleNames(c);
346
347 // Check exclude file
348 if (vm.count("exclude")) {
349 if (!(boost::filesystem::exists(c.exclude) && boost::filesystem::is_regular_file(c.exclude) && boost::filesystem::file_size(c.exclude))) {
350 std::cerr << "Exclude file is missing: " << c.exclude.string() << std::endl;
351 return 1;
352 }
353 c.hasExcludeFile = true;
354 } else c.hasExcludeFile = false;
355
356 // Check input VCF file
357 if (vm.count("vcffile")) {
358 if (!(boost::filesystem::exists(c.vcffile) && boost::filesystem::is_regular_file(c.vcffile) && boost::filesystem::file_size(c.vcffile))) {
359 std::cerr << "Input VCF/BCF file is missing: " << c.vcffile.string() << std::endl;
360 return 1;
361 }
362 htsFile* ifile = bcf_open(c.vcffile.string().c_str(), "r");
363 if (ifile == NULL) {
364 std::cerr << "Fail to open file " << c.vcffile.string() << std::endl;
365 return 1;
366 }
367 bcf_hdr_t* hdr = bcf_hdr_read(ifile);
368 if (hdr == NULL) {
369 std::cerr << "Fail to open index file " << c.vcffile.string() << std::endl;
370 return 1;
371 }
372 bcf_hdr_destroy(hdr);
373 bcf_close(ifile);
374 c.hasVcfFile = true;
375 } else c.hasVcfFile = false;
376
377 // Check outfile
378 if (!vm.count("outfile")) c.outfile = "-";
379 else {
380 if (c.outfile.string() != "-") {
381 if (!_outfileValid(c.outfile)) return 1;
382 }
383 }
384
385 // Show cmd
386 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
387 std::cerr << '[' << boost::posix_time::to_simple_string(now) << "] ";
388 std::cerr << "delly ";
389 for(int i=0; i<argc; ++i) { std::cerr << argv[i] << ' '; }
390 std::cerr << std::endl;
391
392 // Run Tegua
393 if (mode == "pb") c.indelExtension = 0.7;
394 else if (mode == "ont") c.indelExtension = 0.5;
395 return runTegua(c);
396 }
397
398 }
399
400 #endif