A library for working with phylogenetic and population genetic data.
v0.32.0
genome_locus.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_POPULATION_GENOME_LOCUS_H_
2 #define GENESIS_POPULATION_GENOME_LOCUS_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 
34 #include <stdexcept>
35 #include <string>
36 
37 namespace genesis {
38 namespace population {
39 
40 // =================================================================================================
41 // Genome Locus
42 // =================================================================================================
43 
65 {
66  std::string chromosome;
67  size_t position = 0;
68 
69  GenomeLocus( std::string const& chr = "", size_t pos = 0 )
70  : chromosome( chr )
71  , position( pos )
72  {
73  throw_if_invalid_();
74  }
75 
76  ~GenomeLocus() = default;
77 
78  GenomeLocus( GenomeLocus const& ) = default;
79  GenomeLocus( GenomeLocus&& ) = default;
80 
81  GenomeLocus& operator= ( GenomeLocus const& ) = default;
82  GenomeLocus& operator= ( GenomeLocus&& ) = default;
83 
84  bool empty() const
85  {
86  throw_if_invalid_();
87  return chromosome == "" && position == 0;
88  }
89 
90  bool specified() const
91  {
92  throw_if_invalid_();
93  return chromosome != "" && position != 0;
94  }
95 
96  void clear()
97  {
98  chromosome = "";
99  position = 0;
100  }
101 
102 private:
103 
104  void throw_if_invalid_() const
105  {
106  if( chromosome == "" and position > 0 ) {
107  throw std::runtime_error(
108  "GenomeLocus with invalid state: Empty chromosome but non-zero position."
109  );
110  }
111  }
112 };
113 
114 // Alternative version that also has a data field.
115 //
116 // // =================================================================================================
117 // // Genome Locus
118 // // =================================================================================================
119 //
120 // /**
121 // * @brief A single locus, that is, a position on a chromosome.
122 // *
123 // * For our purposes, we define a locus to be one position on one chromosome exactly.
124 // * We use 1-based positions, in order to comply with common chromosome annotation formats.
125 // * Furthermore, we use an empty chromosome string and/or position 0 as indicators of an empty or
126 // * default constructed locus.
127 // *
128 // * When working with multiple loci, we often expect their chromosome names to be sorted in
129 // * lexicographical order. Hence, for example, when comparing two loci on different chromosomes,
130 // * we also take the ordering of their chromosome names into account, see the `locus_...()`
131 // * compare functions for example.
132 // *
133 // * The class takes an extra data type as template parameter, which can be used to store
134 // * arbitrary data for this locus.
135 //
136 // * @see GenomeRegion
137 // * @see GenomeRegionList
138 // */
139 // template<class DataType = EmptyGenomeData>
140 // class GenomeLocus
141 // {
142 // public:
143 //
144 // // -------------------------------------------------------------------------
145 // // Typedefs
146 // // -------------------------------------------------------------------------
147 //
148 // using data_type = DataType;
149 //
150 // // -------------------------------------------------------------------------
151 // // Constructors and Rule of Five
152 // // -------------------------------------------------------------------------
153 //
154 // /**
155 // * @brief Default construct an empty locus.
156 // */
157 // GenomeLocus() = default;
158 //
159 // /**
160 // * @brief Construct a locus with a chromosome and position.
161 // */
162 // GenomeLocus( std::string const& chr, size_t pos )
163 // : GenomeLocus( chr, pos, DataType{} )
164 // {}
165 //
166 // /**
167 // * @brief Construct a locus with a chromosome and position, and copy the data.
168 // */
169 // GenomeLocus( std::string const& chr, size_t pos, DataType const& data )
170 // : GenomeLocus( chr, pos, DataType{ data })
171 // {}
172 //
173 // /**
174 // * @brief Construct a locus with a chromosome and position, and move the data.
175 // */
176 // GenomeLocus( std::string const& chr, size_t pos, DataType&& data )
177 // : chromosome_( chr )
178 // , position_( pos )
179 // , data_( std::move( data ))
180 // {
181 // if( chr.empty() || pos == 0 ) {
182 // throw std::invalid_argument(
183 // "Cannot construct GenomeLocus with empty chromosome or position zero."
184 // );
185 // }
186 // }
187 //
188 // ~GenomeLocus() = default;
189 //
190 // GenomeLocus( GenomeLocus const& ) = default;
191 // GenomeLocus( GenomeLocus&& ) = default;
192 //
193 // GenomeLocus& operator= ( GenomeLocus const& ) = default;
194 // GenomeLocus& operator= ( GenomeLocus&& ) = default;
195 //
196 // // -------------------------------------------------------------------------
197 // // Accessors
198 // // -------------------------------------------------------------------------
199 //
200 // std::string const& chromosome() const
201 // {
202 // return chromosome_;
203 // }
204 //
205 // size_t position() const
206 // {
207 // return position_;
208 // }
209 //
210 // data_type& data()
211 // {
212 // return data_;
213 // }
214 //
215 // data_type const& data() const
216 // {
217 // return data_;
218 // }
219 //
220 // bool empty() const
221 // {
222 // return chromosome_ == "" && position_ == 0;
223 // }
224 //
225 // // -------------------------------------------------------------------------
226 // // Operators
227 // // -------------------------------------------------------------------------
228 //
229 // operator std::string() const
230 // {
231 // return to_string();
232 // }
233 //
234 // std::string to_string() const
235 // {
236 // return chromosome_ + ":" + std::to_string( position_ );
237 // }
238 //
239 // // -------------------------------------------------------------------------
240 // // Member Variables
241 // // -------------------------------------------------------------------------
242 //
243 // private:
244 //
245 // std::string chromosome_;
246 // size_t position_ = 0;
247 //
248 // data_type data_;
249 //
250 // };
251 
252 } // namespace population
253 } // namespace genesis
254 
255 #endif // include guard
genesis::population::GenomeLocus::empty
bool empty() const
Definition: genome_locus.hpp:84
genesis::population::GenomeLocus::~GenomeLocus
~GenomeLocus()=default
genesis::population::GenomeLocus::position
size_t position
Definition: genome_locus.hpp:67
genesis::population::GenomeLocus
A single locus, that is, a position (or coordinate) on a chromosome.
Definition: genome_locus.hpp:64
genesis::population::GenomeLocus::chromosome
std::string chromosome
Definition: genome_locus.hpp:66
genesis::population::GenomeLocus::specified
bool specified() const
Definition: genome_locus.hpp:90
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::GenomeLocus::clear
void clear()
Definition: genome_locus.hpp:96
genesis::population::GenomeLocus::operator=
GenomeLocus & operator=(GenomeLocus const &)=default
genesis::population::GenomeLocus::GenomeLocus
GenomeLocus(std::string const &chr="", size_t pos=0)
Definition: genome_locus.hpp:69