libsequence  1.9.5
allele_counts.cc
1 #include <vector>
2 #include <utility>
3 #include <cstdint>
5 #include <Sequence/AlleleCountMatrix.hpp>
6 
7 using count_type = std::vector<Sequence::AlleleCounts>;
8 
9 namespace
10 {
11  template <typename T>
12  typename count_type::value_type
13  add_counts(const T& row, const std::size_t nsam, const bool nonref,
14  const std::int8_t refstate)
15  {
16  if (nonref && refstate == -1)
17  {
18  return count_type::value_type{ -1, -1 };
19  }
20  std::size_t refindex = static_cast<std::size_t>(refstate);
21  if (nonref
22  && refindex >= static_cast<std::size_t>(row.second - row.first))
23  {
24  throw std::invalid_argument("reference state out of range");
25  }
26  typename count_type::value_type rv{ 0, 0 };
27  int nnon_missing = 0;
28  for (auto i = row.first; i != row.second; ++i)
29  {
30  if (*i > 0)
31  {
32  nnon_missing += *i;
33  if (!nonref
34  || (nonref
35  && static_cast<std::size_t>(i - row.first)
36  != refindex))
37  {
38  ++rv.nstates;
39  }
40  }
41  }
42  rv.nmissing = static_cast<int>(nsam) - nnon_missing;
43  return rv;
44  }
45 } // namespace
46 
47 namespace Sequence
48 {
49  count_type
51  {
52  count_type rv;
53  for (std::size_t i = 0; i < m.nrow; ++i)
54  {
55  auto r = m.row(i);
56  rv.emplace_back(add_counts(r, m.nsam, false, -1));
57  }
58  return rv;
59  }
60 
61  count_type
63  const std::vector<std::int8_t>& refstates)
64  {
65  if (refstates.size() != m.nrow)
66  {
67  throw std::invalid_argument("number of reference states does "
68  "not equal number of sites");
69  }
70  count_type rv;
71  for (std::size_t i = 0; i < m.nrow; ++i)
72  {
73  auto r = m.row(i);
74  rv.emplace_back(add_counts(r, m.nsam, true, refstates[i]));
75  }
76  return rv;
77  }
78 
79  count_type
81  const std::int8_t refstate)
82  {
83  count_type rv;
84  for (std::size_t i = 0; i < m.nrow; ++i)
85  {
86  auto r = m.row(i);
87  rv.emplace_back(add_counts(r, m.nsam, true, refstate));
88  }
89  return rv;
90  }
91 } // namespace Sequence
std::vector< AlleleCounts > allele_counts(const AlleleCountMatrix &m)
Count number of alleles at each site.
The namespace in which this library resides.
Count alleles at variable sites.
Matrix representation of allele counts in a VariantMatrix To be constructed.
std::vector< AlleleCounts > non_reference_allele_counts(const AlleleCountMatrix &m, const std::int8_t refstate)
Count number of non-reference alleles at each site.