3 #include <Sequence/AlleleCountMatrix.hpp> 4 #include <Sequence/summstats/auxillary.hpp> 5 #include "hprime_faywuh_aggregator.hpp" 10 hprime_common(
const std::uint32_t nsam,
const unsigned S,
const double tp,
16 return std::numeric_limits<double>::quiet_NaN();
18 auto a = summstats_aux::a_sub_n(static_cast<std::uint32_t>(nsam));
19 auto b = summstats_aux::b_sub_n(static_cast<std::uint32_t>(nsam));
21 = summstats_aux::b_sub_n_plus1(static_cast<std::uint32_t>(nsam));
24 =
static_cast<double>(S) / a;
25 double tsq = S * (S - 1) / (a * a + b);
26 double n =
static_cast<double>(nsam);
29 = (n * tw) / (2.0 * (n - 1.0))
30 + (2.0 * std::pow(n / (n - 1.0), 2.0) * (b1 - 1.0) - 1.0) * tsq;
31 double vPi = (3.0 * n * (n + 1.0) * tw + 2.0 * (n * n + n + 3.0) * tsq)
32 / (9 * n * (n - 1.0));
34 = ((n + 1.0) / (3.0 * (n - 1.0))) * tw
35 + ((7.0 * n * n + 3.0 * n - 2.0 - 4.0 * n * (n + 1.0) * b1)
36 / (2.0 * std::pow((n - 1.0), 2.0)))
38 return (tp - tl) / std::pow(vThetal + vPi - 2.0 * cov, 0.5);
47 if (ac.counts.empty())
49 return std::numeric_limits<double>::quiet_NaN();
51 auto refindex =
static_cast<std::size_t
>(refstate);
52 if (refindex >= ac.ncol)
54 throw std::invalid_argument(
55 "reference state greater than max allelic state");
57 detail::hprime_faywuh_row_processor rp;
58 for (std::size_t i = 0; i < ac.counts.size(); i += ac.ncol)
60 rp(ac, i, refindex, detail::stat_is_hprime());
62 return hprime_common(static_cast<std::uint32_t>(ac.nsam), rp.S, rp.pi,
68 const std::vector<std::int8_t> &refstates)
70 if (ac.counts.empty())
72 return std::numeric_limits<double>::quiet_NaN();
74 if (refstates.size() != ac.counts.size() / ac.ncol)
76 throw std::invalid_argument(
77 "incorrect number of reference states");
79 if (std::all_of(refstates.begin(), refstates.end(),
80 [](
const std::int8_t i) {
return i < 0; }))
82 throw std::invalid_argument(
83 "all reference states encoded as missing");
86 detail::hprime_faywuh_row_processor rp;
87 std::size_t rstate = 0;
88 for (std::size_t i = 0; i < ac.counts.size(); i += ac.ncol, ++rstate)
90 if (refstates[rstate] >= 0)
93 =
static_cast<std::size_t
>(refstates[rstate]);
94 if (refindex >= ac.ncol)
96 throw std::invalid_argument(
97 "reference state greater than max allelic " 100 rp(ac, i, refindex, detail::stat_is_hprime());
103 return hprime_common(static_cast<std::uint32_t>(ac.nsam), rp.S, rp.pi,
double hprime(const AlleleCountMatrix &ac, const std::int8_t refstate)
The namespace in which this library resides.
Matrix representation of allele counts in a VariantMatrix To be constructed.