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

A template for a class that reads an AGI (Admixture Graph Input) file. The file provides the following information: A set of branch length variable names, a set of admixture proportion parameter names, a component size (K), and K*(K-1)/2 number of expressions representing C matrix entries. More...

#include <jade.agi_reader.hpp>

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

Public Types

typedef TValue value_type
 The value type. More...
 
typedef jade::basic_shunting_yard< value_typeshunting_yard_type
 The shunting yard algorithm used to parse and evaluate expressions. More...
 
typedef shunting_yard_type::args_type args_type
 The arguments used to evaluate the shunting yard algorithm. More...
 

Public Member Functions

 basic_agi_reader (std::istream &in)
 Initializes a new instance of the class by reading and parsing the specified input stream. More...
 
 basic_agi_reader (const std::string &path)
 Initializes a new instance of the class by reading and parsing the file with the specified path. More...
 
const args_typeget_args () const
 Returns a table of arguments that maps variables to values. The table reference returned has all values set to zero. The table can be used to evaluate all expression entries. More...
 
const std::vector< std::string > & get_branch_names () const
 Returns the vector of branch names. More...
 
const std::vector< std::string > & get_proportion_names () const
 Returns the vector of proportion names. More...
 
std::size_t get_k () const
 Returns the number of components. More...
 
const std::vector< shunting_yard_type > & get_entries () const
 Returns the vector of expressions for the C matrix entries. More...
 

Detailed Description

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

A template for a class that reads an AGI (Admixture Graph Input) file. The file provides the following information: A set of branch length variable names, a set of admixture proportion parameter names, a component size (K), and K*(K-1)/2 number of expressions representing C matrix entries.

An example of the file format is as follows:

# Branch length parameters, range: [0, inf)
a b c d e f g
# Admixture proportion parameters, range: [0, 1]
p
# K value
3
# Matrix entries, total number should be: K*(K-1)/2
# They map to a C matrix, e.g. K=3 maps to:
#   0 1
#   1 2
(1 - p) * (b + e + g + f + a) + p * (b + d + a)
p * a + (1 - p) * (g + f + a)
c + g + f + a

Definition at line 42 of file jade.agi_reader.hpp.

Member Typedef Documentation

◆ args_type

template<typename TValue >
typedef shunting_yard_type::args_type jade::basic_agi_reader< TValue >::args_type

The arguments used to evaluate the shunting yard algorithm.

Definition at line 58 of file jade.agi_reader.hpp.

◆ shunting_yard_type

template<typename TValue >
typedef jade::basic_shunting_yard<value_type> jade::basic_agi_reader< TValue >::shunting_yard_type

The shunting yard algorithm used to parse and evaluate expressions.

Definition at line 53 of file jade.agi_reader.hpp.

◆ value_type

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

The value type.

Definition at line 48 of file jade.agi_reader.hpp.

Constructor & Destructor Documentation

◆ basic_agi_reader() [1/2]

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

Initializes a new instance of the class by reading and parsing the specified input stream.

Parameters
inThe input stream.

Definition at line 65 of file jade.agi_reader.hpp.

66  : _args ()
67  , _branch_names (_read_names(in))
68  , _proportion_names (_read_names(in))
69  , _k (_read_size(in))
70  , _entries (_read_entries(in, _k))
71  {
72  std::string token;
73  if (!!(in >> token))
74  throw jade::error() << "unexpected token: " << token;
75 
76  //
77  // Require that all variable names across both sets are unique. In
78  // doing so, build a set of all defined variable names.
79  //
80  std::set<std::string> defined;
81  const auto vectors = { &_branch_names, &_proportion_names };
82  for (const auto vector : vectors)
83  for (const auto & name : *vector)
84  if (!defined.insert(name).second)
85  throw jade::error()
86  << "duplicate variable name '" << name << "'";
87 
88  //
89  // Require that all variables defined in the expressions are found
90  // in the set of defined variable names.
91  //
92  for (const auto & entry : _entries)
93  for (const auto & pair : entry.get_args())
94  if (defined.insert(pair.first).second)
95  throw jade::error()
96  << "undefined variable name '"
97  << pair.first << "' in expression";
98 
99  //
100  // Create a set of the variables used in the expressions.
101  //
102  std::set<std::string> used;
103  for (const auto & entry : _entries)
104  for (const auto & pair : entry.get_args())
105  used.insert(pair.first);
106 
107  //
108  // Remove unused variables from the branch and proportion sets.
109  //
110  {
111  const auto remove = [&used](std::vector<std::string> & names)
112  {
113  const auto end = std::remove_if(
114  names.begin(),
115  names.end(),
116  [&used](const std::string & name)
117  {
118  return used.find(name) == used.end();
119  });
120 
121  names.erase(end, names.end());
122  };
123 
124  remove(_branch_names);
125  remove(_proportion_names);
126  }
127 
128  //
129  // Now that it is verified that all names are unique and that all
130  // variables in the expressions are defined as either branch or
131  // proportions, initialize the set of default arguments based on
132  // the variables used in the expressions.
133  //
134  for (const auto & name : used)
135  _args[name] = value_type(0);
136  }
+ Here is the caller graph for this function:

◆ basic_agi_reader() [2/2]

template<typename TValue >
jade::basic_agi_reader< TValue >::basic_agi_reader ( const std::string &  path)
inlineexplicit

Initializes a new instance of the class by reading and parsing the file with the specified path.

Parameters
pathThe path to the file to read and parse.

Definition at line 143 of file jade.agi_reader.hpp.

144  : _args ()
145  , _branch_names ()
146  , _proportion_names ()
147  , _k (0)
148  , _entries ()
149  {
150  try
151  {
152  std::ifstream in (path);
153  *this = basic_agi_reader(in);
154  }
155  catch (const std::exception & e)
156  {
157  throw jade::error()
158  << "error reading admixture graph input from file '"
159  << path
160  << "': "
161  << e.what();
162  }
163  }
+ Here is the call graph for this function:

Member Function Documentation

◆ get_args()

template<typename TValue >
const args_type& jade::basic_agi_reader< TValue >::get_args ( ) const
inline

Returns a table of arguments that maps variables to values. The table reference returned has all values set to zero. The table can be used to evaluate all expression entries.

Returns
A table mapping variables to values.

Definition at line 171 of file jade.agi_reader.hpp.

172  {
173  return _args;
174  }

◆ get_branch_names()

template<typename TValue >
const std::vector<std::string>& jade::basic_agi_reader< TValue >::get_branch_names ( ) const
inline

Returns the vector of branch names.

Returns
The vector of branch names.

Definition at line 180 of file jade.agi_reader.hpp.

181  {
182  return _branch_names;
183  }
+ Here is the caller graph for this function:

◆ get_entries()

template<typename TValue >
const std::vector<shunting_yard_type>& jade::basic_agi_reader< TValue >::get_entries ( ) const
inline

Returns the vector of expressions for the C matrix entries.

Returns
The vector of expressions for the C matrix entries.

Definition at line 207 of file jade.agi_reader.hpp.

208  {
209  return _entries;
210  }
+ Here is the caller graph for this function:

◆ get_k()

template<typename TValue >
std::size_t jade::basic_agi_reader< TValue >::get_k ( ) const
inline

Returns the number of components.

Returns
The number of components.

Definition at line 198 of file jade.agi_reader.hpp.

199  {
200  return _k;
201  }
+ Here is the caller graph for this function:

◆ get_proportion_names()

template<typename TValue >
const std::vector<std::string>& jade::basic_agi_reader< TValue >::get_proportion_names ( ) const
inline

Returns the vector of proportion names.

Returns
The vector of proportion names.

Definition at line 189 of file jade.agi_reader.hpp.

190  {
191  return _proportion_names;
192  }
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file:
jade::basic_agi_reader::value_type
TValue value_type
The value type.
Definition: jade.agi_reader.hpp:48
jade::basic_error::what
virtual const char * what() const
jade::basic_error
A template for a class representing an exception thrown from this namespace.
Definition: jade.error.hpp:20
jade::basic_agi_reader::basic_agi_reader
basic_agi_reader(std::istream &in)
Initializes a new instance of the class by reading and parsing the specified input stream.
Definition: jade.agi_reader.hpp:65