A library for working with phylogenetic data.
v0.25.0
simple_pileup_input_iterator.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_POPULATION_FORMATS_SIMPLE_PILEUP_INPUT_ITERATOR_H_
2 #define GENESIS_POPULATION_FORMATS_SIMPLE_PILEUP_INPUT_ITERATOR_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2021 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 <lczech@carnegiescience.edu>
23  Department of Plant Biology, Carnegie Institution For Science
24  260 Panama Street, Stanford, CA 94305, USA
25 */
26 
36 
37 #include <string>
38 
39 namespace genesis {
40 namespace population {
41 
42 // =================================================================================================
43 // Simple Pileup Input Iterator
44 // =================================================================================================
45 
75 {
76 public:
77 
78  // -------------------------------------------------------------------------
79  // Member Types
80  // -------------------------------------------------------------------------
81 
84  using pointer = value_type*;
86  using const_reference = value_type const&;
87  using difference_type = std::ptrdiff_t;
88  using iterator_category = std::input_iterator_tag;
89 
90  // -------------------------------------------------------------------------
91  // Constructors and Rule of Five
92  // -------------------------------------------------------------------------
93 
97  SimplePileupInputIterator() = default;
98 
104  std::shared_ptr< utils::BaseInputSource > source,
105  SimplePileupReader const& reader = {}
106  )
107  : input_stream_( std::make_shared<utils::InputStream>( source ))
108  , reader_( reader )
109  {
110  // Read the first record of the file.
111  increment();
112  }
113 
120  std::shared_ptr< utils::BaseInputSource > source,
121  std::vector<size_t> const& sample_indices,
122  SimplePileupReader const& reader = {}
123  )
124  : input_stream_( std::make_shared<utils::InputStream>( source ))
125  , reader_( reader )
126  , use_sample_filter_( true )
127  {
128  // Prepare the sample filter from the indices.
129  sample_filter_ = SimplePileupReader::make_sample_filter( sample_indices );
130 
131  // Read the first record of the file.
132  increment();
133  }
134 
141  std::shared_ptr< utils::BaseInputSource > source,
142  std::vector<bool> const& sample_filter,
143  SimplePileupReader const& reader = {}
144  )
145  : input_stream_( std::make_shared<utils::InputStream>( source ))
146  , reader_( reader )
147  , sample_filter_( sample_filter )
148  , use_sample_filter_( true )
149  {
150  // Read the first record of the file.
151  increment();
152  }
153 
154  ~SimplePileupInputIterator() = default;
155 
156  SimplePileupInputIterator( self_type const& ) = default;
157  SimplePileupInputIterator( self_type&& ) = default;
158 
159  self_type& operator= ( self_type const& ) = default;
160  self_type& operator= ( self_type&& ) = default;
161 
162  // -------------------------------------------------------------------------
163  // Comparators
164  // -------------------------------------------------------------------------
165 
169  explicit operator bool() const
170  {
171  return good_;
172  }
173 
174  bool good() const
175  {
176  return good_;
177  }
178 
179  // -------------------------------------------------------------------------
180  // Accessors
181  // -------------------------------------------------------------------------
182 
184  {
185  return record_;
186  }
187 
189  {
190  return record_;
191  }
192 
194  {
195  return &record_;
196  }
197 
199  {
200  return &record_;
201  }
202 
204  {
205  return record_;
206  }
207 
209  {
210  return record_;
211  }
212 
213  // -------------------------------------------------------------------------
214  // Iteration
215  // -------------------------------------------------------------------------
216 
218  {
219  increment();
220  return *this;
221  }
222 
224  {
225  increment();
226  return *this;
227  }
228 
229  void increment()
230  {
231  if( use_sample_filter_ ) {
232  good_ = reader_.parse_line( *input_stream_, record_, sample_filter_ );
233  } else {
234  good_ = reader_.parse_line( *input_stream_, record_ );
235  }
236  }
237 
238  bool operator==( self_type const& it ) const
239  {
240  return good_ == it.good_;
241  }
242 
243  bool operator!=( self_type const& it ) const
244  {
245  return !(*this == it);
246  }
247 
248  // -------------------------------------------------------------------------
249  // Data Members
250  // -------------------------------------------------------------------------
251 
252 private:
253 
254  // Basic iterator setup and input.
255  bool good_ = false;
256  std::shared_ptr<utils::InputStream> input_stream_;
257 
258  // Reading into records
260  SimplePileupReader reader_;
261 
262  // Sample filtering
263  std::vector<bool> sample_filter_;
264  bool use_sample_filter_ = false;
265 };
266 
267 } // namespace population
268 } // namespace genesis
269 
270 #endif // include guard
genesis::population::SimplePileupInputIterator::self_type
SimplePileupInputIterator self_type
Definition: simple_pileup_input_iterator.hpp:82
genesis::population::SimplePileupInputIterator::operator*
SimplePileupReader::Record & operator*()
Definition: simple_pileup_input_iterator.hpp:208
genesis::population::SimplePileupReader
Reader for line-by-line assessment of (m)pileup files.
Definition: simple_pileup_reader.hpp:68
genesis::population::SimplePileupInputIterator::operator!=
bool operator!=(self_type const &it) const
Definition: simple_pileup_input_iterator.hpp:243
genesis::population::SimplePileupInputIterator::operator=
self_type & operator=(self_type const &)=default
genesis::population::SimplePileupInputIterator::record
SimplePileupReader::Record & record()
Definition: simple_pileup_input_iterator.hpp:188
genesis::population::SimplePileupInputIterator::increment
void increment()
Definition: simple_pileup_input_iterator.hpp:229
input_source.hpp
genesis::population::SimplePileupInputIterator::difference_type
std::ptrdiff_t difference_type
Definition: simple_pileup_input_iterator.hpp:87
genesis::population::SimplePileupReader::Record
Single line/record from a pileup file.
Definition: simple_pileup_reader.hpp:141
genesis::population::SimplePileupInputIterator::SimplePileupInputIterator
SimplePileupInputIterator(std::shared_ptr< utils::BaseInputSource > source, SimplePileupReader const &reader={})
Create an instance that reads from an input source, and optionally take a SimplePileupReader with set...
Definition: simple_pileup_input_iterator.hpp:103
genesis::population::SimplePileupInputIterator::SimplePileupInputIterator
SimplePileupInputIterator(std::shared_ptr< utils::BaseInputSource > source, std::vector< size_t > const &sample_indices, SimplePileupReader const &reader={})
Create an instance that reads from an input source, using only the samples at the indices given in th...
Definition: simple_pileup_input_iterator.hpp:119
genesis::population::SimplePileupInputIterator
Iterate an input source and parse it as a (m)pileup file.
Definition: simple_pileup_input_iterator.hpp:74
simple_pileup_reader.hpp
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::population::SimplePileupInputIterator::good
bool good() const
Definition: simple_pileup_input_iterator.hpp:174
genesis::population::SimplePileupInputIterator::~SimplePileupInputIterator
~SimplePileupInputIterator()=default
genesis::population::SimplePileupInputIterator::SimplePileupInputIterator
SimplePileupInputIterator()=default
Create a default instance, with no input.
genesis::population::SimplePileupReader::parse_line
bool parse_line(utils::InputStream &input_stream, Record &record) const
Read an (m)pileup line.
Definition: simple_pileup_reader.cpp:95
genesis::population::SimplePileupInputIterator::operator->
SimplePileupReader::Record const * operator->() const
Definition: simple_pileup_input_iterator.hpp:193
genesis::population::SimplePileupInputIterator::record
SimplePileupReader::Record const & record() const
Definition: simple_pileup_input_iterator.hpp:183
genesis::population::SimplePileupReader::make_sample_filter
static std::vector< bool > make_sample_filter(std::vector< size_t > const &indices)
Helper function to create a sample filter from a list of sample indices.
Definition: simple_pileup_reader.cpp:114
genesis::population::SimplePileupInputIterator::SimplePileupInputIterator
SimplePileupInputIterator(std::shared_ptr< utils::BaseInputSource > source, std::vector< bool > const &sample_filter, SimplePileupReader const &reader={})
Create an instance that reads from an input source, using only the samples at the indices where the s...
Definition: simple_pileup_input_iterator.hpp:140
genesis::population::SimplePileupInputIterator::operator==
bool operator==(self_type const &it) const
Definition: simple_pileup_input_iterator.hpp:238
genesis::population::SimplePileupInputIterator::const_reference
value_type const & const_reference
Definition: simple_pileup_input_iterator.hpp:86
genesis::population::SimplePileupInputIterator::iterator_category
std::input_iterator_tag iterator_category
Definition: simple_pileup_input_iterator.hpp:88
genesis::population::SimplePileupInputIterator::operator*
SimplePileupReader::Record const & operator*() const
Definition: simple_pileup_input_iterator.hpp:203
genesis::population::SimplePileupInputIterator::operator++
self_type & operator++()
Definition: simple_pileup_input_iterator.hpp:217