]> git.donarmstrong.com Git - samtools.git/blobdiff - bam_sort.c
Bug fix - faidx on RAZF compressed files now working.
[samtools.git] / bam_sort.c
index 225988825139d8decc112df2c3aa7063ce053d03..19a93cb348c7123fc3392d348dbf674b36790306 100644 (file)
@@ -33,18 +33,20 @@ static inline int strnum_cmp(const char *a, const char *b)
 
 typedef struct {
        int i;
-       uint64_t pos;
+       uint64_t pos, idx;
        bam1_t *b;
 } heap1_t;
 
+#define __pos_cmp(a, b) ((a).pos > (b).pos || ((a).pos == (b).pos && ((a).i > (b).i || ((a).i == (b).i && (a).idx > (b).idx))))
+
 static inline int heap_lt(const heap1_t a, const heap1_t b)
 {
        if (g_is_by_qname) {
                int t;
                if (a.b == 0 || b.b == 0) return a.b == 0? 1 : 0;
                t = strnum_cmp(bam1_qname(a.b), bam1_qname(b.b));
-               return (t > 0 || (t == 0 && a.pos > b.pos));
-       } else return (a.pos > b.pos);
+               return (t > 0 || (t == 0 && __pos_cmp(a, b)));
+       } else return __pos_cmp(a, b);
 }
 
 KSORT_INIT(heap, heap1_t, heap_lt)
@@ -76,6 +78,7 @@ void bam_merge_core(int by_qname, const char *out, const char *headers, int n, c
        bam_header_t *hout = 0;
        bam_header_t *hheaders = NULL;
        int i, j;
+       uint64_t idx = 0;
 
        if (headers) {
                tamFile fpheaders = sam_open(headers);
@@ -137,8 +140,10 @@ void bam_merge_core(int by_qname, const char *out, const char *headers, int n, c
                h = heap + i;
                h->i = i;
                h->b = (bam1_t*)calloc(1, sizeof(bam1_t));
-               if (bam_read1(fp[i], h->b) >= 0)
+               if (bam_read1(fp[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->idx = idx++;
+               }
                else h->pos = HEAP_EMPTY;
        }
        fpout = strcmp(out, "-")? bam_open(out, "w") : bam_dopen(fileno(stdout), "w");
@@ -152,6 +157,7 @@ void bam_merge_core(int by_qname, const char *out, const char *headers, int n, c
                bam_write1_core(fpout, &b->core, b->data_len, b->data);
                if ((j = bam_read1(fp[heap->i], b)) >= 0) {
                        heap->pos = ((uint64_t)b->core.tid<<32) | (uint32_t)b->core.pos<<1 | bam1_strand(b);
+                       heap->idx = idx++;
                } else if (j == -1) {
                        heap->pos = HEAP_EMPTY;
                        free(heap->b->data); free(heap->b);
@@ -201,7 +207,7 @@ static inline int bam1_lt(const bam1_p a, const bam1_p b)
 }
 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)
+static void sort_blocks(int n, int k, bam1_p *buf, const char *prefix, const bam_header_t *h, int is_stdout)
 {
        char *name;
        int i;
@@ -210,7 +216,7 @@ static void sort_blocks(int n, int k, bam1_p *buf, const char *prefix, const bam
        name = (char*)calloc(strlen(prefix) + 20, 1);
        if (n >= 0) sprintf(name, "%s.%.4d.bam", prefix, n);
        else sprintf(name, "%s.bam", prefix);
-       fp = bam_open(name, "w");
+       fp = is_stdout? bam_dopen(fileno(stdout), "w") : bam_open(name, "w");
        if (fp == 0) {
                fprintf(stderr, "[sort_blocks] fail to create file %s.\n", name);
                free(name);
@@ -238,7 +244,7 @@ static void sort_blocks(int n, int k, bam1_p *buf, const char *prefix, const bam
   and then merge them by calling bam_merge_core(). This function is
   NOT thread safe.
  */
-void bam_sort_core(int is_by_qname, const char *fn, const char *prefix, size_t max_mem)
+void bam_sort_core_ext(int is_by_qname, const char *fn, const char *prefix, size_t max_mem, int is_stdout)
 {
        int n, ret, k, i;
        size_t mem;
@@ -263,19 +269,20 @@ void bam_sort_core(int is_by_qname, const char *fn, const char *prefix, size_t m
                mem += ret;
                ++k;
                if (mem >= max_mem) {
-                       sort_blocks(n++, k, buf, prefix, header);
+                       sort_blocks(n++, k, buf, prefix, header, is_stdout);
                        mem = 0; k = 0;
                }
        }
        if (ret != -1)
                fprintf(stderr, "[bam_sort_core] truncated file. Continue anyway.\n");
-       if (n == 0) sort_blocks(-1, k, buf, prefix, header);
+       if (n == 0) sort_blocks(-1, k, buf, prefix, header, is_stdout);
        else { // then merge
                char **fns, *fnout;
                fprintf(stderr, "[bam_sort_core] merging from %d files...\n", n+1);
-               sort_blocks(n++, k, buf, prefix, header);
+               sort_blocks(n++, k, buf, prefix, header, is_stdout);
                fnout = (char*)calloc(strlen(prefix) + 20, 1);
-               sprintf(fnout, "%s.bam", prefix);
+               if (is_stdout) sprintf(fnout, "-");
+               else sprintf(fnout, "%s.bam", prefix);
                fns = (char**)calloc(n, sizeof(char*));
                for (i = 0; i < n; ++i) {
                        fns[i] = (char*)calloc(strlen(prefix) + 20, 1);
@@ -300,20 +307,26 @@ void bam_sort_core(int is_by_qname, const char *fn, const char *prefix, size_t m
        bam_close(fp);
 }
 
+void bam_sort_core(int is_by_qname, const char *fn, const char *prefix, size_t max_mem)
+{
+       bam_sort_core_ext(is_by_qname, fn, prefix, max_mem, 0);
+}
+
 int bam_sort(int argc, char *argv[])
 {
        size_t max_mem = 500000000;
-       int c, is_by_qname = 0;
-       while ((c = getopt(argc, argv, "nm:")) >= 0) {
+       int c, is_by_qname = 0, is_stdout = 0;
+       while ((c = getopt(argc, argv, "nom:")) >= 0) {
                switch (c) {
+               case 'o': is_stdout = 1; break;
                case 'n': is_by_qname = 1; break;
                case 'm': max_mem = atol(optarg); break;
                }
        }
        if (optind + 2 > argc) {
-               fprintf(stderr, "Usage: samtools sort [-n] [-m <maxMem>] <in.bam> <out.prefix>\n");
+               fprintf(stderr, "Usage: samtools sort [-on] [-m <maxMem>] <in.bam> <out.prefix>\n");
                return 1;
        }
-       bam_sort_core(is_by_qname, argv[optind], argv[optind+1], max_mem);
+       bam_sort_core_ext(is_by_qname, argv[optind], argv[optind+1], max_mem, is_stdout);
        return 0;
 }