forqs module reference
Forward simulation of Recombination, Quantitative traits, and Selection
 All Classes Groups Pages
Genotype.hpp
1 //
2 // Genotype.hpp
3 //
4 // Created by Darren Kessner with John Novembre
5 //
6 // Copyright (c) 2013 Regents of the University of California
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // * Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // * Neither UCLA nor the names of its contributors may be used to endorse or
20 // promote products derived from this software without specific prior
21 // written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34 
35 
36 #ifndef _GENOTYPER_HPP_
37 #define _GENOTYPER_HPP_
38 
39 
40 #include "Locus.hpp"
41 #include "Configurable.hpp"
42 #include "shared_ptr.hpp"
43 #include <vector>
44 #include <map>
45 #include <set>
46 
47 
48 class ChromosomePairRange;
49 class Organism;
50 class Population;
51 class VariantIndicator;
52 
53 
54 //
55 // genotype encoding:
56 // (allele0, allele1) as single char
57 //
58 // e.g. genotype==0x11 means allele0==1 and allele1==1
59 //
60 
61 
62 inline char genotype_make_pair(char allele_0, char allele_1)
63 {
64  return (allele_0<<4) | allele_1;
65 }
66 
67 
68 inline char genotype_sum(char genotype)
69 {
70  return (genotype>>4) + (genotype & 0x0F);
71 }
72 
73 
74 inline char genotype_first(char genotype)
75 {
76  return (genotype>>4);
77 }
78 
79 
80 inline char genotype_second(char genotype)
81 {
82  return (genotype & 0x0F);
83 }
84 
85 
86 class GenotypeData : public std::vector<char>
87 {
88  public:
89 
90  GenotypeData() {}
91  GenotypeData(const char* begin, const char* end) : std::vector<char>(begin, end) {}
92 
93  double allele_frequency() const; // note: assumes binary alleles (0/1 valued)
94 
95  // when needed: multiple allele case
96  // vector<double> multiple_allele_frequencies() const;
97  // assume alleles are encoded as {0, ..., n-1}, return vector size n
98 };
99 
100 
101 typedef shared_ptr<GenotypeData> GenotypeDataPtr;
102 
103 
104 class GenotypeMap : public std::map<Locus, GenotypeDataPtr> // map locus -> trait_values
105 {
106  public:
107 
108  GenotypeDataPtr get(const Locus& locus) const // returns valid pointer, or throws
109  {
110  if (!count(locus))
111  {
112  std::ostringstream message;
113  message << "[GenotypeMap] Locus " << locus << " not found.";
114  throw std::runtime_error(message.str().c_str());
115  }
116 
117  GenotypeDataPtr result = at(locus);
118 
119  if (!result.get())
120  {
121  std::ostringstream message;
122  message << "[GenotypeMap] Null data at locus " << locus;
123  throw std::runtime_error(message.str().c_str());
124  }
125 
126  return at(locus);
127  }
128 };
129 
130 
131 typedef shared_ptr<GenotypeMap> GenotypeMapPtr;
132 
133 
134 //
135 // Genotyper
136 //
137 
138 
140 {
141  public:
142 
143  // returns genotype for a single organism at a single locus
144  char genotype(const Locus& locus,
145  const Organism& organism,
146  const VariantIndicator& indicator) const;
147 
148  // single organism, range version
149  char genotype(const Locus& locus,
150  const ChromosomePairRange& range,
151  const VariantIndicator& indicator) const;
152 
153  // genotypes population at multiple loci
154  void genotype(const Loci& loci,
155  const Population& population,
156  const VariantIndicator& indicator,
157  GenotypeMap& genotype_map) const;
158 };
159 
160 
161 #endif // _GENOTYPER_HPP_
162