A library for working with phylogenetic and population genetic data.
v0.32.0
fastx_input_stream.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_SEQUENCE_FORMATS_FASTX_INPUT_STREAM_H_
2 #define GENESIS_SEQUENCE_FORMATS_FASTX_INPUT_STREAM_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2024 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@sund.ku.dk>
23  University of Copenhagen, Globe Institute, Section for GeoGenetics
24  Oster Voldgade 5-7, 1350 Copenhagen K, Denmark
25 */
26 
40 
41 #include <cassert>
42 #include <cstddef>
43 #include <iterator>
44 #include <memory>
45 #include <sstream>
46 
47 namespace genesis {
48 namespace sequence {
49 
50 // =================================================================================================
51 // Forward declarations and typedefs
52 // =================================================================================================
53 
54 template<class Reader>
56 
59 
60 // =================================================================================================
61 // Fasta and Fastq Input Stream
62 // =================================================================================================
63 
88 template<class Reader>
89 class FastxInputStream
90 {
91 public:
92 
93  // -------------------------------------------------------------------------
94  // Member Types
95  // -------------------------------------------------------------------------
96 
99  using pointer = value_type*;
101  using difference_type = std::ptrdiff_t;
102  using iterator_category = std::input_iterator_tag;
103 
104  // ======================================================================================
105  // Internal Iterator
106  // ======================================================================================
107 
108 public:
109 
116  class Iterator
117  {
118  // -------------------------------------------------------------------------
119  // Typedefs and Enums
120  // -------------------------------------------------------------------------
121 
122  public:
123 
126  using pointer = value_type const*;
127  using reference = value_type const&;
128  using difference_type = std::ptrdiff_t;
129  using iterator_category = std::input_iterator_tag;
130 
131  // -------------------------------------------------------------------------
132  // Constructors and Rule of Five
133  // -------------------------------------------------------------------------
134 
135  private:
136 
137  Iterator() = default;
138 
139  Iterator( FastxInputStream const* parent )
140  : parent_( parent )
141  {
142  // Safeguard
143  if( ! parent_ ) {
144  return;
145  }
146 
147  // Start reading from the input source into a stream.
148  input_stream_ = std::make_shared<utils::InputStream>( parent_->input_source_ );
149 
150  // Start streaming the data
151  increment_();
152  }
153 
154  public:
155 
156  ~Iterator() = default;
157 
158  Iterator( self_type const& ) = default;
159  Iterator( self_type&& ) = default;
160 
161  Iterator& operator= ( self_type const& ) = default;
162  Iterator& operator= ( self_type&& ) = default;
163 
165 
166  // -------------------------------------------------------------------------
167  // Accessors
168  // -------------------------------------------------------------------------
169 
170  value_type const* operator->() const
171  {
172  return &sequence_;
173  }
174 
176  {
177  return &sequence_;
178  }
179 
180  value_type const& operator*() const
181  {
182  return sequence_;
183  }
184 
186  {
187  return sequence_;
188  }
189 
190  // -------------------------------------------------------------------------
191  // Iteration
192  // -------------------------------------------------------------------------
193 
195  {
196  increment_();
197  return *this;
198  }
199 
200  // self_type operator ++(int)
201  // {
202  // auto cpy = *this;
203  // increment_();
204  // return cpy;
205  // }
206 
216  bool operator==( self_type const& it ) const
217  {
218  return parent_ == it.parent_;
219  }
220 
221  bool operator!=( self_type const& it ) const
222  {
223  return !(*this == it);
224  }
225 
226  // -------------------------------------------------------------------------
227  // Internal Members
228  // -------------------------------------------------------------------------
229 
230  private:
231 
232  // ---------------------------------------------
233  // Increment and Processing Samples
234  // ---------------------------------------------
235 
236  void increment_()
237  {
238  assert( parent_ );
239 
240  // Check whether the input stream is good (not end-of-stream) and can be read from.
241  // If not, we reached its end, so we stop reading in the next iteration.
242  if( ! input_stream_ || ! *input_stream_ ) {
243  parent_ = nullptr;
244  input_stream_ = nullptr;
245  sequence_ = Sequence();
246  return;
247  }
248 
249  parent_->reader_.parse_sequence( *input_stream_, sequence_ );
250  }
251 
252  // -------------------------------------------------------------------------
253  // Data Members
254  // -------------------------------------------------------------------------
255 
256  private:
257 
258  // Parent. If null, this indicates the end of the input and that we are done iterating.
259  FastxInputStream const* parent_ = nullptr;
260 
261  // Data stream to read from.
262  std::shared_ptr<utils::InputStream> input_stream_;
263 
264  // The sequence that we parse the input into and expose to the user.
265  Sequence sequence_;
266  };
267 
268  // ======================================================================================
269  // Main Class
270  // ======================================================================================
271 
272  // -------------------------------------------------------------------------
273  // Constructors and Rule of Five
274  // -------------------------------------------------------------------------
275 
280  : input_source_( nullptr )
281  , reader_()
282  {}
283 
289  std::shared_ptr<utils::BaseInputSource> source
290  )
291  : input_source_( source )
292  , reader_()
293  {}
294 
300  std::shared_ptr<utils::BaseInputSource> source,
301  Reader const& reader
302  )
303  : input_source_( source )
304  , reader_( reader )
305  {}
306 
307  ~FastxInputStream() = default;
308 
309  FastxInputStream( self_type const& ) = default;
310  FastxInputStream( self_type&& ) = default;
311 
312  self_type& operator= ( self_type const& ) = default;
313  self_type& operator= ( self_type&& ) = default;
314 
315  // -------------------------------------------------------------------------
316  // Iteration
317  // -------------------------------------------------------------------------
318 
319  Iterator begin() const
320  {
321  return Iterator( this );
322  }
323 
324  Iterator end() const
325  {
326  return Iterator();
327  }
328 
329  // -------------------------------------------------------------------------
330  // Settings
331  // -------------------------------------------------------------------------
332 
333  std::shared_ptr<utils::BaseInputSource> input_source() const
334  {
335  return input_source_;
336  }
337 
338  Reader& reader()
339  {
340  return reader_;
341  }
342 
343  Reader const& reader() const
344  {
345  return reader_;
346  }
347 
348  // -------------------------------------------------------------------------
349  // Data Members
350  // -------------------------------------------------------------------------
351 
352 private:
353 
354  std::shared_ptr<utils::BaseInputSource> input_source_;
355  Reader reader_;
356 };
357 
358 } // namespace sequence
359 } // namespace genesis
360 
361 #endif // include guard
genesis::sequence::FastxInputStream::self_type
FastxInputStream self_type
Definition: fastx_input_stream.hpp:97
genesis::sequence::FastxInputStream::Iterator::operator*
value_type & operator*()
Definition: fastx_input_stream.hpp:185
genesis::sequence::FastxInputStream::difference_type
std::ptrdiff_t difference_type
Definition: fastx_input_stream.hpp:101
fastq_reader.hpp
fasta_reader.hpp
genesis::sequence::FastxInputStream::reader
Reader & reader()
Definition: fastx_input_stream.hpp:338
genesis::sequence::FastxInputStream::Iterator::operator->
value_type * operator->()
Definition: fastx_input_stream.hpp:175
genesis::sequence::FastxInputStream::Iterator::operator=
Iterator & operator=(self_type const &)=default
genesis::sequence::FastxInputStream::operator=
self_type & operator=(self_type const &)=default
genesis::sequence::Sequence
Definition: sequence/sequence.hpp:40
genesis::sequence::FastxInputStream::Iterator
Internal iterator over the sequences.
Definition: fastx_input_stream.hpp:116
genesis::sequence::FastxInputStream::Iterator::pointer
value_type const * pointer
Definition: fastx_input_stream.hpp:126
genesis::sequence::FastxInputStream::reader
Reader const & reader() const
Definition: fastx_input_stream.hpp:343
std.hpp
Provides some valuable additions to STD.
genesis::sequence::FastxInputStream::Iterator::operator!=
bool operator!=(self_type const &it) const
Definition: fastx_input_stream.hpp:221
genesis::sequence::FastxInputStream::input_source
std::shared_ptr< utils::BaseInputSource > input_source() const
Definition: fastx_input_stream.hpp:333
input_source.hpp
input_stream.hpp
genesis::sequence::FastxInputStream::~FastxInputStream
~FastxInputStream()=default
genesis::sequence::FastxInputStream::Iterator::operator++
self_type & operator++()
Definition: fastx_input_stream.hpp:194
genesis::sequence::FastxInputStream::Iterator::reference
value_type const & reference
Definition: fastx_input_stream.hpp:127
genesis::sequence::FastxInputStream::Iterator::operator*
value_type const & operator*() const
Definition: fastx_input_stream.hpp:180
genesis::sequence::FastxInputStream::Iterator::iterator_category
std::input_iterator_tag iterator_category
Definition: fastx_input_stream.hpp:129
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::sequence::FastxInputStream::Iterator::~Iterator
~Iterator()=default
genesis::sequence::FastxInputStream::Iterator::operator->
value_type const * operator->() const
Definition: fastx_input_stream.hpp:170
genesis::sequence::FastxInputStream::FastxInputStream
FastxInputStream(std::shared_ptr< utils::BaseInputSource > source)
Create an instance that reads from an input source, using a default FastaReader or FastqReader.
Definition: fastx_input_stream.hpp:288
genesis::sequence::FastxInputStream::Iterator::operator==
bool operator==(self_type const &it) const
Compare two iterators for equality.
Definition: fastx_input_stream.hpp:216
genesis::sequence::FastxInputStream::Iterator::difference_type
std::ptrdiff_t difference_type
Definition: fastx_input_stream.hpp:128
genesis::sequence::FastxInputStream
Stream through an input source and parse it as Fasta or Fastq sequences.
Definition: fastx_input_stream.hpp:55
genesis::sequence::FastxInputStream::Iterator::FastxInputStream
friend FastxInputStream
Definition: fastx_input_stream.hpp:164
genesis::sequence::FastxInputStream::iterator_category
std::input_iterator_tag iterator_category
Definition: fastx_input_stream.hpp:102
genesis::sequence::FastxInputStream::FastxInputStream
FastxInputStream(std::shared_ptr< utils::BaseInputSource > source, Reader const &reader)
Create an instance that reads from an input source, using the settings of a given FastaReader oder Fa...
Definition: fastx_input_stream.hpp:299
genesis::sequence::FastxInputStream::FastxInputStream
FastxInputStream()
Create a default instance, with no input.
Definition: fastx_input_stream.hpp:279
genesis::sequence::FastxInputStream::end
Iterator end() const
Definition: fastx_input_stream.hpp:324
sequence.hpp
genesis::sequence::FastxInputStream::begin
Iterator begin() const
Definition: fastx_input_stream.hpp:319