ʻOhana
Population structure, admixture history, and selection using learning methods.
Public Types | Public Member Functions
jade::basic_ped_reader< TValue > Class Template Reference

A template for a class that reads PED data. More...

#include <jade.ped_reader.hpp>

+ Collaboration diagram for jade::basic_ped_reader< TValue >:

Public Types

typedef TValue value_type
 The value type. More...
 

Public Member Functions

 basic_ped_reader (std::istream &in)
 Initializes a new instance of the class. More...
 
std::string str () const
 
void write (std::ostream &out) const
 Writes the PED data to the specified output stream. More...
 
void write (char const *const path)
 Writes the PED data to the specified output file. More...
 
void write (const std::string &path)
 Writes the PED data to the specified output file. More...
 

Detailed Description

template<typename TValue>
class jade::basic_ped_reader< TValue >

A template for a class that reads PED data.

Definition at line 18 of file jade.ped_reader.hpp.

Member Typedef Documentation

◆ value_type

template<typename TValue >
typedef TValue jade::basic_ped_reader< TValue >::value_type

The value type.

Definition at line 22 of file jade.ped_reader.hpp.

Constructor & Destructor Documentation

◆ basic_ped_reader()

template<typename TValue >
jade::basic_ped_reader< TValue >::basic_ped_reader ( std::istream &  in)
inlineexplicit

Initializes a new instance of the class.

Parameters
inThe input stream.

Definition at line 27 of file jade.ped_reader.hpp.

29  : _buf ()
30  , _rows (0)
31  , _cols (0)
32  {
33  size_t line = 1;
34  size_t col = 0;
35  for (;;)
36  {
37  const auto ch = in.get();
38 
39  if (ch < 0)
40  {
41  if (col > 0)
42  throw error("unexpected end of data");
43  break;
44  }
45 
46  if (ch == '\n')
47  {
48  if (col > 0)
49  throw error() << "premature end of line " << line;
50  line++;
51  continue;
52  }
53 
54  if (ch != '\t')
55  continue;
56 
57  if (++col < 6)
58  continue;
59 
60  col = 1;
61  for (;;)
62  {
63  const auto ch1 = _read(in, line);
64 
65  _require(in, line, ' ');
66 
67  const auto ch2 = _read(in, line);
68 
69  const auto symbol =
70  ch1 == '0' || ch2 == '0' ? '3' :
71  ch1 != ch2 ? '1' :
72  ch1 == '2' ? '0' :
73  '2';
74 
75  _buf.push_back(symbol);
76 
77  if (in.peek() < 0 || in.peek() == '\n')
78  break;
79 
80  _require(in, line, '\t');
81 
82  if (_rows > 0 && col == _cols)
83  throw error()
84  << "expected " << _cols << " pairs but encountered "
85  << "at least " << col + 1 << " on line " << line;
86 
87  col++;
88  }
89 
90  if (_rows == 0)
91  _cols = col;
92 
93  else if (col != _cols)
94  throw error()
95  << "expected " << _cols << " genotype pairs but "
96  << "encountered " << col << " on line " << line;
97 
98  col = 0;
99  _rows++;
100  }
101 
102  if (_cols == 0)
103  _rows = 0;
104  }

Member Function Documentation

◆ str()

template<typename TValue >
std::string jade::basic_ped_reader< TValue >::str ( ) const
inline
Returns
A string representation of this instance.

Definition at line 109 of file jade.ped_reader.hpp.

110  {
111  std::ostringstream out;
112  write(out);
113  return out.str();
114  }
+ Here is the call graph for this function:

◆ write() [1/3]

template<typename TValue >
void jade::basic_ped_reader< TValue >::write ( char const *const  path)
inline

Writes the PED data to the specified output file.

Parameters
pathThe output path.

Definition at line 136 of file jade.ped_reader.hpp.

138  {
139  assert(path != nullptr);
140  std::ofstream out (path);
141  if (!out.good())
142  throw error() << "error opening '" << path << "' for writing";
143  write(out);
144  }
+ Here is the call graph for this function:

◆ write() [2/3]

template<typename TValue >
void jade::basic_ped_reader< TValue >::write ( const std::string &  path)
inline

Writes the PED data to the specified output file.

Parameters
pathThe output path.

Definition at line 149 of file jade.ped_reader.hpp.

151  {
152  write(path.c_str());
153  }
+ Here is the call graph for this function:

◆ write() [3/3]

template<typename TValue >
void jade::basic_ped_reader< TValue >::write ( std::ostream &  out) const
inline

Writes the PED data to the specified output stream.

Parameters
outThe output stream.

Definition at line 119 of file jade.ped_reader.hpp.

122  {
123  out << _rows << ' ' << _cols << std::endl;
124 
125  if (_cols == 0)
126  return;
127 
128  size_t i = 0;
129  for (const auto ch : _buf)
130  out << ch << (++i % _cols == 0 ? '\n' : ' ');
131  }
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file:
jade::basic_ped_reader::write
void write(std::ostream &out) const
Writes the PED data to the specified output stream.
Definition: jade.ped_reader.hpp:119