A toolkit for working with phylogenetic data.
v0.18.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-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 
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 
110 {
111 public:
112 
113  // ---------------------------------------------------------------------
114  // Enums and Structs
115  // ---------------------------------------------------------------------
116 
120  struct Line
121  {
122  std::string name;
123  std::string rank;
124  };
125 
126  // ---------------------------------------------------------------------
127  // Constructor and Rule of Five
128  // ---------------------------------------------------------------------
129 
130  TaxonomyReader();
131  ~TaxonomyReader() = default;
132 
133  TaxonomyReader( TaxonomyReader const& ) = default;
134  TaxonomyReader( TaxonomyReader&& ) = default;
135 
136  TaxonomyReader& operator= ( TaxonomyReader const& ) = default;
137  TaxonomyReader& operator= ( TaxonomyReader&& ) = default;
138 
139  // ---------------------------------------------------------------------
140  // Reading
141  // ---------------------------------------------------------------------
142 
143  void from_stream( std::istream& is, Taxonomy& tax ) const;
144  void from_file ( std::string const& fn, Taxonomy& tax ) const;
145  void from_string( std::string const& is, Taxonomy& tax ) const;
146 
147  // ---------------------------------------------------------------------
148  // Parsing
149  // ---------------------------------------------------------------------
150 
151  void parse_document(
152  utils::InputStream& it,
153  Taxonomy& tax
154  ) const;
155 
158  ) const;
159 
160  // ---------------------------------------------------------------------
161  // Properties
162  // ---------------------------------------------------------------------
163 
166 
167  TaxonomyReader& name_field_position( int value );
168  int name_field_position() const;
169 
170  TaxonomyReader& rank_field_position( int value );
171  int rank_field_position() const;
172 
173  TaxonomyReader& expect_strict_order( bool value );
174  bool expect_strict_order() const;
175 
176  // ---------------------------------------------------------------------
177  // Members
178  // ---------------------------------------------------------------------
179 
180 private:
181 
182  utils::CsvReader csv_reader_;
183  TaxopathParser taxopath_parser_;
184 
185  int name_field_position_ = 0;
186  int rank_field_position_ = -1;
187  bool expect_strict_order_ = false;
188 
189 };
190 
191 } // namespace taxonomy
192 } // namespace genesis
193 
194 #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.
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.
Stream interface for reading data from an InputSource, that keeps track of line and column counters...
Read Taxonomy file formats.