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

A template for a class that reads BEAGLE data and is capable of writing it to a stream as a likelihood genotype matrix. More...

#include <jade.bgl_reader.hpp>

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

Public Types

typedef TValue value_type
 The value type. More...
 

Public Member Functions

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

Detailed Description

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

A template for a class that reads BEAGLE data and is capable of writing it to a stream as a likelihood genotype matrix.

Definition at line 19 of file jade.bgl_reader.hpp.

Member Typedef Documentation

◆ value_type

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

The value type.

Definition at line 23 of file jade.bgl_reader.hpp.

Constructor & Destructor Documentation

◆ basic_bgl_reader()

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

Initializes a new instance of the class.

Parameters
inThe input stream.

Definition at line 28 of file jade.bgl_reader.hpp.

30  : _buf ()
31  , _rows (0)
32  , _cols (0)
33  {
34  std::string temp;
35  std::getline(in, temp);
36  if (!in.good())
37  throw error() << "failed to read header of beagle data.";
38 
39  std::istringstream header_in (temp);
40  while (header_in >> temp)
41  _rows++;
42  if (_rows < 3 || _rows % 3 != 0)
43  throw error() << "invalid number of columns "
44  << "in beagle header: " << _rows;
45 
46  _rows = (_rows - 3) / 3;
47 
48  for (;;)
49  {
50  std::string marker;
51  if (!(in >> marker))
52  break;
53 
54  _cols++;
55 
56  if (!(in >> temp))
57  throw error()
58  << "cannot read first allele for marker '"
59  << marker << "' on line " << _cols + 1
60  << " of beagle data";
61 
62  if (!(in >> temp))
63  throw error()
64  << "cannot read second allele for marker '"
65  << marker << "' on line " << _cols + 1
66  << " of beagle data";
67 
68  for (size_t i = 0; i < _rows; i++)
69  {
70  for (size_t j = 0; j < 3; j++)
71  {
72  value_type value;
73  if (!(in >> value) || value < value_type(0) ||
74  value > value_type(1))
75  throw error()
76  << "encountered invalid percentage in column "
77  << 3 + i * 3 + j << " for marker '" << marker
78  << "' on line " << _cols + 1
79  << " of beagle data";
80  _buf.push_back(value);
81  }
82  }
83 
84  if (!in.good())
85  throw error() << "invalid data on line " << _cols + 1
86  << " of beagle data";
87 
88  const auto ch = in.peek();
89  if (ch >= 0 && ch != '\n')
90  throw error()
91  << "expected end of line but encountered "
92  << _format(ch) << " on line "
93  << _cols + 1 << " of beagle data";
94  }
95 
96  if (_cols == 0)
97  _rows = 0;
98  }

Member Function Documentation

◆ str()

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

Definition at line 103 of file jade.bgl_reader.hpp.

104  {
105  std::ostringstream out;
106  write(out);
107  return out.str();
108  }
+ Here is the call graph for this function:

◆ write() [1/3]

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

Writes the BEAGLE data to the specified output file.

Parameters
pathThe output path.

Definition at line 161 of file jade.bgl_reader.hpp.

163  {
164  assert(path != nullptr);
165  std::ofstream out (path);
166  if (!out.good())
167  throw error() << "error opening '" << path << "' for writing";
168  write(out);
169  }
+ Here is the call graph for this function:

◆ write() [2/3]

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

Writes the BEAGLE data to the specified output file.

Parameters
pathThe output path.

Definition at line 174 of file jade.bgl_reader.hpp.

176  {
177  write(path.c_str());
178  }
+ Here is the call graph for this function:

◆ write() [3/3]

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

Writes the BEAGLE data to the specified output stream as a likelihood genotype matrix.

Parameters
outThe output stream.

Definition at line 114 of file jade.bgl_reader.hpp.

117  {
118  if (_cols == 0)
119  {
120  out << "0 0" << std::endl;
121  return;
122  }
123 
124  const auto r3 = _rows * 3;
125  const auto cr3 = _cols * _rows * 3;
126 
127  auto grp_ptr = _buf.data();
128  const auto grp_end = grp_ptr + 3;
129  for (;;)
130  {
131  out << _rows << ' ' << _cols << std::endl;
132 
133  auto row_ptr = grp_ptr;
134  const auto row_end = row_ptr + r3;
135  while (row_ptr != row_end)
136  {
137  auto col_ptr = row_ptr;
138  const auto col_end = col_ptr + cr3;
139  for (;;)
140  {
141  out << *col_ptr;
142  col_ptr += r3;
143  if (col_ptr == col_end)
144  break;
145  out << '\t';
146  }
147 
148  out << '\n';
149  row_ptr += 3;
150  }
151 
152  if (++grp_ptr == grp_end)
153  break;
154  out << std::endl;
155  }
156  }
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file:
jade::basic_bgl_reader::value_type
TValue value_type
The value type.
Definition: jade.bgl_reader.hpp:23
jade::basic_bgl_reader::write
void write(std::ostream &out) const
Writes the BEAGLE data to the specified output stream as a likelihood genotype matrix.
Definition: jade.bgl_reader.hpp:114