-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFasta.C
81 lines (60 loc) · 1.58 KB
/
CFasta.C
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
/****************************************************************
CFasta.C
BOOM : Bioinformatics Object Oriented Modules
Copyright (C)2012 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
****************************************************************/
#include <iostream>
#include "CFasta.H"
using namespace std;
using namespace BOOM;
CFasta::CFasta(const String &filename)
: f(filename), deflineRegex(">(\\S+).*"), filename(filename)
{
// ctor
}
CFasta::~CFasta()
{
if(f.isOpen()) f.close();
}
void CFasta::close()
{
f.close();
}
bool CFasta::getNextSequence(String &defline,String &id,
Vector<double> &values)
{
// First, skip blank lines
String line;
if(!nextDefline.isEmpty()) { line=nextDefline; nextDefline=""; }
else while(!f.eof()) {
line=f.getline();
if(!line.isEmpty()) break;
}
if(line.isEmpty()) return false;
// Parse defline
if(!deflineRegex.match(line))
throw String("Can't parse defline in file ")+filename+" : "+line;
id=deflineRegex[1];
// Read sequence values
while(!f.eof()) {
line=f.getline();
if(deflineRegex.match(line)) {
nextDefline=line;
return true;
}
BOOM::Vector<BOOM::String> &fields=*line.getFields();
BOOM::Vector<BOOM::String>::iterator cur=fields.begin(), end=fields.end();
for(; cur!=end ; ++cur) {
double x=(*cur).asDouble();
values.push_back(x);
}
delete &fields;
}
return true;
}
bool CFasta::eof()
{
return f.eof();
}