1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include "fasta.hpp"
#include <boost/algorithm/string/trim.hpp>
using boost::algorithm::trim;
#include <algorithm>
using std::min;
#include <ostream>
using std::endl;
#include <stdexcept>
using std::runtime_error;
namespace sequence{
FastaRecord::FastaRecord(const string &h)
: header(h)
{
}
FastaRecord::FastaRecord()
{
}
istream &operator>>(istream &inputStream, FastaRecord &fr)
{
fr.header.clear();
fr.sequence.clear();
if('>' != inputStream.get())
throw runtime_error("Error in FASTA file format.");
getline(inputStream, fr.header);
while(inputStream.good() && '>' != inputStream.peek()){
string line;
getline(inputStream, line);
fr.sequence += line;
}
return inputStream;
}
ostream &operator<<(ostream &outputStream, const FastaRecord &fr)
{
outputStream << '>' << fr.header;
for(string::size_type i = 0; i < fr.sequence.length(); ++i){
if(0 == i % CHARS_PER_LINE)
outputStream << '\n';
outputStream << fr.sequence[i];
}
outputStream << endl;
return outputStream;
}
istream &operator>>(istream &inputStream, Fasta &fa)
{
fa.clear();
string line;
while(getline(inputStream, line))
if(not line.empty() and '>' == line[0])
fa.push_back(FastaRecord(line.substr(1)));
else{
trim(line);
if(not line.empty())
fa.rbegin()->sequence += line;
}
return inputStream;
}
ostream &operator<<(ostream &outputStream, const Fasta &fa)
{
for(Fasta::const_iterator i = fa.begin(); i != fa.end(); ++i)
outputStream << *i;
return outputStream;
}
}
|