-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsplit.c
executable file
·328 lines (241 loc) · 9.59 KB
/
split.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
#include "utils.h"
KHASH_MAP_INIT_STR(split, Map)
void kh_split_destroy(khash_t(split) *h);
void split_usage();
typedef struct {
int length;
char *left;
char *right;
char *barcode;
char *directory;
int setting;
} opt_t;
void split_opt_init(opt_t *opt);
void split_opt_inspect(opt_t *opt);
void split_make_fs(opt_t *opt);
static khash_t(split) *h;
static khash_t(kreg) *barcode_h;
void split_load_barcode( opt_t *opt, khash_t(split) *h, khash_t(kreg) *barcode_h );
void split ( opt_t *opt, khash_t(split) *h, khash_t(kreg) *barcode_h );
int split_main (int argc, char *argv[]){
opt_t opt;
split_opt_init(&opt);
int c;
if(argc == 1) split_usage();
while ((c = getopt(argc, argv, "1:2:b:l:d:h")) >= 0) {
if (c == '1') {
opt.left = optarg;
opt.setting++;
}else if (c == '2') {
opt.right = optarg;
opt.setting++;
}else if (c == 'b') {
opt.barcode = optarg;
opt.setting++;
}else if (c == 'd') {
opt.directory = optarg;
opt.setting++;
}else if (c == 'l') {
opt.length = atoi(optarg);
}else if (c == 'h') {
split_usage();
}
}
split_opt_inspect(&opt);
split_make_fs(&opt);
h = kh_init(split);
barcode_h = kh_init(kreg);
split_load_barcode(&opt, h, barcode_h);
split(&opt, h, barcode_h);
kh_split_destroy(h);
kh_kreg_destroy(barcode_h);
return 0;
}
void split_opt_init(opt_t *opt){
memset(opt, 0, sizeof(opt_t));
opt->length = 5;
opt->left = NULL;
opt->right = NULL;
opt->barcode = NULL;
opt->directory = NULL;
opt->setting = 0;
}
void split_opt_inspect(opt_t *opt){
if(opt->left == NULL) fprintf(stderr, "please specify forward reads file, -1 STR!\n\n");
if(opt->right == NULL) fprintf(stderr, "please specify reverse reads file, -2 STR!\n\n");
if(opt->barcode == NULL) fprintf(stderr, "please barcode mapping file, -b STR!\n\n");
if(opt->directory == NULL) fprintf(stderr, "please specify output directory, -d STR!\n\n");
if(opt->setting != 4){
split_usage();
exit(-1);
}
}
void split_usage(){
fprintf(stderr, "\nUsage: atlas-utils split [options]\n\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " -1 STR forward reads file;\n");
fprintf(stderr, " -2 STR reverse reads file;\n");
fprintf(stderr, " -b STR barcode mapping file;\n");
fprintf(stderr, " -d STR output filename directory;\n");
fprintf(stderr, " -l INT minimum barcode length, default: 5;\n");
fprintf(stderr, " -h display usage;\n");
fprintf(stderr, "\nExample:\natlas-utils split -b barcodes.txt -1 I365.R1.fastq.gz -2 I365.R2.fastq.gz -l 5 -d I365\n\n");
exit(-1);
}
void kh_split_destroy(khash_t(split) *h){
khint_t k;
if (h == 0) return;
for (k = 0; k < kh_end(h); ++k){
if (kh_exist(h, k)){
Map map = kh_val(h, k);
free( (char*)kh_key(h, k) );
free( map.fs );
free( map.rs );
fclose( map.fh );
fclose( map.rh );
}
}
kh_destroy(split, h);
}
void split_make_fs(opt_t *opt){
kstring_t kv = {0, 0, 0};
ksprintf(&kv, "mkdir -p %s", opt->directory);
if( system(kv.s) != 0 ) exit (-1);
free(kv.s);
}
void split_load_barcode( opt_t *opt, khash_t(split) *h, khash_t(kreg) *barcode_h) {
gzFile fp;
fp = strcmp(opt->barcode, "-")? gzopen(opt->barcode, "r") : gzdopen(fileno(stdin), "r");
if (fp) {
kstream_t *ks;
ks = ks_init(fp);
kstring_t kt = {0, 0, 0};
kstring_t kk = {0, 0, 0};
kstring_t kv = {0, 0, 0};
kstring_t linker = {0, 0, 0};
int *fields, n, ret;
khint_t k;
while( ks_getuntil( ks, '\n', &kt, 0) >= 0){
if(kt.s[0] == '#') continue;
fields = ksplit(&kt, '\t', &n);
if(n != 3){
fprintf(stderr, "[ERR]: barcode file format error!\n");
exit(1);
}
if(strlen(kt.s + fields[1]) < opt->length || strlen(kt.s + fields[2]) < opt->length ){
fprintf(stderr, "[ERR]: barcode seed length should shorter than barcode sequence!\n");
exit(1);
}
kk.l = kv.l = 0;
ksprintf(&kk, "%s/%s_1.fastq", opt->directory, kt.s);
ksprintf(&kv, "%s/%s_2.fastq", opt->directory, kt.s);
FILE *fv = fopen(kk.s, "w");
FILE *rv = fopen(kv.s, "w");
Map map = {strdup(kt.s + fields[1]),
strdup(kt.s + fields[2]),
strlen(kt.s + fields[1]),
strlen(kt.s + fields[2]),
fv,
rv};
k = kh_put(split, h, kt.s, &ret);
kh_key(h, k) = strdup( kt.s );
kh_val(h, k) = map;
linker.l = 0;
kputsn(kt.s + fields[1], opt->length, &linker);
kputc('+', &linker);
kputsn(kt.s + fields[2], opt->length, &linker);
k = kh_get(kreg, barcode_h, linker.s);
if(k == kh_end(barcode_h)){
k = kh_put(kreg, barcode_h, linker.s , &ret);
kh_key(barcode_h, k) = strdup( linker.s );
kstring_t ktmp = {0, 0, 0};
kputs(kt.s, &ktmp);
kh_val(barcode_h, k) = ktmp;
}else{
kputc('+', &kh_val(barcode_h, k));
kputs(kt.s, &kh_val(barcode_h, k));
}
}
if(kk.s != NULL ) free(kk.s);
if(kt.s != NULL ) free(kt.s);
if(kv.s != NULL ) free(kv.s);
if(linker.s != NULL ) free(linker.s);
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", opt->barcode);
exit(1);
}
}
void split( opt_t *opt, khash_t(split) *h, khash_t(kreg) *barcode_h ){
gzFile fp_fv, fp_rv;
fp_fv = strcmp(opt->left, "-")? gzopen(opt->left, "r") : gzdopen(fileno(stdin), "r");
fp_rv = strcmp(opt->right, "-")? gzopen(opt->right, "r") : gzdopen(fileno(stdin), "r");
if(fp_fv && fp_rv){
kseq_t *kseq[2];
kseq[0] = kseq_init(fp_fv);
kseq[1] = kseq_init(fp_rv);
kstring_t linker = {0, 0, 0};
kstring_t kk = {0, 0, 0};
kstring_t kv = {0, 0, 0};
khint_t k, t;
int *fields, i, n;
while (kseq_read(kseq[0]) >= 0) {
if (kseq_read(kseq[1]) < 0) {
fprintf(stderr, "[W::%s]: the 2nd file has fewer records.\n", __func__);
break;
}
//forwards
linker.l = 0;
kputsn(kseq[0]->seq.s, opt->length, &linker); kputc('+', &linker); kputsn(kseq[1]->seq.s, opt->length, &linker);
k = kh_get(kreg, barcode_h, linker.s);
if( k != kh_end(h) ){
fields = ksplit(&kh_val(barcode_h, k), '+', &n);
for (i = 0; i < n; ++i){
t = kh_get(split, h, kh_val(barcode_h, k).s + fields[i]);
if(t == kh_end(h)) {
fprintf(stderr, "[ERR]: barcode map error!\n");
exit(1);
}else{
kk.l = kv.l = 0;
kputsn(kseq[0]->seq.s, kh_val(h, t).fl , &kk);
kputsn(kseq[1]->seq.s, kh_val(h, t).rl, &kv);
if(strcmp(kk.s, kh_val(h, t).fs) == 0 && strcmp(kv.s, kh_val(h, t).rs) == 0 ){
print_kseq(kseq[0], kh_val(h, t).fh);
print_kseq(kseq[1], kh_val(h, t).rh);
}
}
}
}
//reverse
linker.l = 0;
kputsn(kseq[1]->seq.s, opt->length, &linker); kputc('+', &linker); kputsn(kseq[0]->seq.s, opt->length, &linker);
k = kh_get(kreg, barcode_h, linker.s);
if( k != kh_end(h) ){
fields = ksplit(&kh_val(barcode_h, k), '+', &n);
for (i = 0; i < n; ++i){
t = kh_get(split, h, kh_val(barcode_h, k).s + fields[i]);
if(t == kh_end(h)) {
fprintf(stderr, "[ERR]: barcode map error!\n");
exit(1);
}else{
kk.l = kv.l = 0;
kputsn(kseq[0]->seq.s, kh_val(h, t).rl , &kk);
kputsn(kseq[1]->seq.s, kh_val(h, t).fl, &kv);
if(strcmp(kk.s, kh_val(h, t).rs) == 0 && strcmp(kv.s, kh_val(h, t).fs) == 0 ){
print_kseq(kseq[0], kh_val(h, t).fh);
print_kseq(kseq[1], kh_val(h, t).rh);
}
}
}
}
}
if (kseq_read(kseq[1]) >= 0)
fprintf(stderr, "[W::%s]: the 1st file has fewer records.\n", __func__);
if(kk.s != NULL ) free(kk.s);
if(kv.s != NULL ) free(kv.s);
if(linker.s != NULL ) free(linker.s);
kseq_destroy(kseq[0]); gzclose(fp_fv);
kseq_destroy(kseq[1]); gzclose(fp_rv);
}
}