libsequence  1.9.5
AlleleCountMatrix.cc
1 #include <stdexcept>
2 #include <Sequence/AlleleCountMatrix.hpp>
3 #include <Sequence/StateCounts.hpp>
4 
5 namespace Sequence
6 {
7  std::vector<std::int32_t>
8  AlleleCountMatrix::init_counts(const VariantMatrix& m)
9  {
10  if (m.max_allele < 0)
11  {
12  throw std::invalid_argument("matrix max_allele must be >= 0");
13  }
14  std::vector<std::int32_t> counts;
15  counts.reserve(m.nsam * static_cast<std::size_t>(m.max_allele + 1));
16  StateCounts c;
17  for (std::size_t i = 0; i < m.nsites; ++i)
18  {
19  auto r = get_ConstRowView(m, i);
20  if (static_cast<std::int8_t>(c.max_allele_idx) > m.max_allele)
21  {
22  throw std::runtime_error("found allele value greater "
23  "than matrix.max_allele");
24  }
25  c(r);
26  for (std::size_t j = 0;
27  j < static_cast<std::size_t>(m.max_allele + 1); ++j)
28  {
29  counts.push_back(c.counts[j]);
30  }
31  }
32  return counts;
33  }
34 
35  AlleleCountMatrix::AlleleCountMatrix(const VariantMatrix& m)
36  : counts(init_counts(m)),
37  ncol(!m.data.empty() ? static_cast<std::size_t>(m.max_allele) + 1
38  : 0),
39  nrow(!m.data.empty() ? counts.size() / ncol : 0), nsam(m.nsam)
40  {
41  }
42 
43  std::pair<std::vector<std::int32_t>::const_iterator,
44  std::vector<std::int32_t>::const_iterator>
45  AlleleCountMatrix::row(const std::size_t i) const
46  {
47  if (i >= nrow)
48  {
49  throw std::out_of_range("row index out of range");
50  }
51  return std::make_pair(counts.begin() + i * ncol,
52  counts.begin() + i * ncol + ncol);
53  }
54 } // namespace Sequence
STL namespace.
The namespace in which this library resides.
ConstRowView get_ConstRowView(const VariantMatrix &m, const std::size_t row)
Return a ConstRowView from VariantMatrix m at row row. std::out_of_range is thrown if row is out of r...