1 /* The MIT License
2 
3    Copyright (c) 2015 Adrian Tan <atks@umich.edu>
4 
5    Permission is hereby granted, free of charge, to any person obtaining a copy
6    of this software and associated documentation files (the "Software"), to deal
7    in the Software without restriction, including without limitation the rights
8    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9    copies of the Software, and to permit persons to whom the Software is
10    furnished to do so, subject to the following conditions:
11 
12    The above copyright notice and this permission notice shall be included in
13    all copies or substantial portions of the Software.
14 
15    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21    THE SOFTWARE.
22 */
23 
24 #ifndef BCF_SINGLE_GENOTYPING_BUFFERED_READER_H
25 #define BCF_SINGLE_GENOTYPING_BUFFERED_READER_H
26 
27 #include "hts_utils.h"
28 #include "utils.h"
29 #include "genotyping_record.h"
30 #include "snp_genotyping_record.h"
31 #include "indel_genotyping_record.h"
32 #include "vntr_genotyping_record.h"
33 #include "bcf_ordered_reader.h"
34 #include "bcf_ordered_writer.h"
35 #include "variant.h"
36 #include "variant_manip.h"
37 #include "log_tool.h"
38 #include "augmented_bam_record.h"
39 
40 /**
41  * Wrapper for BCFOrderedReader.
42  *
43  * VCF records are wrapped in GenotyingRecord and are
44  * maintained in a buffer.
45  *
46  */
47 class BCFSingleGenotypingBufferedReader
48 {
49     public:
50 
51     ///////
52     //i/o//
53     ///////
54     BCFOrderedReader* odr;
55     BCFOrderedWriter* odw;
56 
57     //////////////////
58     //buffer related//
59     //////////////////
60     std::list<GenotypingRecord*> buffer;
61     std::string chrom;
62     AugmentedBAMRecord as;
63 
64     Variant variant;
65 
66     ///////////
67     //options//
68     ///////////
69     bool output_annotations;
70 
71     /////////
72     //stats//
73     /////////
74     uint32_t no_snps_genotyped;
75     uint32_t no_indels_genotyped;
76     uint32_t no_vntrs_genotyped;
77 
78     /////////
79     //tools//
80     /////////
81     VariantManip *vm;
82     LogTool lt;
83     faidx_t *fai;
84 
85     /**
86      * Constructor.
87      */
88     BCFSingleGenotypingBufferedReader(std::string input_vcf_file, std::vector<GenomeInterval>& intervals, std::string output_vcf_file);
89 
90     /**
91      * Collects sufficient statistics from read for variants to be genotyped.
92      */
93     void process_read(bam_hdr_t *h, bam1_t *s);
94 
95     /**
96      * Compute SNP genotype likelihoods in PHRED scale.
97      */
98     void compute_snp_pl(std::vector<int32_t>& alleles,
99                         std::vector<uint32_t>& quals,
100                         uint32_t ploidy, uint32_t no_alleles,
101                         std::vector<uint32_t>& pls,
102                         float& pl_offset);
103 
104     /**
105      * Compute Indel genotype likelihoods in PHRED scale.
106      */
107     void compute_indel_pl(std::vector<int32_t>& alleles,
108                           std::vector<uint32_t>& quals,
109                           uint32_t ploidy, uint32_t no_alleles,
110                           std::vector<uint32_t>& pls);
111 
112     /**
113      * Compute Indel allele likelihoods in PHRED scale.
114      */
115     void compute_indel_al(char lflank_state[], uint32_t lflank_qual[],
116                           char rflank_state[], uint32_t rflank_qual[],
117                           std::vector<std::string>& alleles,
118                           std::string& obs_indel,
119                           std::vector<uint32_t>& aqs,
120                           std::vector<int32_t>& als,
121                           std::string& dls);
122 
123     /**
124      * Collects sufficient statistics from read for variants to be genotyped.
125      */
126     void collect_sufficient_statistics(GenotypingRecord *g,  AugmentedBAMRecord& as);
127 
128     /**
129      * Flush records.
130      */
131     void flush(bam_hdr_t *h, bam1_t *s, bool flush_all=false);
132 
133     /**
134      * Genotype variant and print to odw.
135      */
136     void genotype_and_print(GenotypingRecord* g);
137 
138     /**
139      * Create appropriate genotyping record.
140      */
141     GenotypingRecord* create_genotyping_record(bcf_hdr_t* h, bcf1_t* v, uint32_t ploidy, Variant& variant);
142 };
143 
144 #endif