Debian Codegraph @

src/vcf_writer.cpp

raw symbols SCIP
1 #include "vcf_writer.h"
2
3 int vcf_writer::init_header() {
4 this->hdr = bcf_hdr_init("w");
5 int res;
6 res = bcf_hdr_append(
7 hdr,
8 "##ALT=<ID=*,Description=\"Represents allele(s) other than observed.\">");
9 res = bcf_hdr_append(
10 hdr,
11 "##INFO=<ID=DP,Number=1,Type=Integer,Description=\"Raw read depth\">");
12 res = bcf_hdr_append(
13 hdr,
14 "##INFO=<ID=AD,Number=R,Type=Integer,Description=\"Total read depth for "
15 "each allele (based on minimum quality threshold)\">");
16 res = bcf_hdr_append(
17 hdr,
18 "##INFO=<ID=ADF,Number=R,Type=Integer,Description=\"Total read depth for "
19 "each allele on forward strand (based on minimum quality threshold)\">");
20 res = bcf_hdr_append(
21 hdr,
22 "##INFO=<ID=ADR,Number=R,Type=Integer,Description=\"Total read depth for "
23 "each allele on reverse strand (based on minimum quality threshold)\">");
24 res = bcf_hdr_append(
25 hdr,
26 "##INFO=<ID=AF,Number=A,Type=Float,Description=\"Allele frequency for "
27 "each ALT allele in the same order as listed (estimated from primary "
28 "data, not called genotypes)\">");
29 res = bcf_hdr_append(
30 hdr, ("##contig=<ID=cref,assembly=" + this->region + ">").c_str());
31 res = bcf_hdr_append(
32 hdr, "##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">");
33 res = bcf_hdr_append(hdr,
34 "##FORMAT=<ID=GQ,Number=1,Type=Integer,Description="
35 "\"Genotype Quality\">");
36 res = bcf_hdr_append(hdr,
37 "##FILTER=<ID=fobs,Description=\"Fisher's exact test to "
38 "check if frequency of the iSNV is significantly higher "
39 "than the mean error rate at that position\">");
40 bcf_hdr_add_sample(hdr, this->sample_name.c_str());
41 if (res != 0) return -1;
42 return 0;
43 }
44
45 int vcf_writer::init(char _mode, std::string fname, std::string region,
46 std::string sample_name, std::string ref_path) {
47 this->ref = new ref_antd(ref_path);
48 this->region = region;
49 this->sample_name = sample_name;
50 std::string mode = "w";
51 mode += _mode;
52 vcfFile *vcf_file = bcf_open(fname.c_str(), mode.c_str());
53 this->init_header();
54 if (hdr == NULL || vcf_file == NULL) {
55 std::cout << "Unable to write BCF/VCF file" << std::endl;
56 return -1;
57 }
58 if (bcf_hdr_write(vcf_file, hdr) < 0) {
59 std::cout << "Unable to write BCF/VCF file" << std::endl;
60 return -1;
61 }
62 return 0;
63 }
64
65 int vcf_writer::write_record(uint32_t pos, std::vector<allele> &alleles,
66 char ref) {
67 bcf1_t *rec = bcf_init();
68 rec->rid = rec->rid = bcf_hdr_name2id(this->hdr, this->region.c_str());
69 rec->pos = pos;
70 allele aref = alleles.at(find_ref_in_allele(alleles, ref));
71 rec->qual = aref.mean_qual;
72 }
73
74 int main(int argc, char *argv[]) {
75 vcfFile *vcf_file = bcf_open("../data/test.vcf", "w");
76 bcf_hdr_t *h = bcf_hdr_init("w");
77 int res;
78
79 if (res < 0) std::cout << "Unable to write BCF header" << std::endl;
80 // Add sample
81 bcf_hdr_add_sample(h, "TEST");
82 // bcf_hdr_add_sample(h, NULL);
83 bcf_hdr_write(vcf_file, h);
84 // Write record
85 bcf1_t *rec = bcf_init();
86 rec->rid = rec->rid = bcf_hdr_name2id(h, "20");
87 rec->pos = 0;
88 rec->qual = 20;
89 // ID
90 bcf_update_id(h, rec, "rs6054257");
91 // INFO
92 bcf_update_alleles_str(h, rec, "G,A,TA");
93 // FILTER
94 int32_t tmpi = bcf_hdr_id2int(h, BCF_DT_ID, "PASS");
95 bcf_update_filter(h, rec, &tmpi, 1);
96 // FORMAT
97 int32_t *tmpia = (int *)malloc(bcf_hdr_nsamples(h) * 2 * sizeof(int));
98 tmpia[0] = bcf_gt_phased(0);
99 tmpia[1] = bcf_gt_phased(0);
100 bcf_update_genotypes(h, rec, &tmpi, bcf_hdr_nsamples(h));
101 tmpia[0] = 12;
102 tmpia[1] = 4;
103 bcf_update_format_int32(h, rec, "GQ", tmpia, bcf_hdr_nsamples(h));
104 tmpi = 1;
105 bcf_update_info_int32(h, rec, "NS", &tmpi, 1);
106 if (res < 0) std::cout << "Alleles not updated" << std::endl;
107 int32_t td = 12;
108 int32_t ad = 4;
109 bcf_update_info_int32(h, rec, "DP", &td, 1);
110 bcf_update_info_int32(h, rec, "AD", &ad, 1);
111 std::cout << bcf_hdr_nsamples(h) << std::endl;
112 std::cout << rec->n_sample << std::endl;
113 bcf_write1(vcf_file, h, rec);
114 bcf_hdr_destroy(h);
115 bcf_close(vcf_file);
116 return 0;
117 }