A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2017 Lucas Czech
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 
34 #include <string>
35 #include <unordered_map>
36 #include <vector>
37 
38 namespace genesis {
39 
40 // =================================================================================================
41 // Forward Declarations
42 // =================================================================================================
43 
44 namespace utils {
45  class InputStream;
46  class JsonDocument;
47 }
48 
49 namespace placement {
50  class Sample;
51  class SampleSet;
52 }
53 
54 // =================================================================================================
55 // Jplace Reader
56 // =================================================================================================
57 
58 namespace placement {
59 
91 {
92  // ---------------------------------------------------------------------
93  // Constructor and Rule of Five
94  // ---------------------------------------------------------------------
95 
96 public:
97 
98  JplaceReader() = default;
99  ~JplaceReader() = default;
100 
101  JplaceReader( JplaceReader const& ) = default;
102  JplaceReader( JplaceReader&& ) = default;
103 
104  JplaceReader& operator= ( JplaceReader const& ) = default;
105  JplaceReader& operator= ( JplaceReader&& ) = default;
106 
107  // ---------------------------------------------------------------------
108  // Reading
109  // ---------------------------------------------------------------------
110 
111 public:
112 
116  Sample from_stream( std::istream& is ) const;
117 
121  Sample from_file( std::string const& fn ) const;
122 
126  Sample from_string( std::string const& jplace ) const;
127 
132 
136  SampleSet from_files( std::vector<std::string> const& fns ) const;
137 
141  SampleSet from_strings( std::vector<std::string> const& jps ) const;
142 
148  void from_files ( std::vector<std::string> const& fns, SampleSet& set ) const;
149 
155  void from_strings ( std::vector<std::string> const& jps, SampleSet& set ) const;
156 
157  // ---------------------------------------------------------------------
158  // Processing
159  // ---------------------------------------------------------------------
160 
161 private:
162 
167  void process_json_version( utils::JsonDocument const& doc ) const;
168 
173  void process_json_metadata( utils::JsonDocument const& doc, Sample& smp ) const;
174 
179  void process_json_tree( utils::JsonDocument const& doc, Sample& smp ) const;
180 
185  std::vector<std::string> process_json_fields( utils::JsonDocument const& doc ) const;
186 
191  void process_json_placements(
192  utils::JsonDocument& doc,
193  Sample& smp,
194  std::vector<std::string> fields
195  ) const;
196 
197  // ---------------------------------------------------------------------
198  // Jplace Version
199  // ---------------------------------------------------------------------
200 
201 public:
202 
206  static std::string version ();
207 
214  static bool check_version ( size_t version );
215 
219  static bool check_version ( std::string const& version );
220 
221  // ---------------------------------------------------------------------
222  // Properties
223  // ---------------------------------------------------------------------
224 
225 public:
226 
252  kIgnore,
253 
260  kLog,
261 
268  kCorrect,
269 
274 
278  kThrow
279  };
280 
285 
295 
296  // ---------------------------------------------------------------------
297  // Members
298  // ---------------------------------------------------------------------
299 
300 private:
301 
302  InvalidNumberBehaviour invalid_number_behaviour_ = InvalidNumberBehaviour::kIgnore;
303 
304 };
305 
306 } // namespace placement
307 } // namespace genesis
308 
309 #endif // include guard
SampleSet from_files(std::vector< std::string > const &fns) const
Read a list of files and parse them as a Jplace document into a SampleSet.
Sample from_document(utils::JsonDocument &doc) const
Take a JsonDocument and parse it as a Jplace document into a Sample.
static bool check_version(size_t version)
Checks whether the version of the jplace format works with this parser.
InvalidNumberBehaviour invalid_number_behaviour() const
Return the currenlty set InvalidNumberBehaviour.
static std::string version()
Returns the version number that this class is written for. Currently, this is "3".
Throw an std::runtime_error when encountering an invalid number.
Store a set of Samples with associated names.
Definition: sample_set.hpp:52
Sample from_file(std::string const &fn) const
Read a file and parse it as a Jplace document into a Sample.
Store a Json value of any kind.
Manage a set of Pqueries along with the PlacementTree where the PqueryPlacements are placed on...
Definition: sample.hpp:68
JplaceReader & operator=(JplaceReader const &)=default
Sample from_string(std::string const &jplace) const
Parse a string as a Jplace document into a Sample.
Correct invalid numbers to the next best correct number.
Sample from_stream(std::istream &is) const
Read jplace data from a stream into a Sample.
SampleSet from_strings(std::vector< std::string > const &jps) const
Parse a list of strings as a Jplace document into a SampleSet.
InvalidNumberBehaviour
Enum to determine the behaviour of the reader in case of invalid numbers.