Debian Codegraph @

src/bolog.h

raw symbols SCIP
1 #ifndef BOLOG_H
2 #define BOLOG_H
3
4 #include <boost/math/special_functions/round.hpp>
5 #include <boost/math/distributions/normal.hpp>
6
7 namespace torali {
8
9 #define SMALLEST_GL -1000
10
11 template<typename TPrecision>
12 struct BoLog {
13 typedef TPrecision value_type;
14
15 std::vector<TPrecision> phred2prob;
16
17 BoLog() {
18 for(int i = 0; i <= boost::math::round(-10 * SMALLEST_GL); ++i) phred2prob.push_back(std::pow(TPrecision(10), -(TPrecision(i)/TPrecision(10))));
19 }
20 };
21
22
23 template<typename TBoLog, typename TMapqVector>
24 inline void
25 _computeGLs(TBoLog const& bl, TMapqVector const& mapqRef, TMapqVector const& mapqAlt, float* gls, int32_t* gqval, int32_t* gts, int const file_c) {
26 typedef typename TBoLog::value_type FLP;
27 FLP gl[3];
28
29 // Compute genotype likelihoods
30 for(unsigned int geno=0; geno<=2; ++geno) gl[geno]=0;
31 unsigned int peDepth=mapqRef.size() + mapqAlt.size();
32 for(typename TMapqVector::const_iterator mapqRefIt = mapqRef.begin();mapqRefIt!=mapqRef.end();++mapqRefIt) {
33 gl[0] += std::log10(bl.phred2prob[*mapqRefIt]);
34 gl[1] += std::log10(bl.phred2prob[*mapqRefIt] + (FLP(1) - bl.phred2prob[*mapqRefIt]));
35 gl[2] += std::log10(FLP(1) - bl.phred2prob[*mapqRefIt]);
36 }
37 for(typename TMapqVector::const_iterator mapqAltIt = mapqAlt.begin();mapqAltIt!=mapqAlt.end();++mapqAltIt) {
38 gl[0] += std::log10(FLP(1) - bl.phred2prob[*mapqAltIt]);
39 gl[1] += std::log10((FLP(1) - bl.phred2prob[*mapqAltIt]) + bl.phred2prob[*mapqAltIt]);
40 gl[2] += std::log10(bl.phred2prob[*mapqAltIt]);
41 }
42 gl[1] += -FLP(peDepth) * std::log10(FLP(2));
43 unsigned int glBest=0;
44 FLP glBestVal=gl[glBest];
45 for(unsigned int geno=1; geno<=2; ++geno) {
46 if (gl[geno] > glBestVal) {
47 glBestVal=gl[geno];
48 glBest = geno;
49 }
50 }
51 // Rescale by best genotype
52 for(unsigned int geno=0; geno<=2; ++geno) {
53 gl[geno] -= glBestVal;
54 // Cap at smallest GL
55 gl[geno] = (gl[geno] > SMALLEST_GL) ? gl[geno] : SMALLEST_GL;
56 }
57
58 // Phred-scaled genotype likelihoods
59 uint32_t pl[3];
60 pl[0] = (uint32_t) boost::math::round(-10 * gl[0]);
61 pl[1] = (uint32_t) boost::math::round(-10 * gl[1]);
62 pl[2] = (uint32_t) boost::math::round(-10 * gl[2]);
63 if ((peDepth) && (pl[0] + pl[1] + pl[2] > 0)) {
64 FLP likelihood = (FLP) std::log10((1-1/(bl.phred2prob[pl[0]]+bl.phred2prob[pl[1]]+bl.phred2prob[pl[2]])));
65 likelihood = (likelihood > SMALLEST_GL) ? likelihood : SMALLEST_GL;
66 gqval[file_c] = (int32_t) boost::math::round(-10 * likelihood);
67 if (glBest==0) {
68 gts[file_c * 2] = bcf_gt_unphased(1);
69 gts[file_c * 2 + 1] = bcf_gt_unphased(1);
70 } else if (glBest==1) {
71 gts[file_c * 2] = bcf_gt_unphased(0);
72 gts[file_c * 2 + 1] = bcf_gt_unphased(1);
73 } else {
74 gts[file_c * 2] = bcf_gt_unphased(0);
75 gts[file_c * 2 + 1] = bcf_gt_unphased(0);
76 }
77 } else {
78 gts[file_c * 2] = bcf_gt_missing;
79 gts[file_c * 2 + 1] = bcf_gt_missing;
80 gqval[file_c] = 0;
81 }
82 gls[file_c * 3 + 2] = (float) gl[0];
83 gls[file_c * 3 + 1] = (float) gl[1];
84 gls[file_c * 3] = (float) gl[2];
85 }
86
87
88 template<typename TConfig>
89 inline int32_t
90 _computeCNLs(TConfig const& c, double const mean, double const sd, float* gl, int32_t* gqval, int32_t const file_c) {
91 // Compute copy-number likelihoods
92 boost::math::normal s(mean, sd);
93 for(uint32_t geno=0; geno< MAX_CN; ++geno) {
94 double prob = boost::math::pdf(s, geno);
95 gl[file_c * MAX_CN + geno] = std::log10(prob);
96 gl[file_c * MAX_CN + geno] = (gl[file_c * MAX_CN + geno] > SMALLEST_GL) ? gl[file_c * MAX_CN + geno] : SMALLEST_GL;
97 }
98 uint32_t glBest=file_c * MAX_CN + 0;
99 uint32_t glBest2nd=file_c * MAX_CN + 1;
100 if (gl[glBest] < gl[glBest2nd]) {
101 glBest = file_c * MAX_CN + 1;
102 glBest2nd = file_c * MAX_CN + 0;
103 }
104 for(uint32_t geno=2; geno < MAX_CN; ++geno) {
105 if (gl[file_c * MAX_CN + geno] > gl[glBest2nd]) {
106 if (gl[file_c * MAX_CN + geno] > gl[glBest]) {
107 glBest2nd = glBest;
108 glBest = file_c * MAX_CN + geno;
109 } else {
110 glBest2nd = file_c * MAX_CN + geno;
111 }
112 }
113 }
114
115 // Variant quality
116 double glObs = std::log10(boost::math::pdf(s, mean));
117 glObs = (glObs > SMALLEST_GL) ? glObs : SMALLEST_GL;
118 uint32_t plVariant = (uint32_t) boost::math::round(-10 * glObs);
119 uint32_t plPloidy = (uint32_t) boost::math::round(-10 * gl[file_c * MAX_CN + boost::math::iround(c.ploidy)]);
120 int32_t varqual = plPloidy - plVariant;
121
122 // GQ
123 uint32_t plBest = (uint32_t) boost::math::round(-10 * gl[glBest]);
124 uint32_t plBest2nd = (uint32_t) boost::math::round(-10 * gl[glBest2nd]);
125 gqval[file_c] = plBest2nd - plBest;
126
127 // Rescale by best genotype
128 double glBestVal = gl[glBest];
129 for(uint32_t geno=0; geno< MAX_CN; ++geno) gl[file_c * MAX_CN + geno] -= glBestVal;
130
131 // Variant quality
132 return varqual;
133 }
134
135 template<typename TConfig>
136 inline int32_t
137 _computeCNLs(TConfig const& c, double const mean, double const sd, float* gl, int32_t* gqval) {
138 return _computeCNLs(c, mean, sd, gl, gqval, 0);
139 }
140
141 }
142
143 #endif