 |
ʻOhana
Population structure, admixture history, and selection using learning methods.
|
7 #ifndef JADE_PED_READER_HPP__
8 #define JADE_PED_READER_HPP__
10 #include "jade.error.hpp"
17 template <
typename TValue>
37 const auto ch = in.get();
42 throw error(
"unexpected end of data");
49 throw error() <<
"premature end of line " << line;
63 const auto ch1 = _read(in, line);
65 _require(in, line,
' ');
67 const auto ch2 = _read(in, line);
70 ch1 ==
'0' || ch2 ==
'0' ?
'3' :
75 _buf.push_back(symbol);
77 if (in.peek() < 0 || in.peek() ==
'\n')
80 _require(in, line,
'\t');
82 if (_rows > 0 && col == _cols)
84 <<
"expected " << _cols <<
" pairs but encountered "
85 <<
"at least " << col + 1 <<
" on line " << line;
93 else if (col != _cols)
95 <<
"expected " << _cols <<
" genotype pairs but "
96 <<
"encountered " << col <<
" on line " << line;
111 std::ostringstream out;
123 out << _rows <<
' ' << _cols << std::endl;
129 for (
const auto ch : _buf)
130 out << ch << (++i % _cols == 0 ?
'\n' :
' ');
137 char const *
const path)
139 assert(path !=
nullptr);
140 std::ofstream out (path);
142 throw error() <<
"error opening '" << path <<
"' for writing";
150 const std::string & path)
157 static std::string _format(
const int ch)
159 std::ostringstream
str;
162 str <<
"end of data";
163 else if (ch ==
'\'')
str <<
"'\\''";
164 else if (ch ==
'\n')
str <<
"'\\n'";
165 else if (ch ==
'\r')
str <<
"'\\r'";
166 else if (ch ==
'\t')
str <<
"'\\t'";
167 else if (0 != std::isprint(ch))
168 str <<
"'" << char(ch) <<
"'";
170 str <<
"0x" << std::setfill(
'0') << std::hex
171 << std::setw(2) << ch;
177 static int _read(std::istream & in,
const size_t line)
179 const auto ch = in.get();
181 if (ch !=
'0' && ch !=
'1' && ch !=
'2')
183 <<
"expected genotype '0', '1', or '2' but encountered "
184 << _format(ch) <<
" on line " << line <<
"; correct this "
185 <<
"by regenerating your data using the -recode12 option "
192 static void _require(
197 const auto actual = in.get();
198 if (actual == expected)
202 <<
"expected " << _format(expected) <<
" but encountered "
203 << _format(actual) <<
" on line " << line;
206 std::vector<char> _buf;
212 #endif // JADE_PED_READER_HPP__
void write(char const *const path)
Writes the PED data to the specified output file.
void write(const std::string &path)
Writes the PED data to the specified output file.
TValue value_type
The value type.
void write(std::ostream &out) const
Writes the PED data to the specified output stream.
A template for a class that reads PED data.
basic_ped_reader(std::istream &in)
Initializes a new instance of the class.
A template for a class representing an exception thrown from this namespace.