Debian Codegraph @

src/third-party/htslib/kfunc.cpp

raw
1 /* The MIT License
2 Copyright (C) 2010, 2013-2014, 2020 Genome Research Ltd.
3 Copyright (C) 2011 Attractive Chaos <[email protected]>
4 Permission is hereby granted, free of charge, to any person obtaining
5 a copy of this software and associated documentation files (the
6 "Software"), to deal in the Software without restriction, including
7 without limitation the rights to use, copy, modify, merge, publish,
8 distribute, sublicense, and/or sell copies of the Software, and to
9 permit persons to whom the Software is furnished to do so, subject to
10 the following conditions:
11 The above copyright notice and this permission notice shall be
12 included in all copies or substantial portions of the Software.
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
17 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #include <pbcopper/third-party/htslib/kfunc.h>
24
25 #include <boost/math/special_functions/gamma.hpp>
26
27 #include <math.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30
31 // log\binom{n}{k}
32 static double lbinom(int n, int k)
33 {
34 if (k == 0 || n == k) return 0;
35 namespace bm = boost::math;
36 return bm::lgamma(n+1) - bm::lgamma(k+1) - bm::lgamma(n-k+1);
37 }
38
39 // n11 n12 | n1_
40 // n21 n22 | n2_
41 //-----------+----
42 // n_1 n_2 | n
43
44 // hypergeometric distribution
45 static double hypergeo(int n11, int n1_, int n_1, int n)
46 {
47 return exp(lbinom(n1_, n11) + lbinom(n-n1_, n_1-n11) - lbinom(n, n_1));
48 }
49
50 typedef struct {
51 int n11, n1_, n_1, n;
52 double p;
53 } hgacc_t;
54
55 // incremental version of hypergenometric distribution
56 static double hypergeo_acc(int n11, int n1_, int n_1, int n, hgacc_t *aux)
57 {
58 if (n1_ || n_1 || n) {
59 aux->n11 = n11; aux->n1_ = n1_; aux->n_1 = n_1; aux->n = n;
60 } else { // then only n11 changed; the rest fixed
61 if (n11%11 && n11 + aux->n - aux->n1_ - aux->n_1) {
62 if (n11 == aux->n11 + 1) { // incremental
63 aux->p *= (double)(aux->n1_ - aux->n11) / n11
64 * (aux->n_1 - aux->n11) / (n11 + aux->n - aux->n1_ - aux->n_1);
65 aux->n11 = n11;
66 return aux->p;
67 }
68 if (n11 == aux->n11 - 1) { // incremental
69 aux->p *= (double)aux->n11 / (aux->n1_ - n11)
70 * (aux->n11 + aux->n - aux->n1_ - aux->n_1) / (aux->n_1 - n11);
71 aux->n11 = n11;
72 return aux->p;
73 }
74 }
75 aux->n11 = n11;
76 }
77 aux->p = hypergeo(aux->n11, aux->n1_, aux->n_1, aux->n);
78 return aux->p;
79 }
80
81 double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two)
82 {
83 int i, j, max, min;
84 double p, q, left, right;
85 hgacc_t aux;
86 int n1_, n_1, n;
87
88 n1_ = n11 + n12; n_1 = n11 + n21; n = n11 + n12 + n21 + n22; // calculate n1_, n_1 and n
89 max = (n_1 < n1_) ? n_1 : n1_; // max n11, for right tail
90 min = n1_ + n_1 - n; // not sure why n11-n22 is used instead of min(n_1,n1_)
91 if (min < 0) min = 0; // min n11, for left tail
92 *two = *_left = *_right = 1.;
93 if (min == max) return 1.; // no need to do test
94 q = hypergeo_acc(n11, n1_, n_1, n, &aux); // the probability of the current table
95
96 if (q == 0.0) {
97 /*
98 If here, the calculated probablility is so small it can't be stored
99 in a double, which is possible when the table contains fairly large
100 numbers. If this happens, most of the calculation can be skipped
101 as 'left', 'right' and '*two' will be (to a good approximation) 0.0.
102 The returned values '*_left' and '*_right' depend on which side
103 of the hypergeometric PDF 'n11' sits. This can be found by
104 comparing with the mode of the distribution, the formula for which
105 can be found at:
106 https://en.wikipedia.org/wiki/Hypergeometric_distribution
107 Note that in the comparison we multiply through by the denominator
108 of the mode (n + 2) to avoid a division.
109 */
110 if ((int64_t) n11 * ((int64_t) n + 2) < ((int64_t) n_1 + 1) * ((int64_t) n1_ + 1)) {
111 // Peak to right of n11, so probability will be lower for all
112 // of the region from min to n11 and higher for at least some
113 // of the region from n11 to max; hence abs(i-n11) will be 0,
114 // abs(j-n11) will be > 0 and:
115 *_left = 0.0; *_right = 1.0; *two = 0.0;
116 return 0.0;
117 } else {
118 // Peak to left of n11, so probability will be lower for all
119 // of the region from n11 to max and higher for at least some
120 // of the region from min to n11; hence abs(i-n11) will be > 0,
121 // abs(j-n11) will be 0 and:
122 *_left = 1.0; *_right = 0.0; *two = 0.0;
123 return 0.0;
124 }
125 }
126
127 // left tail
128 p = hypergeo_acc(min, 0, 0, 0, &aux);
129 for (left = 0., i = min + 1; p < 0.99999999 * q && i<=max; ++i) // loop until underflow
130 left += p, p = hypergeo_acc(i, 0, 0, 0, &aux);
131 --i;
132 if (p < 1.00000001 * q) left += p;
133 else --i;
134 // right tail
135 p = hypergeo_acc(max, 0, 0, 0, &aux);
136 for (right = 0., j = max - 1; p < 0.99999999 * q && j>=0; --j) // loop until underflow
137 right += p, p = hypergeo_acc(j, 0, 0, 0, &aux);
138 ++j;
139 if (p < 1.00000001 * q) right += p;
140 else ++j;
141 // two-tail
142 *two = left + right;
143 if (*two > 1.) *two = 1.;
144 // adjust left and right
145 if (abs(i - n11) < abs(j - n11)) right = 1. - left + q;
146 else left = 1.0 - right + q;
147 *_left = left; *_right = right;
148 return q;
149 }