1 module test.vcfwriter; 2 3 import std.stdio; 4 5 import dhtslib.vcf; 6 import dhtslib.htslib.vcf; 7 8 int main() 9 { 10 writeln("dhtslib ⚡ VCFWriter"); 11 12 VCFWriter w = VCFWriter("output.vcf"); 13 14 w.addHeaderLineRaw("##source=dhtslib-vcfwriterV0.4"); 15 w.addHeaderLineKV("phasing", "none"); 16 w.addHeaderLineKV("contig", "<ID=chr3,length=999999,assembly=hg19>"); 17 //w.addSample("SAMPLE01"); 18 19 // These should be equivalent: raw htslib call, templated fn 20 bcf_hdr_append(w.vcfhdr.hdr, "##FILTER=<ID=triallelic,Description=\"Triallelic site\">"); 21 w.addTag!"FILTER"("noisy", "Noisy region"); 22 23 w.addTag!"INFO"("NS", "1", "Integer", "Number of Samples With Data"); 24 w.addTag!"INFO"("XFL", "1", "Float", "Floating point number(s)"); 25 w.addTag!"INFO"("XF", "1", "Flag", "Bool something something mumble"); 26 27 // These should be equivalent: raw htslib call, templated fn with string 2nd param, templated fn with int 2nd param 28 bcf_hdr_append(w.vcfhdr.hdr, "##FORMAT=<ID=DP,Number=1,Type=Integer,Description=\"Read Depth\">"); 29 w.addTag!"FORMAT"("DDP", "1", "Integer", "De-Duplicated Read Depth (Diamond Dallas Page)"); 30 w.addTag!"FORMAT"("XDP", 1, "Integer", "X depth"); // pass integer as second param instead of string 31 32 // Test vector valued tags 33 w.addTag!"FORMAT"("XXX", 2, "Integer", "Array test"); 34 35 w.addSample("Sample1"); 36 w.addSample("Sample2"); 37 38 w.writeHeader(); 39 auto vcfhdr = w.getHeader(); 40 41 // for genotype/format tag value arrays 42 const int x = 100; 43 const int y = 200; 44 const int z = 300; 45 const int zz= 400; 46 47 //string[] filters = ["TRIALLELIC", "GOATS"]; 48 string[] filters = ["PASS", "triallelic", "nonex"]; 49 VCFRecord r = new VCFRecord(vcfhdr, "chr3", 999, "rs321", "C", "T", 40, filters); 50 r.addInfo("NS", 1); 51 r.addInfo("XS", "Hello"); 52 r.addInfo("XFL", 2.1); 53 r.addFormat("DP", [x,x] ); 54 w.writeRecord(r); 55 56 r = new VCFRecord(vcfhdr, "chr3", 1001, "", "", "", 30, ""); 57 r.setAlleles("A", "G"); 58 r.addInfo("XF", true); 59 r.addFormat("DP", [x,x] ); 60 w.writeRecord(r); 61 62 r = new VCFRecord(vcfhdr, "chr3", 1002, "", "", "", 30, "PASS"); 63 r.setAlleles("A", "T", "TCGA"); 64 r.addFormat("DP", [x,x] ); 65 r.addFormat("XDP", [x,y]); 66 w.writeRecord(r); 67 68 r = new VCFRecord(vcfhdr, "chr3", 1003, "", "A", "G", 30, "PASS"); 69 r.addFormat("XXX", [x, y, z, zz]); 70 w.writeRecord(r); 71 72 r = new VCFRecord(vcfhdr, "chr3", 1004, "", "G", "A", 20, "PASS"); 73 r.add!"FORMAT"("DP", [x,y]); 74 //r.addValue!("FORMAT", int)("DP", [x,y]); 75 w.writeRecord(r); 76 /+ 77 filters = []; 78 w.addRecord("chr3", 999, "", "C,T", 40, filters); 79 w.addRecord("chr3", 999999, ".", "A,T", 29.5, filters); 80 w.addRecord("chr3", 1000000, ".", "A,AC", 111.222, filters); 81 w.addRecord("chr3", 440, ".", "CG,C", 30, filters); 82 83 //w.addRecord("chr1", 999, ".", "A,G", 30, filters); 84 85 writeln(*w.rows[0]); 86 87 bcf1_t *b = new bcf1_t; 88 b.pos = 111; 89 90 w.addRecord(b); 91 92 w.writeFile(); 93 +/ 94 95 96 return 0; 97 }