-
Notifications
You must be signed in to change notification settings - Fork 49
/
ReadFiles.hpp
276 lines (240 loc) · 5.67 KB
/
ReadFiles.hpp
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// The class for reading reads from a file
#ifndef _MOURISL_READS
#define _MOURISL_READS
#include <stdio.h>
#include <string.h>
#include <zlib.h>
#include "defs.h"
#include "kseq.h"
#define MAX_READ_FILE 100
KSEQ_INIT( gzFile, gzread ) ;
struct _Read
{
char *id ;
char *seq ;
char *qual ;
} ;
class ReadFiles
{
private:
gzFile gzFp[MAX_READ_FILE] ;
kseq_t *inSeq[MAX_READ_FILE] ;
bool hasMate[MAX_READ_FILE] ;
int FILE_TYPE[MAX_READ_FILE] ; // 0-FASTA, 1-FASTQ
int fpUsed ;
int currentFpInd ;
void GetFileName( char *in, char *out )
{
int i, j ;
int len = (int)strlen( in ) ;
for ( i = len ; i >= 0 && in[i] != '.' && in[i] != '/' ; --i )
;
for ( j = len ; j >= 0 && in[j] != '/' ; --j )
;
if ( i >= 0 && in[i] == '.' )
{
in[i] = '\0' ;
strcpy( out, in + j + 1 ) ;
in[i] = '.' ;
}
else
{
strcpy( out, in + j + 1 ) ;
}
}
public:
char *id ;
char *seq ;
char *qual ;
ReadFiles(): fpUsed(0), currentFpInd(0)
{
id = seq = qual = NULL ;
}
~ReadFiles()
{
int i ;
if ( id != NULL )
free( id ) ;
if ( seq != NULL )
free( seq ) ;
if ( qual != NULL )
free( qual ) ;
for ( i = 0 ; i < fpUsed ; ++i )
{
kseq_destroy( inSeq[i] ) ;
gzclose( gzFp[i] ) ;
}
}
void AddReadFile( const char *file, bool fileHasMate )
{
if ( fpUsed >= MAX_READ_FILE )
{
fprintf( stderr, "The number of read files exceeds the limit %d.\n", MAX_READ_FILE ) ;
exit( 1 ) ;
}
gzFp[ fpUsed ] = gzopen( file, "r" ) ;
inSeq[ fpUsed ] = kseq_init( gzFp[ fpUsed ] ) ;
kseq_read( inSeq[ fpUsed ] ) ;
if ( inSeq[ fpUsed ]->qual.l == 0 )
{
FILE_TYPE[ fpUsed ] = 0 ;
//qual[0] = '\0' ;
}
else
{
FILE_TYPE[ fpUsed ] = 1 ;
}
/*else
{
fprintf( stderr, "\"%s\"'s format is wrong.\n", file ) ;
exit( 1 ) ;
}*/
//printf( "%s %s\n", inSeq[fpUsed]->name.s, inSeq[fpUsed]->comment.s ) ;
gzrewind( gzFp[ fpUsed ]) ;
kseq_rewind( inSeq[ fpUsed] ) ;
//kseq_read( inSeq[ fpUsed ] ) ;
//printf( "%s %s\n", inSeq[fpUsed]->name.s, inSeq[fpUsed]->comment.s ) ;
//gzrewind( gzFp[ fpUsed ]) ;
//kseq_rewind( inSeq[ fpUsed] ) ;
hasMate[ fpUsed ] = fileHasMate ;
++fpUsed ;
}
bool HasQuality()
{
return ( FILE_TYPE[ currentFpInd ] != 0 ) ;
}
void Rewind()
{
int i ;
for ( i = 0 ; i < fpUsed ; ++i )
{
gzrewind( gzFp[i] ) ;
kseq_rewind( inSeq[i] ) ;
}
if ( id != NULL )
free( id ) ;
if ( seq != NULL )
free( seq ) ;
if ( qual != NULL )
free( qual ) ;
id = seq = qual = NULL ;
currentFpInd = 0 ;
}
int Next()
{
//int len ;
//char buffer[2048] ;
while ( currentFpInd < fpUsed && ( kseq_read( inSeq[ currentFpInd ] ) < 0 ) )
{
++currentFpInd ;
}
if ( currentFpInd >= fpUsed )
return 0 ;
/*printf( "%s %s\n", id, inSeq[currentFpInd ]->comment.s ) ;
if ( inSeq[currentFpInd]->comment.l )
{
id[ inSeq[currentFpInd ]->name.l ] = ' ' ;
strcpy( &id[ inSeq[currentFpInd]->name.l + 1], inSeq[ currentFpInd]->comment.s ) ;
}*/
if ( id != NULL )
free( id ) ;
if ( seq != NULL )
free( seq ) ;
if ( qual != NULL )
free( qual ) ;
id = strdup( inSeq[ currentFpInd ]->name.s ) ;
int len = strlen( id ) ;
if ( ( id[len - 1] == '1' || id[len - 1] == '2' )
&& id[len - 2] == '/' )
{
id[len - 2] = '\0' ;
}
seq = strdup( inSeq[ currentFpInd ]->seq.s ) ;
if ( inSeq[ currentFpInd ]->qual.l )
qual = strdup( inSeq[ currentFpInd]->qual.s ) ;
else
qual = NULL ;
return 1 ;
}
int NextWithBuffer( char **id, char **seq, char **qual, bool removeReturn = true, bool stopWhenFileEnds = false )
{
//int len ;
//char buffer[2048] ;
while ( currentFpInd < fpUsed && ( kseq_read( inSeq[ currentFpInd ] ) < 0 ) )
{
++currentFpInd ;
if ( stopWhenFileEnds )
return -1 ;
}
if ( currentFpInd >= fpUsed )
return 0 ;
if ( *id != NULL )
free( *id ) ;
if ( *seq != NULL )
free( *seq ) ;
if ( *qual != NULL )
free( *qual ) ;
*id = strdup( inSeq[ currentFpInd ]->name.s ) ;
*seq = strdup( inSeq[ currentFpInd ]->seq.s ) ;
/*if ( removeReturn )
{
int i ;
for ( i = strlen( *seq ) - 1 ; i >= 0 ; --i )
if ( (*seq)[i] != '\n' )
break ;
(*seq)[i + 1] = '\0' ;
}*/
if ( inSeq[ currentFpInd ]->qual.l )
*qual = strdup( inSeq[ currentFpInd]->qual.s ) ;
else
*qual = NULL ;
return 1 ;
}
// Get a batch of reads, it terminates until the buffer is full or
// the file ends.
int GetBatch( struct _Read *readBatch, int maxBatchSize, int &fileInd, bool trimReturn, bool stopWhenFileEnds )
{
int batchSize = 0 ;
while ( batchSize < maxBatchSize )
{
int tmp = NextWithBuffer( &readBatch[ batchSize].id, &readBatch[batchSize].seq,
&readBatch[batchSize].qual, trimReturn, stopWhenFileEnds ) ;
if ( tmp == -1 && batchSize > 0 )
{
--currentFpInd ;
fileInd = currentFpInd ;
return batchSize ; // Finished read current file.
}
else if ( tmp == -1 && batchSize == 0 )
continue ; // The current read file is empty
else if ( tmp == 0 && batchSize == 0 )
{
fileInd = currentFpInd ;
return 0 ; // Finished reading
}
++batchSize ;
}
fileInd = currentFpInd ;
return batchSize ;
}
void FreeBatch(struct _Read *readBatch, int batchSize)
{
int i ;
for (i = 0 ; i < batchSize ; ++i)
{
free(readBatch[i].id) ;
free(readBatch[i].seq) ;
if (readBatch[i].qual)
free(readBatch[i].qual) ;
}
}
int GetFpUsed()
{
return fpUsed ;
}
} ;
// The class handling read in a batch of reads
/*class ReadBatch
{
} ;*/
#endif