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 "Population.hpp"
42 #include "Configurable.hpp"
43 #include "MSFormat.hpp"
44 #include "shared_ptr.hpp"
45 #include <vector>
46 #include <map>
47 #include <set>
48 
49 
50 class VariantIndicator;
51 
52 
53 //
54 // genotype encoding:
55 // (allele0, allele1) as single char
56 //
57 // e.g. genotype==0x11 means allele0==1 and allele1==1
58 //
59 
60 
61 inline char genotype_make_pair(char allele_0, char allele_1)
62 {
63  return (allele_0<<4) | allele_1;
64 }
65 
66 
67 inline char genotype_sum(char genotype)
68 {
69  return (genotype>>4) + (genotype & 0x0F);
70 }
71 
72 
73 inline char genotype_first(char genotype)
74 {
75  return (genotype>>4);
76 }
77 
78 
79 inline char genotype_second(char genotype)
80 {
81  return (genotype & 0x0F);
82 }
83 
84 
85 class GenotypeData : public std::vector<char>
86 {
87  public:
88 
89  GenotypeData() {}
90  GenotypeData(const char* begin, const char* end) : std::vector<char>(begin, end) {}
91 
92  double allele_frequency() const; // note: assumes binary alleles (0/1 valued)
93 
94  // TODO when needed: multiple allele case
95  // vector<double> multiple_allele_frequencies() const;
96  // assume alleles are encoded as {0, ..., n-1}, return vector size n
97 };
98 
99 
100 typedef shared_ptr<GenotypeData> GenotypeDataPtr;
101 typedef std::map<Locus, GenotypeDataPtr> GenotypeMap;
102 typedef shared_ptr<GenotypeMap> GenotypeMapPtr;
103 
104 
105 //
106 // Genotyper
107 //
108 
109 
111 {
112  public:
113 
114  // returns genotype for a single organism at a single locus
115  char genotype(const Locus& locus,
116  const Organism& organism,
117  const VariantIndicator& indicator) const;
118 
119  // single organism, range version
120  char genotype(const Locus& locus,
121  const ChromosomePairRange& range,
122  const VariantIndicator& indicator) const;
123 
124  // genotypes population at multiple loci
125  GenotypeMapPtr genotype(const Loci& loci,
126  const Population& population,
127  const VariantIndicator& indicator) const;
128 };
129 
130 
131 #endif // _GENOTYPER_HPP_
132