A library for working with phylogenetic and population genetic data.
v0.32.0
simple_pileup_input_stream.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_POPULATION_FORMAT_SIMPLE_PILEUP_INPUT_STREAM_H_
2 #define GENESIS_POPULATION_FORMAT_SIMPLE_PILEUP_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 <lczech@carnegiescience.edu>
23  Department of Plant Biology, Carnegie Institution For Science
24  260 Panama Street, Stanford, CA 94305, USA
25 */
26 
37 
38 #include <string>
39 
40 namespace genesis {
41 namespace population {
42 
43 // =================================================================================================
44 // Simple Pileup Input Stream
45 // =================================================================================================
46 
78 template<class T = SimplePileupReader::Record>
80 {
81 public:
82 
83  // -------------------------------------------------------------------------
84  // Member Types
85  // -------------------------------------------------------------------------
86 
88  using value_type = T;
89  using pointer = value_type*;
91  using const_reference = value_type const&;
92  using difference_type = std::ptrdiff_t;
93  using iterator_category = std::input_iterator_tag;
94 
95  // -------------------------------------------------------------------------
96  // Constructors and Rule of Five
97  // -------------------------------------------------------------------------
98 
102  SimplePileupInputStream() = default;
103 
109  std::shared_ptr< utils::BaseInputSource > source,
110  SimplePileupReader const& reader = {}
111  )
112  : input_stream_( std::make_shared<utils::InputStream>( source ))
113  , reader_( reader )
114  {
115  // Read the first record of the file.
116  increment();
117  }
118 
119  // /* *
120  // * @brief Create an instance that reads from an input source, using only the samples at the
121  // * indices given in the @p sample_indices, and optionally take a SimplePileupReader with
122  // * settings to be used.
123  // */
124  // SimplePileupInputStream(
125  // std::shared_ptr< utils::BaseInputSource > source,
126  // std::vector<size_t> const& sample_indices,
127  // SimplePileupReader const& reader = {}
128  // )
129  // : input_stream_( std::make_shared<utils::InputStream>( source ))
130  // , reader_( reader )
131  // , use_sample_filter_( true )
132  // {
133  // // Prepare the sample filter from the indices.
134  // sample_filter_ = utils::make_bool_vector_from_indices( sample_indices );
135  //
136  // // Read the first record of the file.
137  // increment();
138  // }
139 
146  std::shared_ptr< utils::BaseInputSource > source,
147  std::vector<bool> const& sample_filter,
148  SimplePileupReader const& reader = {}
149  )
150  : input_stream_( std::make_shared<utils::InputStream>( source ))
151  , reader_( reader )
152  , sample_filter_( sample_filter )
153  , use_sample_filter_( true )
154  {
155  // Read the first record of the file.
156  increment();
157  }
158 
159  ~SimplePileupInputStream() = default;
160 
161  SimplePileupInputStream( self_type const& ) = default;
162  SimplePileupInputStream( self_type&& ) = default;
163 
164  self_type& operator= ( self_type const& ) = default;
165  self_type& operator= ( self_type&& ) = default;
166 
167  // -------------------------------------------------------------------------
168  // Comparators
169  // -------------------------------------------------------------------------
170 
174  explicit operator bool() const
175  {
176  return good_;
177  }
178 
179  bool good() const
180  {
181  return good_;
182  }
183 
184  // -------------------------------------------------------------------------
185  // Accessors
186  // -------------------------------------------------------------------------
187 
188  SimplePileupReader const& reader() const
189  {
190  return reader_;
191  }
192 
193  T const& record() const
194  {
195  return record_;
196  }
197 
198  T& record()
199  {
200  return record_;
201  }
202 
203  T const* operator ->() const
204  {
205  return &record_;
206  }
207 
209  {
210  return &record_;
211  }
212 
213  T const& operator*() const
214  {
215  return record_;
216  }
217 
219  {
220  return record_;
221  }
222 
223  // -------------------------------------------------------------------------
224  // Iteration
225  // -------------------------------------------------------------------------
226 
228  {
229  increment();
230  return *this;
231  }
232 
234  {
235  increment();
236  return *this;
237  }
238 
239  void increment()
240  {
241  increment_();
242  }
243 
244  bool operator==( self_type const& it ) const
245  {
246  return good_ == it.good_;
247  }
248 
249  bool operator!=( self_type const& it ) const
250  {
251  return !(*this == it);
252  }
253 
254  // -------------------------------------------------------------------------
255  // Internal Members
256  // -------------------------------------------------------------------------
257 
258 private:
259 
260  void increment_();
261 
262  // -------------------------------------------------------------------------
263  // Data Members
264  // -------------------------------------------------------------------------
265 
266 private:
267 
268  // Basic iterator setup and input.
269  bool good_ = false;
270  std::shared_ptr<utils::InputStream> input_stream_;
271 
272  // Reading into records
273  SimplePileupReader reader_;
274  size_t sample_size_ = 0;
275  T record_;
276 
277  // Sample filtering
278  std::vector<bool> sample_filter_;
279  bool use_sample_filter_ = false;
280 };
281 
282 // -------------------------------------------------------------------------
283 // Explicit Specialiyations in Namespace Scope
284 // -------------------------------------------------------------------------
285 
286 template<>
287 inline void SimplePileupInputStream<SimplePileupReader::Record>::increment_()
288 {
289  // We resize to the size that we had before (or 0 if we are just starting),
290  // so that the parser can check the correct sample size.
291  // We cannot rely on the samples keeping their size, as the user might have moved the data.
292  record_.samples.resize( sample_size_ );
293  if( use_sample_filter_ ) {
294  good_ = reader_.parse_line_record( *input_stream_, record_, sample_filter_ );
295  } else {
296  good_ = reader_.parse_line_record( *input_stream_, record_ );
297  }
298  sample_size_ = record_.samples.size();
299 }
300 
301 template<>
302 inline void SimplePileupInputStream<Variant>::increment_()
303 {
304  // Same as above
305  record_.samples.resize( sample_size_ );
306  if( use_sample_filter_ ) {
307  good_ = reader_.parse_line_variant( *input_stream_, record_, sample_filter_ );
308  } else {
309  good_ = reader_.parse_line_variant( *input_stream_, record_ );
310  }
311  sample_size_ = record_.samples.size();
312 }
313 
314 } // namespace population
315 } // namespace genesis
316 
317 #endif // include guard
genesis::population::SimplePileupInputStream::operator->
T const * operator->() const
Definition: simple_pileup_input_stream.hpp:203
helper.hpp
genesis::population::SimplePileupReader
Reader for line-by-line assessment of (m)pileup files.
Definition: simple_pileup_reader.hpp:78
genesis::population::SimplePileupInputStream::SimplePileupInputStream
SimplePileupInputStream(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_stream.hpp:145
genesis::population::SimplePileupInputStream::operator=
self_type & operator=(self_type const &)=default
genesis::population::SimplePileupInputStream::operator*
T const & operator*() const
Definition: simple_pileup_input_stream.hpp:213
genesis::population::SimplePileupInputStream::SimplePileupInputStream
SimplePileupInputStream()=default
Create a default instance, with no input.
genesis::population::SimplePileupInputStream::operator!=
bool operator!=(self_type const &it) const
Definition: simple_pileup_input_stream.hpp:249
genesis::population::SimplePileupInputStream::good
bool good() const
Definition: simple_pileup_input_stream.hpp:179
genesis::population::SimplePileupInputStream::pointer
value_type * pointer
Definition: simple_pileup_input_stream.hpp:89
input_source.hpp
genesis::population::SimplePileupInputStream::reference
value_type & reference
Definition: simple_pileup_input_stream.hpp:90
genesis::population::SimplePileupInputStream::operator*
T & operator*()
Definition: simple_pileup_input_stream.hpp:218
genesis::population::SimplePileupInputStream
Iterate an input source and parse it as a (m)pileup file.
Definition: simple_pileup_input_stream.hpp:79
genesis::population::SimplePileupInputStream::operator++
self_type & operator++()
Definition: simple_pileup_input_stream.hpp:227
genesis::population::SimplePileupInputStream::record
T & record()
Definition: simple_pileup_input_stream.hpp:198
genesis::population::SimplePileupInputStream::iterator_category
std::input_iterator_tag iterator_category
Definition: simple_pileup_input_stream.hpp:93
genesis::population::SimplePileupInputStream::value_type
T value_type
Definition: simple_pileup_input_stream.hpp:88
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::SimplePileupInputStream::reader
SimplePileupReader const & reader() const
Definition: simple_pileup_input_stream.hpp:188
genesis::population::SimplePileupInputStream::operator==
bool operator==(self_type const &it) const
Definition: simple_pileup_input_stream.hpp:244
genesis::population::SimplePileupInputStream::increment
void increment()
Definition: simple_pileup_input_stream.hpp:239
genesis::population::SimplePileupInputStream::difference_type
std::ptrdiff_t difference_type
Definition: simple_pileup_input_stream.hpp:92
genesis::population::SimplePileupInputStream::record
T const & record() const
Definition: simple_pileup_input_stream.hpp:193
genesis::population::SimplePileupInputStream::SimplePileupInputStream
SimplePileupInputStream(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_stream.hpp:108
genesis::population::SimplePileupInputStream::self_type
SimplePileupInputStream self_type
Definition: simple_pileup_input_stream.hpp:87
genesis::population::SimplePileupReader::parse_line_record
bool parse_line_record(utils::InputStream &input_stream, Record &record) const
Read an (m)pileup line, as a Record.
Definition: simple_pileup_reader.cpp:162
genesis::population::SimplePileupInputStream::const_reference
value_type const & const_reference
Definition: simple_pileup_input_stream.hpp:91
genesis::population::SimplePileupInputStream::~SimplePileupInputStream
~SimplePileupInputStream()=default