Debian Codegraph @

src/C++/Quiver/Diploid.cpp

raw
1 // Author: David Alexander
2
3 #include <ConsensusCore/Quiver/Diploid.hpp>
4
5 #include <ConsensusCore/Mutation.hpp>
6
7 #include <algorithm>
8 #include <cassert>
9 #include <cfloat>
10 #include <cmath>
11 #include <iostream>
12 #include <numeric>
13 #include <utility>
14 #include <vector>
15
16 #include <boost/numeric/ublas/matrix.hpp>
17 #include <boost/numeric/ublas/matrix_expression.hpp>
18 #include <boost/numeric/ublas/matrix_proxy.hpp>
19 #include <boost/numeric/ublas/vector.hpp>
20
21 using std::vector;
22 using std::accumulate;
23 using std::pair;
24 using std::make_pair;
25 using std::cout;
26 using std::endl;
27
28 typedef boost::numeric::ublas::vector<float> fvec;
29 typedef boost::numeric::ublas::matrix<float> fmat;
30 typedef boost::numeric::ublas::matrix_column<const fmat> fmat_column;
31
32 #if defined(_MSC_VER) && _MSC_VER < 1800
33
34 // compute log(1+x) without losing precision for small values of x
35 float log1p(float x)
36 {
37 if (x <= -1.0) {
38 std::stringstream os;
39 os << "Invalid input argument (" << x << "); must be greater than -1.0";
40 throw std::invalid_argument(os.str());
41 }
42
43 if (fabs(x) > 1e-4) {
44 // x is large enough that the obvious evaluation is OK
45 return log(1.0 + x);
46 }
47
48 // Use Taylor approx. log(1 + x) = x - x^2/2 with error roughly x^3/3
49 // Since |x| < 10^-4, |x|^3 < 10^-12, relative error less than 10^-8
50
51 return (-0.5 * x + 1.0) * x;
52 }
53
54 #endif
55
56 namespace ConsensusCore {
57
58 // This needs to be configurable.
59 DEBUG_ONLY(const int MUTATIONS_PER_SITE = 9;) // NOLINT
60 const int LENGTH_DIFFS[] = {0, 0, 0, 0, 1, 1, 1, 1, -1};
61
62 DiploidSite::DiploidSite(int allele0, int allele1, float logBayesFactor,
63 std::vector<int> alleleForRead)
64 : Allele0(allele0)
65 , Allele1(allele1)
66 , LogBayesFactor(logBayesFactor)
67 , AlleleForRead(alleleForRead)
68 {
69 }
70
71 static inline float logaddexp(float x, float y)
72 {
73 float diff = x - y;
74 if (diff > 0) {
75 return x + std::log1p(std::exp(-diff));
76 } else {
77 return y + std::log1p(std::exp(diff));
78 }
79 }
80
81 //
82 // Computes Pr(R | hom)
83 //
84 static float HomozygousLogLikelihood(const fmat& siteScores)
85 {
86 int G = siteScores.size2();
87 fvec gScores(G);
88 for (int g = 0; g < G; g++) {
89 gScores(g) = sum(column(siteScores, g));
90 }
91 return accumulate(gScores.begin(), gScores.end(), -FLT_MAX, logaddexp);
92 }
93
94 //
95 // Computes: Pr(R | het)
96 //
97 static float HeterozygousLogLikelihood(const fmat& siteScores, int* allele0, int* allele1)
98 {
99 assert(siteScores.size2() == MUTATIONS_PER_SITE);
100
101 int I = siteScores.size1();
102 int G = siteScores.size2();
103 float log2 = log(2);
104
105 vector<float> varScores;
106 float runningMax = -FLT_MAX;
107 int runningAllele0 = -1;
108 int runningAllele1 = -1;
109 for (int g0 = 0; g0 < G; g0++) {
110 for (int g1 = g0 + 1; g1 < G; g1++) {
111 if (LENGTH_DIFFS[g0] == LENGTH_DIFFS[g1]) {
112 float total = -I * log2;
113 for (int i = 0; i < I; i++) {
114 total += logaddexp(siteScores(i, g0), siteScores(i, g1));
115 }
116 varScores.push_back(total);
117 if (total > runningMax) {
118 runningMax = total;
119 runningAllele0 = g0;
120 runningAllele1 = g1;
121 }
122 }
123 }
124 }
125 if (allele0 != NULL && allele1 != NULL) {
126 *allele0 = runningAllele0;
127 *allele1 = runningAllele1;
128 }
129 return accumulate(varScores.begin(), varScores.end(), -FLT_MAX, logaddexp);
130 }
131
132 static inline fmat ToMatrix(const float* siteScores, int dim1, int dim2)
133 {
134 // Kind of kludgy. I blame ublas--a pretty crummy matrix library.
135 fmat M(dim1, dim2);
136 std::copy(siteScores, siteScores + dim1 * dim2, M.data().begin());
137 return M;
138 }
139
140 #if 0
141 static void PrintMatrix(const fmat& siteScores)
142 {
143 for (int i = 0; i < (int)siteScores.size1(); i++)
144 {
145 for (int j = 0; j < (int)siteScores.size2(); j++)
146 {
147 cout << siteScores(i, j) << " ";
148 }
149 cout << endl;
150 }
151 cout << endl;
152 }
153 #endif // 0
154
155 vector<int> AssignReadsToAlleles(const fmat& siteScores, int allele0, int allele1)
156 {
157 int I = siteScores.size1();
158 vector<int> assignment(I, -1);
159 for (int i = 0; i < I; i++) {
160 assignment[i] = (siteScores(i, allele0) > siteScores(i, allele1) ? 0 : 1);
161 }
162 return assignment;
163 }
164
165 // Is the site detected as a heterozygote?
166 // - If not, return NULL.
167 // - If so, return a pointer to a new DiploidSite object
168 // logPriorRatio >= 0 is log {Pr(hom)/Pr(het)}
169 DiploidSite* IsSiteHeterozygous(const float* siteScores, int dim1, int dim2, float logPriorRatio)
170 {
171 // First column of siteScores must correspond to no-op mutation.
172 int allele0, allele1;
173
174 fmat M = ToMatrix(siteScores, dim1, dim2);
175 float homScore = HomozygousLogLikelihood(M);
176 float hetScore = HeterozygousLogLikelihood(M, &allele0, &allele1);
177 float logBF = hetScore - homScore;
178
179 if (logBF - logPriorRatio > 0) {
180 return new DiploidSite(allele0, allele1, logBF, AssignReadsToAlleles(M, allele0, allele1));
181 } else {
182 return NULL;
183 }
184 }
185 }