-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.c
238 lines (201 loc) · 7.52 KB
/
main.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
/* 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 <unistd.h>
#include "sff.h"
/* D E F I N E S *************************************************************/
#define VERSION "0.9.2"
#define PRG_NAME "sff2fastq"
#define FASTQ_FILENAME_MAX_LENGTH 1024
#define SFF_FILENAME_MAX_LENGTH 1024
/* P R O T O T Y P E S *******************************************************/
void help_message(void);
void version_info(void);
void process_options(int argc, char *argv[]);
void process_sff_to_fastq(char *sff_file, char *fastq_file, int trim_flag);
void construct_fastq_entry(FILE *fp,
char *name,
char *bases,
uint8_t *quality,
int nbases);
/* G L O B A L S *************************************************************/
char fastq_file[FASTQ_FILENAME_MAX_LENGTH] = { '\0' };
char sff_file[SFF_FILENAME_MAX_LENGTH] = { '\0' };
// trimming is enabled by default -- like 454 tools
int opt_trim = 1;
/* M A I N *******************************************************************/
int main(int argc, char *argv[]) {
process_options(argc, argv);
process_sff_to_fastq(sff_file, fastq_file, opt_trim);
return 0;
}
/* F U N C T I O N S *********************************************************/
void
help_message() {
fprintf(stdout, "Usage: %s %s %s\n", PRG_NAME, "[options]", "[sff_file]");
fprintf(stdout, "\t%-20s%-20s\n", "-h", "This help message");
fprintf(stdout, "\t%-20s%-20s\n", "-v", "Program and version information");
fprintf(stdout, "\t%-20s%-20s\n", "-n", "Output the untrimmed sequence and quality scores");
fprintf(stdout, "\t%-20s%-20s %s\n",
"-o <fastq_file>",
"Desired fastq output file.",
"If not specified, defaults to stdout");
}
void
version_info() {
fprintf(stdout, "%s -- version: %s\n", PRG_NAME, VERSION);
}
void
process_options(int argc, char *argv[]) {
int c;
int index;
char *opt_o_value = NULL;
while( (c = getopt(argc, argv, "hvno:")) != -1 ) {
switch(c) {
case 'h':
help_message();
exit(0);
break;
case 'v':
version_info();
exit(0);
break;
case 'o':
opt_o_value = optarg;
break;
case 'n':
opt_trim = 0; /* disable trimming -- like 454 tools */
break;
case '?':
exit(1);
default:
abort();
}
}
if ( opt_o_value != NULL ) {
strncpy(fastq_file, opt_o_value, FASTQ_FILENAME_MAX_LENGTH);
}
/* process the remaining command line arguments */
for (index = optind; index < argc; index++) {
strncpy(sff_file, argv[index], SFF_FILENAME_MAX_LENGTH);
}
// /* just take the first passed in non-getopt argument as the sff file */
// strncpy(sff_file, argv[optind], SFF_FILENAME_MAX_LENGTH);
}
void
process_sff_to_fastq(char *sff_file, char *fastq_file, int trim_flag) {
sff_common_header h;
sff_read_header rh;
sff_read_data rd;
FILE *sff_fp, *fastq_fp;
if ( !strlen(sff_file) ) {
sff_fp = stdin;
}
else if ( (sff_fp = fopen(sff_file, "r")) == NULL ) {
fprintf(stderr,
"[err] Could not open file '%s' for reading.\n", sff_file);
exit(1);
}
read_sff_common_header(sff_fp, &h);
verify_sff_common_header(PRG_NAME, VERSION, &h);
// printf("size of header: %d \n", sizeof(sff_common_header));
// printf("\tmagic : 0x%x\n" , h.magic);
// printf("\tindex_offset : 0x%llx\n", h.index_offset);
// printf("\tindex_len : 0x%x\n" , h.index_len);
// printf("\tnumreads : 0x%x\n" , h.nreads);
// printf("\theader_len : 0x%x\n" , h.header_len);
// printf("\tkey_len : 0x%x\n" , h.key_len);
// printf("\tflow_len : 0x%x\n" , h.flow_len);
// printf("\tflowgram_fmt : 0x%x\n" , h.flowgram_format);
// printf("\tflow : %s\n " , h.flow);
// printf("\tkey : %s\n " , h.key);
// printf("\n\n");
if ( !strlen(fastq_file) ) {
fastq_fp = stdout;
}
else {
if ( (fastq_fp = fopen(fastq_file, "w")) == NULL ) {
fprintf(stderr,
"[err] Could not open file '%s' for writing.\n",
fastq_file);
exit(1);
}
}
int left_clip = 0, right_clip = 0, nbases = 0;
char *name;
char *bases;
uint8_t *quality;
register int i;
int numreads = (int) h.nreads;
for (i = 0; i < numreads; i++) {
read_sff_read_header(sff_fp, &rh);
read_sff_read_data(sff_fp, &rd, h.flow_len, rh.nbases);
/* get clipping points */
get_clip_values(rh, trim_flag, &left_clip, &right_clip);
nbases = right_clip - left_clip;
/* create bases string */
bases = get_read_bases(rd, left_clip, right_clip);
/* create quality array */
quality = get_read_quality_values(rd, left_clip, right_clip);
/* create read name string */
int name_length = (int) rh.name_len + 1; // account for NULL termination
name = (char *) malloc( name_length * sizeof(char) );
if (!name) {
fprintf(stderr, "Out of memory! For read name string!\n");
exit(1);
}
memset(name, '\0', (size_t) name_length);
strncpy(name, rh.name, (size_t) rh.name_len);
if (strlen(bases) > 0) {
construct_fastq_entry(fastq_fp, name, bases, quality, nbases);
}
free(name);
free(bases);
free(quality);
free_sff_read_header(&rh);
free_sff_read_data(&rd);
}
free_sff_common_header(&h);
fclose(fastq_fp);
fclose(sff_fp);
}
void
construct_fastq_entry(FILE *fp,
char *name,
char *bases,
uint8_t *quality,
int nbases) {
register int j;
uint8_t quality_char;
/* print out the name/sequence blocks */
fprintf(fp, "@%s\n%s\n+%s\n", name, bases, name);
// /* print out quality values (as integer) */
// for (j = 0; j < nbases; j++) {
// printf("%d ", (int) rd.quality[j] );
// }
// printf("\n");
/* print out quality values (as characters)
* formula taken from http://maq.sourceforge.net/fastq.shtml
*/
for (j = 0; j < nbases; j++) {
quality_char = (quality[j] <= 93 ? quality[j] : 93) + 33;
fprintf(fp, "%c", (char) quality_char );
}
fprintf(fp, "\n");
}