libsequence  1.9.5
GranthamWeights.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/Grantham.hpp>
27 #include <Sequence/Translate.hpp>
30 
31 using std::string;
32 
33 namespace Sequence
34 {
35  WeightingScheme2::weights2_t GranthamWeights2::operator()(const std::string &codon1, const std::string &codon2,
36  GeneticCodes code) const
42  {
43  Grantham gdist;
44  auto intermediates = Intermediates2(codon1,codon2);
45  //assign weights to pathways
46  //weights are assiged by the total length of each path, as
47  //measured by Grantham distances
48  double len_path_1 = 0.0, len_path_2 = 0.0;
49 
50  string t1 = Translate (codon1.begin(),codon1.end(), code);
51  string t2 = Translate (intermediates[0].begin(),
52  intermediates[0].end(), code);
53  len_path_1 += gdist ((t1)[0], (t2)[0]);
54 
55  t1 = Translate (intermediates[0].begin(),intermediates[0].end(), code);
56  t2 = Translate (codon2.begin(),codon2.end(), code);
57  len_path_1 += gdist ((t1)[0], (t2)[0]);
58 
59  t1 = Translate (codon1.begin(),codon1.end(), code);
60  t2 = Translate (intermediates[1].begin(),intermediates[1].end(), code);
61  len_path_2 += gdist ((t1)[0], (t2)[0]);
62 
63  t1 = Translate (intermediates[1].begin(),intermediates[1].end(), code);
64  t2 = Translate (codon2.begin(),codon2.end(), code);
65  len_path_2 += gdist ((t1)[0], (t2)[0]);
66 
67  //calculate the weights themselves
68  //double w_path1 = 0., w_path2 = 0.,
69  double w_tot = 0.;
70  weights2_t __weights;
71  __weights[0]=0.;
72  __weights[1]=0.;
73 
74  if (fabs(len_path_1-0.) <= DBL_EPSILON && fabs(len_path_2-0.) <= DBL_EPSILON)
75  {
76  __weights[0] = __weights[1] = 0.5;
77  w_tot = 1.;
78  }
79  else
80  {
81  __weights[0] = 1. - len_path_1 / (len_path_1 + len_path_2);
82  __weights[1] = 1. - len_path_2 / (len_path_1 + len_path_2);
83  w_tot = __weights[0] + __weights[1];
84  }
85 
86  __weights[0] /= w_tot;
87  __weights[1] /= w_tot;
88  return __weights;
89  }
90 
91  WeightingScheme3::weights3_t GranthamWeights3::operator()(const std::string &codon1, const std::string &codon2,
92  Sequence::GeneticCodes code) const
98  {
99  Grantham gdist;
100  auto intermediates = Intermediates3(codon1,codon2);
101  double len_path_1 = 0.0, len_path_2 = 0.0, len_path_3 =
102  0., len_path_4 = 0., len_path_5 = 0., len_path_6 = 0.;
103 
104  string t1 = Translate (codon1.begin(), codon1.end(),code);
105  string t2 = Translate (intermediates[0].begin(), intermediates[0].end(),code);
106  double dist = gdist ((t1)[0],(t2)[0]);
107  len_path_1 += dist;
108 
109 
110  t1 = Translate (intermediates[0].begin(), intermediates[0].end(), code);
111  t2 = Translate (intermediates[1].begin(), intermediates[1].end(), code);
112  dist = gdist ((t1)[0],(t2)[0]);
113  len_path_1 += dist;
114 
115 
116  t1 = Translate (intermediates[1].begin(), intermediates[1].end(),code);
117  t2 = Translate (codon2.begin(),codon2.end(), code);
118  dist = gdist ((t1)[0],(t2)[0]);
119  len_path_1 += dist;
120 
121  //path2
122 
123  t1 = Translate (codon1.begin(),codon1.end(), code);
124  t2 = Translate (intermediates[0].begin(),intermediates[0].end(), code);
125  dist = gdist ((t1)[0],(t2)[0]);
126  len_path_2 += dist;
127 
128  t1 = Translate (intermediates[0].begin(),intermediates[0].end(), code);
129  t2 = Translate (intermediates[2].begin(),intermediates[2].end(), code);
130  dist = gdist ((t1)[0],(t2)[0]);
131  len_path_2 += dist;
132 
133  t1 = Translate (intermediates[2].begin(),intermediates[2].end(), code);
134  t2 = Translate (codon2.begin(),codon2.end(), code);
135  dist = gdist ((t1)[0],(t2)[0]);
136  len_path_2 += dist;
137 
138  //path 3
139  t1 = Translate (codon1.begin(),codon1.end(), code);
140  t2 = Translate (intermediates[3].begin(),intermediates[3].end(), code);
141  dist = gdist ((t1)[0],(t2)[0]);
142  len_path_3 += dist;
143 
144  t1 = Translate (intermediates[3].begin(), intermediates[3].end(),code);
145  t2 = Translate (intermediates[4].begin(), intermediates[4].end(), code);
146  dist = gdist ((t1)[0],(t2)[0]);
147  len_path_3 += dist;
148 
149  t1 = Translate (intermediates[4].begin(), intermediates[4].end(), code);
150  t2 = Translate (codon2.begin(),codon2.end(), code);
151  dist = gdist ((t1)[0],(t2)[0]);
152  len_path_3 += dist;
153 
154  //path4
155  t1 = Translate (codon1.begin(),codon1.end(), code);
156  t2 = Translate (intermediates[3].begin(), intermediates[3].end(), code);
157  dist = gdist ((t1)[0],(t2)[0]);
158  len_path_4 += dist;
159 
160  t1 = Translate (intermediates[3].begin(), intermediates[3].end(), code);
161  t2 = Translate (intermediates[5].begin(), intermediates[5].end(), code);
162  dist = gdist ((t1)[0],(t2)[0]);
163  len_path_4 += dist;
164 
165  t1 = Translate (intermediates[5].begin(), intermediates[5].end(), code);
166  t2 = Translate (codon2.begin(),codon2.end(), code);
167  dist = gdist ((t1)[0],(t2)[0]);
168  len_path_4 += dist;
169 
170  //path 5
171  t1 = Translate (codon1.begin(),codon1.end(), code);
172  t2 = Translate (intermediates[6].begin(), intermediates[6].end(), code);
173  dist = gdist ((t1)[0],(t2)[0]);
174  len_path_5 += dist;
175 
176  t1 = Translate (intermediates[6].begin(), intermediates[6].end(), code);
177  t2 = Translate (intermediates[7].begin(), intermediates[7].end(), code);
178  dist = gdist ((t1)[0],(t2)[0]);
179  len_path_5 += dist;
180 
181  t1 = Translate (intermediates[7].begin(), intermediates[7].end(), code);
182  t2 = Translate (codon2.begin(),codon2.end(), code);
183  dist = gdist ((t1)[0],(t2)[0]);
184  len_path_5 += dist;
185 
186  //path 6
187  t1 = Translate (codon1.begin(),codon1.end(), code);
188  t2 = Translate (intermediates[6].begin(), intermediates[6].end(), code);
189  dist = gdist ((t1)[0],(t2)[0]);
190  len_path_6 += dist;
191 
192  t1 = Translate (intermediates[6].begin(), intermediates[6].end(), code);
193  t2 = Translate (intermediates[8].begin(), intermediates[8].end(), code);
194  dist = gdist ((t1)[0],(t2)[0]);
195  len_path_6 += dist;
196 
197  t1 = Translate (intermediates[8].begin(), intermediates[8].end(), code);
198  t2 = Translate (codon2.begin(),codon2.end(), code);
199  dist = gdist ((t1)[0],(t2)[0]);
200  len_path_6 += dist;
201 
202  weights3_t __weights;
203  __weights[0] = 1. / len_path_1;
204  __weights[1] = 1. /len_path_2;
205  __weights[2] = 1. /len_path_3;
206  __weights[3] = 1. / len_path_4;
207  __weights[4] = 1. /len_path_5;
208  __weights[5] = 1./ len_path_6;
209 
210  //scale weights to sum to 1
211  double w_tot =
212  __weights[0] + __weights[1] + __weights[2] + __weights[3] + __weights[4] + __weights[5];
213  __weights[0] /= w_tot;
214  __weights[1] /= w_tot;
215  __weights[2] /= w_tot;
216  __weights[3] /= w_tot;
217  __weights[4] /= w_tot;
218  __weights[5] /= w_tot;
219  return __weights;
220  }
221 }
std::string Translate(std::string::const_iterator beg, std::string::const_iterator end, Sequence::GeneticCodes genetic_code=GeneticCodes::UNIVERSAL, const char &gapchar='-')
Definition: Translate.cc:223
The namespace in which this library resides.
declaration of classes to weight codons by Grantham distance (i.e. for Sequence::Comeron95). Declares Sequence::GranthamWeights2 and Sequence::GranthamWeights3
Inter2_t Intermediates2(const std::string &codon1, const std::string &codon2)
Calculate the intermediate codons between a pair of codons diverged at 2 positions.
declares Sequence::Translate,a function to translate CDS sequences into peptide sequences ...
Grantham&#39;s distances (Sequence::Grantham)
declarations of Sequence::Intermediates2 and Sequence::Intermediates3
Grantham&#39;s distances.
Definition: Grantham.hpp:40
weights2_t operator()(const std::string &codon1, const std::string &codon2, Sequence::GeneticCodes genetic_code) const
Inter3_t Intermediates3(const std::string &codon1, const std::string &codon2)
Calculate the intermediate codons between a pair of codons diverged at 3 positions.
weights3_t operator()(const std::string &codon1, const std::string &codon2, Sequence::GeneticCodes genetic_code) const