libsequence  1.9.5
filtering.cc
1 #include <Sequence/variant_matrix/filtering.hpp>
2 #include <algorithm>
3 #include <functional>
4 #include <limits>
5 #include <cmath>
6 
7 namespace Sequence
8 {
9  template <typename T>
10  std::int32_t
11  filter_common(
12  VariantMatrix &m, const std::function<bool(const T &)> &f,
13  const std::function<T(VariantMatrix &, const std::size_t)> &viewmaker,
14  std::size_t &dim)
15  {
16  constexpr auto remove_pos = std::is_same<T, RowView>::value;
17  std::int32_t rv = 0;
18  for (std::size_t i = 0; i < dim; ++i)
19  {
20  auto view = viewmaker(m, i);
21  if (f(view))
22  {
23  ++rv;
24  std::transform(
25  view.begin(), view.end(), view.begin(),
26  [](double) { return VariantMatrix::mask; });
27  if (remove_pos)
28  {
29  m.positions[i]
30  = std::numeric_limits<double>::quiet_NaN();
31  }
32  }
33  }
34  if (rv)
35  {
36  if (remove_pos)
37  {
38  m.positions.erase(
39  std::remove_if(
40  m.positions.begin(), m.positions.end(),
41  [](double d) { return std::isnan(d); }),
42  m.positions.end());
43  }
44  m.data.erase(std::remove(m.data.begin(), m.data.end(),
46  m.data.end());
47  dim -= static_cast<std::size_t>(rv);
48  }
49  return rv;
50  }
51 
52  std::int32_t
53  filter_sites(VariantMatrix &m,
54  const std::function<bool(const RowView &)> &f)
55  {
56  return filter_common<RowView>(
57  m, f, [](VariantMatrix &m,
58  const std::size_t i) { return get_RowView(m, i); },
59  m.nsites);
60  }
61 
62  std::int32_t
63  filter_haplotypes(VariantMatrix &m,
64  const std::function<bool(const ColView &)> &f)
65  {
66  return filter_common<ColView>(
67  m, f, [](VariantMatrix &m,
68  const std::size_t i) { return get_ColView(m, i); },
69  m.nsam);
70  }
71 }
internal::row_view_< std::int8_t * > RowView
View of a VariantMatrix row (a variable site)
The namespace in which this library resides.
ConstRowView get_RowView(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...
ColView get_ColView(VariantMatrix &m, const std::size_t col)
Return a ColView from VariantMatrix m at col col. std::out_of_range is thcoln if col is out of range...
internal::col_view_< std::int8_t * > ColView
View of a VariantMatrix column ("haplotype")
static const std::int8_t mask
Reserved value for masked data.