10 static int g_is_by_qname = 0;
12 static inline int strnum_cmp(const char *a, const char *b)
15 pa = (char*)a; pb = (char*)b;
17 if (isdigit(*pa) && isdigit(*pb)) {
19 ai = strtol(pa, &pa, 10);
20 bi = strtol(pb, &pb, 10);
21 if (ai != bi) return ai<bi? -1 : ai>bi? 1 : 0;
23 if (*pa != *pb) break;
28 return (pa-a) < (pb-b)? -1 : (pa-a) > (pb-b)? 1 : 0;
29 return *pa<*pb? -1 : *pa>*pb? 1 : 0;
32 #define HEAP_EMPTY 0xffffffffffffffffull
40 static inline int heap_lt(const heap1_t a, const heap1_t b)
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);
48 KSORT_INIT(heap, heap1_t, heap_lt)
50 void bam_merge_core(int by_qname, const char *out, int n, char * const *fn)
54 bam_header_t *hout = 0;
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) {
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]);
71 for (j = 0; j < hout->n_targets; ++j) {
72 if (strcmp(hout->target_name[j], hin->target_name[j])) {
73 fprintf(stderr, "[bam_merge_core] different target sequence name: '%s' != '%s' in file '%s'. Abort!\n",
74 hout->target_name[j], hin->target_name[j], fn[i]);
77 if (hout->target_len[j] != hin->target_len[j])
78 fprintf(stderr, "[bam_merge_core] different target sequence length: %d != %d in file '%s'. Continue.\n",
79 hout->target_len[j], hin->target_len[j], fn[i]);
81 bam_header_destroy(hin);
85 h->b = (bam1_t*)calloc(1, sizeof(bam1_t));
86 if (bam_read1(fp[i], h->b) >= 0)
87 h->pos = ((uint64_t)h->b->core.tid<<32) | (uint32_t)h->b->core.pos<<1 | bam1_strand(h->b);
88 else h->pos = HEAP_EMPTY;
90 fpout = strcmp(out, "-")? bam_open(out, "w") : bam_dopen(fileno(stdout), "w");
92 bam_header_write(fpout, hout);
93 bam_header_destroy(hout);
95 ks_heapmake(heap, n, heap);
96 while (heap->pos != HEAP_EMPTY) {
98 bam_write1_core(fpout, &b->core, b->data_len, b->data);
99 if ((j = bam_read1(fp[heap->i], b)) >= 0)
100 heap->pos = ((uint64_t)b->core.tid<<32) | (uint32_t)b->core.pos<<1 | bam1_strand(b);
101 else if (j == -1) heap->pos = HEAP_EMPTY;
102 else fprintf(stderr, "[bam_merge_core] '%s' is truncated. Continue anyway.\n", fn[heap->i]);
103 ks_heapadjust(heap, 0, n, heap);
106 for (i = 0; i != n; ++i) {
108 free(heap[i].b->data);
112 free(fp); free(heap);
114 int bam_merge(int argc, char *argv[])
116 int c, is_by_qname = 0;
117 while ((c = getopt(argc, argv, "n")) >= 0) {
119 case 'n': is_by_qname = 1; break;
122 if (optind + 2 >= argc) {
123 fprintf(stderr, "Usage: samtools merge [-n] <out.bam> <in1.bam> <in2.bam> [...]\n");
126 bam_merge_core(is_by_qname, argv[optind], argc - optind - 1, argv + optind + 1);
130 typedef bam1_t *bam1_p;
132 static inline int bam1_lt(const bam1_p a, const bam1_p b)
135 int t = strnum_cmp(bam1_qname(a), bam1_qname(b));
136 return (t < 0 || (t == 0 && (((uint64_t)a->core.tid<<32|a->core.pos) < ((uint64_t)b->core.tid<<32|b->core.pos))));
137 } else return (((uint64_t)a->core.tid<<32|a->core.pos) < ((uint64_t)b->core.tid<<32|b->core.pos));
139 KSORT_INIT(sort, bam1_p, bam1_lt)
141 static void sort_blocks(int n, int k, bam1_p *buf, const char *prefix, const bam_header_t *h)
146 ks_mergesort(sort, k, buf, 0);
147 name = (char*)calloc(strlen(prefix) + 20, 1);
148 if (n >= 0) sprintf(name, "%s.%.4d.bam", prefix, n);
149 else sprintf(name, "%s.bam", prefix);
150 assert(fp = bam_open(name, "w"));
152 bam_header_write(fp, h);
153 for (i = 0; i < k; ++i)
154 bam_write1_core(fp, &buf[i]->core, buf[i]->data_len, buf[i]->data);
158 void bam_sort_core(int is_by_qname, const char *fn, const char *prefix, size_t max_mem)
162 bam_header_t *header;
166 g_is_by_qname = is_by_qname;
168 fp = strcmp(fn, "-")? bam_open(fn, "r") : bam_dopen(fileno(stdin), "r");
170 header = bam_header_read(fp);
171 buf = (bam1_t**)calloc(max_mem / BAM_CORE_SIZE, sizeof(bam1_t*));
174 if (buf[k] == 0) buf[k] = (bam1_t*)calloc(1, sizeof(bam1_t));
176 if ((ret = bam_read1(fp, b)) < 0) break;
179 if (mem >= max_mem) {
180 sort_blocks(n++, k, buf, prefix, header);
185 fprintf(stderr, "[bam_sort_core] truncated file. Continue anyway.\n");
186 if (n == 0) sort_blocks(-1, k, buf, prefix, header);
189 fprintf(stderr, "[bam_sort_core] merging from %d files...\n", n+1);
190 sort_blocks(n++, k, buf, prefix, header);
191 fnout = (char*)calloc(strlen(prefix) + 20, 1);
192 sprintf(fnout, "%s.bam", prefix);
193 fns = (char**)calloc(n, sizeof(char*));
194 for (i = 0; i < n; ++i) {
195 fns[i] = (char*)calloc(strlen(prefix) + 20, 1);
196 sprintf(fns[i], "%s.%.4d.bam", prefix, i);
198 bam_merge_core(is_by_qname, fnout, n, fns);
200 for (i = 0; i < n; ++i) {
206 for (k = 0; k < max_mem / BAM_CORE_SIZE; ++k) {
213 bam_header_destroy(header);
217 int bam_sort(int argc, char *argv[])
219 size_t max_mem = 500000000;
220 int c, is_by_qname = 0;
221 while ((c = getopt(argc, argv, "nm:")) >= 0) {
223 case 'n': is_by_qname = 1; break;
224 case 'm': max_mem = atol(optarg); break;
227 if (optind + 2 > argc) {
228 fprintf(stderr, "Usage: samtools sort [-n] [-m <maxMem>] <in.bam> <out.prefix>\n");
231 bam_sort_core(is_by_qname, argv[optind], argv[optind+1], max_mem);