libsequence  1.9.5
faywuh.cc
1 #include <functional>
2 #include <Sequence/AlleleCountMatrix.hpp>
3 #include "hprime_faywuh_aggregator.hpp"
4 
5 namespace Sequence
6 {
7  double
8  faywuh(const AlleleCountMatrix& ac, const std::int8_t refstate)
9  {
10  if (ac.counts.empty())
11  {
12  return std::numeric_limits<double>::quiet_NaN();
13  }
14  auto refindex = static_cast<std::size_t>(refstate);
15  if (refindex >= ac.ncol)
16  {
17  throw std::invalid_argument(
18  "reference state greater than max allelic state");
19  }
20  detail::hprime_faywuh_row_processor rp;
21  for (std::size_t i = 0; i < ac.counts.size(); i += ac.ncol)
22  {
23  rp(ac, i, refindex, detail::stat_is_faywuh());
24  }
25  if (!rp.S)
26  {
27  return std::numeric_limits<double>::quiet_NaN();
28  }
29  return rp.pi - rp.theta;
30  }
31 
32  double
34  const std::vector<std::int8_t>& refstates)
35  {
36  if (ac.counts.empty())
37  {
38  return std::numeric_limits<double>::quiet_NaN();
39  }
40  if (refstates.size() != ac.counts.size() / ac.ncol)
41  {
42  throw std::invalid_argument(
43  "incorrect number of reference states");
44  }
45  if (std::all_of(refstates.begin(), refstates.end(),
46  [](const std::int8_t i) { return i < 0; }))
47  {
48  throw std::invalid_argument(
49  "all reference states encoded as missing");
50  }
51 
52  detail::hprime_faywuh_row_processor rp;
53  std::size_t rstate = 0;
54  for (std::size_t i = 0; i < ac.counts.size(); i += ac.ncol, ++rstate)
55  {
56  if (refstates[rstate] >= 0)
57  {
58  auto refindex
59  = static_cast<std::size_t>(refstates[rstate]);
60  if (refindex >= ac.ncol)
61  {
62  throw std::invalid_argument(
63  "reference state greater than max allelic "
64  "state");
65  }
66  rp(ac, i, refindex, detail::stat_is_faywuh());
67  }
68  }
69  if (!rp.S)
70  {
71  return std::numeric_limits<double>::quiet_NaN();
72  }
73  return rp.pi - rp.theta;
74  }
75 } // namespace Sequence
The namespace in which this library resides.
double faywuh(const AlleleCountMatrix &ac, const std::int8_t refstate)
Fay and Wu&#39;s H.
Definition: faywuh.cc:8
Matrix representation of allele counts in a VariantMatrix To be constructed.