A toolkit for working with phylogenetic data.
v0.19.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-2018 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 
34 #include <iosfwd>
35 #include <string>
36 #include <unordered_map>
37 #include <vector>
38 
39 namespace genesis {
40 
41 // =================================================================================================
42 // Forward Declarations
43 // =================================================================================================
44 
45 namespace utils {
46  class InputStream;
47  class JsonDocument;
48 }
49 
50 namespace placement {
51  class Sample;
52  class SampleSet;
53 }
54 
55 // =================================================================================================
56 // Jplace Reader
57 // =================================================================================================
58 
59 namespace placement {
60 
92 {
93  // ---------------------------------------------------------------------
94  // Constructor and Rule of Five
95  // ---------------------------------------------------------------------
96 
97 public:
98 
99  JplaceReader() = default;
100  ~JplaceReader() = default;
101 
102  JplaceReader( JplaceReader const& ) = default;
103  JplaceReader( JplaceReader&& ) = default;
104 
105  JplaceReader& operator= ( JplaceReader const& ) = default;
106  JplaceReader& operator= ( JplaceReader&& ) = default;
107 
108  // ---------------------------------------------------------------------
109  // Reading
110  // ---------------------------------------------------------------------
111 
112 public:
113 
117  Sample from_stream( std::istream& is ) const;
118 
122  Sample from_file( std::string const& fn ) const;
123 
127  Sample from_string( std::string const& jplace ) const;
128 
133 
137  SampleSet from_files( std::vector<std::string> const& fns ) const;
138 
142  SampleSet from_strings( std::vector<std::string> const& jps ) const;
143 
149  void from_files ( std::vector<std::string> const& fns, SampleSet& set ) const;
150 
156  void from_strings ( std::vector<std::string> const& jps, SampleSet& set ) const;
157 
158  // ---------------------------------------------------------------------
159  // Processing
160  // ---------------------------------------------------------------------
161 
162 private:
163 
168  void process_json_version( utils::JsonDocument const& doc ) const;
169 
174  void process_json_metadata( utils::JsonDocument const& doc, Sample& smp ) const;
175 
180  void process_json_tree( utils::JsonDocument const& doc, Sample& smp ) const;
181 
186  std::vector<std::string> process_json_fields( utils::JsonDocument const& doc ) const;
187 
192  void process_json_placements(
193  utils::JsonDocument& doc,
194  Sample& smp,
195  std::vector<std::string> fields
196  ) const;
197 
198  // ---------------------------------------------------------------------
199  // Jplace Version
200  // ---------------------------------------------------------------------
201 
202 public:
203 
207  static std::string version ();
208 
215  static bool check_version ( size_t version );
216 
220  static bool check_version ( std::string const& version );
221 
222  // ---------------------------------------------------------------------
223  // Properties
224  // ---------------------------------------------------------------------
225 
226 public:
227 
253  kIgnore,
254 
261  kLog,
262 
269  kCorrect,
270 
275 
279  kThrow
280  };
281 
286 
296 
297  // ---------------------------------------------------------------------
298  // Members
299  // ---------------------------------------------------------------------
300 
301 private:
302 
303  InvalidNumberBehaviour invalid_number_behaviour_ = InvalidNumberBehaviour::kIgnore;
304 
305 };
306 
307 } // namespace placement
308 } // namespace genesis
309 
310 #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.