libsequence  1.9.5
msformat.hpp
1 #ifndef SEQUENCE_VARIANT_MATRIX_MSFORMAT_HPP__
2 #define SEQUENCE_VARIANT_MATRIX_MSFORMAT_HPP__
3 
4 #include <istream>
5 #include <Sequence/VariantMatrix.hpp>
6 
7 namespace Sequence
8 {
10 
18  template <typename streamtype>
19  inline VariantMatrix
20  from_msformat(streamtype& input_stream)
21  {
22  char ch;
23  while (!input_stream.eof())
24  {
25  input_stream >> ch;
26  if (ch == ':')
27  break;
28  }
29  std::string temp;
30  std::size_t S;
31  input_stream >> S >> temp;
32  std::vector<double> pos(S);
33  for (std::size_t i = 0; i < S; ++i)
34  {
35  input_stream >> pos[i];
36  }
37  std::vector<std::int8_t> data;
38  char next_token;
39  while (!input_stream.eof()
40  && static_cast<char>(input_stream.peek()) != '/')
41  {
42  input_stream >> next_token >> std::ws;
43  data.push_back((next_token == '0') ? 0 : 1);
44  }
45  input_stream >> std::ws;
46  //We now have to transpose the input matrix
47  decltype(data) data_t(data.size(), -1);
48  std::size_t nsam = data.size() / static_cast<std::size_t>(S);
49  std::size_t k = 0;
50  for (std::size_t i = 0; i < static_cast<std::size_t>(S); ++i)
51  {
52  for (std::size_t j = 0; j < nsam; ++j)
53  {
54  data_t[k++] = data[i + j * S];
55  }
56  }
57  return VariantMatrix(std::move(data_t), std::move(pos));
58  }
59 
60  template <typename output_stream>
61  inline void
62  to_msformat(const VariantMatrix& m, output_stream& o)
70  {
71  o << "//\nsegsites: " << m.nsites << "\npositions: ";
72  for (auto& p : m.positions)
73  {
74  o << p << ' ';
75  }
76  o << '\n';
77  for (std::size_t i = 0; i < m.nsam; ++i)
78  {
79  auto col = get_ConstColView(m, i);
80  for (auto state : col)
81  {
82  o << static_cast<int>(state);
83  }
84  if (i < m.nsam - 1)
85  {
86  o << '\n';
87  }
88  }
89  }
90 } // namespace Sequence
91 
92 #endif
VariantMatrix from_msformat(streamtype &input_stream)
Create VariantMatrix from "ms"-like input format.
Definition: msformat.hpp:20
The namespace in which this library resides.
std::size_t nsites
Number of sites in data set.
std::vector< double > positions
Position of sites.
Matrix representation of variation data.
std::size_t nsam
Sample size of data set.
ConstColView get_ConstColView(VariantMatrix &m, const std::size_t col)
Return a ConstColView from VariantMatrix m at col col. std::out_of_range is thcoln if col is out of r...
void to_msformat(const VariantMatrix &m, output_stream &o)
Write VariantMatrix in "ms" format.
Definition: msformat.hpp:62