]> git.donarmstrong.com Git - samtools.git/blob - bam_sort.c
* Merge from branches/dev/
[samtools.git] / bam_sort.c
1 #include <stdlib.h>
2 #include <ctype.h>
3 #include <assert.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include "bam.h"
8 #include "ksort.h"
9
10 static int g_is_by_qname = 0;
11
12 static inline int strnum_cmp(const char *a, const char *b)
13 {
14         char *pa, *pb;
15         pa = (char*)a; pb = (char*)b;
16         while (*pa && *pb) {
17                 if (isdigit(*pa) && isdigit(*pb)) {
18                         long ai, bi;
19                         ai = strtol(pa, &pa, 10);
20                         bi = strtol(pb, &pb, 10);
21                         if (ai != bi) return ai<bi? -1 : ai>bi? 1 : 0;
22                 } else {
23                         if (*pa != *pb) break;
24                         ++pa; ++pb;
25                 }
26         }
27         if (*pa == *pb)
28                 return (pa-a) < (pb-b)? -1 : (pa-a) > (pb-b)? 1 : 0;
29         return *pa<*pb? -1 : *pa>*pb? 1 : 0;
30 }
31
32 #define HEAP_EMPTY 0xffffffffffffffffull
33
34 typedef struct {
35         int i;
36         uint64_t pos;
37         bam1_t *b;
38 } heap1_t;
39
40 static inline int heap_lt(const heap1_t a, const heap1_t b)
41 {
42         if (g_is_by_qname) {
43                 int t = strnum_cmp(bam1_qname(a.b), bam1_qname(b.b));
44                 return (t > 0 || (t == 0 && a.pos > b.pos));
45         } else return (a.pos > b.pos);
46 }
47
48 KSORT_INIT(heap, heap1_t, heap_lt)
49
50 void bam_merge_core(int by_qname, const char *out, int n, char * const *fn)
51 {
52         bamFile fpout, *fp;
53         heap1_t *heap;
54         bam_header_t *hout = 0;
55         int i, j;
56
57         g_is_by_qname = by_qname;
58         fp = (bamFile*)calloc(n, sizeof(bamFile));
59         heap = (heap1_t*)calloc(n, sizeof(heap1_t));
60         for (i = 0; i != n; ++i) {
61                 heap1_t *h;
62                 bam_header_t *hin;
63                 assert(fp[i] = bam_open(fn[i], "r"));
64                 hin = bam_header_read(fp[i]);
65                 if (i == 0) hout = hin;
66                 else { // validate multiple baf
67                         if (hout->n_targets != hin->n_targets) {
68                                 fprintf(stderr, "[bam_merge_core] file '%s' has different number of target sequences. Abort!\n", fn[i]);
69                                 abort();
70                         }
71                         for (j = 0; j < hout->n_targets; ++j) {
72                                 if (strcmp(hout->target_name[j], hin->target_name[j]) || hout->target_len[j] != hin->target_len[j]) {
73                                         fprintf(stderr, "[bam_merge_core] file '%s' has a different target sequence. Abort!\n", fn[i]);
74                                         abort();
75                                 }
76                         }
77                         bam_header_destroy(hin);
78                 }
79                 h = heap + i;
80                 h->i = i;
81                 h->b = (bam1_t*)calloc(1, sizeof(bam1_t));
82                 if (bam_read1(fp[i], h->b) >= 0)
83                         h->pos = ((uint64_t)h->b->core.tid<<32) | (uint32_t)h->b->core.pos<<1 | bam1_strand(h->b);
84                 else h->pos = HEAP_EMPTY;
85         }
86         fpout = strcmp(out, "-")? bam_open(out, "w") : bam_dopen(fileno(stdout), "w");
87         assert(fpout);
88         bam_header_write(fpout, hout);
89         bam_header_destroy(hout);
90
91         ks_heapmake(heap, n, heap);
92         while (heap->pos != HEAP_EMPTY) {
93                 bam1_t *b = heap->b;
94                 bam_write1_core(fpout, &b->core, b->data_len, b->data);
95                 if ((j = bam_read1(fp[heap->i], b)) >= 0)
96                         heap->pos = ((uint64_t)b->core.tid<<32) | (uint32_t)b->core.pos<<1 | bam1_strand(b);
97                 else if (j == -1) heap->pos = HEAP_EMPTY;
98                 else fprintf(stderr, "[bam_merge_core] '%s' is truncated. Continue anyway.\n", fn[heap->i]);
99                 ks_heapadjust(heap, 0, n, heap);
100         }
101
102         for (i = 0; i != n; ++i) {
103                 bam_close(fp[i]);
104                 free(heap[i].b->data);
105                 free(heap[i].b);
106         }
107         bam_close(fpout);
108         free(fp); free(heap);
109 }
110 int bam_merge(int argc, char *argv[])
111 {
112         int c, is_by_qname = 0;
113         while ((c = getopt(argc, argv, "n")) >= 0) {
114                 switch (c) {
115                 case 'n': is_by_qname = 1; break;
116                 }
117         }
118         if (optind + 3 >= argc) {
119                 fprintf(stderr, "Usage: samtools merge [-n] <out.bam> <in1.bam> <in2.bam> [...]\n");
120                 return 1;
121         }
122         bam_merge_core(is_by_qname, argv[optind], argc - optind - 1, argv + optind + 1);
123         return 0;
124 }
125
126 typedef bam1_t *bam1_p;
127
128 static inline int bam1_lt(const bam1_p a, const bam1_p b)
129 {
130         if (g_is_by_qname) {
131                 int t = strnum_cmp(bam1_qname(a), bam1_qname(b));
132                 return (t < 0 || (t == 0 && (((uint64_t)a->core.tid<<32|a->core.pos) < ((uint64_t)b->core.tid<<32|b->core.pos))));
133         } else return (((uint64_t)a->core.tid<<32|a->core.pos) < ((uint64_t)b->core.tid<<32|b->core.pos));
134 }
135 KSORT_INIT(sort, bam1_p, bam1_lt)
136
137 static void sort_blocks(int n, int k, bam1_p *buf, const char *prefix, const bam_header_t *h)
138 {
139         char *name;
140         int i;
141         bamFile fp;
142         ks_mergesort(sort, k, buf, 0);
143         name = (char*)calloc(strlen(prefix) + 20, 1);
144         if (n >= 0) sprintf(name, "%s.%.4d.bam", prefix, n);
145         else sprintf(name, "%s.bam", prefix);
146         assert(fp = bam_open(name, "w"));
147         free(name);
148         bam_header_write(fp, h);
149         for (i = 0; i < k; ++i)
150                 bam_write1_core(fp, &buf[i]->core, buf[i]->data_len, buf[i]->data);
151         bam_close(fp);
152 }
153
154 void bam_sort_core(int is_by_qname, const char *fn, const char *prefix, size_t max_mem)
155 {
156         int n, ret, k, i;
157         size_t mem;
158         bam_header_t *header;
159         bamFile fp;
160         bam1_t *b, **buf;
161
162         g_is_by_qname = is_by_qname;
163         n = k = 0; mem = 0;
164         fp = strcmp(fn, "-")? bam_open(fn, "r") : bam_dopen(fileno(stdin), "r");
165         assert(fp);
166         header = bam_header_read(fp);
167         buf = (bam1_t**)calloc(max_mem / BAM_CORE_SIZE, sizeof(bam1_t*));
168         // write sub files
169         for (;;) {
170                 if (buf[k] == 0) buf[k] = (bam1_t*)calloc(1, sizeof(bam1_t));
171                 b = buf[k];
172                 if ((ret = bam_read1(fp, b)) < 0) break;
173                 mem += ret;
174                 ++k;
175                 if (mem >= max_mem) {
176                         sort_blocks(n++, k, buf, prefix, header);
177                         mem = 0; k = 0;
178                 }
179         }
180         if (ret != -1)
181                 fprintf(stderr, "[bam_sort_core] truncated file. Continue anyway.\n");
182         if (n == 0) sort_blocks(-1, k, buf, prefix, header);
183         else { // then merge
184                 char **fns, *fnout;
185                 fprintf(stderr, "[bam_sort_core] merging from %d files...\n", n+1);
186                 sort_blocks(n++, k, buf, prefix, header);
187                 fnout = (char*)calloc(strlen(prefix) + 20, 1);
188                 sprintf(fnout, "%s.bam", prefix);
189                 fns = (char**)calloc(n, sizeof(char*));
190                 for (i = 0; i < n; ++i) {
191                         fns[i] = (char*)calloc(strlen(prefix) + 20, 1);
192                         sprintf(fns[i], "%s.%.4d.bam", prefix, i);
193                 }
194                 bam_merge_core(is_by_qname, fnout, n, fns);
195                 free(fnout);
196                 for (i = 0; i < n; ++i) {
197                         unlink(fns[i]);
198                         free(fns[i]);
199                 }
200                 free(fns);
201         }
202         for (k = 0; k < max_mem / BAM_CORE_SIZE; ++k) {
203                 if (buf[k]) {
204                         free(buf[k]->data);
205                         free(buf[k]);
206                 }
207         }
208         free(buf);
209         bam_header_destroy(header);
210         bam_close(fp);
211 }
212
213 int bam_sort(int argc, char *argv[])
214 {
215         size_t max_mem = 500000000;
216         int c, is_by_qname = 0;
217         while ((c = getopt(argc, argv, "nm:")) >= 0) {
218                 switch (c) {
219                 case 'n': is_by_qname = 1; break;
220                 case 'm': max_mem = atol(optarg); break;
221                 }
222         }
223         if (optind + 2 > argc) {
224                 fprintf(stderr, "Usage: samtools sort [-n] [-m <maxMem>] <in.baf> <out.prefix>\n");
225                 return 1;
226         }
227         bam_sort_core(is_by_qname, argv[optind], argv[optind+1], max_mem);
228         return 0;
229 }