-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbedtk.c
More file actions
708 lines (650 loc) · 17.3 KB
/
bedtk.c
File metadata and controls
708 lines (650 loc) · 17.3 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
#include <zlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <unistd.h>
#include "cgranges.h"
#include "ketopt.h"
#include "kseq.h"
KSTREAM_INIT(gzFile, gzread, 0x10000)
#define BEDTK_VERSION "1.2-r34"
/*****************
* Faster printf *
*****************/
static inline void str_enlarge(kstring_t *s, int l)
{
if (s->l + l + 1 > s->m) {
s->m = s->l + l + 1;
kroundup32(s->m);
s->s = (char*)realloc(s->s, s->m);
}
}
static inline void str_copy(kstring_t *s, const char *st, const char *en)
{
str_enlarge(s, en - st);
memcpy(&s->s[s->l], st, en - st);
s->l += en - st;
}
static void mm_sprintf_lite(kstring_t *s, const char *fmt, ...)
{
char buf[16]; // for integer to string conversion
const char *p, *q;
va_list ap;
va_start(ap, fmt);
for (q = p = fmt; *p; ++p) {
if (*p == '%') {
if (p > q) str_copy(s, q, p);
++p;
if (*p == 'd') {
int c, i, l = 0;
unsigned int x;
c = va_arg(ap, int);
x = c >= 0? c : -c;
do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0);
if (c < 0) buf[l++] = '-';
str_enlarge(s, l);
for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
} else if (*p == 'u') {
int i, l = 0;
uint32_t x;
x = va_arg(ap, uint32_t);
do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0);
str_enlarge(s, l);
for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
} else if (*p == 's') {
char *r = va_arg(ap, char*);
str_copy(s, r, r + strlen(r));
} else if (*p == 'c') {
str_enlarge(s, 1);
s->s[s->l++] = va_arg(ap, int);
} else abort();
q = p + 1;
}
}
if (p > q) str_copy(s, q, p);
va_end(ap);
s->s[s->l] = 0;
}
/***************
* BED3 parser *
***************/
typedef struct {
int32_t l;
char *s;
} bed_rest1_t;
typedef struct {
int64_t n, m;
bed_rest1_t *a;
} bed_rest_t;
static char *parse_vcf(char *s, int32_t *st_, int32_t *en_, char **r) // r points to the end of contig name
{
char *p, *q, *ctg = 0;
int32_t i, st = -1, en = -1;
*r = 0;
if (s[0] == '#') {
*r = s;
return 0;
}
for (i = 0, p = q = s;; ++q) {
if (*q == '\t' || *q == '\0') {
int c = *q;
*q = 0;
if (i == 0) {
ctg = p;
*r = q + 1;
} else if (i == 1) {
st = atol(p) - 1;
} else if (i == 3) {
en = st + (q - p);
} else if (i == 7) {
char *s = 0;
if (strncmp(p, "END=", 4) == 0) {
s = p + 4;
} else {
s = strstr(p, ";END=");
if (s) s += 5;
}
if (s) en = strtol(s, &s, 10);
}
if (i != 0) *q = c;
++i, p = q + 1;
if (i == 8 || c == '\0') break;
}
}
*st_ = st, *en_ = en;
return i >= 7? ctg : 0;
}
static char *parse_paf(char *s, int32_t *st_, int32_t *en_, char **r)
{
char *p, *q, *ctg = 0;
int32_t i, st = -1, en = -1;
*r = 0;
for (i = 0, p = q = s;; ++q) {
if (*q == '\t' || *q == '\0') {
int c = *q;
*q = 0;
if (i == 5) {
ctg = p;
*r = q + 1;
} else if (i == 7) {
st = atol(p);
} else if (i == 8) {
en = atol(p);
}
if (i != 5) *q = c;
++i, p = q + 1;
if (i == 9 || c == '\0') break;
}
}
*st_ = st, *en_ = en;
return i >= 9? ctg : 0;
}
static char *parse_bed3b(char *s, int32_t *st_, int32_t *en_, char **r)
{
char *p, *q, *ctg = 0;
int32_t i, st = -1, en = -1;
if (r) *r = 0;
for (i = 0, p = q = s;; ++q) {
if (*q == '\t' || *q == '\0') {
int c = *q;
*q = 0;
if (i == 0) ctg = p;
else if (i == 1) st = atol(p);
else if (i == 2) {
en = atol(p);
if (r && c != 0) *r = q, *q = c;
}
++i, p = q + 1;
if (i == 3 || c == '\0') break;
}
}
*st_ = st, *en_ = en;
return i >= 3? ctg : 0;
}
static char *parse_bed3(char *s, int32_t *st_, int32_t *en_)
{
return parse_bed3b(s, st_, en_, 0);
}
static cgranges_t *read_bed3b(const char *fn, bed_rest_t *r, const char *fn_order)
{
gzFile fp;
cgranges_t *cr;
kstream_t *ks;
kstring_t str = {0,0,0};
int64_t k = 0;
fp = fn && strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(0, "r");
if (fp == 0) {
fprintf(stderr, "ERROR: failed to open the input file\n");
return 0;
}
cr = cr_init();
if (fn_order) {
gzFile fp;
if ((fp = gzopen(fn_order, "r")) == 0) {
fprintf(stderr, "ERROR: failed to open the list file\n");
return 0;
}
ks = ks_init(fp);
while (ks_getuntil(ks, KS_SEP_LINE, &str, 0) >= 0) {
char *p;
for (p = str.s; *p && !isspace(*p); ++p);
*p = 0;
cr_add_ctg(cr, str.s, 0);
}
ks_destroy(ks);
gzclose(fp);
}
ks = ks_init(fp);
if (r) r->m = r->n = 0, r->a = 0;
while (ks_getuntil(ks, KS_SEP_LINE, &str, 0) >= 0) {
char *ctg, *rest;
int32_t st, en;
ctg = parse_bed3b(str.s, &st, &en, &rest);
if (ctg) {
cr_add(cr, ctg, st, en, k);
if (r) {
bed_rest1_t *p;
if (r->n == r->m) {
int64_t old_m = r->m;
r->m = r->m? r->m<<1 : 16;
r->a = (bed_rest1_t*)realloc(r->a, r->m * sizeof(bed_rest1_t));
memset(&r->a[old_m], 0, (r->m - old_m) * sizeof(bed_rest1_t));
}
p = &r->a[r->n++];
p->l = rest? str.l - (rest - str.s) : 0;
if (rest) {
p->s = (char*)malloc(p->l + 1);
memcpy(p->s, rest, p->l);
p->s[p->l] = 0;
}
}
++k;
}
}
if (k > INT32_MAX)
fprintf(stderr, "WARNING: more than %d records; some functionality may not work!\n", INT32_MAX);
free(str.s);
ks_destroy(ks);
gzclose(fp);
return cr;
}
static cgranges_t *read_bed3(const char *fn)
{
return read_bed3b(fn, 0, 0);
}
/*********
* Tools *
*********/
int main_isec(int argc, char *argv[])
{
cgranges_t *cr, *qr;
ketopt_t o = KETOPT_INIT;
int64_t i, m_b = 0, *b = 0, n_b;
int c;
char *fn_order = 0;
while ((c = ketopt(&o, argc, argv, 1, "s:", 0)) >= 0) {
if (c == 's') fn_order = o.arg;
}
if (argc - o.ind < 1 || (argc - o.ind < 2 && isatty(0))) {
printf("Usage: bedtk isec [options] <A.bed> <B.bed>\n");
printf("Options:\n");
printf(" -s FILE list of contig IDs to specify the output order []\n");
return 1;
}
cr = read_bed3b(argv[o.ind], 0, fn_order);
assert(cr);
cr_index2(cr, 1);
qr = read_bed3b(o.ind + 1 < argc? argv[o.ind + 1] : 0, 0, fn_order);
assert(qr);
if (!cr_is_sorted(qr)) cr_sort(qr);
cr_merge_pre_index(qr);
for (i = 0; i < qr->n_r; ++i) {
cr_intv_t *q = &qr->r[i];
int32_t st1 = (int32_t)q->x, en1 = q->y, cov_st = 0, cov_en = 0;
int64_t j;
char *ctg = qr->ctg[q->x>>32].name;
n_b = cr_overlap(cr, ctg, st1, en1, &b, &m_b);
for (j = 0; j < n_b; ++j) {
cr_intv_t *r = &cr->r[b[j]];
int32_t st0 = cr_st(r), en0 = cr_en(r);
if (st0 < st1) st0 = st1;
if (en0 > en1) en0 = en1;
if (st0 > cov_en) {
if (cov_en > cov_st) printf("%s\t%d\t%d\n", ctg, cov_st, cov_en);
cov_st = st0, cov_en = en0;
} else cov_en = cov_en > en0? cov_en : en0;
}
if (cov_en > cov_st) printf("%s\t%d\t%d\n", ctg, cov_st, cov_en);
}
free(b);
cr_destroy(qr);
cr_destroy(cr);
return 0;
}
static int64_t cal_cov(const cgranges_t *cr, int64_t n_b, const int64_t *b, int64_t st1, int64_t en1, int64_t *cnt_, int64_t *depth_)
{
int64_t j, cnt = 0, cov = 0, cov_st = 0, cov_en = 0, depth = 0;
for (j = 0; j < n_b; ++j) {
cr_intv_t *r = &cr->r[b[j]];
int32_t st0 = cr_st(r), en0 = cr_en(r);
if (st0 < st1) st0 = st1;
if (en0 > en1) en0 = en1;
if (st0 > cov_en) {
cov += cov_en - cov_st;
cov_st = st0, cov_en = en0;
} else cov_en = cov_en > en0? cov_en : en0;
++cnt, depth += en0 - st0;
}
cov += cov_en - cov_st;
*cnt_ = cnt, *depth_ = depth;
return cov;
}
int main_flt(int argc, char *argv[])
{
cgranges_t *cr;
ketopt_t o = KETOPT_INIT;
int64_t m_b = 0, *b = 0, n_b;
int c, win = 0, vcf_in = 0, paf_in = 0, test_con = 0, non_sat = 0;
double min_frac = 0.0;
gzFile fp;
kstream_t *ks;
kstring_t str = {0,0,0}, out = {0,0,0};
while ((c = ketopt(&o, argc, argv, 1, "cw:Cvpf:", 0)) >= 0) {
if (c == 'c') vcf_in = 1;
else if (c == 'p') paf_in = 1;
else if (c == 'C') test_con = 1;
else if (c == 'v') non_sat = 1;
else if (c == 'w') win = atol(o.arg);
else if (c == 'f') min_frac = atof(o.arg);
}
if (argc - o.ind < 1 || (argc - o.ind < 2 && isatty(0))) {
printf("Usage: bedtk flt [options] <loaded.bed> <streamed.bed>\n");
printf("Options:\n");
printf(" -c the second input is VCF\n");
printf(" -p the second input is PAF\n");
printf(" -C print records contained in the union of <loaded.bed>\n");
printf(" -v print non-satisfying records\n");
printf(" -w INT window size [0]\n");
printf(" -f FLOAT min overlap fraction [%g]\n", min_frac);
return 1;
}
if (vcf_in && paf_in) {
fprintf(stderr, "ERROR: -c and -p can't be applied at the same time\n");
return 1;
}
cr = read_bed3(argv[o.ind]);
assert(cr);
cr_index2(cr, 1);
fp = o.ind + 1 < argc && strcmp(argv[o.ind + 1], "-")? gzopen(argv[o.ind + 1], "r") : gzdopen(0, "r");
assert(fp);
ks = ks_init(fp);
while (ks_getuntil(ks, KS_SEP_LINE, &str, 0) >= 0) {
int32_t st1, en1, st2, en2, sat = 0;
int64_t i;
char *ctg, *rest;
if (paf_in) {
ctg = parse_paf(str.s, &st1, &en1, &rest);
} else if (vcf_in) {
ctg = parse_vcf(str.s, &st1, &en1, &rest);
if (ctg == 0 && rest && rest[0] == '#')
puts(rest);
} else ctg = parse_bed3b(str.s, &st1, &en1, &rest);
if (ctg == 0) continue;
st2 = st1 - win, en2 = en1 + win;
if (st2 < 0) st2 = 0;
n_b = cr_overlap(cr, ctg, st2, en2, &b, &m_b);
if (test_con) {
for (i = 0, sat = 0; i < n_b; ++i) {
cr_intv_t *r = &cr->r[b[i]];
if (cr_st(r) <= st2 && en2 <= cr_en(r)) {
sat = 1;
break;
}
}
} else if (min_frac > 0.0) {
int64_t cov, cnt, depth;
cov = cal_cov(cr, n_b, b, st1, en1, &cnt, &depth);
sat = (cov >= (en1 - st1) * min_frac);
} else sat = (n_b > 0);
out.l = 0;
if ((sat && !non_sat) || (!sat && non_sat)) {
if (paf_in) {
*(rest - 1) = '\t';
fwrite(str.s, 1, str.l, stdout);
putchar('\n');
} else if (vcf_in) {
mm_sprintf_lite(&out, "%s\t", ctg);
fwrite(out.s, 1, out.l, stdout);
puts(rest);
} else {
mm_sprintf_lite(&out, "%s\t%d\t%d", ctg, st1, en1);
fwrite(out.s, 1, out.l, stdout);
if (rest) puts(rest);
else putchar('\n');
}
}
}
free(str.s);
ks_destroy(ks);
gzclose(fp);
free(b);
cr_destroy(cr);
return 0;
}
int main_cov(int argc, char *argv[])
{
cgranges_t *cr;
gzFile fp;
ketopt_t o = KETOPT_INIT;
kstream_t *ks;
kstring_t str = {0,0,0}, out = {0,0,0};
int64_t m_b = 0, *b = 0, n_b;
int c, cnt_only = 0, contained = 0, print_depth = 0;
while ((c = ketopt(&o, argc, argv, 1, "cCd", 0)) >= 0) {
if (c == 'c') cnt_only = 1;
else if (c == 'C') contained = 1;
else if (c == 'd') print_depth = 1;
}
if (argc - o.ind < 1 || (argc - o.ind < 2 && isatty(0))) {
printf("Usage: bedtk cov [options] <loaded.bed> <streamed.bed>\n");
printf("Options:\n");
printf(" -c only count; no breadth of depth\n");
printf(" -C containment only\n");
return 1;
}
cr = read_bed3(argv[o.ind]);
assert(cr);
cr_index(cr);
fp = o.ind+1 < argc && strcmp(argv[o.ind + 1], "-")? gzopen(argv[o.ind + 1], "r") : gzdopen(0, "r");
assert(fp);
ks = ks_init(fp);
while (ks_getuntil(ks, KS_SEP_LINE, &str, 0) >= 0) {
int32_t st1, en1;
char *ctg;
int64_t cnt = 0, cov = 0, depth = 0;
ctg = parse_bed3(str.s, &st1, &en1);
if (ctg == 0) continue;
if (contained)
n_b = cr_contain(cr, ctg, st1, en1, &b, &m_b);
else
n_b = cr_overlap(cr, ctg, st1, en1, &b, &m_b);
out.l = 0;
if (!cnt_only) {
cov = cal_cov(cr, n_b, b, st1, en1, &cnt, &depth);
if (print_depth)
mm_sprintf_lite(&out, "%s\t%d\t%d\t%d\t%d\t%d\n", ctg, st1, en1, (int)cnt, (int)cov, (int)depth);
else
mm_sprintf_lite(&out, "%s\t%d\t%d\t%d\t%d\n", ctg, st1, en1, (int)cnt, (int)cov);
} else {
mm_sprintf_lite(&out, "%s\t%d\t%d\t%d\n", ctg, st1, en1, (int)n_b);
}
fputs(out.s, stdout);
}
free(b);
free(str.s);
ks_destroy(ks);
gzclose(fp);
cr_destroy(cr);
return 0;
}
int main_sub(int argc, char *argv[])
{
cgranges_t *cr;
gzFile fp;
ketopt_t o = KETOPT_INIT;
kstream_t *ks;
kstring_t str = {0,0,0}, out = {0,0,0};
int64_t m_b = 0, *b = 0, n_b;
int32_t c, min_len = 0;
while ((c = ketopt(&o, argc, argv, 1, "l:", 0)) >= 0) {
if (c == 'l') min_len = atoi(o.arg);
}
if (argc - o.ind < 2) {
printf("Usage: bedtk sub <minuend.bed> <subtrahend.bed>\n");
printf("Note: <subtrahend.bed> is loaded into memory.\n");
return 1;
}
cr = read_bed3(argv[o.ind + 1]);
assert(cr);
cr_index2(cr, 1);
fp = strcmp(argv[o.ind], "-")? gzopen(argv[o.ind], "r") : gzdopen(0, "r");
assert(fp);
ks = ks_init(fp);
while (ks_getuntil(ks, KS_SEP_LINE, &str, 0) >= 0) {
int32_t st1, en1, x;
char *ctg, *rest;
int64_t j;
ctg = parse_bed3b(str.s, &st1, &en1, &rest);
if (ctg == 0) continue;
n_b = cr_overlap(cr, ctg, st1, en1, &b, &m_b);
for (j = 0, x = st1; j < n_b; ++j) {
cr_intv_t *r = &cr->r[b[j]];
int32_t st0 = cr_st(r), en0 = cr_en(r);
if (st0 < st1) st0 = st1;
if (en0 > en1) en0 = en1;
if (st0 > x && st0 - x >= min_len) {
out.l = 0;
mm_sprintf_lite(&out, "%s\t%d\t%d", ctg, x, st0);
if (rest) mm_sprintf_lite(&out, "%s", rest);
puts(out.s);
}
x = en0;
}
if (x < en1 && en1 - x >= min_len) {
out.l = 0;
mm_sprintf_lite(&out, "%s\t%d\t%d", ctg, x, en1);
if (rest) mm_sprintf_lite(&out, "%s", rest);
puts(out.s);
}
}
free(b);
free(out.s);
free(str.s);
ks_destroy(ks);
gzclose(fp);
cr_destroy(cr);
return 0;
}
int main_merge(int argc, char *argv[])
{
cgranges_t *cr;
ketopt_t o = KETOPT_INIT;
int c, assume_srt = 0;
while ((c = ketopt(&o, argc, argv, 1, "s", 0)) >= 0) {
if (c == 's') assume_srt = 1;
}
if (argc - o.ind < 1 && isatty(0)) {
printf("Usage: bedtk merge [options] <in.bed>\n");
printf("Options:\n");
printf(" -s assume the input is sorted (NOT implemented yet)\n");
return 1;
}
if (assume_srt) {
fprintf(stderr, "ERROR: NOT implemented yet\n");
} else {
int64_t i;
cr = read_bed3(o.ind < argc? argv[o.ind] : 0);
assert(cr);
if (!cr_is_sorted(cr)) cr_sort(cr);
cr_merge_pre_index(cr);
for (i = 0; i < cr->n_r; ++i) {
const cr_intv_t *r = &cr->r[i];
printf("%s\t%d\t%d\n", cr->ctg[r->x>>32].name, (int32_t)r->x, r->y);
}
cr_destroy(cr);
}
return 0;
}
int main_sum(int argc, char *argv[])
{
cgranges_t *cr;
ketopt_t o = KETOPT_INIT;
int c, merge = 0;
int64_t sum = 0;
while ((c = ketopt(&o, argc, argv, 1, "m", 0)) >= 0) {
if (c == 'm') merge = 1;
}
if (argc - o.ind < 1 && isatty(0)) {
printf("Usage: bedtk sum [options] <in.bed>\n");
printf("Options:\n");
printf(" -m merge overlapping regions\n");
return 1;
}
if (!merge) {
gzFile fp;
kstream_t *ks;
kstring_t str = {0,0,0};
fp = o.ind < argc && strcmp(argv[o.ind], "-")? gzopen(argv[o.ind], "r") : gzdopen(0, "r");
assert(fp);
ks = ks_init(fp);
while (ks_getuntil(ks, KS_SEP_LINE, &str, 0) >= 0) {
int32_t st1, en1;
char *ctg;
ctg = parse_bed3(str.s, &st1, &en1);
if (ctg == 0) continue;
sum += en1 - st1;
}
free(str.s);
ks_destroy(ks);
gzclose(fp);
} else {
int64_t i;
cr = read_bed3(o.ind < argc? argv[o.ind] : 0);
assert(cr);
if (!cr_is_sorted(cr)) cr_sort(cr);
cr_merge_pre_index(cr);
for (i = 0; i < cr->n_r; ++i)
sum += cr->r[i].y - (int32_t)cr->r[i].x;
cr_destroy(cr);
}
printf("%lld\n", (long long)sum);
return 0;
}
int main_sort(int argc, char *argv[])
{
cgranges_t *cr;
ketopt_t o = KETOPT_INIT;
int c;
int64_t i;
bed_rest_t rest;
char *fn_order = 0;
kstring_t str = {0,0,0};
while ((c = ketopt(&o, argc, argv, 1, "s:", 0)) >= 0)
if (c == 's') fn_order = o.arg;
if (argc - o.ind < 1 && isatty(0)) {
printf("Usage: bedtk sort [options] <in.bed>\n");
printf("Options:\n");
printf(" -s FILE list of contig IDs to specify the order []\n");
return 1;
}
cr = read_bed3b(o.ind < argc? argv[o.ind] : 0, &rest, fn_order);
assert(cr);
if (!cr_is_sorted(cr)) cr_sort(cr);
for (i = 0; i < cr->n_r; ++i) {
const cr_intv_t *r = &cr->r[i];
str.l = 0;
mm_sprintf_lite(&str, "%s\t%d\t%d", cr->ctg[r->x>>32].name, (int32_t)r->x, r->y);
if (rest.a[r->label].l) mm_sprintf_lite(&str, "%s", rest.a[r->label].s);
puts(str.s);
}
free(str.s);
cr_destroy(cr);
for (i = 0; i < rest.n; ++i) free(rest.a[i].s);
free(rest.a);
return 0;
}
/*****************
* Main function *
*****************/
static int usage(FILE *fp)
{
fprintf(fp, "Usage: bedtk <command> <arguments>\n");
fprintf(fp, "Command:\n");
fprintf(fp, " isec intersection (bedtools intersect)\n");
fprintf(fp, " flt filter BED/VCF file (bedtools intersect/window)\n");
fprintf(fp, " cov breadth of coverage (bedtools coverage)\n");
fprintf(fp, " sub subtraction (bedtools subtract)\n");
fprintf(fp, " merge merge overlapping regions (bedtools merge)\n");
fprintf(fp, " sort sort regions (bedtools sort)\n");
fprintf(fp, " sum total region length\n");
fprintf(fp, " version version number\n");
return fp == stdout? 0 : 1;
}
int main(int argc, char *argv[])
{
if (argc == 1) return usage(stdout);
if (strcmp(argv[1], "isec") == 0) return main_isec(argc-1, argv+1);
else if (strcmp(argv[1], "flt") == 0) return main_flt(argc-1, argv+1);
else if (strcmp(argv[1], "cov") == 0) return main_cov(argc-1, argv+1);
else if (strcmp(argv[1], "sub") == 0) return main_sub(argc-1, argv+1);
else if (strcmp(argv[1], "merge") == 0) return main_merge(argc-1, argv+1);
else if (strcmp(argv[1], "sum") == 0) return main_sum(argc-1, argv+1);
else if (strcmp(argv[1], "sort") == 0) return main_sort(argc-1, argv+1);
else if (strcmp(argv[1], "version") == 0) {
puts(BEDTK_VERSION);
return 0;
} else {
fprintf(stderr, "ERROR: unrecognized command '%s'. Abort!\n", argv[1]);
return 1;
}
}