libsequence  1.9.5
tajd.cc
1 #include <cmath>
2 #include <limits>
3 #include <Sequence/AlleleCountMatrix.hpp>
4 #include <Sequence/summstats/auxillary.hpp>
5 
6 namespace Sequence
7 {
8  double
10  {
11  double pi = 0.0;
12  int S = 0;
13  std::int32_t max_nsam = 0;
14  for (std::size_t i = 0; i < ac.counts.size(); i += ac.ncol)
15  {
16  std::int32_t nsam = 0;
17  double homozygosity = 0.0;
18  int nstates = 0;
19  for (std::size_t j = i; j < i + ac.ncol; ++j)
20  {
21  if (ac.counts[j] > 0)
22  {
23  ++nstates;
24  nsam += ac.counts[j];
25  homozygosity += static_cast<double>(
26  ac.counts[j] * (ac.counts[j] - 1));
27  }
28  }
29 
30  if (nstates)
31  {
32  max_nsam = std::max(max_nsam, nsam);
33  S += nstates - 1;
34  pi += 1.0
35  - homozygosity
36  / static_cast<double>(nsam * (nsam - 1));
37  }
38  }
39  if (!S)
40  {
41  return std::numeric_limits<double>::quiet_NaN();
42  }
43  auto a1 = summstats_aux::a_sub_n(static_cast<std::uint32_t>(max_nsam));
44  double w = static_cast<double>(S) / a1;
45  auto a2 = summstats_aux::b_sub_n(static_cast<std::uint32_t>(max_nsam));
46  auto dn = static_cast<double>(max_nsam);
47  double b1 = (dn + 1.0) / (3.0 * (dn - 1.0));
48  double b2
49  = (2.0 * (std::pow(dn, 2.0) + dn + 3.0)) / (9.0 * dn * (dn - 1.0));
50  double c1 = b1 - 1.0 / a1;
51  double c2 = b2 - (dn + 2.0) / (a1 * dn) + a2 / std::pow(a1, 2.0);
52  double e1 = c1 / a1;
53  double e2 = c2 / (std::pow(a1, 2.0) + a2);
54  double denominator = std::pow((e1 * S + e2 * S * (S - 1.0)), 0.5);
55  return (pi - w) / denominator;
56  }
57 } // namespace Sequence
double tajd(const AlleleCountMatrix &ac)
Tajima&#39;s D.
Definition: tajd.cc:9
The namespace in which this library resides.
Matrix representation of allele counts in a VariantMatrix To be constructed.