libsequence  1.9.5
TwoSubs.cc
1 /*
2 
3  Copyright (C) 2003-2009 Kevin Thornton, krthornt[]@[]uci.edu
4 
5  Remove the brackets to email me.
6 
7  This file is part of libsequence.
8 
9  libsequence is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  libsequence is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  long with libsequence. If not, see <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #include <cmath>
25 #include <cfloat>
26 #include <Sequence/SingleSub.hpp>
30 #include <Sequence/TwoSubs.hpp>
31 
32 namespace Sequence
33 {
34  struct TwoSubs::TwoSubsImpl
35  {
36  double p0, p2S, p2V, p4, q0, q2S, q2V,q4;
37  double p0_b1, p2S_b1, p2V_b1, p4_b1, q0_b1, q2S_b1, q2V_b1, q4_b1;
38  double p0_b2, p2S_b2, p2V_b2, p4_b2, q0_b2, q2S_b2, q2V_b2, q4_b2;
39  double p0_b3, p2S_b3, p2V_b3, p4_b3, q0_b3, q2S_b3, q2V_b3, q4_b3;
40  double p0_b4, p2S_b4, p2V_b4, p4_b4, q0_b4, q2S_b4, q2V_b4, q4_b4;
41  void Calculate (const RedundancyCom95 & sitesObj, const std::string &codon1,
42  const std::string &int_1, const std::string &int_2,
43  const std::string &codon2, const double w_path1,
44  const double w_path2);
45  TwoSubsImpl(void) : p0(0.), p2S(0.), p2V(0.), p4(0.), q0(0.), q2S(0.), q2V(0.),q4(0.),
46  p0_b1(0.), p2S_b1(0.), p2V_b1(0.), p4_b1(0.), q0_b1(0.), q2S_b1(0.), q2V_b1(0.), q4_b1(0.),
47  p0_b2(0.), p2S_b2(0.), p2V_b2(0.), p4_b2(0.), q0_b2(0.), q2S_b2(0.), q2V_b2(0.), q4_b2(0.),
48  p0_b3(0.), p2S_b3(0.), p2V_b3(0.), p4_b3(0.), q0_b3(0.), q2S_b3(0.), q2V_b3(0.), q4_b3(0.),
49  p0_b4(0.), p2S_b4(0.), p2V_b4(0.), p4_b4(0.), q0_b4(0.), q2S_b4(0.), q2V_b4(0.), q4_b4(0.)
50  {
51  }
52  };
53 
54  TwoSubs::TwoSubs() : impl(std::unique_ptr<TwoSubsImpl>(new TwoSubsImpl()))
55  {
56  }
57 
58  void TwoSubs::operator() (const RedundancyCom95 & sitesObj,
59  const std::string & codon1, const std::string & codon2,
60  const Sequence::WeightingScheme2 *weights2)
69  {
70  impl->p0= impl->p2S= impl->p2V= impl->p4= impl->q0= impl->q2S= impl->q2V=impl->q4=0.;
71  impl->p0_b1= impl->p2S_b1= impl->p2V_b1= impl->p4_b1= impl->q0_b1= impl->q2S_b1= impl->q2V_b1= impl->q4_b1=0.;
72  impl->p0_b2= impl->p2S_b2= impl->p2V_b2= impl->p4_b2= impl->q0_b2= impl->q2S_b2= impl->q2V_b2= impl->q4_b2=0.;
73  impl->p0_b3= impl->p2S_b3= impl->p2V_b3= impl->p4_b3= impl->q0_b3= impl->q2S_b3= impl->q2V_b3= impl->q4_b3=0.;
74  impl->p0_b4= impl->p2S_b4= impl->p2V_b4= impl->p4_b4= impl->q0_b4= impl->q2S_b4= impl->q2V_b4= impl->q4_b4=0.;
75  auto intermediates = Intermediates2(codon1,codon2);
76  auto weights = weights2->operator()(codon1,codon2,sitesObj.gencode());
77  impl->Calculate (sitesObj, codon1, intermediates[0], codon2, intermediates[1], weights[0], weights[1]);
78  }
79 
80  TwoSubs::~TwoSubs (void)
81  {}
82 
83  void
84  TwoSubs::TwoSubsImpl::Calculate (const RedundancyCom95 & sitesObj, const std::string & codon1,
85  const std::string & int_1, const std::string & int_2,
86  const std::string & codon2, const double w_path1,
87  const double w_path2)
91  {
92  //count the changes along each branch
93  //branches that go through stop codons are counted as
94  //containing no changes on them.
95  SingleSub Single;
96  Single(sitesObj,codon1,int_1);
97  p0_b1 = Single.P0();
98  p2S_b1 =Single.P2S();
99  p2V_b1 =Single.P2V();
100  p4_b1 = Single.P4();
101  q0_b1 = Single.Q0();
102  q2S_b1 =Single.Q2S();
103  q2V_b1 =Single.Q2V();
104  q4_b1 = Single.Q4();
105 
106  Single (sitesObj, int_1, codon2);
107  p0_b2 = Single.P0();
108  p2S_b2 =Single.P2S();
109  p2V_b2 =Single.P2V();
110  p4_b2 = Single.P4();
111  q0_b2 = Single.Q0();
112  q2S_b2 =Single.Q2S();
113  q2V_b2 =Single.Q2V();
114  q4_b2 = Single.Q4();
115 
116 
117  Single (sitesObj, codon1, int_2);
118  p0_b3 = Single.P0();
119  p2S_b3 =Single.P2S();
120  p2V_b3 =Single.P2V();
121  p4_b3 = Single.P4();
122  q0_b3 = Single.Q0();
123  q2S_b3 =Single.Q2S();
124  q2V_b3 =Single.Q2V();
125  q4_b3 = Single.Q4();
126 
127  Single (sitesObj, int_2, codon2);
128  p0_b4 = Single.P0();
129  p2S_b4 =Single.P2S();
130  p2V_b4 =Single.P2V();
131  p4_b4 = Single.P4();
132  q0_b4 = Single.Q0();
133  q2S_b4 =Single.Q2S();
134  q2V_b4 =Single.Q2V();
135  q4_b4 = Single.Q4();
136 
137 
138  p0 = (p0_b1 + p0_b2) * w_path1
139  + (p0_b3 + p0_b4) * w_path2;
140  p2S = (p2S_b1 + p2S_b2) * w_path1
141  + (p2S_b3 + p2S_b4) * w_path2;
142  p2V = (p2V_b1 + p2V_b2) * w_path1
143  + (p2V_b3 + p2V_b4) * w_path2;
144  p4 = (p4_b1 + p4_b2) * w_path1
145  + (p4_b3 + p4_b4) * w_path2;
146  q0 = (q0_b1 + q0_b2) * w_path1
147  + (q0_b3 + q0_b4) * w_path2;
148  q2S = (q2S_b1 + q2S_b2) * w_path1
149  + (q2S_b3 + q2S_b4) * w_path2;
150  q2V = (q2V_b1 + q2V_b2) * w_path1
151  + (q2V_b3 + q2V_b4) * w_path2;
152  q4 = (q4_b1 + q4_b2) * w_path1
153  + (q4_b3 + q4_b4) * w_path2;
154  }
155 
156 
157 
158  double
159  TwoSubs::P0 (void) const
163  {
164  return impl->p0;
165  }
166 
167  double
168  TwoSubs::P2S (void) const
172  {
173  return impl->p2S;
174  }
175 
176  double
177  TwoSubs::P2V (void) const
181  {
182  return impl->p2V;
183  }
184 
185  double
186  TwoSubs::P4 (void) const
190  {
191  return impl->p4;
192  }
193 
194  double
195  TwoSubs::Q0 (void) const
199  {
200  return impl->q0;
201  }
202 
203  double
204  TwoSubs::Q2S (void) const
208  {
209  return impl->q2S;
210  }
211 
212  double
213  TwoSubs::Q2V (void) const
217  {
218  return impl->q2V;
219  }
220 
221  double
222  TwoSubs::Q4 (void) const
226  {
227  return impl->q4;
228  }
229 
230 }
double P2S(void) const
Definition: SingleSub.cc:78
double Q0(void) const
Definition: SingleSub.cc:104
double Q2S(void) const
Definition: TwoSubs.cc:204
double Q2S(void) const
Definition: SingleSub.cc:113
abstract interface to weighting schemes when codons differ at 2 positions
double Q2V(void) const
Definition: SingleSub.cc:122
used by Sequence::Comeron95, class Sequence::TwoSubs calculates divergence between codons that differ...
STL namespace.
The namespace in which this library resides.
double Q0(void) const
Definition: TwoSubs.cc:195
Deal with codons differing at 1 position.
Definition: SingleSub.hpp:45
Inter2_t Intermediates2(const std::string &codon1, const std::string &codon2)
Calculate the intermediate codons between a pair of codons diverged at 2 positions.
class Sequence::RedundancyCom95
double P2S(void) const
Definition: TwoSubs.cc:168
used by Sequence::Comeron95, class Sequence::SingleSub calculates divergence between codons that diff...
declarations of Sequence::Intermediates2 and Sequence::Intermediates3
double P0(void) const
Definition: SingleSub.cc:69
double P4(void) const
Definition: SingleSub.cc:96
double P2V(void) const
Definition: TwoSubs.cc:177
double Q4(void) const
Definition: SingleSub.cc:131
abstract interface to weighting schemes when codons differ at 2 positions
void operator()(const RedundancyCom95 &sitesObj, const std::string &cod1, const std::string &cod2, const Sequence::WeightingScheme2 *weights2)
Definition: TwoSubs.cc:58
double P2V(void) const
Definition: SingleSub.cc:87
double P4(void) const
Definition: TwoSubs.cc:186
Calculate redundancy of a genetic code using Comeron&#39;s counting scheme.
double Q2V(void) const
Definition: TwoSubs.cc:213
double P0(void) const
Definition: TwoSubs.cc:159
double Q4(void) const
Definition: TwoSubs.cc:222