-
Notifications
You must be signed in to change notification settings - Fork 3
/
FastQParser.cpp
136 lines (103 loc) · 2.62 KB
/
FastQParser.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* FastQParser
* Date: Dec-16-2012
* Author : Gabriel Renaud gabriel.reno [at sign here ] gmail.com
*
*/
#include "FastQParser.h"
FastQParser::FastQParser(string file,bool isFasta){
this->isFasta = isFasta;
fastqFile.open(file.c_str(), ios::in); // open the streams
if (fastqFile.good()) {
//fine
}else{
cerr<<"Unable to open the file "<<file<<endl;
exit(1);
}
numberOfTimesHasDataWasCalled=0;
initPointer=false;
}
FastQParser::~FastQParser(){
fastqFile.close();
// delete toreturn;
}
bool FastQParser::hasData(){
if(!initPointer){
initPointer=true;
}else{
delete(toreturn);
}
numberOfTimesHasDataWasCalled++;
bool flag;
if(isFasta){
flag=(bool) getline(fastqFile,currentline1);
if(!flag){
return false;
}else{
if(currentline1.length() > 0 && currentline1[0] != '>'){
cerr<<"Parse error#1 with line "<<currentline1<<endl;
exit(1);
}
}
string * id = new string(currentline1);
string * seq = new string();
while(flag){
char peekChar=fastqFile.peek();
if(peekChar == EOF){
toreturn=new FastQObj(id,seq,0,isFasta);
return true;
}
if(peekChar == '>'){//beginning of next record
toreturn=new FastQObj(id,seq,0,isFasta);
return true;
}
flag=(bool) getline(fastqFile,currentline2);
if(!flag){
cerr<<"Parse error#2 with line "<<currentline1<<endl;
exit(1);
}else{
seq->append(currentline2);
}
}
}else{
flag=(bool) getline(fastqFile,currentline1);
if(!flag){
return false;
}else{
if(currentline1.length() > 0 && currentline1[0] != '@'){
cerr<<"Parse error#3 with line "<<currentline1<<endl;
exit(1);
}
}
flag= (bool) getline(fastqFile,currentline2);
if(!flag){
cerr<<"Parse error#4 with line "<<currentline2<<endl;
exit(1);
}
flag= (bool) getline(fastqFile,currentline3);
if(!flag){
cerr<<"Parse error#5 with line "<<currentline3<<endl;
exit(1);
}else{
if(currentline3.length() > 0 && currentline3[0] != '+'){
cerr<<"Parse error#6 with line "<<currentline3<<endl;
exit(1);
}
}
flag=(bool) getline(fastqFile,currentline4);
if(!flag){
cerr<<"Parse error#7 with line "<<currentline4<<endl;
exit(1);
}
toreturn=new FastQObj(¤tline1,¤tline2,¤tline4,isFasta);
}
return true;
}
FastQObj * FastQParser::getData(){
if(numberOfTimesHasDataWasCalled != 1){
cerr<<"The subroutine hasData must have been called once prior to calling getData it was called: "<<numberOfTimesHasDataWasCalled<<" times " <<endl;
exit(1);
}
numberOfTimesHasDataWasCalled=0;
return toreturn;
}