A library for working with phylogenetic and population genetic data.
v0.27.0
genome_region.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_POPULATION_GENOME_REGION_H_
2 #define GENESIS_POPULATION_GENOME_REGION_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2022 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 
34 #include <iosfwd>
35 #include <stdexcept>
36 #include <string>
37 #include <type_traits>
38 #include <vector>
39 
40 namespace genesis {
41 namespace population {
42 
43 // =================================================================================================
44 // Genome Region
45 // =================================================================================================
46 
61 {
62 public:
63 
64  std::string chromosome;
65  size_t start = 0;
66  size_t end = 0;
67 
68  GenomeRegion( std::string const& chr = "", size_t s = 0, size_t e = 0 )
69  : chromosome( chr )
70  , start(s)
71  , end(e)
72  {
73  if( s > e ) {
74  throw std::invalid_argument(
75  "Cannot create GenomeRegion with start == " +
76  std::to_string( start ) + " > end == " + std::to_string( end )
77  );
78  }
79  }
80 
81  bool empty() const
82  {
83  return chromosome == "" && start == 0 && end == 0;
84  }
85 
86  bool valid() const
87  {
88  return chromosome != "" && start != 0 && end != 0 && start <= end;
89  }
90 };
91 
92 // Alternative version that also has a data field.
93 //
94 // // =================================================================================================
95 // // Genome Region
96 // // =================================================================================================
97 //
98 // /**
99 // * @brief A region (between a start and and end position) on a chromosome.
100 // *
101 // * This can be used to represent a gene, a feature, or just generally a region of interest.
102 // * We use a simple form with a chromosome name, and a start and end position, both inclusive,
103 // * that is, a closed interval. Both start and end can also be identical, in which case they
104 // * denote a single position; see also GenomeLocus for a class representing this.
105 // *
106 // * We use 1-based positions, in order to comply with common chromosome annotation formats.
107 // * Furthermore, we use an empty chromosome string and/or positions 0 as indicators of an empty or
108 // * default constructed locus.
109 // *
110 // * The class takes an extra data type as template parameter, which can be used to store
111 // * arbitrary data for this locus.
112 // *
113 // * @see GenomeLocus
114 // * @see GenomeRegionList
115 // */
116 // template<class DataType = EmptyGenomeData>
117 // class GenomeRegion
118 // {
119 // public:
120 //
121 // // -------------------------------------------------------------------------
122 // // Typedefs
123 // // -------------------------------------------------------------------------
124 //
125 // using data_type = DataType;
126 //
127 // // -------------------------------------------------------------------------
128 // // Constructors and Rule of Five
129 // // -------------------------------------------------------------------------
130 //
131 // /**
132 // * @brief Default construct an empty region.
133 // */
134 // GenomeRegion() = default;
135 //
136 // /**
137 // * @brief Construct a region with a chromosome and a start and end position.
138 // */
139 // GenomeRegion( std::string const& chr, size_t start, size_t end )
140 // : chromosome_( chr )
141 // , start_(start)
142 // , end_(end)
143 // : GenomeRegion( chr, start, end, DataType{} )
144 // {
145 // if( chr.empty() || start == 0 || end == 0 ) {
146 // throw std::invalid_argument(
147 // "Cannot construct GenomeRegion with empty chromosome or position zero."
148 // );
149 // }
150 // if( start > end ) {
151 // throw std::invalid_argument(
152 // "Cannot construct GenomeRegion with start > end."
153 // );
154 // }
155 // }
156 //
157 // /**
158 // * @brief Construct a region with a chromosome and a start and end position,
159 // * and copy the data.
160 // */
161 // GenomeRegion( std::string const& chr, size_t start, size_t end, DataType const& data )
162 // : GenomeRegion( chr, start, end, DataType{ data })
163 // {}
164 //
165 // /**
166 // * @brief Construct a region with a chromosome and a start and end position,
167 // * and move the data.
168 // */
169 // GenomeRegion( std::string const& chr, size_t start, size_t end, DataType&& data )
170 // : chromosome_( chr )
171 // , start_(start)
172 // , end_(end)
173 // , data_( std::move( data ))
174 // {
175 // if( chr.empty() || start == 0 || end == 0 ) {
176 // throw std::invalid_argument(
177 // "Cannot construct GenomeRegion with empty chromosome or position zero."
178 // );
179 // }
180 // }
181 //
182 // /**
183 // * @brief Construct a region from a GenomeLocus of the same data type.
184 // *
185 // * This uses the GenomeLocus::position() for both start and end, and copies the data.
186 // */
187 // template<
188 // class OtherDataType,
189 // typename std::enable_if< std::is_same<DataType, OtherDataType>::value >::type = 0
190 // >
191 // GenomeRegion( GenomeLocus<OtherDataType> const& locus )
192 // : GenomeRegion(
193 // locus.chromosome(), locus.position(), locus.position(), locus.data()
194 // )
195 // {}
196 //
197 // /**
198 // * @brief Construct a region from a GenomeLocus of the same data type.
199 // *
200 // * This uses the GenomeLocus::position() for both start and end, and moves the data.
201 // */
202 // template<
203 // class OtherDataType,
204 // typename std::enable_if< std::is_same<DataType, OtherDataType>::value >::type = 0
205 // >
206 // GenomeRegion( GenomeLocus<OtherDataType>&& locus )
207 // : GenomeRegion(
208 // locus.chromosome(), locus.position(), locus.position(), std::move( locus.data() )
209 // )
210 // {}
211 //
212 // /**
213 // * @brief Construct a region from a GenomeLocus of a different data type.
214 // *
215 // * This uses the GenomeLocus::position() for both start and end,
216 // * and default constructs the data.
217 // */
218 // template<
219 // class OtherDataType,
220 // typename std::enable_if< ! std::is_same<DataType, OtherDataType>::value >::type = 0
221 // >
222 // GenomeRegion( GenomeLocus<OtherDataType> const& locus )
223 // : GenomeRegion( locus.chromosome(), locus.position(), locus.position() )
224 // {}
225 //
226 // ~GenomeRegion() = default;
227 //
228 // GenomeRegion( GenomeRegion const& ) = default;
229 // GenomeRegion( GenomeRegion&& ) = default;
230 //
231 // GenomeRegion& operator= ( GenomeRegion const& ) = default;
232 // GenomeRegion& operator= ( GenomeRegion&& ) = default;
233 //
234 // // -------------------------------------------------------------------------
235 // // Accessors
236 // // -------------------------------------------------------------------------
237 //
238 // std::string const& chromosome() const
239 // {
240 // return chromosome_;
241 // }
242 //
243 // size_t start() const
244 // {
245 // return start_;
246 // }
247 //
248 // size_t end() const
249 // {
250 // return end_;
251 // }
252 //
253 // data_type& data()
254 // {
255 // return data_;
256 // }
257 //
258 // data_type const& data() const
259 // {
260 // return data_;
261 // }
262 //
263 // size_t length() const
264 // {
265 // // Closed interval, so we need to add 1.
266 // return end_ - start_ + 1;
267 // }
268 //
269 // bool empty() const
270 // {
271 // return chromosome_ == "" && start_ == 0 && end_ == 0;
272 // }
273 //
274 // // -------------------------------------------------------------------------
275 // // Operators
276 // // -------------------------------------------------------------------------
277 //
278 // operator std::string() const
279 // {
280 // return to_string();
281 // }
282 //
283 // std::string to_string() const
284 // {
285 // if( start_ == 0 && end_ == 0 ) {
286 // return chromosome_;
287 // } else if( start_ == end_ ) {
288 // return chromosome_ + ":" + std::to_string( start_ );
289 // } else {
290 // return
291 // chromosome_ + ":" +
292 // std::to_string( start_ ) + "-" +
293 // std::to_string( end_ )
294 // ;
295 // }
296 // }
297 //
298 // // -------------------------------------------------------------------------
299 // // Member Variables
300 // // -------------------------------------------------------------------------
301 //
302 // std::string chromosome_;
303 // size_t start_ = 0;
304 // size_t end_ = 0;
305 //
306 // data_type data_;
307 //
308 // };
309 
310 } // namespace population
311 } // namespace genesis
312 
313 #endif // include guard
genesis::population::GenomeRegion::start
size_t start
Definition: genome_region.hpp:65
genesis::population::GenomeRegion::chromosome
std::string chromosome
Definition: genome_region.hpp:64
genesis::population::GenomeRegion::valid
bool valid() const
Definition: genome_region.hpp:86
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: functions/genome_locus.hpp:48
genesis::population::GenomeRegion::GenomeRegion
GenomeRegion(std::string const &chr="", size_t s=0, size_t e=0)
Definition: genome_region.hpp:68
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::GenomeRegion
A region (between two positions) on a chromosome.
Definition: genome_region.hpp:60
genesis::population::GenomeRegion::empty
bool empty() const
Definition: genome_region.hpp:81
genesis::population::GenomeRegion::end
size_t end
Definition: genome_region.hpp:66