baseComp.cc
#include <Sequence/Fasta.hpp>
#include <Sequence/stateCounter.hpp>
#include <Sequence/SeqUtilities.hpp>
#include <algorithm>
#include <iostream>
#include <functional>
using namespace std;
using namespace Sequence;
struct outputFreq : public std::binary_function<std::pair<char,unsigned>,unsigned,void>
{
inline void operator ()(const std::pair<char,unsigned> &p, const unsigned &u) const
{
std::cout << p.first << " (" << double(p.second)/double(u) << ") ";
}
};
int main ()
{
Fasta x;
while(!cin.fail())
{
cin >> x;
stateCounter count = for_each(x.begin(),x.end(),stateCounter('-'));
cout << "Base composition for sequence "<<x.GetName()<<"\n";
if (count.ndna == false)
{
unsigned len = x.length() - count.gap - count.n;
double percA = (count.a>0) ? double(count.a)/double(len) : 0.;
double percG = (count.g>0) ? double(count.g)/double(len) : 0.;
double percC = (count.c>0) ? double(count.c)/double(len) : 0.;
double percT = (count.t>0) ? double(count.t)/double(len) : 0.;
std::cout << "using Sequence::stateCounter: "
<< "A (" << percA << ") "
<< "G (" << percG << ") "
<< "C (" << percC << ") "
<< "T (" << percT << ")\n";
}
else
{
cout << "non-DNA character encountered. Skipping...\n";
}
std::map<char,unsigned> m = Sequence::makeCountList(x.begin(),x.end());
std::cout << "using Sequence::makeCountList(): ";
std::for_each(m.begin(),m.end(),std::bind2nd(outputFreq(),x.length()));
std::cout << std::endl;
}
}