ʻOhana
Population structure, admixture history, and selection using learning methods.
jade.genotype_matrix_factory.hpp
1 /* -------------------------------------------------------------------------
2  Ohana
3  Copyright (c) 2015-2020 Jade Cheng (\___/)
4  Jade Cheng <info@jade-cheng.com> (='.'=)
5  ------------------------------------------------------------------------- */
6 
7 #ifndef JADE_GENOTYPE_MATRIX_FACTORY_HPP__
8 #define JADE_GENOTYPE_MATRIX_FACTORY_HPP__
9 
10 #include "jade.discrete_genotype_matrix.hpp"
11 #include "jade.likelihood_genotype_matrix.hpp"
12 
13 namespace jade
14 {
15  ///
16  /// A template for a class that creates genotype matrices based on files and
17  /// their file extensions.
18  ///
19  template <typename TValue>
21  {
22  public:
23  /// The value type.
24  typedef TValue value_type;
25 
26  /// The genotype matrix type.
29 
30  /// The discrete genotype matrix type.
33 
34  /// The likelihood genotype matrix type.
37 
38  ///
39  /// Creates a genotype matrix based on values from a file. This function
40  /// determines what kind of genotype matrix to create based on the file
41  /// extension of the specified path.
42  ///
43  /// \return A pointer to a new genotype matrix.
44  ///
45  /// \throws An exception if the path has no file extension or if the
46  /// file cannot be parsed successfully.
47  ///
49  const std::string & path) ///< The path to the file.
50  {
51  const auto period = path.find_last_of('.');
52  if (period == std::string::npos)
53  throw error() << "missing file extension for G matrix '"
54  << path << "'.";
55 
56  const auto extension = path.substr(period);
57  if (extension == ".dgm")
58  return new discrete_genotype_matrix_type(path);
59 
60  if (extension == ".lgm")
61  return new likelihood_genotype_matrix_type(path);
62 
63  throw error() << "unsupported file extension for G matrix '"
64  << path << "'.";
65  }
66 
67  ///
68  /// Creates a genotype matrix based on values from a file. This function
69  /// determines what kind of genotype matrix to create based on the file
70  /// extension of the specified path.
71  ///
72  /// \return A pointer to a new genotype matrix.
73  ///
74  /// \throws An exception if the path has no file extension or if the
75  /// file cannot be parsed successfully.
76  ///
77  inline static genotype_matrix_type * create(
78  char const * const path) ///< The path to the file.
79  {
80  assert(path != nullptr);
81  return create(std::string(path));
82  }
83  };
84 }
85 
86 #endif // JADE_GENOTYPE_MATRIX_FACTORY_HPP__
jade::basic_genotype_matrix_factory::value_type
TValue value_type
The value type.
Definition: jade.genotype_matrix_factory.hpp:24
jade::basic_genotype_matrix_factory::create
static genotype_matrix_type * create(const std::string &path)
Creates a genotype matrix based on values from a file. This function determines what kind of genotype...
Definition: jade.genotype_matrix_factory.hpp:48
jade::basic_genotype_matrix_factory::genotype_matrix_type
basic_genotype_matrix< value_type > genotype_matrix_type
The genotype matrix type.
Definition: jade.genotype_matrix_factory.hpp:28
jade::basic_genotype_matrix_factory::discrete_genotype_matrix_type
basic_discrete_genotype_matrix< value_type > discrete_genotype_matrix_type
The discrete genotype matrix type.
Definition: jade.genotype_matrix_factory.hpp:32
jade::basic_genotype_matrix_factory::create
static genotype_matrix_type * create(char const *const path)
Creates a genotype matrix based on values from a file. This function determines what kind of genotype...
Definition: jade.genotype_matrix_factory.hpp:77
jade::basic_likelihood_genotype_matrix
A template class implementing operations for a likelihood genotype matrix type.
Definition: jade.likelihood_genotype_matrix.hpp:21
jade::basic_genotype_matrix
A template for an abstract class implementing operations for a genotype matrix.
Definition: jade.genotype_matrix.hpp:26
jade::basic_discrete_genotype_matrix
A template for a class implementing operations for a discrete genotype matrix.
Definition: jade.discrete_genotype_matrix.hpp:22
jade::basic_genotype_matrix_factory::likelihood_genotype_matrix_type
basic_likelihood_genotype_matrix< value_type > likelihood_genotype_matrix_type
The likelihood genotype matrix type.
Definition: jade.genotype_matrix_factory.hpp:36
jade::basic_genotype_matrix_factory
A template for a class that creates genotype matrices based on files and their file extensions.
Definition: jade.genotype_matrix_factory.hpp:21
jade::basic_error
A template for a class representing an exception thrown from this namespace.
Definition: jade.error.hpp:20