-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_fasta.cpp
43 lines (38 loc) · 1.06 KB
/
get_fasta.cpp
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
// read a DNA sequence in FASTA format
#include "MCFbio.hpp"
#include <istream>
// Industrial strength fasta-format sequence reader
// appends the sequence to anything already in seq
std::istream &mcf::get_fasta(
std::istream &strm, std::vector<unsigned> &seq, std::string &title,
unsigned (*translator)(char)) // not allowed to repeat the default argument?
{
std::string t;
unsigned old_size = seq.size();
char c;
bool titflag = false; // have we read a title line yet?
while (strm >> c) {
if (isalpha(c)) {
seq.push_back(translator(c));
} else if (c == '>') {
if (titflag || seq.size() != old_size) {
strm.unget();
break;
} else {
std::getline(strm, t);
titflag = true;
}
} else if (c == '#') { // skip comments
std::string junk;
std::getline(strm, junk);
}
}
// if reached EOF but read something, clear the stream state:
if (strm.eof() && (titflag || seq.size() != old_size))
strm.clear();
if (strm)
title = t;
else
seq.resize(old_size);
return strm;
}