A library for working with phylogenetic and population genetic data.
v0.27.0
taxonomy_reader.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TAXONOMY_FORMATS_TAXONOMY_READER_H_
2 #define GENESIS_TAXONOMY_FORMATS_TAXONOMY_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 
37 
38 #include <string>
39 
40 namespace genesis {
41 
42 // =================================================================================================
43 // Forward Declarations
44 // =================================================================================================
45 
46 namespace utils {
47 
48  class InputStream;
49 
50 } // namespace utils
51 
52 namespace taxonomy {
53 
54  class Taxonomy;
55 
56 // =================================================================================================
57 // Taxonomy Reader
58 // =================================================================================================
59 
109 {
110 public:
111 
112  // ---------------------------------------------------------------------
113  // Enums and Structs
114  // ---------------------------------------------------------------------
115 
119  struct Line
120  {
121  std::string name;
122  std::string rank;
123  std::string id;
124  };
125 
126  // ---------------------------------------------------------------------
127  // Constructor and Rule of Five
128  // ---------------------------------------------------------------------
129 
135  TaxonomyReader();
136  ~TaxonomyReader() = default;
137 
138  TaxonomyReader( TaxonomyReader const& ) = default;
139  TaxonomyReader( TaxonomyReader&& ) = default;
140 
141  TaxonomyReader& operator= ( TaxonomyReader const& ) = default;
142  TaxonomyReader& operator= ( TaxonomyReader&& ) = default;
143 
144  // ---------------------------------------------------------------------
145  // Reading
146  // ---------------------------------------------------------------------
147 
154  void read( std::shared_ptr<utils::BaseInputSource> source, Taxonomy& target ) const;
155 
162  Taxonomy read( std::shared_ptr<utils::BaseInputSource> source ) const;
163 
164  // ---------------------------------------------------------------------
165  // Parsing
166  // ---------------------------------------------------------------------
167 
171  void parse_document(
172  utils::InputStream& it,
173  Taxonomy& tax
174  ) const;
175 
183  ) const;
184 
185  // ---------------------------------------------------------------------
186  // Properties
187  // ---------------------------------------------------------------------
188 
202 
211 
230  TaxonomyReader& name_field_position( int value );
231 
237  int name_field_position() const;
238 
253  TaxonomyReader& rank_field_position( int value );
254 
260  int rank_field_position() const;
261 
276  TaxonomyReader& id_field_position( int value );
277 
283  int id_field_position() const;
284 
306  TaxonomyReader& expect_strict_order( bool value );
307 
313  bool expect_strict_order() const;
314 
315  // ---------------------------------------------------------------------
316  // Members
317  // ---------------------------------------------------------------------
318 
319 private:
320 
321  utils::CsvReader csv_reader_;
322  TaxopathParser taxopath_parser_;
323 
324  int name_field_position_ = 0;
325  int rank_field_position_ = -1;
326  int id_field_position_ = -1;
327  bool expect_strict_order_ = false;
328 
329 };
330 
331 } // namespace taxonomy
332 } // namespace genesis
333 
334 #endif // include guard
genesis::taxonomy::TaxonomyReader::read
void read(std::shared_ptr< utils::BaseInputSource > source, Taxonomy &target) const
Read taxonomy data from an input source, and add the contents to a Taxonomy.
Definition: taxonomy_reader.cpp:66
genesis::utils::InputStream
Stream interface for reading data from an InputSource, that keeps track of line and column counters.
Definition: input_stream.hpp:81
taxopath_parser.hpp
genesis::taxonomy::TaxonomyReader::expect_strict_order
bool expect_strict_order() const
Return whether currently the reader expects a strict order of taxa.
Definition: taxonomy_reader.cpp:210
genesis::taxonomy::TaxonomyReader
Read Taxonomy file formats.
Definition: taxonomy_reader.hpp:108
genesis::taxonomy::TaxonomyReader::Line
Internal helper structure that stores the relevant data of one line while reading.
Definition: taxonomy_reader.hpp:119
genesis::taxonomy::TaxonomyReader::name_field_position
int name_field_position() const
Get the currently set position of the field in each line where the taxon name is located.
Definition: taxonomy_reader.cpp:177
genesis::taxonomy::TaxonomyReader::~TaxonomyReader
~TaxonomyReader()=default
input_source.hpp
reader.hpp
genesis::taxonomy::TaxonomyReader::id_field_position
int id_field_position() const
Get the currently set position of the field in each line where the ID is located.
Definition: taxonomy_reader.cpp:199
genesis::taxonomy::TaxonomyReader::Line::id
std::string id
Definition: taxonomy_reader.hpp:123
genesis::taxonomy::TaxonomyReader::operator=
TaxonomyReader & operator=(TaxonomyReader const &)=default
genesis::taxonomy::TaxonomyReader::Line::rank
std::string rank
Definition: taxonomy_reader.hpp:122
genesis::taxonomy::TaxonomyReader::TaxonomyReader
TaxonomyReader()
Default constructor.
Definition: taxonomy_reader.cpp:57
genesis::taxonomy::TaxonomyReader::rank_field_position
int rank_field_position() const
Get the currently set position of the field in each line where the rank name is located.
Definition: taxonomy_reader.cpp:188
genesis::taxonomy::TaxonomyReader::csv_reader
utils::CsvReader & csv_reader()
Get the CsvReader used for reading a taxonomy file.
Definition: taxonomy_reader.cpp:154
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::taxonomy::Taxonomy
Store a Taxonomy, i.e., a nested hierarchy of Taxa.
Definition: taxonomy/taxonomy.hpp:96
genesis::taxonomy::TaxonomyReader::Line::name
std::string name
Definition: taxonomy_reader.hpp:121
genesis::taxonomy::TaxonomyReader::parse_line
Line parse_line(utils::InputStream &it) const
Read a single line of a taxonomy file and return the contained name and rank.
Definition: taxonomy_reader.cpp:109
genesis::taxonomy::TaxonomyReader::parse_document
void parse_document(utils::InputStream &it, Taxonomy &tax) const
Parse all data from an InputStream into a Taxonomy object.
Definition: taxonomy_reader.cpp:83
genesis::taxonomy::TaxopathParser
Helper class to parse a string containing a taxonomic path string into a Taxopath object.
Definition: taxopath_parser.hpp:81
genesis::taxonomy::TaxonomyReader::taxopath_parser
TaxopathParser & taxopath_parser()
Get the TaxopathParser used for parsing taxonomic path strings.
Definition: taxonomy_reader.cpp:159
genesis::utils::CsvReader
Read Comma/Character Separated Values (CSV) data and other delimiter-separated formats.
Definition: utils/formats/csv/reader.hpp:70