-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsff.c
375 lines (317 loc) · 11.7 KB
/
sff.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* L I C E N S E *************************************************************/
/*
Copyright (C) 2009, 2010 Indraniel Das <[email protected]>
and Washington University in St. Louis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>
*/
/* I N C L U D E S ***********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sff.h"
/* F U N C T I O N S *********************************************************/
void
read_sff_common_header(FILE *fp, sff_common_header *h) {
char *flow;
char *key;
int header_size;
fread(&(h->magic) , sizeof(uint32_t), 1, fp);
fread(&(h->version) , sizeof(char) , 4, fp);
fread(&(h->index_offset) , sizeof(uint64_t), 1, fp);
fread(&(h->index_len) , sizeof(uint32_t), 1, fp);
fread(&(h->nreads) , sizeof(uint32_t), 1, fp);
fread(&(h->header_len) , sizeof(uint16_t), 1, fp);
fread(&(h->key_len) , sizeof(uint16_t), 1, fp);
fread(&(h->flow_len) , sizeof(uint16_t), 1, fp);
fread(&(h->flowgram_format), sizeof(uint8_t) , 1, fp);
/* sff files are in big endian notation so adjust appropriately */
h->magic = htobe32(h->magic);
h->index_offset = htobe64(h->index_offset);
h->index_len = htobe32(h->index_len);
h->nreads = htobe32(h->nreads);
h->header_len = htobe16(h->header_len);
h->key_len = htobe16(h->key_len);
h->flow_len = htobe16(h->flow_len);
/* finally appropriately allocate and read the flow and key strings */
flow = (char *) malloc( h->flow_len * sizeof(char) );
if (!flow) {
fprintf(stderr,
"Out of memory! Could not allocate header flow string!\n");
exit(1);
}
key = (char *) malloc( h->key_len * sizeof(char) );
if (!key) {
fprintf(stderr,
"Out of memory! Could not allocate header key string!\n");
exit(1);
}
fread(flow, sizeof(char), h->flow_len, fp);
h->flow = flow;
fread(key , sizeof(char), h->key_len , fp);
h->key = key;
/* the common header section should be a multiple of 8-bytes
if the header is not, it is zero-byte padded to make it so */
header_size = sizeof(h->magic)
+ sizeof(*(h->version))*4
+ sizeof(h->index_offset)
+ sizeof(h->index_len)
+ sizeof(h->nreads)
+ sizeof(h->header_len)
+ sizeof(h->key_len)
+ sizeof(h->flow_len)
+ sizeof(h->flowgram_format)
+ (sizeof(char) * h->flow_len)
+ (sizeof(char) * h->key_len);
if ( !(header_size % PADDING_SIZE == 0) ) {
read_padding(fp, header_size);
}
}
void
read_padding(FILE *fp, int header_size) {
int remainder = PADDING_SIZE - (header_size % PADDING_SIZE);
uint8_t padding[remainder];
fread(padding, sizeof(uint8_t), remainder, fp);
}
void
free_sff_common_header(sff_common_header *h) {
free(h->flow);
free(h->key);
}
void
verify_sff_common_header(char *prg_name,
char *prg_version,
sff_common_header *h) {
/* ensure that the magic file type is valid */
if (h->magic != SFF_MAGIC) {
fprintf(stderr, "The SFF header has magic value '%d' \n", h->magic);
fprintf(stderr,
"[err] %s (version %s) %s : '%d' \n",
prg_name,
prg_version,
"only knows how to deal an SFF magic value of type",
SFF_MAGIC);
free_sff_common_header(h);
exit(2);
}
/* ensure that the version header is valid */
if ( memcmp(h->version, SFF_VERSION, SFF_VERSION_LENGTH) ) {
fprintf(stderr, "The SFF file has header version: ");
int i;
char *sff_header_version = h->version;
for (i=0; i < SFF_VERSION_LENGTH; i++) {
printf("0x%02x ", sff_header_version[i]);
}
printf("\n");
fprintf(stderr,
"[err] %s (version %s) %s : ",
prg_name,
prg_version,
"only knows how to deal an SFF header version: ");
char valid_header_version[SFF_VERSION_LENGTH] = SFF_VERSION;
for (i=0; i < SFF_VERSION_LENGTH; i++) {
printf("0x%02x ", valid_header_version[i]);
}
free_sff_common_header(h);
exit(2);
}
}
void
read_sff_read_header(FILE *fp, sff_read_header *rh) {
char *name;
int header_size;
fread(&(rh->header_len) , sizeof(uint16_t), 1, fp);
fread(&(rh->name_len) , sizeof(uint16_t), 1, fp);
fread(&(rh->nbases) , sizeof(uint32_t), 1, fp);
fread(&(rh->clip_qual_left) , sizeof(uint16_t), 1, fp);
fread(&(rh->clip_qual_right) , sizeof(uint16_t), 1, fp);
fread(&(rh->clip_adapter_left) , sizeof(uint16_t), 1, fp);
fread(&(rh->clip_adapter_right), sizeof(uint16_t), 1, fp);
/* sff files are in big endian notation so adjust appropriately */
rh->header_len = htobe16(rh->header_len);
rh->name_len = htobe16(rh->name_len);
rh->nbases = htobe32(rh->nbases);
rh->clip_qual_left = htobe16(rh->clip_qual_left);
rh->clip_qual_right = htobe16(rh->clip_qual_right);
rh->clip_adapter_left = htobe16(rh->clip_adapter_left);
rh->clip_adapter_right = htobe16(rh->clip_adapter_right);
/* finally appropriately allocate and read the read_name string */
name = (char *) malloc( rh->name_len * sizeof(char) );
if (!name) {
fprintf(stderr,
"Out of memory! Could not allocate header flow string!\n");
exit(1);
}
fread(name, sizeof(char), rh->name_len, fp);
rh->name = name;
/* the section should be a multiple of 8-bytes, if not,
it is zero-byte padded to make it so */
header_size = sizeof(rh->header_len)
+ sizeof(rh->name_len)
+ sizeof(rh->nbases)
+ sizeof(rh->clip_qual_left)
+ sizeof(rh->clip_qual_right)
+ sizeof(rh->clip_adapter_left)
+ sizeof(rh->clip_adapter_right)
+ (sizeof(char) * rh->name_len);
if ( !(header_size % PADDING_SIZE == 0) ) {
read_padding(fp, header_size);
}
}
void
free_sff_read_header(sff_read_header *h) {
free(h->name);
}
void
read_sff_read_data(FILE *fp,
sff_read_data *rd,
uint16_t nflows,
uint32_t nbases) {
uint16_t *flowgram;
uint8_t *flow_index;
uint8_t *quality;
char *bases;
int data_size;
int i;
/* allocate the appropriate arrays */
flowgram = (uint16_t *) malloc( nflows * sizeof(uint16_t) );
if (!flowgram) {
fprintf(stderr,
"Out of memory! Could not allocate for a read flowgram!\n");
exit(1);
}
flow_index = (uint8_t *) malloc( nbases * sizeof(uint8_t) );
if (!flow_index) {
fprintf(stderr,
"Out of memory! Could not allocate for a read flow index!\n");
exit(1);
}
quality = (uint8_t *) malloc( nbases * sizeof(uint8_t) );
if (!quality) {
fprintf(stderr,
"Out of memory! Could not allocate for a read quality string!\n");
exit(1);
}
bases = (char * ) malloc( nbases * sizeof(char) );
if (!bases) {
fprintf(stderr,
"Out of memory! Could not allocate for a read base string!\n");
exit(1);
}
/* read from the sff file and assign to sff_read_data */
fread(flowgram, sizeof(uint16_t), (size_t) nflows, fp);
/* sff files are in big endian notation so adjust appropriately */
for (i = 0; i < nflows; i++) {
flowgram[i] = htobe16(flowgram[i]);
}
rd->flowgram = flowgram;
fread(flow_index, sizeof(uint8_t), (size_t) nbases, fp);
rd->flow_index = flow_index;
fread(bases, sizeof(char), (size_t) nbases, fp);
rd->bases = bases;
fread(quality, sizeof(uint8_t), (size_t) nbases, fp);
rd->quality = quality;
/* the section should be a multiple of 8-bytes, if not,
it is zero-byte padded to make it so */
data_size = (sizeof(uint16_t) * nflows) // flowgram size
+ (sizeof(uint8_t) * nbases) // flow_index size
+ (sizeof(char) * nbases) // bases size
+ (sizeof(uint8_t) * nbases); // quality size
if ( !(data_size % PADDING_SIZE == 0) ) {
read_padding(fp, data_size);
}
}
void
free_sff_read_data(sff_read_data *d) {
free(d->flowgram);
free(d->flow_index);
free(d->bases);
free(d->quality);
}
/* as described in section 13.3.8.2 "Read Header Section" in the
454 Genome Sequencer Data Analysis Software Manual
see (http://sequence.otago.ac.nz/download/GS_FLX_Software_Manual.pdf) */
void
get_clip_values(sff_read_header rh,
int trim_flag,
int *left_clip,
int *right_clip) {
if (trim_flag) {
(*left_clip) =
(int) max(1, max(rh.clip_qual_left, rh.clip_adapter_left));
// account for the 1-based index value
*left_clip = *left_clip - 1;
(*right_clip) = (int) min(
(rh.clip_qual_right == 0 ? rh.nbases : rh.clip_qual_right ),
(rh.clip_adapter_right == 0 ? rh.nbases : rh.clip_adapter_right)
);
}
else {
(*left_clip) = 0;
(*right_clip) = (int) rh.nbases;
}
}
char*
get_read_bases(sff_read_data rd,
int left_clip,
int right_clip) {
char *bases;
// account for NULL termination
int bases_length = (right_clip - left_clip) + 1;
if (bases_length < 1) {
bases = (char *) malloc(1);
return bases;
};
// inititalize the bases string/array
bases = (char *) malloc( bases_length * sizeof(char) );
if (bases == NULL) {
fprintf(stderr, "Out of memory! For read bases string!\n");
exit(1);
}
memset(bases, '\0', (size_t) bases_length);
// copy the relative substring
int start = left_clip;
int stop = right_clip;
int i, j = 0;
for (i = start; i < stop; i++) {
*(bases + j) = *(rd.bases + i);
j++;
}
return bases;
}
uint8_t*
get_read_quality_values(sff_read_data rd,
int left_clip,
int right_clip) {
uint8_t *quality;
// account for NULL termination
int quality_length = (right_clip - left_clip) + 1;
if (quality_length < 1) {
quality = (uint8_t *) malloc(1);
return quality;
};
// inititalize the quality array
quality = (uint8_t *) malloc( quality_length * sizeof(uint8_t) );
if (!quality) {
fprintf(stderr, "Out of memory! For read quality array!\n");
exit(1);
}
memset(quality, '\0', (size_t) quality_length);
// copy the relative substring
int start = left_clip;
int stop = right_clip;
int i, j = 0;
for (i = start; i < stop; i++) {
*(quality + j) = *(rd.quality + i);
j++;
}
return quality;
}