static int g_is_by_qname = 0;
-static inline int strnum_cmp(const char *a, const char *b)
+static int strnum_cmp(const char *_a, const char *_b)
{
- char *pa, *pb;
- pa = (char*)a; pb = (char*)b;
+ const unsigned char *a = (const unsigned char*)_a, *b = (const unsigned char*)_b;
+ const unsigned char *pa = a, *pb = b;
while (*pa && *pb) {
if (isdigit(*pa) && isdigit(*pb)) {
- long ai, bi;
- ai = strtol(pa, &pa, 10);
- bi = strtol(pb, &pb, 10);
- if (ai != bi) return ai<bi? -1 : ai>bi? 1 : 0;
+ while (*pa == '0') ++pa;
+ while (*pb == '0') ++pb;
+ while (isdigit(*pa) && isdigit(*pb) && *pa == *pb) ++pa, ++pb;
+ if (isdigit(*pa) && isdigit(*pb)) {
+ int i = 0;
+ while (isdigit(pa[i]) && isdigit(pb[i])) ++i;
+ return isdigit(pa[i])? 1 : isdigit(pb[i])? -1 : (int)*pa - (int)*pb;
+ } else if (isdigit(*pa)) return 1;
+ else if (isdigit(*pb)) return -1;
+ else if (pa - a != pb - b) return pa - a < pb - b? 1 : -1;
} else {
- if (*pa != *pb) break;
+ if (*pa != *pb) return (int)*pa - (int)*pb;
++pa; ++pb;
}
}
- if (*pa == *pb)
- return (pa-a) < (pb-b)? -1 : (pa-a) > (pb-b)? 1 : 0;
- return *pa<*pb? -1 : *pa>*pb? 1 : 0;
+ return *pa? 1 : *pb? -1 : 0;
}
#define HEAP_EMPTY 0xffffffffffffffffull
#define MERGE_RG 1
#define MERGE_UNCOMP 2
+#define MERGE_LEVEL1 4
+#define MERGE_FORCE 8
/*!
@abstract Merge multiple sorted BAM.
@discussion Padding information may NOT correctly maintained. This
function is NOT thread safe.
*/
-int bam_merge_core(int by_qname, const char *out, const char *headers, int n, char * const *fn,
- int flag, const char *reg)
+int bam_merge_core2(int by_qname, const char *out, const char *headers, int n, char * const *fn, int flag, const char *reg, int n_threads)
{
bamFile fpout, *fp;
heap1_t *heap;
h->i = i;
h->b = (bam1_t*)calloc(1, sizeof(bam1_t));
if (bam_iter_read(fp[i], iter[i], h->b) >= 0) {
- h->pos = ((uint64_t)h->b->core.tid<<32) | (uint32_t)h->b->core.pos<<1 | bam1_strand(h->b);
+ h->pos = ((uint64_t)h->b->core.tid<<32) | (uint32_t)((int32_t)h->b->core.pos+1)<<1 | bam1_strand(h->b);
h->idx = idx++;
}
else h->pos = HEAP_EMPTY;
}
- if (flag & MERGE_UNCOMP) {
- fpout = strcmp(out, "-")? bam_open(out, "wu") : bam_dopen(fileno(stdout), "wu");
- } else {
- fpout = strcmp(out, "-")? bam_open(out, "w") : bam_dopen(fileno(stdout), "w");
- }
+ if (flag & MERGE_UNCOMP) fpout = strcmp(out, "-")? bam_open(out, "wu") : bam_dopen(fileno(stdout), "wu");
+ else if (flag & MERGE_LEVEL1) fpout = strcmp(out, "-")? bam_open(out, "w1") : bam_dopen(fileno(stdout), "w1");
+ else fpout = strcmp(out, "-")? bam_open(out, "w") : bam_dopen(fileno(stdout), "w");
if (fpout == 0) {
fprintf(stderr, "[%s] fail to create the output file.\n", __func__);
return -1;
}
bam_header_write(fpout, hout);
bam_header_destroy(hout);
+ if (!(flag & MERGE_UNCOMP)) bgzf_mt(fpout, n_threads, 256);
ks_heapmake(heap, n, heap);
while (heap->pos != HEAP_EMPTY) {
}
bam_write1_core(fpout, &b->core, b->data_len, b->data);
if ((j = bam_iter_read(fp[heap->i], iter[heap->i], b)) >= 0) {
- heap->pos = ((uint64_t)b->core.tid<<32) | (uint32_t)b->core.pos<<1 | bam1_strand(b);
+ heap->pos = ((uint64_t)b->core.tid<<32) | (uint32_t)((int)b->core.pos+1)<<1 | bam1_strand(b);
heap->idx = idx++;
} else if (j == -1) {
heap->pos = HEAP_EMPTY;
return 0;
}
+int bam_merge_core(int by_qname, const char *out, const char *headers, int n, char * const *fn, int flag, const char *reg)
+{
+ return bam_merge_core2(by_qname, out, headers, n, fn, flag, reg, 0);
+}
+
int bam_merge(int argc, char *argv[])
{
- int c, is_by_qname = 0, flag = 0, ret = 0;
+ int c, is_by_qname = 0, flag = 0, ret = 0, n_threads = 0;
char *fn_headers = NULL, *reg = 0;
- while ((c = getopt(argc, argv, "h:nruR:")) >= 0) {
+ while ((c = getopt(argc, argv, "h:nru1R:f@:")) >= 0) {
switch (c) {
case 'r': flag |= MERGE_RG; break;
+ case 'f': flag |= MERGE_FORCE; break;
case 'h': fn_headers = strdup(optarg); break;
case 'n': is_by_qname = 1; break;
+ case '1': flag |= MERGE_LEVEL1; break;
case 'u': flag |= MERGE_UNCOMP; break;
case 'R': reg = strdup(optarg); break;
+ case '@': n_threads = atoi(optarg); break;
}
}
if (optind + 2 >= argc) {
fprintf(stderr, "Options: -n sort by read names\n");
fprintf(stderr, " -r attach RG tag (inferred from file names)\n");
fprintf(stderr, " -u uncompressed BAM output\n");
+ fprintf(stderr, " -f overwrite the output BAM if exist\n");
+ fprintf(stderr, " -1 compress level 1\n");
+ fprintf(stderr, " -@ INT number of BAM compression threads [0]\n");
fprintf(stderr, " -R STR merge file in the specified region STR [all]\n");
fprintf(stderr, " -h FILE copy the header in FILE to <out.bam> [in1.bam]\n\n");
fprintf(stderr, "Note: Samtools' merge does not reconstruct the @RG dictionary in the header. Users\n");
fprintf(stderr, " the header dictionary in merging.\n\n");
return 1;
}
- if (bam_merge_core(is_by_qname, argv[optind], fn_headers, argc - optind - 1, argv + optind + 1, flag, reg) < 0) ret = 1;
+ if (!(flag & MERGE_FORCE) && strcmp(argv[optind], "-")) {
+ FILE *fp = fopen(argv[optind], "rb");
+ if (fp != NULL) {
+ fclose(fp);
+ fprintf(stderr, "[%s] File '%s' exists. Please apply '-f' to overwrite. Abort.\n", __func__, argv[optind]);
+ return 1;
+ }
+ }
+ if (bam_merge_core2(is_by_qname, argv[optind], fn_headers, argc - optind - 1, argv + optind + 1, flag, reg, n_threads) < 0) ret = 1;
free(reg);
free(fn_headers);
return ret;
{
if (g_is_by_qname) {
int t = strnum_cmp(bam1_qname(a), bam1_qname(b));
- return (t < 0 || (t == 0 && (((uint64_t)a->core.tid<<32|a->core.pos) < ((uint64_t)b->core.tid<<32|b->core.pos))));
- } else return (((uint64_t)a->core.tid<<32|a->core.pos) < ((uint64_t)b->core.tid<<32|b->core.pos));
+ return (t < 0 || (t == 0 && (((uint64_t)a->core.tid<<32|(a->core.pos+1)) < ((uint64_t)b->core.tid<<32|(b->core.pos+1)))));
+ } else return (((uint64_t)a->core.tid<<32|(a->core.pos+1)) < ((uint64_t)b->core.tid<<32|(b->core.pos+1)));
}
KSORT_INIT(sort, bam1_p, bam1_lt)
static void sort_blocks(int n, int k, bam1_p *buf, const char *prefix, const bam_header_t *h, int is_stdout)
{
- char *name;
+ char *name, mode[3];
int i;
bamFile fp;
ks_mergesort(sort, k, buf, 0);
name = (char*)calloc(strlen(prefix) + 20, 1);
- if (n >= 0) sprintf(name, "%s.%.4d.bam", prefix, n);
- else sprintf(name, "%s.bam", prefix);
- fp = is_stdout? bam_dopen(fileno(stdout), "w") : bam_open(name, "w");
+ if (n >= 0) {
+ sprintf(name, "%s.%.4d.bam", prefix, n);
+ strcpy(mode, "w1");
+ } else {
+ sprintf(name, "%s.bam", prefix);
+ strcpy(mode, "w");
+ }
+ fp = is_stdout? bam_dopen(fileno(stdout), mode) : bam_open(name, mode);
if (fp == 0) {
fprintf(stderr, "[sort_blocks] fail to create file %s.\n", name);
free(name);
bam_sort_core_ext(is_by_qname, fn, prefix, max_mem, 0);
}
+
+size_t bam_sort_get_max_mem(char *max_mem_string)
+{
+ char c;
+ size_t max_mem;
+ size_t multiplier=1;
+ c=max_mem_string[strlen(max_mem_string)-1];
+ switch(c) {
+ case 'G':
+ multiplier*=1024;
+ case 'M':
+ multiplier*=1024;
+ case 'K':
+ multiplier*=1024;
+ case 'B':
+ max_mem_string[strlen(max_mem_string)-1]='\0';
+ break;
+ default:
+ break;
+ }
+ max_mem = multiplier * atol(max_mem_string);
+ // max_mem should be checked that it was not zero after atol!
+ return max_mem;
+}
+
int bam_sort(int argc, char *argv[])
{
size_t max_mem = 500000000;
switch (c) {
case 'o': is_stdout = 1; break;
case 'n': is_by_qname = 1; break;
- case 'm': max_mem = atol(optarg); break;
+ case 'm': max_mem = bam_sort_get_max_mem(optarg); break;
}
}
if (optind + 2 > argc) {