A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fasta_input_iterator.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_SEQUENCE_FORMATS_FASTA_INPUT_ITERATOR_H_
2 #define GENESIS_SEQUENCE_FORMATS_FASTA_INPUT_ITERATOR_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 
38 
39 #include <cstddef>
40 #include <iterator>
41 #include <memory>
42 #include <sstream>
43 
44 namespace genesis {
45 namespace sequence {
46 
47 // =================================================================================================
48 // Fasta Input Iterator
49 // =================================================================================================
50 
80 {
81 public:
82 
83  // -------------------------------------------------------------------------
84  // Member Types
85  // -------------------------------------------------------------------------
86 
89  using pointer = value_type&;
91  using difference_type = std::ptrdiff_t;
92  using iterator_category = std::input_iterator_tag;
93 
94  // -------------------------------------------------------------------------
95  // Constructors and Rule of Five
96  // -------------------------------------------------------------------------
97 
102  : reader_()
103  , sequence_()
104  {}
105 
110  : reader_( reader )
111  , sequence_()
112  {}
113 
114  ~FastaInputIterator() = default;
115 
116  FastaInputIterator( self_type const& ) = default;
117  FastaInputIterator( self_type&& ) = default;
118 
119  self_type& operator= ( self_type const& ) = default;
120  self_type& operator= ( self_type&& ) = default;
121 
122  // -------------------------------------------------------------------------
123  // Reading
124  // -------------------------------------------------------------------------
125 
131  self_type& from_stream( std::istream& input_stream )
132  {
133  // Create input stream and read first sequence.
134  input_stream_ = std::make_shared<utils::InputStream>(
135  utils::make_unique< utils::StreamInputSource >( input_stream )
136  );
137  increment();
138  return *this;
139  }
140 
146  self_type& from_file( std::string const& file_name )
147  {
148  // Create input stream and read first sequence.
149  input_stream_ = std::make_shared<utils::InputStream>(
150  utils::make_unique< utils::FileInputSource >( file_name )
151  );
152  increment();
153  return *this;
154  }
155 
161  self_type& from_string( std::string const& input_string )
162  {
163  // Create input stream and read first sequence.
164  input_stream_ = std::make_shared<utils::InputStream>(
165  utils::make_unique< utils::StringInputSource >( input_string )
166  );
167  increment();
168  return *this;
169  }
170 
171  // -------------------------------------------------------------------------
172  // Comparators
173  // -------------------------------------------------------------------------
174 
175  bool operator == ( self_type const& other ) const
176  {
177  return input_stream_ == other.input_stream_;
178  }
179 
180  bool operator != ( self_type const& other ) const
181  {
182  return !( *this == other );
183  }
184 
188  explicit operator bool() const
189  {
190  return good_;
191  }
192 
193  // -------------------------------------------------------------------------
194  // Accessors
195  // -------------------------------------------------------------------------
196 
197  value_type const& operator * () const
198  {
199  return sequence_;
200  }
201 
202  value_type const* operator -> () const
203  {
204  return &sequence_;
205  }
206 
207  value_type const& dereference() const
208  {
209  return sequence_;
210  }
211 
218  {
219  return reader_;
220  }
221 
222  // -------------------------------------------------------------------------
223  // Iteration
224  // -------------------------------------------------------------------------
225 
227  {
228  increment();
229  return *this;
230  }
231 
232  void increment()
233  {
234  // Check whether the input stream is good (not end-of-stream) and can be read from.
235  // If not, we reached its end, so we stop reading in the next iteration.
236  if( input_stream_ == nullptr || ! *input_stream_ ) {
237  good_ = false;
238  return;
239  }
240 
241  reader_.parse_sequence( *input_stream_, sequence_ );
242  }
243 
244  // -------------------------------------------------------------------------
245  // Data Members
246  // -------------------------------------------------------------------------
247 
248 private:
249 
250  std::shared_ptr<utils::InputStream> input_stream_;
251  bool good_ = true;
252 
253  FastaReader reader_;
254  Sequence sequence_;
255 };
256 
257 } // namespace sequence
258 } // namespace genesis
259 
260 #endif // include guard
FastaInputIterator()
Create a default instance, using a default FastaReader.
Read Fasta sequence data.
bool parse_sequence(utils::InputStream &input_stream, Sequence &sequence) const
Parse a Sequence in Fasta format.
FastaReader & reader()
Return the FastaReader used for this iterator.
Provides some valuable additions to STD.
self_type & from_string(std::string const &input_string)
Start reading from an input file.
FastaInputIterator(FastaReader const &reader)
Create an instance that copies the settings of a given FastaReader.
self_type & operator=(self_type const &)=default
bool operator==(self_type const &other) const
self_type & from_file(std::string const &file_name)
Start reading from an input string.
bool operator!=(self_type const &other) const
Iterate an input stream and parse it as Fasta sequences.
self_type & from_stream(std::istream &input_stream)
Start reading from an input stream.