A library for working with phylogenetic and population genetic data.
v0.32.0
map_bim_reader.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_POPULATION_FORMAT_MAP_BIM_READER_H_
2 #define GENESIS_POPULATION_FORMAT_MAP_BIM_READER_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 
39 
40 #include <cstdint>
41 #include <functional>
42 #include <string>
43 #include <utility>
44 #include <vector>
45 
46 namespace genesis {
47 namespace population {
48 
49 // =================================================================================================
50 // MAP/BIM Reader
51 // =================================================================================================
52 
91 {
92 public:
93 
94  // -------------------------------------------------------------------------
95  // Typedefs and Enums
96  // -------------------------------------------------------------------------
97 
109  struct Feature
110  {
111  std::string chromosome;
112  std::string variant_id;
113  double position;
114  int64_t coordinate;
115  char allele_1;
116  char allele_2;
117  };
118 
119  // -------------------------------------------------------------------------
120  // Constructors and Rule of Five
121  // -------------------------------------------------------------------------
122 
123  MapBimReader() = default;
124  ~MapBimReader() = default;
125 
126  MapBimReader( MapBimReader const& ) = default;
127  MapBimReader( MapBimReader&& ) = default;
128 
129  MapBimReader& operator= ( MapBimReader const& ) = default;
130  MapBimReader& operator= ( MapBimReader&& ) = default;
131 
132  // ---------------------------------------------------------------------
133  // Reading
134  // ---------------------------------------------------------------------
135 
139  std::vector<Feature> read(
140  std::shared_ptr< utils::BaseInputSource > source
141  ) const;
142 
152  std::shared_ptr< utils::BaseInputSource > source
153  ) const;
154 
165  std::shared_ptr< utils::BaseInputSource > source,
166  bool merge = true
167  ) const;
168 
175  std::shared_ptr< utils::BaseInputSource > source,
176  GenomeRegionList& target,
177  bool merge = true
178  ) const;
179 
180  // -------------------------------------------------------------------------
181  // Settings
182  // -------------------------------------------------------------------------
183 
185  {
186  return skip_negative_coordinates_;
187  }
188 
190  {
191  skip_negative_coordinates_ = value;
192  return *this;
193  }
194 
195  // -------------------------------------------------------------------------
196  // Internal Helpers
197  // -------------------------------------------------------------------------
198 
199 private:
200 
205  void read_(
206  std::shared_ptr< utils::BaseInputSource > source,
207  std::function<void(Feature&&)> callback
208  ) const;
209 
217  size_t parse_line_(
218  utils::InputStream& input_stream,
219  Feature& feature,
220  std::vector<std::string>& buffer
221  ) const;
222 
223  // -------------------------------------------------------------------------
224  // Member Variables
225  // -------------------------------------------------------------------------
226 
227  bool skip_negative_coordinates_ = true;
228 
229 };
230 
231 } // namespace population
232 } // namespace genesis
233 
234 #endif // include guard
genesis::population::MapBimReader::operator=
MapBimReader & operator=(MapBimReader const &)=default
genesis::utils::InputStream
Stream interface for reading data from an InputSource, that keeps track of line and column counters.
Definition: input_stream.hpp:88
genesis::population::MapBimReader::read_as_genome_locus_set
GenomeLocusSet read_as_genome_locus_set(std::shared_ptr< utils::BaseInputSource > source) const
Read an input source, and return its content as a GenomeLocusSet.
Definition: map_bim_reader.cpp:60
genesis::population::MapBimReader::skip_negative_coordinates
bool skip_negative_coordinates() const
Definition: map_bim_reader.hpp:184
genesis::population::merge
SampleCounts merge(SampleCounts const &p1, SampleCounts const &p2)
Merge the counts of two SampleCountss.
Definition: population/function/functions.cpp:400
genesis::population::MapBimReader::Feature::coordinate
int64_t coordinate
Definition: map_bim_reader.hpp:114
genesis::population::MapBimReader::read_as_genome_region_list
GenomeRegionList read_as_genome_region_list(std::shared_ptr< utils::BaseInputSource > source, bool merge=true) const
Read a map/bim input source, and return its content as a GenomeRegionList.
Definition: map_bim_reader.cpp:70
genesis::population::MapBimReader::read
std::vector< Feature > read(std::shared_ptr< utils::BaseInputSource > source) const
Read a map/bim input source, and return its content as a list of Feature structs.
Definition: map_bim_reader.cpp:50
genesis::population::GenomeLocusSet
List of positions/coordinates in a genome, for each chromosome.
Definition: genome_locus_set.hpp:75
genesis::population::MapBimReader::Feature
Store all values that can typically appear in the columns of a map/bim file.
Definition: map_bim_reader.hpp:109
genesis::population::GenomeRegionList
List of regions in a genome, for each chromosome.
Definition: genome_region_list.hpp:95
genesis::population::MapBimReader
Reader for map/bim files as used by PLINK.
Definition: map_bim_reader.hpp:90
genesis::population::MapBimReader::MapBimReader
MapBimReader()=default
input_source.hpp
genome_region.hpp
input_stream.hpp
genesis::population::MapBimReader::Feature::allele_1
char allele_1
Definition: map_bim_reader.hpp:115
genesis::population::MapBimReader::skip_negative_coordinates
MapBimReader & skip_negative_coordinates(bool value)
Definition: map_bim_reader.hpp:189
genome_region_list.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
genome_locus_set.hpp
genesis::population::MapBimReader::~MapBimReader
~MapBimReader()=default
genesis::population::MapBimReader::Feature::chromosome
std::string chromosome
Definition: map_bim_reader.hpp:111
genesis::population::MapBimReader::Feature::position
double position
Definition: map_bim_reader.hpp:113
genesis::population::MapBimReader::Feature::variant_id
std::string variant_id
Definition: map_bim_reader.hpp:112
genesis::population::MapBimReader::Feature::allele_2
char allele_2
Definition: map_bim_reader.hpp:116