libsequence  1.9.5
CodonTable.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 
26 #include <Sequence/Seq.hpp>
27 #include <Sequence/CodonTable.hpp>
28 #include <Sequence/Comparisons.hpp>
29 #include <algorithm>
30 #include <functional>
31 #include <iterator>
32 #include <iostream>
33 #include <cctype>
34 #include <vector>
35 #include <string>
36 #include <utility>
37 
38 using ::toupper;
39 
40 namespace
41 {
42  typedef std::pair< std::string, int > CodonFreq;
43  //typedef std::string::size_type sst;
44 
45  //our alphabet consists of nucleotide characters
46  static const unsigned int alphsize = 4;
47  static const char alphabet[alphsize] ={'A','C','G','T'};
48 
49  //Sequence::CodonUsageTable doWork(const std::string &sequence)
50  Sequence::CodonUsageTable doWork( std::string::const_iterator beg,
51  std::string::const_iterator end )
52  {
53  Sequence::CodonUsageTable UsageTable;
54  for (unsigned i = 0 ; i < alphsize ; ++i)
55  for (unsigned j = 0 ; j < alphsize ; ++j)
56  for (unsigned k = 0 ; k < alphsize ; ++k)
57  {
58  std::string codon;
59  codon += alphabet[i];
60  codon += alphabet[j];
61  codon += alphabet[k];
62  std::string::const_iterator::difference_type pos = 0;
63  unsigned count = 0;
64  for( ; (beg+pos) < end ; pos += 3 )
65  {
66  //Ask if positions beg to beg+3 differ from codon, but do
67  //not allow missing data to result in the answer to be false
68  if( ! Sequence::Different( std::string(beg+pos,beg+pos+3),codon,false) )
69  {
70  ++count;
71  }
72  }
73  UsageTable.push_back( CodonFreq(codon,count) );
74  }
75  return UsageTable;
76  }
77 }
78 
79 namespace Sequence
80 {
87  {
88  return doWork(sequence->begin(),sequence->end());
89  }
90 
91  CodonUsageTable makeCodonUsageTable(const std::string &sequence)
97  {
98  return doWork(sequence.begin(),sequence.end());
99  }
100 
101  CodonUsageTable makeCodonUsageTable(std::string::const_iterator beg,
102  std::string::const_iterator end)
109  {
110  //we do the typecast to string because it allows the use of std::string::find,
111  //which is efficient for searching
112  return doWork(beg,end);
113  }
114 }
class Sequence::Seq, an abstract base class for Sequences
Abstract interface to sequence objects.
Definition: Seq.hpp:46
The namespace in which this library resides.
iterator begin()
Definition: Seq.cc:189
std::vector< std::pair< std::string, int > > CodonUsageTable
Definition: typedefs.hpp:43
facility to count codons in CDS sequence, function Sequence::makeCodonUsageTable
iterator end()
Definition: Seq.cc:198
bool Different(const std::string &seq1, const std::string &seq2, const bool &skip_missing=true, const bool &nucleic_acid=true)
Definition: Comparisons.cc:98
delcaration of routines for comparing DNA sequences This file declares a set of functions useful for ...
CodonUsageTable makeCodonUsageTable(const Seq *sequence)
Definition: CodonTable.cc:81