A library for working with phylogenetic and population genetic data.
v0.27.0
fastq_input_iterator.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_SEQUENCE_FORMATS_FASTQ_INPUT_ITERATOR_H_
2 #define GENESIS_SEQUENCE_FORMATS_FASTQ_INPUT_ITERATOR_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 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 
39 
40 #include <cstddef>
41 #include <iterator>
42 #include <memory>
43 #include <sstream>
44 
45 namespace genesis {
46 namespace sequence {
47 
48 // =================================================================================================
49 // Fastq 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 FastqInputIterator( 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  FastqInputIterator( std::shared_ptr<utils::BaseInputSource> source, FastqReader const& settings )
131  : input_stream_( std::make_shared<utils::InputStream>( source ))
132  , sequence_()
133  , reader_( settings )
134  {
135  increment();
136  }
137 
138  ~FastqInputIterator() = default;
139 
140  FastqInputIterator( self_type const& ) = default;
141  FastqInputIterator( 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 FastqInputIterator();
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  FastqReader reader_;
250 };
251 
252 } // namespace sequence
253 } // namespace genesis
254 
255 #endif // include guard
genesis::sequence::FastqInputIterator::difference_type
std::ptrdiff_t difference_type
Definition: fastq_input_iterator.hpp:99
genesis::sequence::FastqInputIterator::operator++
self_type & operator++()
Definition: fastq_input_iterator.hpp:212
fastq_reader.hpp
genesis::sequence::FastqInputIterator::dereference
value_type const & dereference() const
Definition: fastq_input_iterator.hpp:182
genesis::sequence::Sequence
Definition: sequence/sequence.hpp:40
genesis::sequence::FastqInputIterator::increment
void increment()
Definition: fastq_input_iterator.hpp:225
genesis::sequence::FastqInputIterator::FastqInputIterator
FastqInputIterator(std::shared_ptr< utils::BaseInputSource > source, FastqReader const &settings)
Create an instance that reads from an input source, using the settings of a given FastqReader.
Definition: fastq_input_iterator.hpp:130
genesis::sequence::FastqReader
Read Fastq sequence data.
Definition: fastq_reader.hpp:149
std.hpp
Provides some valuable additions to STD.
genesis::sequence::FastqInputIterator::iterator_category
std::input_iterator_tag iterator_category
Definition: fastq_input_iterator.hpp:100
input_source.hpp
genesis::sequence::FastqInputIterator::FastqInputIterator
FastqInputIterator()
Create a default instance, with no input.
Definition: fastq_input_iterator.hpp:109
input_stream.hpp
genesis::sequence::FastqInputIterator::FastqInputIterator
FastqInputIterator(std::shared_ptr< utils::BaseInputSource > source)
Create an instance that reads from an input source, using a default FastqReader.
Definition: fastq_input_iterator.hpp:118
genesis::sequence::FastqInputIterator::begin
self_type & begin()
Beginning of the iterator.
Definition: fastq_input_iterator.hpp:197
genesis::sequence::FastqInputIterator::self_type
FastqInputIterator self_type
Definition: fastq_input_iterator.hpp:95
genesis::sequence::FastqInputIterator::end
self_type end()
End of the iterator.
Definition: fastq_input_iterator.hpp:207
genesis::sequence::FastqInputIterator::operator=
self_type & operator=(self_type const &)=default
genesis::sequence::FastqInputIterator::operator->
value_type const * operator->() const
Definition: fastq_input_iterator.hpp:177
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::FastqInputIterator::operator==
bool operator==(self_type const &other) const
Definition: fastq_input_iterator.hpp:150
genesis::sequence::FastqInputIterator
Iterate an input source and parse it as Fastq sequences.
Definition: fastq_input_iterator.hpp:87
genesis::sequence::FastqInputIterator::operator!=
bool operator!=(self_type const &other) const
Definition: fastq_input_iterator.hpp:155
genesis::sequence::FastqInputIterator::operator*
value_type const & operator*() const
Definition: fastq_input_iterator.hpp:172
genesis::sequence::FastqReader::parse_sequence
bool parse_sequence(utils::InputStream &input_stream, Sequence &sequence) const
Parse a Sequence in Fastq format.
Definition: fastq_reader.cpp:95
sequence.hpp
genesis::sequence::FastqInputIterator::~FastqInputIterator
~FastqInputIterator()=default