A library for working with phylogenetic and population genetic data.
v0.27.0
jplace_reader.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_PLACEMENT_FORMATS_JPLACE_READER_H_
2 #define GENESIS_PLACEMENT_FORMATS_JPLACE_READER_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2019 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
35 
36 #include <iosfwd>
37 #include <memory>
38 #include <string>
39 #include <unordered_map>
40 #include <vector>
41 
42 namespace genesis {
43 
44 // =================================================================================================
45 // Forward Declarations
46 // =================================================================================================
47 
48 namespace utils {
49  class JsonDocument;
50 }
51 
52 namespace tree {
53  class TreeEdge;
54 }
55 
56 namespace placement {
58 
59  class Pquery;
60  class Sample;
61  class SampleSet;
62 }
63 
64 // =================================================================================================
65 // Jplace Reader
66 // =================================================================================================
67 
68 namespace placement {
69 
100 {
101  // ---------------------------------------------------------------------
102  // Constructor and Rule of Five
103  // ---------------------------------------------------------------------
104 
105 public:
106 
107  JplaceReader() = default;
108  ~JplaceReader() = default;
109 
110  JplaceReader( JplaceReader const& ) = default;
111  JplaceReader( JplaceReader&& ) = default;
112 
113  JplaceReader& operator= ( JplaceReader const& ) = default;
114  JplaceReader& operator= ( JplaceReader&& ) = default;
115 
116  // ---------------------------------------------------------------------
117  // Reading
118  // ---------------------------------------------------------------------
119 
120 public:
121 
128  Sample read(
129  std::shared_ptr<utils::BaseInputSource> source
130  ) const;
131 
137  Sample read(
139  ) const;
140 
144  SampleSet read(
145  std::vector<std::shared_ptr<utils::BaseInputSource>> sources
146  ) const;
147 
154  void read(
155  std::vector<std::shared_ptr<utils::BaseInputSource>> sources,
156  SampleSet& target
157  ) const;
158 
159  // ---------------------------------------------------------------------
160  // Processing
161  // ---------------------------------------------------------------------
162 
163 private:
164 
174  int get_jplace_version_( utils::JsonDocument const& doc ) const;
175 
180  void process_jplace_version_( utils::JsonDocument const& doc ) const;
181 
186  void process_jplace_metadata_( utils::JsonDocument const& doc, Sample& smp ) const;
187 
192  void process_jplace_tree_( utils::JsonDocument const& doc, Sample& smp ) const;
193 
198  std::vector<std::string> process_jplace_fields_( utils::JsonDocument const& doc ) const;
199 
204  void process_jplace_placements_(
205  utils::JsonDocument& doc,
206  Sample& smp,
207  std::vector<std::string> const& fields
208  ) const;
209 
213  void process_jplace_placements_p_(
214  utils::JsonDocument const& pqry_obj,
215  Pquery& pquery,
216  std::vector<std::string> const& fields,
217  std::unordered_map<size_t, PlacementTreeEdge*> const& edge_num_map
218  ) const;
219 
223  void process_jplace_placements_nm_(
224  utils::JsonDocument const& pqry_obj,
225  Pquery& pquery
226  ) const;
227 
228  // ---------------------------------------------------------------------
229  // Properties
230  // ---------------------------------------------------------------------
231 
232 public:
233 
259  kIgnore,
260 
267  kLog,
268 
275  kFix,
276 
280  kLogAndFix,
281 
285  kThrow
286  };
287 
292  {
293  return invalid_number_behaviour_;
294  }
295 
305  {
306  invalid_number_behaviour_ = val;
307  return *this;
308  }
309 
310  // ---------------------------------------------------------------------
311  // Members
312  // ---------------------------------------------------------------------
313 
314 private:
315 
316  InvalidNumberBehaviour invalid_number_behaviour_ = InvalidNumberBehaviour::kIgnore;
317 
318 };
319 
320 } // namespace placement
321 } // namespace genesis
322 
323 #endif // include guard
genesis::placement::SampleSet
Store a set of Samples with associated names.
Definition: sample_set.hpp:54
genesis::placement::Sample
Manage a set of Pqueries along with the PlacementTree where the PqueryPlacements are placed on.
Definition: sample.hpp:68
genesis::placement::JplaceReader::read
Sample read(std::shared_ptr< utils::BaseInputSource > source) const
Read from an input source.
Definition: jplace_reader.cpp:74
genesis::utils::JsonDocument
Store a Json value of any kind.
Definition: json/document.hpp:114
genesis::placement::JplaceReader::InvalidNumberBehaviour::kThrow
@ kThrow
Throw an std::runtime_error when encountering an invalid number.
genesis::placement::JplaceReader::InvalidNumberBehaviour::kFix
@ kFix
Fix invalid numbers to the next best correct number.
genesis::placement::JplaceReader::InvalidNumberBehaviour::kLog
@ kLog
Log invalid numbers to LOG_WARN.
genesis::placement::JplaceReader::invalid_number_behaviour
InvalidNumberBehaviour invalid_number_behaviour() const
Return the currenlty set InvalidNumberBehaviour.
Definition: jplace_reader.hpp:291
genesis::placement::JplaceReader::~JplaceReader
~JplaceReader()=default
input_source.hpp
genesis::placement::JplaceReader::JplaceReader
JplaceReader()=default
genesis::placement::Pquery
A pquery holds a set of PqueryPlacements and a set of PqueryNames.
Definition: pquery.hpp:82
genesis::placement::JplaceReader::invalid_number_behaviour
JplaceReader & invalid_number_behaviour(InvalidNumberBehaviour val)
Set the InvalidNumberBehaviour.
Definition: jplace_reader.hpp:304
genesis::tree::TreeEdge
Definition: edge.hpp:60
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::placement::JplaceReader
Read Jplace data.
Definition: jplace_reader.hpp:99
genesis::placement::JplaceReader::InvalidNumberBehaviour::kIgnore
@ kIgnore
Ignore invalid numbers.
genesis::placement::JplaceReader::InvalidNumberBehaviour
InvalidNumberBehaviour
Enum to determine the behaviour of the reader in case of invalid numbers.
Definition: jplace_reader.hpp:252
genesis::placement::JplaceReader::InvalidNumberBehaviour::kLogAndFix
@ kLogAndFix
Combination of kLog and kFix.
genesis::placement::JplaceReader::operator=
JplaceReader & operator=(JplaceReader const &)=default