A toolkit for working with phylogenetic data.
v0.18.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-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 
37 
38 #include <iterator>
39 
40 namespace genesis {
41 namespace sequence {
42 
43 // =================================================================================================
44 // Fasta Input Iterator
45 // =================================================================================================
46 
67 {
68 public:
69 
70  // -------------------------------------------------------------------------
71  // Member Types
72  // -------------------------------------------------------------------------
73 
75  using iterator_category = std::input_iterator_tag;
76 
77  // -------------------------------------------------------------------------
78  // Constructors and Rule of Five
79  // -------------------------------------------------------------------------
80 
82  : reader_()
83  , input_stream_( nullptr )
84  , sequence_()
85  {}
86 
88  : reader_()
89  , input_stream_( &in )
90  , sequence_()
91  {
92  // Read first sequence.
93  increment();
94  }
95 
97  : reader_( reader )
98  , input_stream_( &in )
99  , sequence_()
100  {
101  // Read first sequence.
102  increment();
103  }
104 
105  ~FastaInputIterator() = default;
106 
107  FastaInputIterator( self_type const& ) = delete;
108  FastaInputIterator( self_type&& ) = default;
109 
110  self_type& operator= ( self_type const& ) = delete;
111  self_type& operator= ( self_type&& ) = default;
112 
113  // -------------------------------------------------------------------------
114  // Comparators
115  // -------------------------------------------------------------------------
116 
117  bool operator == ( self_type const& other ) const
118  {
119  return input_stream_ == other.input_stream_;
120  }
121 
122  bool operator != ( self_type const& other ) const
123  {
124  return !( *this == other );
125  }
126 
130  explicit operator bool() const
131  {
132  return good_;
133  }
134 
135  // -------------------------------------------------------------------------
136  // Accessors
137  // -------------------------------------------------------------------------
138 
139  Sequence const& operator * () const
140  {
141  return dereference();
142  }
143 
144  Sequence const* operator -> () const
145  {
146  return &dereference();
147  }
148 
149  Sequence const& dereference() const
150  {
151  return sequence_;
152  }
153 
160  {
161  return reader_;
162  }
163 
164  // -------------------------------------------------------------------------
165  // Iteration
166  // -------------------------------------------------------------------------
167 
169  {
170  increment();
171  return *this;
172  }
173 
174  void increment()
175  {
176  // Check whether the input stream is good (not end-of-stream) and can be read from.
177  // If not, we reached its end, so we stop reading in the next iteration.
178  if( input_stream_ == nullptr || ! *input_stream_ ) {
179  good_ = false;
180  return;
181  }
182 
183  reader_.parse_sequence( *input_stream_, sequence_ );
184  }
185 
186  // -------------------------------------------------------------------------
187  // Data Members
188  // -------------------------------------------------------------------------
189 
190 private:
191 
192  FastaReader reader_;
193  utils::InputStream* input_stream_;
194  Sequence sequence_;
195  bool good_ = true;
196 };
197 
198 } // namespace sequence
199 } // namespace genesis
200 
201 #endif // include guard
FastaInputIterator(utils::InputStream &in, FastaReader const &reader)
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.
bool operator==(self_type const &other) const
bool operator!=(self_type const &other) const
self_type & operator=(self_type const &)=delete
Iterate an input stream and parse it as fasta sequences.
Stream interface for reading data from an InputSource, that keeps track of line and column counters...