-
Notifications
You must be signed in to change notification settings - Fork 6
/
gzfastq_uniq_sort.c
332 lines (306 loc) · 10.4 KB
/
gzfastq_uniq_sort.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
// gcc -g -O3 -Wall gzfastq_uniq_sort.c -o gzfastq_uniq_sort -I/share/software/software/zlib_1.2.8_install/include -I. -L/share/software/software/zlib_1.2.8_install/lib -L. -l hashtbl -lz
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <zlib.h>
#include <time.h>
#include <sys/time.h>
#include "hashtbl.h"
#define free_Name_Quality(line) \
do{ \
free(line.name); \
free(line.quality); \
}while (0)
#define free_Fastq(line) \
do{ \
free((line)->seq); \
free_Name_Quality((line)->Name_quality); \
free((line)); \
}while (0)
typedef struct _Name_Quality_
{
char *name;
char *quality;
}Name_Quality;
typedef struct _fastq_
{
Name_Quality Name_quality;
char *seq;
}Fastq;
typedef struct _new_fastq_
{
unsigned long count;
Name_Quality uniq_fastq[2];
}New_Fastq;
struct globalArgs_t {
const char *read1;
const char *read2;
short ends;
const char *outfile;
} globalArgs;
static inline Fastq *readNextNode(gzFile fq,char *buf);
gzFile creat_gzinfile(const char *infile);
gzFile creat_gzoutfile(const char *outfile,const char *suffix);
HASHTBL *load_fastq_file(const char *fq_file1,const char *fq_file2,unsigned long *total_reads_count,long long begin,short *strLen);
unsigned long count_read(gzFile fq,char *buf);
void display_usage(char * argv[]);
static inline int compare_hashed_data_count(const void *a, const void *b);
void qsort_output_hash(HASHTBL *hashtbl,const char *outfile,short strLen);
void output_hash(HASHTBL *hashtbl,const char *outfile,short strLen);
static inline long long usec(void);
long long usec(void){
struct timeval tv;
gettimeofday(&tv,NULL);
return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
}
Fastq *readNextNode(gzFile fq,char *buf) {
Fastq *line = (Fastq *)malloc(sizeof(Fastq));
buf=gzgets(fq,buf,1024*sizeof(char));
if (!gzeof(fq)) {
*(buf+strlen(buf)-1)='\0';
line->Name_quality.name = strdup(buf);
buf=gzgets(fq,buf,1024*sizeof(char));
*(buf+strlen(buf)-1)='\0'; //remove the last \n in the buf
line->seq = strdup(buf);
buf=gzgets(fq,buf,1024*sizeof(char));
buf=gzgets(fq,buf,1024*sizeof(char));
*(buf+strlen(buf)-1)='\0';
line->Name_quality.quality = strdup(buf);
return line;
} else {
free(line);
return NULL;
}
}
gzFile creat_gzinfile(const char *infile) {
gzFile fiz=gzopen(infile,"rb");
if(fiz == NULL) {
fprintf(stderr,"open file %s failed\n",infile);
exit(1);
}
return fiz;
}
gzFile creat_gzoutfile(const char *outfile,const char *suffix) {
char *out=(char *)calloc(strlen(outfile)+128,sizeof(char));
sprintf(out,"%s%s",outfile,suffix);
gzFile fo = gzopen(out,"wb");
if (fo == NULL){
fprintf(stderr,"open file %s failed\n",out);
exit(1);
}
return fo;
}
HASHTBL *load_fastq_file(const char *fq_file1,const char *fq_file2,unsigned long *total_reads_count,long long begin,short *strLen)
{
char *buf = (char *)malloc(1024 * sizeof(char));
gzFile fastq1=creat_gzinfile(fq_file1);
HSIZE elecnt=(HSIZE)count_read(fastq1,buf);
// fprintf(stderr,"Finished %s count_read at %.3f s\n",fq_file1,(double)(usec()-begin)/CLOCKS_PER_SEC);
HSIZE tblsiz=1.34*elecnt;
gzFile fastq2 = NULL;
if (globalArgs.ends>1){
fastq2=creat_gzinfile(fq_file2);
}
HASHTBL *tbl = hashtbl_create(tblsiz, NULL);
Fastq *line1=NULL,*line2=NULL;
char *pair_seq=(char *)calloc(1024 , sizeof(char));
while (1){
line1=readNextNode(fastq1,buf);
if (line1==NULL) break;
if (! *strLen) *strLen=(short)strlen(line1->seq);
// if (strlen(line1->seq)>75) {
// strncpy(pair_seq,line1->seq,50);
// }else{
strncpy(pair_seq,line1->seq,strlen(line1->seq));
// }
if (globalArgs.ends>1) {
line2=readNextNode(fastq2,buf);
if (line2==NULL || strncmp(line1->Name_quality.name,line2->Name_quality.name,strchr(line1->Name_quality.name,' ')-line1->Name_quality.name)){
fprintf(stderr,"error at %ld: %s\n",*total_reads_count,line1->Name_quality.name);
break;
}
// if (strlen(line2->seq)>75) {
// strncpy(pair_seq+50,line2->seq,50);
// }else{
strncpy(pair_seq+strlen(line1->seq),line2->seq,strlen(line2->seq));
// }
}
void *data=hashtbl_get(tbl,pair_seq);
if (data==NULL){
New_Fastq *new_fastq=(New_Fastq *)malloc(sizeof(New_Fastq));
new_fastq->uniq_fastq[0]=line1->Name_quality;
if (globalArgs.ends>1) new_fastq->uniq_fastq[1]=line2->Name_quality;
new_fastq->count=1;
hashtbl_insert(tbl, pair_seq, (void *)new_fastq);
}
else{
((*(New_Fastq *)data).count)++;
free_Fastq(line1);
if (globalArgs.ends>1) free_Fastq(line2);
}
(*total_reads_count)++;
if(*total_reads_count % (elecnt/10)==0){
fprintf(stderr,"loaded %lu at %.3f s\n",*total_reads_count,(double)(usec()-begin)/CLOCKS_PER_SEC);
}
memset(pair_seq,0,1024 * sizeof(char));
}
gzclose(fastq1);
free(pair_seq);
if (globalArgs.ends>1) gzclose(fastq2);
fprintf(stderr,"unique reads number = %d\n",(int)tbl->count);
fprintf(stderr,"Finished load hash at %.3f s\n",(double)(usec()-begin)/CLOCKS_PER_SEC);
return tbl;
}
unsigned long count_read(gzFile fq,char *buf){
unsigned long i=0;
while (gzgets(fq,buf,1024*sizeof(char))!=NULL){
gzgets(fq,buf,1024*sizeof(char));
gzgets(fq,buf,1024*sizeof(char));
gzgets(fq,buf,1024*sizeof(char));
i++;
}
gzrewind(fq);
fprintf(stderr,"total_reads_num: %ld\n",i);
return i;
}
void display_usage(char * argv[]){
char *buffer=(char* )malloc(10240*sizeof(char));
const char* usage=
"\nCopyright (c) Berrygenomics 2012-2013\n" \
"Contact: XiongXu <[email protected]> <[email protected]> \n" \
"Discription:\n If you haven't appointed the read2 file, this program just handle the read1 file. Otherwise, it will also handle the read2 fastq file, and treat each of the joined read1 and read2 sequence as the key, 52193025 with 100 nt length pair end reads will cost about 22G memery(with 44%% unique pair end reads).\n" \
"Usage: %s [-1 READ1] [-2 READ2] [-o OUTFILE] [-h] \n" \
"Example1:\n %s -1 /home/litc/work/mate_pair/data/Maize/M017_L7_I030_R1.clean.fastq -2 /home/litc/work/mate_pair/data/Maize/M017_L7_I030_R2.clean.fastq -o pair_out \n" \
"Example2:\n %s -1 /home/litc/work/mate_pair/data/Maize/M017_L7_I030_R1.clean.fastq -o single_out \n" \
"\n" \
" [-1 READ1] = fastq formated file1. [required]\n" \
" [-2 READ2] = fastq formated file2. [option]\n" \
" [-o OUTPUT] = OUTPUT file. [required]\n" \
" [-h] = This helpful help screen. [option]\n" \
"\n";
sprintf(buffer,usage,argv[0],argv[0],argv[0]);
fprintf(stderr,"%s",buffer);
free(buffer);
exit(1);
}
int compare_hashed_data_count(const void *a, const void *b) {
return ((New_Fastq *)(*(ENTRY* const *)b)->data)->count-((New_Fastq *)(*(ENTRY* const *)a)->data)->count;
}
void qsort_output_hash(HASHTBL *hashtbl,const char *outfile,short strLen){
gzFile out1=creat_gzoutfile(outfile,"_1_uniq.fq.gz");
gzFile out2=NULL;
if (globalArgs.ends>1) out2=creat_gzoutfile(outfile,"_2_uniq.fq.gz");
ENTRY **nodes=dump_hash_table(hashtbl);
// fprintf(stderr,"Start qsort\n");
qsort(nodes, hashtbl->count, sizeof(ENTRY *), compare_hashed_data_count);
unsigned long i;
char Sequence[strLen+1];
for (i=0;i<hashtbl->count ;i++ ) {
New_Fastq *temp_data=(New_Fastq *)(nodes[i]->data);
memcpy(Sequence,nodes[i]->key,strLen*sizeof(char));
Sequence[strLen]=0;
if (globalArgs.ends>1) {
gzprintf(out2,"%s\t%ld\n%s\n+\n%s\n",temp_data->uniq_fastq[1].name,temp_data->count,
nodes[i]->key+strLen,temp_data->uniq_fastq[1].quality);
free_Name_Quality(temp_data->uniq_fastq[1]);
}
gzprintf(out1,"%s\t%ld\n%s\n+\n%s\n",temp_data->uniq_fastq[0].name,temp_data->count,
Sequence,temp_data->uniq_fastq[0].quality);
free_Name_Quality(temp_data->uniq_fastq[0]);
free(temp_data);
}
free(nodes);
gzclose(out1);
if (globalArgs.ends>1) gzclose(out2);
}
void output_hash(HASHTBL *hashtbl,const char *outfile,short strLen){
gzFile out1=creat_gzoutfile(outfile,"_1_uniq.fq.gz");
gzFile out2=NULL;
if (globalArgs.ends>1) out2=creat_gzoutfile(outfile,"_2_uniq.fq.gz");
unsigned long i;
ENTRY *entry;
char Sequence[strLen+1];
for (i = 0; i < hashtbl->size; ++i) {
entry = hashtbl->nodes[i];
while (entry) {
New_Fastq *temp_data=(New_Fastq *)(entry->data);
memcpy(Sequence,entry->key,strLen*sizeof(char));
Sequence[strLen]=0;
if (globalArgs.ends>1) {
gzprintf(out2,"%s\t%ld\n%s\n+\n%s\n",temp_data->uniq_fastq[1].name,temp_data->count,
entry->key+strLen,temp_data->uniq_fastq[1].quality);
free_Name_Quality(temp_data->uniq_fastq[1]);
}
gzprintf(out1,"%s\t%ld\n%s\n+\n%s\n",temp_data->uniq_fastq[0].name,temp_data->count,
Sequence,temp_data->uniq_fastq[0].quality);
free_Name_Quality(temp_data->uniq_fastq[0]);
free(temp_data);
entry = entry->next;
}
}
gzclose(out1);
if (globalArgs.ends>1) gzclose(out2);
}
void hash_data_delete(void *DATA) {
ENTRY *node=(ENTRY *)DATA;
// free(node->data);
free(node);
}
int main(int argc, char *argv[]){
int opt = 0;
globalArgs.read1=NULL;
globalArgs.read2=NULL;
globalArgs.ends=0;
globalArgs.outfile="out";
const char *optString = "1:2:o:h?";
if (argc<2) {
display_usage(argv);
exit(1);
}
opt = getopt( argc, argv, optString );
while( opt != -1 ) {
switch( opt ) {
case '1':
globalArgs.read1 = optarg;
globalArgs.outfile = optarg;
globalArgs.ends++;
break;
case '2':
globalArgs.read2 = optarg;
globalArgs.ends++;
break;
case 'o':
globalArgs.outfile = optarg;
break;
case '?': /* fall-through is intentional */
case 'h':
display_usage(argv);
break;
default:
fprintf(stderr,"error parameter!\n");
break;
}
opt = getopt( argc, argv, optString );
}
long long begin;
begin=usec();
unsigned long total_reads_num=0;
fprintf(stderr,"%s",globalArgs.read1);
if (globalArgs.read2){
fprintf(stderr,"\t%s\n",globalArgs.read2);
}else{
fprintf(stderr,"\n");
}
short SeqLen=0;
HASHTBL *hash = load_fastq_file(globalArgs.read1,globalArgs.read2,&total_reads_num,begin,&SeqLen);
fprintf(stderr,"hash size: %ld\ntotal reads = %ld\n",(unsigned long)hash->size,total_reads_num);
fprintf(stderr,"unique reads percentage: %.3f%%\n",(float)hash->count/total_reads_num*100);
// output_hash(hash,globalArgs.outfile,SeqLen);
qsort_output_hash(hash,globalArgs.outfile,SeqLen);
// fprintf(stderr,"Finished qsort_output_hash at %.3f s\n",(double)(usec()-begin)/CLOCKS_PER_SEC);
hashtbl_destroy(hash,hash_data_delete);
fprintf(stderr,"Finished at %.3f s\n",(double)(usec()-begin)/CLOCKS_PER_SEC);
return 0;
}