-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadfastaq.h
240 lines (187 loc) · 4.51 KB
/
readfastaq.h
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <iomanip> //setprecision
#include <algorithm> // sort, reverse
#include <gzstream.h>
#include <vector> //setprecision
using std::cout;
using std::endl;
using std::vector;
using std::string;
static gzFile fp;
static vector<int> rlen;
static vector<string> rseq;
static vector<string> rqual;
static vector<string> rname;
static vector<string> rcomment;
// ---------------------------------------- //
int fasttype(char* file)
// ---------------------------------------- //
{
char fq[5]={"@"};
char fa[5]={">"};
string ttname;
int isfq=0;
igzstream infile(file);
getline(infile,ttname);
string ftype=ttname.substr(0,1);
if(ftype==fa) isfq=0;
else isfq=1;
return(isfq);
}
// ---------------------------------------- //
int readfastq(char* file, int saveinfo=0, int readseq=0, int minlen=0, string selctg="")
// ---------------------------------------- //
{
igzstream infile(file);
char fq[5]={"@"};
char fa[5]={">"};
char plus[5]={"+"};
int nseq=0;
rlen.reserve(100000);
rname.reserve(100000);
if(readseq){
rseq.reserve(100000);
rqual.reserve(100000);
}
string read;
string lname;
string lcomment="";
string lseq="";
int seqlen=0;
int quallen=0;
string lqual;
int seqlines=0;
int stop=1;
while(stop){
getline(infile,read);
if(read.substr(0,1)==fq){ // name
nseq++;
if(nseq>1){ // previous
if(seqlen>=minlen){
if(saveinfo){
rname.push_back(lname);
if(lcomment.size())rcomment.push_back(lcomment);
rlen.push_back(seqlen);
if(readseq)rseq.push_back(lseq);
if(readseq)rqual.push_back(lqual);
}
//cout << fa << lname ;
// if(lcomment.size()) cout << lcomment <<endl;
// else cout << endl;
if(quallen != seqlen)
cout << " ERROR! seq length different from quality lenght!! " << endl;
}
}
size_t ns=0;
size_t nt=0;
ns=read.find(" ");
nt=read.find("\t");
if(ns!=std::string::npos) {
lname=read.substr(1,ns);
lcomment=read.substr(ns+1,read.size());
}else if(nt!=std::string::npos) {
lname=read.substr(1,nt);
lcomment=read.substr(nt+1,read.size());
}else{
lname=read.erase(0,1);
}
if(readseq){
lseq="";
lqual="";
}
seqlen=0;
seqlines=0;
quallen=0;
}else if(read.substr(0,1)==plus){ // + and qual
for(int ll=0; ll<seqlines; ll++){
getline(infile,read);
if(readseq)lqual.append(read);
quallen+=read.size();
}
}else{ // sequence
if(readseq)lseq.append(read);
seqlines++;
seqlen+=read.size();
}
// EOF
if(infile.eof()){ // previous
if(seqlen>=minlen){
if(saveinfo){
rname.push_back(lname);
if(lcomment.size())rcomment.push_back(lcomment);
rlen.push_back(seqlen);
if(readseq)rseq.push_back(lseq);
if(readseq)rqual.push_back(lqual);
}
if(quallen != seqlen)
cout << " ERROR! seq length different from quality lenght!! " << endl;
}
stop=0;
}
}//read loop
return 0;
}
// ---------------------------------------- //
int readfasta(char* file, int saveinfo=0, int readseq=0, int minlen=0, string selctg="")
// ---------------------------------------- //
{
igzstream infile(file);
char fa[5]={">"};
int nseq=0;
rlen.reserve(100000);
rname.reserve(100000);
if(readseq)
rseq.reserve(100000);
string read;
string lname;
string lcomment="";
string lseq="";
int seqlen=0;
int stop=1;
while(stop){
getline(infile,read);
if(read.substr(0,1)==fa){ // name
nseq++;
if(nseq>1){ // previous
if(seqlen>=minlen){
if(saveinfo){
rname.push_back(lname);
rlen.push_back(seqlen);
if(readseq)rseq.push_back(lseq);
if(lcomment.size())rcomment.push_back(lcomment);
}
}
}
size_t ns=0;
size_t nt=0;
ns=read.find(" ");
nt=read.find("\t");
if(ns!=std::string::npos) {
lname=read.substr(1,ns);
lcomment=read.substr(ns+1,read.size());
}else if(nt!=std::string::npos) {
lname=read.substr(1,nt);
lcomment=read.substr(nt+1,read.size());
}else{
lname=read.erase(0,1);
}
if(readseq)lseq="";
seqlen=0;
}else{ // sequence
if(readseq)lseq.append(read);
seqlen+=read.size();
}
// EOF
if(infile.eof()){ // previous
if(seqlen>=minlen){
if(saveinfo){
rname.push_back(lname);
rlen.push_back(seqlen);
if(readseq)rseq.push_back(lseq);
if(lcomment.size())rcomment.push_back(lcomment);
}
}
stop=0;
}
}//read loop
return 0;
}