libsequence  1.9.5
windows.cc
1 #include <Sequence/variant_matrix/windows.hpp>
2 
3 namespace Sequence
4 {
5  VariantMatrix
6  make_window(const VariantMatrix& m, const double beg, const double end)
7  {
8  if (end < beg)
9  {
10  throw std::invalid_argument("end must be >= beg");
11  }
12  auto pb
13  = std::lower_bound(m.positions.begin(), m.positions.end(), beg);
14  auto pe = std::upper_bound(pb, m.positions.end(), end);
15  decltype(m.data) data;
16  decltype(m.positions) pos;
17  if (pb == m.positions.end())
18  {
19  return VariantMatrix(std::move(data), std::move(pos));
20  }
21 
22  pos.assign(pb, pe);
23  for (auto i = pb; i < pe; ++i)
24  {
25  auto v = get_ConstRowView(
26  m, static_cast<std::size_t>(
27  std::distance(m.positions.begin(), i)));
28  data.insert(data.end(), v.begin(), v.end());
29  }
30  return VariantMatrix(std::move(data), std::move(pos));
31  }
32 
34  make_slice(const VariantMatrix& m, const double beg, const double end,
35  const std::size_t i, const std::size_t j)
36  {
37  if (end < beg)
38  {
39  throw std::invalid_argument("end must be >= beg");
40  }
41  if (!(j > i))
42  {
43  throw std::invalid_argument("i must be < j");
44  }
45  if (i >= m.nsam || j >= m.nsam)
46  {
47  throw std::invalid_argument("slice indexes out of range");
48  }
49  auto pb
50  = std::lower_bound(m.positions.begin(), m.positions.end(), beg);
51  auto pe = std::upper_bound(pb, m.positions.end(), end);
52  decltype(m.data) data;
53  decltype(m.positions) pos;
54  if (pb == m.positions.end() || i == j)
55  {
56  return VariantMatrix(std::move(data), std::move(pos));
57  }
58  pos.assign(pb, pe);
59  for (auto pi = pb; pi < pe; ++pi)
60  {
61  auto v = get_ConstRowView(
62  m, static_cast<std::size_t>(
63  std::distance(m.positions.begin(), pi)));
64  data.insert(data.end(), v.begin() + i, v.begin() + j);
65  }
66  return VariantMatrix(std::move(data), std::move(pos));
67  }
68 } // namespace Sequence
VariantMatrix make_window(const VariantMatrix &m, const double beg, const double end)
Return a window from a VariantMatrix.
Definition: windows.cc:6
The namespace in which this library resides.
std::vector< double > positions
Position of sites.
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...
Matrix representation of variation data.
std::size_t nsam
Sample size of data set.
VariantMatrix make_slice(const VariantMatrix &m, const double beg, const double end, const std::size_t i, const std::size_t j)
Return a slice from a VariantMatrix.
Definition: windows.cc:34
std::vector< std::int8_t > data
Data stored in matrix form with rows as sites.