libsequence  1.9.5
SingleSub.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 <cassert>
25 #include <Sequence/Comparisons.hpp>
27 #include <Sequence/SingleSub.hpp>
28 #include <Sequence/Translate.hpp>
29 #include <sstream>
30 #include <stdexcept>
31 using namespace std;
32 
33 namespace Sequence
34 {
35  struct SingleSub::SingleSubImpl
36  {
37  double q0i, q2Si, q2Vi, q4i, q0j, q2Sj, q2Vj, q4j, p0i, p2Si, p2Vi,
38  p4i, p0j, p2Sj, p2Vj, p4j;
39  void Calculate (const RedundancyCom95 & sitesObj, const std::string & cod1,
40  const std::string & cod2);
41  SingleSubImpl(): q0i(0.), q2Si(0.), q2Vi(0.), q4i(0.), q0j(0.), q2Sj(0.), q2Vj(0.), q4j(0.), p0i(0.), p2Si(0.), p2Vi(0.),
42  p4i(0.), p0j(0.), p2Sj(0.), p2Vj(0.), p4j(0.)
43  {
44  }
45  };
46 
47  SingleSub::SingleSub(void) : impl(std::unique_ptr<SingleSub::SingleSubImpl>(new SingleSub::SingleSubImpl()))
48  {
49  }
50 
51  SingleSub::~SingleSub()
52  {
53  }
54 
55  void SingleSub::operator() (const RedundancyCom95 & sitesObj,
56  const std::string & cod1,
57  const std::string & cod2)
64  {
65  assert(cod1.length() == 3 && cod2.length() == 3);
66  impl->Calculate (sitesObj, cod1, cod2);
67  }
68 
69  double SingleSub::P0(void) const
73  {
74  return (impl->p0i+impl->p0j)/2.0;
75  }
76 
77 
78  double SingleSub::P2S(void) const
82  {
83  return (impl->p2Si+impl->p2Sj)/2.0;
84  }
85 
86 
87  double SingleSub::P2V(void) const
91  {
92  return (impl->p2Vi+impl->p2Vj)/2.0;
93  }
94 
95 
96  double SingleSub::P4(void) const
100  {
101  return (impl->p4i+impl->p4j)/2.0;
102  }
103 
104  double SingleSub::Q0(void) const
108  {
109  return (impl->q0i+impl->q0j)/2.0;
110  }
111 
112 
113  double SingleSub::Q2S(void) const
117  {
118  return (impl->q2Si+impl->q2Sj)/2.0;
119  }
120 
121 
122  double SingleSub::Q2V(void) const
126  {
127  return (impl->q2Vi+impl->q2Vj)/2.0;
128  }
129 
130 
131  double SingleSub::Q4(void) const
135  {
136  return (impl->q4i+impl->q4j)/2.0;
137  }
138 
139  void
140  SingleSub::SingleSubImpl::Calculate (const RedundancyCom95 & sitesObj, const std::string & codon1,
141  const std::string & codon2)
145  {
146  q0i = q2Si = q2Vi = q4i = q0j = q2Sj = q2Vj = q4j = p0i = p2Si =
147  p2Vi = p4i = p0j = p2Sj = p2Vj = p4j = 0.0;
148 
149  unsigned pos = 0, k = 0;//, type = 0;
150  double d;
151  for (k = 0; k <= 2; ++k)
152  {
153  if (codon1[k] != codon2[k])
154  {
155  pos = k;
156  }
157  }
158  Mutations type = TsTv (codon1[pos], codon2[pos]);
159  if( type == Mutations::Unknown )
160  {
161  ostringstream o;
162  o << "SingleSub.cc: mutation between " << codon1 << " and " << codon2
163  << " at position " << pos
164  << " is neither a transition nor a transversion";
165  throw std::runtime_error(o.str().c_str());
166  }
167  assert(type==Mutations::Ts || type==Mutations::Tv);
168 
169  switch (pos)
170  {
171  case 0: //mutation at first position
172  switch (type)
173  {
174  case Mutations::Ts: //transition
175  d = sitesObj.FirstNon(codon1);
176  if( d < 1. )
177  {
178  d = sitesObj.First2S(codon1);
179  if(d > 0.)
180  {
181  p2Si += 1.;
182  }
183  else
184  {
185  p2Vi += 1.;
186  }
187  }
188  else
189  {
190  p0i += 1.;
191  }
192  d = sitesObj.FirstNon(codon2);
193  if( d < 1. )
194  {
195  d = sitesObj.First2S(codon2);
196  if(d > 0.)
197  {
198  p2Sj += 1.;
199  }
200  else
201  {
202  p2Vj += 1.;
203  }
204  }
205  else
206  {
207  p0j += 1.;
208  }
209  break;
210  case Mutations::Tv: //transversion
211  d = sitesObj.FirstNon(codon1);
212  if( d < 1. )
213  {
214  d = sitesObj.First2V(codon1);
215  if(d > 0.)
216  {
217  q2Vi += 1.;
218  }
219  else
220  {
221  q2Si += 1.;
222  }
223  }
224  else
225  {
226  q0i += 1.;
227  }
228  d = sitesObj.FirstNon(codon2);
229  if( d < 1. )
230  {
231  d = sitesObj.First2V(codon2);
232  if(d > 0.)
233  {
234  q2Vj += 1.;
235  }
236  else
237  {
238  q2Sj += 1.;
239  }
240  }
241  else
242  {
243  q0j += 1.;
244  }
245  break;
246  case Mutations::Unknown:
247  throw std::runtime_error( "SingleSub: mutation type unknown" );
248  break;
249  }
250  break;
251  case 1: //mutation at second position
252  switch (type)
253  {
254  case Mutations::Ts: //transition
255  p0i += 1.0;
256  p0j += 1.0;
257  break;
258  case Mutations::Tv:
259  q0i += 1.0;
260  q0j += 1.0;
261  break;
262  case Mutations::Unknown:
263  throw std::runtime_error( "SingleSub: mutation type unknown" );
264  break;
265  }
266  break;
267  case 2: //mutation at third position
268  switch (type)
269  {
270  case Mutations::Ts:
271  d = sitesObj.ThirdNon (codon1);
272  if ( d < 1.)
273  {
274  d = sitesObj.ThirdFour (codon1);
275  if ( d < 1. )
276  {
277  d = sitesObj.Third2S (codon1);
278  if ( d > 0. )
279  {
280  p2Si += 1.;
281  }
282  else
283  {
284  p2Vi += 1.;
285  }
286  }
287  else
288  {
289  p4i += 1.;
290  }
291  }
292  else
293  {
294  p0i += 1.;
295  }
296 
297  d = sitesObj.ThirdNon (codon2);
298  if ( d < 1.)
299  {
300  d = sitesObj.ThirdFour (codon2);
301  if ( d < 1. )
302  {
303  d = sitesObj.Third2S (codon2);
304  if ( d > 0. )
305  {
306  p2Sj += 1.;
307  }
308  else
309  {
310  p2Vj += 1.;
311  }
312  }
313  else
314  {
315  p4j += 1.;
316  }
317  }
318  else
319  {
320  p0j += 1.;
321  }
322 
323  break;
324  //case 2:
325  case Mutations::Tv: //transversion
326  d = sitesObj.ThirdNon (codon1);
327  if ( d < 1.)
328  {
329  d = sitesObj.ThirdFour (codon1);
330  if ( d < 1. )
331  {
332  d = sitesObj.Third2V (codon1);
333  if ( d > 0. )
334  {
335  q2Vi += 1.;
336  }
337  else
338  {
339  q2Si += 1.;
340  }
341  }
342  else
343  {
344  q4i += 1.;
345  }
346  }
347  else
348  {
349  q0i += 1.;
350  }
351 
352  d = sitesObj.ThirdNon (codon2);
353  if ( d < 1.)
354  {
355  d = sitesObj.ThirdFour (codon2);
356  if ( d < 1. )
357  {
358  d = sitesObj.Third2V (codon2);
359  if ( d > 0. )
360  {
361  q2Vj += 1.;
362  }
363  else
364  {
365  q2Sj += 1.;
366  }
367  }
368  else
369  {
370  q4j += 1.;
371  }
372  }
373  else
374  {
375  q0j += 1.;
376  }
377 
378  break;
379  case Mutations::Unknown:
380  throw std::runtime_error( "SingleSub: mutation type unknown" );
381  break;
382  }
383  break;
384  }
385  }
386 }
double P2S(void) const
Definition: SingleSub.cc:78
Mutations TsTv(const char &i, const char &j)
Definition: Comparisons.cc:35
double Q0(void) const
Definition: SingleSub.cc:104
double Q2S(void) const
Definition: SingleSub.cc:113
double Q2V(void) const
Definition: SingleSub.cc:122
STL namespace.
The namespace in which this library resides.
double First2V(const std::string &codon) const
double FirstNon(const std::string &codon) const
double ThirdFour(const std::string &codon) const
declares Sequence::Translate,a function to translate CDS sequences into peptide sequences ...
class Sequence::RedundancyCom95
double ThirdNon(const std::string &codon) const
used by Sequence::Comeron95, class Sequence::SingleSub calculates divergence between codons that diff...
double P0(void) const
Definition: SingleSub.cc:69
double P4(void) const
Definition: SingleSub.cc:96
double Third2V(const std::string &codon) const
double Q4(void) const
Definition: SingleSub.cc:131
double P2V(void) const
Definition: SingleSub.cc:87
double First2S(const std::string &codon) const
delcaration of routines for comparing DNA sequences This file declares a set of functions useful for ...
Calculate redundancy of a genetic code using Comeron&#39;s counting scheme.
void operator()(const RedundancyCom95 &sitesObj, const std::string &cod1, const std::string &cod2)
Definition: SingleSub.cc:55
double Third2S(const std::string &codon) const