codons.cc
#include<string>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<utility>
#include<vector>
#include <Sequence/Fasta.hpp>
#include <Sequence/CodonTable.hpp>
#include <Sequence/CountingOperators.hpp>
using namespace std;
using namespace Sequence;
typedef string::size_type sst;
struct GorC : public unary_function< char, bool >
{
inline bool operator()(const char &ch) const
{
return ( ch == 'G' || ch == 'C' );
}
};
int main(int argc, char *argv[])
{
Fasta sequence;
CodonUsageTable totalCodonUsage;
while (! cin.fail())
{
cin >> sequence;
unsigned length = sequence.length();
unsigned numgaps = count(sequence.begin(),sequence.end(),'-');
unsigned num_missing = count(sequence.begin(),sequence.end(),'N');
unsigned datalen = length-numgaps-num_missing;
unsigned G_or_C = 0;
G_or_C = count_if(sequence.begin(),sequence.end(),GorC());
double GC = double(G_or_C)/double(datalen);
CodonUsageTable UsageTable= makeCodonUsageTable(&sequence);
totalCodonUsage += UsageTable;
string seq = sequence.GetSeq();
sst pos = 0;
unsigned G3=0,C3=0;
while ( (pos = seq.find("G",pos)) != string::npos)
{
if (unsigned(pos+1) % 3 == 0.)
{
++G3;
}
++pos;
}
pos = 0;
while ( (pos = seq.find("C",pos)) != string::npos )
{
if (unsigned(pos+1) % 3 == 0.)
{
++C3;
}
++pos;
}
double GC3 = double(G3+C3)/( double(datalen) / 3. );
cout << "Codon Usage Table for: "<< sequence.GetName() << endl;
cout << "Sequence length is: "<< sequence.length()<<endl;
cout << "Length exluding gap characters and missing data is: "<<datalen<<endl;
cout << "GC content of coding sequence is: "<<GC<<endl;
cout << "GC3 content of coding sequence is: "<<GC3<<endl;
unsigned pretty = 1;
for(unsigned i = 0 ; i < UsageTable.size() ; ++i)
{
if (pretty < 8)
{
cout << UsageTable[i].first << '\t' << UsageTable[i].second << '\t';
}
else
{
cout << UsageTable[i].first << '\t' << UsageTable[i].second << endl;
pretty = 0;
}
++pretty;
}
cout << "//"<<endl;
}
cout << "Codon Usage Table for all regions\n";
unsigned pretty = 1;
for(unsigned i = 0 ; i < totalCodonUsage.size() ; ++i)
{
if (pretty < 8)
{
cout << totalCodonUsage[i].first << '\t' << totalCodonUsage[i].second << '\t';
}
else
{
cout << totalCodonUsage[i].first << '\t' << totalCodonUsage[i].second << endl;
pretty = 0;
}
++pretty;
}
cout << "//\n";
}