libsequence  1.9.5
Translate.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 <Sequence/Translate.hpp>
25 #include <algorithm>
26 #include <cctype>
27 #include <stdexcept>
28 
29 namespace
30 {
31  char Universal (const char codon[4],
32  const char & gapchar)
33  {
34  //handle gaps A codon with a single or 2
35  //gap characters returns an X, since it's ambiguous,
36  //and a codon that is all gaps (---)
37  //returns a -
38  std::ptrdiff_t ngaps = (std::count(&codon[0],&codon[0]+3,gapchar));
39  if (ngaps == 3)
40  {
41  return '-';
42  }
43  else if (ngaps > 0 && ngaps < 3)
44  {
45  return 'X';
46  }
47 
48  switch (codon[0])
49  {
50  //first position is A
51  case 'A':
52  switch (codon[1])
53  {
54  case 'T':
55  switch (codon[2])
56  {
57  case 'G':
58  return 'M';
59  break;
60  default:
61  return 'I';
62  break;
63  }
64  break;
65  case 'C':
66  return 'T';
67  break;
68  case 'A':
69  switch (codon[2])
70  {
71  case 'T':
72  return 'N';
73  break;
74  case 'C':
75  return 'N';
76  break;
77  case 'A':
78  return 'K';
79  break;
80  case 'G':
81  return 'K';
82  break;
83  }
84  case 'G':
85  switch (codon[2])
86  {
87  case 'A':
88  return 'R';
89  break;
90  case 'G':
91  return 'R';
92  break;
93  default:
94  return 'S';
95  break;
96  }
97  break;
98  }
99  break;
100  //first Position is T
101  case 'T':
102  switch (codon[1])
103  {
104  case 'T':
105  switch (codon[2])
106  {
107  case 'T':
108  return 'F';
109  break;
110  case 'C':
111  return 'F';
112  break;
113  case 'A':
114  return 'L';
115  break;
116  case 'G':
117  return 'L';
118  break;
119  }
120  break;
121  case 'C':
122  return 'S';
123  break;
124  case 'A':
125  switch (codon[2])
126  {
127  case 'T':
128  return 'Y';
129  break;
130  case 'C':
131  return 'Y';
132  break;
133  default:
134  return '*';
135  break;
136  }
137  break;
138  case 'G':
139  switch (codon[2])
140  {
141  case 'A':
142  return '*';
143  break;
144  case 'G':
145  return 'W';
146  break;
147  default:
148  return 'C';
149  break;
150  }
151  break;
152  }
153 
154  break;
155  //first is G
156  case 'G':
157  switch (codon[1])
158  {
159  case 'T':
160  return 'V';
161  break;
162  case 'C':
163  return 'A';
164  break;
165  case 'G':
166  return 'G';
167  break;
168  case 'A':
169  switch (codon[2])
170  {
171  case 'A':
172  return 'E';
173  break;
174  case 'G':
175  return 'E';
176  break;
177  default:
178  return 'D';
179  break;
180  }
181  break;
182  }
183  break;
184  //first is C
185  case 'C':
186  switch (codon[1])
187  {
188  case 'T':
189  return 'L';
190  break;
191  case 'C':
192  return 'P';
193  break;
194  case 'G':
195  return 'R';
196  break;
197  case 'A':
198  switch (codon[2])
199  {
200  case 'A':
201  return 'Q';
202  break;
203  case 'G':
204  return 'Q';
205  break;
206  default:
207  return 'H';
208  break;
209  }
210  break;
211  }
212  break;
213  default:
214  return 'X';
215  break;
216  }
217  return 'X';
218  }
219 }
220 
221 namespace Sequence
222 {
223  std::string Translate(std::string::const_iterator beg,
224  std::string::const_iterator end,
225  Sequence::GeneticCodes genetic_code,
226  const char & gapchar)
227 
228 
229  {
230  if (beg > (end-3)) //if the range is less than 3 in length (1 codon), return an empty string
231  return std::string();
232 
233  std::string translation;
234  char codon[4];
235  codon[3] = '\0'; //null terminate for cleanliness
236  while(beg < end)
237  {
238  codon[0] = char(std::toupper(*beg));
239  codon[1] = char(std::toupper(*(beg+1)));
240  codon[2] = char(std::toupper(*(beg+2)));
241  switch (genetic_code)
242  {
243  case GeneticCodes::UNIVERSAL:
244  translation += Universal (codon,gapchar);
245  break;
246  default:
247  throw std::runtime_error ("Translate.cc: Translate(), invalid genetic code passed");
248  break;
249  }
250  beg += 3;
251  }
252  return translation;
253  }
254 
255 }
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.
declares Sequence::Translate,a function to translate CDS sequences into peptide sequences ...