A toolkit for working with phylogenetic data.
v0.24.0
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-2020 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 
39 
40 #include <cstddef>
41 #include <iterator>
42 #include <memory>
43 #include <sstream>
44 
45 namespace genesis {
46 namespace sequence {
47 
48 // =================================================================================================
49 // Fasta Input Iterator
50 // =================================================================================================
51 
88 {
89 public:
90 
91  // -------------------------------------------------------------------------
92  // Member Types
93  // -------------------------------------------------------------------------
94 
97  using pointer = value_type&;
99  using difference_type = std::ptrdiff_t;
100  using iterator_category = std::input_iterator_tag;
101 
102  // -------------------------------------------------------------------------
103  // Constructors and Rule of Five
104  // -------------------------------------------------------------------------
105 
110  : input_stream_( nullptr )
111  , sequence_()
112  , reader_()
113  {}
114 
118  explicit FastaInputIterator( std::shared_ptr<utils::BaseInputSource> source )
119  : input_stream_( std::make_shared<utils::InputStream>( source ))
120  , sequence_()
121  , reader_()
122  {
123  increment();
124  }
125 
130  FastaInputIterator( std::shared_ptr<utils::BaseInputSource> source, FastaReader const& settings )
131  : input_stream_( std::make_shared<utils::InputStream>( source ))
132  , sequence_()
133  , reader_( settings )
134  {
135  increment();
136  }
137 
138  ~FastaInputIterator() = default;
139 
140  FastaInputIterator( self_type const& ) = default;
141  FastaInputIterator( self_type&& ) = default;
142 
143  self_type& operator= ( self_type const& ) = default;
144  self_type& operator= ( self_type&& ) = default;
145 
146  // -------------------------------------------------------------------------
147  // Comparators
148  // -------------------------------------------------------------------------
149 
150  bool operator == ( self_type const& other ) const
151  {
152  return input_stream_ == other.input_stream_;
153  }
154 
155  bool operator != ( self_type const& other ) const
156  {
157  return !( *this == other );
158  }
159 
163  explicit operator bool() const
164  {
165  return good_;
166  }
167 
168  // -------------------------------------------------------------------------
169  // Accessors
170  // -------------------------------------------------------------------------
171 
172  value_type const& operator * () const
173  {
174  return sequence_;
175  }
176 
177  value_type const* operator -> () const
178  {
179  return &sequence_;
180  }
181 
182  value_type const& dereference() const
183  {
184  return sequence_;
185  }
186 
187  // -------------------------------------------------------------------------
188  // Iteration
189  // -------------------------------------------------------------------------
190 
198  {
199  return *this;
200  }
201 
208  {
209  return self_type();
210  }
211 
213  {
214  increment();
215  return *this;
216  }
217 
219  {
220  auto r = *this;
221  increment();
222  return r;
223  }
224 
225  void increment()
226  {
227  // Check whether the input stream is good (not end-of-stream) and can be read from.
228  // If not, we reached its end, so we stop reading in the next iteration.
229  if( ! input_stream_ || ! *input_stream_ ) {
230  good_ = false;
231  input_stream_ = nullptr;
232  sequence_ = Sequence();
233  return;
234  }
235 
236  reader_.parse_sequence( *input_stream_, sequence_ );
237  }
238 
239  // -------------------------------------------------------------------------
240  // Data Members
241  // -------------------------------------------------------------------------
242 
243 private:
244 
245  bool good_ = true;
246  std::shared_ptr<utils::InputStream> input_stream_;
247 
248  Sequence sequence_;
249  FastaReader reader_;
250 };
251 
252 } // namespace sequence
253 } // namespace genesis
254 
255 #endif // include guard
bool operator==(self_type const &other) const
FastaInputIterator()
Create a default instance, with no input.
bool parse_sequence(utils::InputStream &input_stream, Sequence &sequence) const
Parse a Sequence in Fasta format.
Read Fasta sequence data.
STL namespace.
FastaInputIterator(std::shared_ptr< utils::BaseInputSource > source, FastaReader const &settings)
Create an instance that reads from an input source, using the settings of a given FastaReader...
bool operator!=(self_type const &other) const
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
FastaInputIterator(std::shared_ptr< utils::BaseInputSource > source)
Create an instance that reads from an input source, using a default FastaReader.
Provides some valuable additions to STD.
self_type & operator=(self_type const &)=default
self_type & begin()
Beginning of the iterator.
self_type end()
End of the iterator.
Iterate an input source and parse it as Fasta sequences.