libsequence  1.9.5
VariantMatrix.hpp
1 #ifndef SEQUENCE_VARIANT_MATRIX_HPP__
2 #define SEQUENCE_VARIANT_MATRIX_HPP__
3 
4 #include <algorithm>
5 #include <cstdint>
6 #include <cstddef>
7 #include <utility>
8 #include <vector>
9 #include <limits>
10 #include <stdexcept>
11 #include <type_traits>
12 
13 static_assert(sizeof(std::int8_t) == sizeof(char),
14  "sizeof(char) is not 8 bits");
15 
16 namespace Sequence
17 {
21 
45  {
46  private:
47  std::int8_t
48  set_max_allele(const std::int8_t max_allele_value)
49  {
50  if (max_allele_value < 0 && !data.empty())
51  {
52  auto itr = std::max_element(data.begin(), data.end());
53  return *itr;
54  }
55  //special case to allow construction of empty data sets
56  //without throwing an exception
57  else if (data.empty())
58 
59  {
60  return 0;
61  }
62  return max_allele_value;
63  }
64 
65  public:
67  std::vector<std::int8_t> data;
69  std::vector<double> positions;
71  std::size_t nsites;
73  std::size_t nsam;
75  static const std::int8_t mask;
78  using value_type = std::int8_t;
80  const std::int8_t max_allele;
81  template <typename data_input, typename positions_input>
82  VariantMatrix(data_input&& data_, positions_input&& positions_,
83  const std::int8_t max_allele_value = -1)
88  : data(std::forward<data_input>(data_)),
89  positions(std::forward<positions_input>(positions_)),
90  nsites(positions.size()),
91  nsam((nsites > 0) ? data.size() / positions.size() : 0),
92  max_allele{ set_max_allele(max_allele_value) }
93  {
94  if (max_allele < 0)
95  {
96  throw std::invalid_argument("max allele must be >= 0");
97  }
98  if ((!data.empty() && !positions.empty())
99  && data.size() % positions.size() != 0.0)
100  {
101  throw std::invalid_argument("incorrect dimensions");
102  }
103  }
104 
105  // Non range-checked access
106 
109  std::int8_t& get(const std::size_t site, const std::size_t haplotype);
112  const std::int8_t& get(const std::size_t site,
113  const std::size_t haplotype) const;
114 
115  // Ranged-checked access after std::vector<T>::at.
118  std::int8_t& at(const std::size_t site, const std::size_t haplotype);
121  const std::int8_t& at(const std::size_t site,
122  const std::size_t haplotype) const;
123  };
124 
125 } // namespace Sequence
126 
127 #endif
const std::int8_t max_allele
Max allelic value stored in matrix.
STL namespace.
The namespace in which this library resides.
std::int8_t & at(const std::size_t site, const std::size_t haplotype)
Get data from marker site and haplotype haplotype. std::out_of_range is thrown if indexes are invalid...
std::size_t nsites
Number of sites in data set.
VariantMatrix(data_input &&data_, positions_input &&positions_, const std::int8_t max_allele_value=-1)
"Perfect-forwarding" constructor.
std::vector< double > positions
Position of sites.
Matrix representation of variation data.
std::size_t nsam
Sample size of data set.
std::vector< std::int8_t > data
Data stored in matrix form with rows as sites.
static const std::int8_t mask
Reserved value for masked data.