 |
ʻOhana
Population structure, admixture history, and selection using learning methods.
|
7 #ifndef JADE_BGL_READER_HPP__
8 #define JADE_BGL_READER_HPP__
10 #include "jade.error.hpp"
18 template <
typename TValue>
35 std::getline(in, temp);
37 throw error() <<
"failed to read header of beagle data.";
39 std::istringstream header_in (temp);
40 while (header_in >> temp)
42 if (_rows < 3 || _rows % 3 != 0)
43 throw error() <<
"invalid number of columns "
44 <<
"in beagle header: " << _rows;
46 _rows = (_rows - 3) / 3;
58 <<
"cannot read first allele for marker '"
59 << marker <<
"' on line " << _cols + 1
64 <<
"cannot read second allele for marker '"
65 << marker <<
"' on line " << _cols + 1
68 for (
size_t i = 0; i < _rows; i++)
70 for (
size_t j = 0; j < 3; j++)
76 <<
"encountered invalid percentage in column "
77 << 3 + i * 3 + j <<
" for marker '" << marker
78 <<
"' on line " << _cols + 1
80 _buf.push_back(value);
85 throw error() <<
"invalid data on line " << _cols + 1
88 const auto ch = in.peek();
89 if (ch >= 0 && ch !=
'\n')
91 <<
"expected end of line but encountered "
92 << _format(ch) <<
" on line "
93 << _cols + 1 <<
" of beagle data";
105 std::ostringstream out;
120 out <<
"0 0" << std::endl;
124 const auto r3 = _rows * 3;
125 const auto cr3 = _cols * _rows * 3;
127 auto grp_ptr = _buf.data();
128 const auto grp_end = grp_ptr + 3;
131 out << _rows <<
' ' << _cols << std::endl;
133 auto row_ptr = grp_ptr;
134 const auto row_end = row_ptr + r3;
135 while (row_ptr != row_end)
137 auto col_ptr = row_ptr;
138 const auto col_end = col_ptr + cr3;
143 if (col_ptr == col_end)
152 if (++grp_ptr == grp_end)
162 char const *
const path)
164 assert(path !=
nullptr);
165 std::ofstream out (path);
167 throw error() <<
"error opening '" << path <<
"' for writing";
175 const std::string & path)
182 static std::string _format(
const int ch)
184 std::ostringstream
str;
187 str <<
"end of data";
188 else if (ch ==
'\'')
str <<
"'\\''";
189 else if (ch ==
'\n')
str <<
"'\\n'";
190 else if (ch ==
'\r')
str <<
"'\\r'";
191 else if (ch ==
'\t')
str <<
"'\\t'";
192 else if (0 != std::isprint(ch))
193 str <<
"'" << char(ch) <<
"'";
195 str <<
"0x" << std::setfill(
'0') << std::hex
196 << std::setw(2) << ch;
201 std::vector<value_type> _buf;
207 #endif // JADE_BGL_READER_HPP__
TValue value_type
The value type.
void write(std::ostream &out) const
Writes the BEAGLE data to the specified output stream as a likelihood genotype matrix.
void write(char const *const path)
Writes the BEAGLE data to the specified output file.
A template for a class representing an exception thrown from this namespace.
A template for a class that reads BEAGLE data and is capable of writing it to a stream as a likelihoo...
basic_bgl_reader(std::istream &in)
Initializes a new instance of the class.
void write(const std::string &path)
Writes the BEAGLE data to the specified output file.