forked from abyzovlab/CNVnator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastaParser.cpp
50 lines (45 loc) · 1.09 KB
/
FastaParser.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
44
45
46
47
48
49
#include "FastaParser.hh"
const int FastaParser::sbuffs=65536;
FastaParser::FastaParser(string fileName) :
buff(""),
c_name(""),
c_data(""),
n_cinb(0),
eof(false)
{
inFile=gzopen(fileName.c_str(), "rb");
if (inFile == NULL) {
cerr << "Fasta parser: Can't open file '" << fileName << "'." << endl;
}
sbuff=new char[sbuffs];
}
FastaParser::~FastaParser()
{
gzclose(inFile);
delete [] sbuff;
}
bool FastaParser::nextChromosome() {
if ((inFile==NULL) || (eof && n_cinb==0)) return false;
while(!eof && (n_cinb<2)) {
int n = gzread(inFile, sbuff, sbuffs-1);
if (n>0) {
sbuff[n]=0;
char *pch=strchr(sbuff,'>');
while (pch!=NULL) { n_cinb++;pch=strchr(pch+1,'>');}
buff.append(sbuff,n);
} else eof=true;
}
buff.erase(0,buff.find(">")+1);
c_name=buff.substr(0,buff.find(" "));
buff.erase(0,buff.find("\n")+1);
if(!eof) {
c_data=buff.substr(0,buff.find(">"));
buff.erase(0,buff.find(">"));
} else {
c_data=buff;
buff.clear();
}
n_cinb--;
c_data.erase(std::remove(c_data.begin(), c_data.end(), '\n'),c_data.end());
return true;
}