A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
36 
37 #include <string>
38 
39 namespace genesis {
40 
41 // =================================================================================================
42 // Forward Declarations
43 // =================================================================================================
44 
45 namespace utils {
46 
47  class InputStream;
48 
49 } // namespace utils
50 
51 namespace taxonomy {
52 
53  class Taxonomy;
54 
55 // =================================================================================================
56 // Taxonomy Reader
57 // =================================================================================================
58 
112 {
113 public:
114 
115  // ---------------------------------------------------------------------
116  // Enums and Structs
117  // ---------------------------------------------------------------------
118 
122  struct Line
123  {
124  std::string name;
125  std::string rank;
126  std::string id;
127  };
128 
129  // ---------------------------------------------------------------------
130  // Constructor and Rule of Five
131  // ---------------------------------------------------------------------
132 
138  TaxonomyReader();
139  ~TaxonomyReader() = default;
140 
141  TaxonomyReader( TaxonomyReader const& ) = default;
142  TaxonomyReader( TaxonomyReader&& ) = default;
143 
144  TaxonomyReader& operator= ( TaxonomyReader const& ) = default;
145  TaxonomyReader& operator= ( TaxonomyReader&& ) = default;
146 
147  // ---------------------------------------------------------------------
148  // Reading
149  // ---------------------------------------------------------------------
150 
155  void from_stream( std::istream& is, Taxonomy& tax ) const;
156 
160  void from_file ( std::string const& fn, Taxonomy& tax ) const;
161 
165  void from_string( std::string const& is, Taxonomy& tax ) const;
166 
171  Taxonomy from_stream( std::istream& is ) const;
172 
176  Taxonomy from_file ( std::string const& fn ) const;
177 
181  Taxonomy from_string( std::string const& is ) const;
182 
183  // ---------------------------------------------------------------------
184  // Parsing
185  // ---------------------------------------------------------------------
186 
190  void parse_document(
191  utils::InputStream& it,
192  Taxonomy& tax
193  ) const;
194 
202  ) const;
203 
204  // ---------------------------------------------------------------------
205  // Properties
206  // ---------------------------------------------------------------------
207 
221 
230 
249  TaxonomyReader& name_field_position( int value );
250 
256  int name_field_position() const;
257 
272  TaxonomyReader& rank_field_position( int value );
273 
279  int rank_field_position() const;
280 
295  TaxonomyReader& id_field_position( int value );
296 
302  int id_field_position() const;
303 
325  TaxonomyReader& expect_strict_order( bool value );
326 
332  bool expect_strict_order() const;
333 
334  // ---------------------------------------------------------------------
335  // Members
336  // ---------------------------------------------------------------------
337 
338 private:
339 
340  utils::CsvReader csv_reader_;
341  TaxopathParser taxopath_parser_;
342 
343  int name_field_position_ = 0;
344  int rank_field_position_ = -1;
345  int id_field_position_ = -1;
346  bool expect_strict_order_ = false;
347 
348 };
349 
350 } // namespace taxonomy
351 } // namespace genesis
352 
353 #endif // include guard
void parse_document(utils::InputStream &it, Taxonomy &tax) const
Parse all data from an InputStream into a Taxonomy object.
int name_field_position() const
Get the currently set position of the field in each line where the taxon name is located.
TaxonomyReader()
Default constructor.
void from_string(std::string const &is, Taxonomy &tax) const
Read a string with taxonomy data and add its contents to a Taxonomy.
utils::CsvReader & csv_reader()
Get the CsvReader used for reading a taxonomy file.
Store a Taxonomy, i.e., a nested hierarchy of Taxa.
int rank_field_position() const
Get the currently set position of the field in each line where the rank name is located.
TaxonomyReader & operator=(TaxonomyReader const &)=default
TaxopathParser & taxopath_parser()
Get the TaxopathParser used for parsing taxonomic path strings.
Read Comma Separated Values (CSV) data and other delimiter-separated formats.
Helper class to parse a string containing a taxonomic path string into a Taxopath object...
void from_file(std::string const &fn, Taxonomy &tax) const
Read a taxonomy file and add its contents to a Taxonomy.
Line parse_line(utils::InputStream &it) const
Read a single line of a taxonomy file and return the contained name and rank.
bool expect_strict_order() const
Return whether currently the reader expects a strict order of taxa.
void from_stream(std::istream &is, Taxonomy &tax) const
Read taxonomy data until the end of the stream is reached, and add the contents to a Taxonomy...
Internal helper structure that stores the relevant data of one line while reading.
int id_field_position() const
Get the currently set position of the field in each line where the ID is located. ...
Stream interface for reading data from an InputSource, that keeps track of line and column counters...
Read Taxonomy file formats.