Mercurial > repos > lsong10 > psiclass
comparison PsiCLASS-1.0.2/samtools-0.1.19/bam_sort.c @ 0:903fc43d6227 draft default tip
Uploaded
author | lsong10 |
---|---|
date | Fri, 26 Mar 2021 16:52:45 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:903fc43d6227 |
---|---|
1 #include <stdlib.h> | |
2 #include <ctype.h> | |
3 #include <assert.h> | |
4 #include <errno.h> | |
5 #include <stdio.h> | |
6 #include <string.h> | |
7 #include <unistd.h> | |
8 #include "bam.h" | |
9 #include "ksort.h" | |
10 | |
11 static int g_is_by_qname = 0; | |
12 | |
13 static int strnum_cmp(const char *_a, const char *_b) | |
14 { | |
15 const unsigned char *a = (const unsigned char*)_a, *b = (const unsigned char*)_b; | |
16 const unsigned char *pa = a, *pb = b; | |
17 while (*pa && *pb) { | |
18 if (isdigit(*pa) && isdigit(*pb)) { | |
19 while (*pa == '0') ++pa; | |
20 while (*pb == '0') ++pb; | |
21 while (isdigit(*pa) && isdigit(*pb) && *pa == *pb) ++pa, ++pb; | |
22 if (isdigit(*pa) && isdigit(*pb)) { | |
23 int i = 0; | |
24 while (isdigit(pa[i]) && isdigit(pb[i])) ++i; | |
25 return isdigit(pa[i])? 1 : isdigit(pb[i])? -1 : (int)*pa - (int)*pb; | |
26 } else if (isdigit(*pa)) return 1; | |
27 else if (isdigit(*pb)) return -1; | |
28 else if (pa - a != pb - b) return pa - a < pb - b? 1 : -1; | |
29 } else { | |
30 if (*pa != *pb) return (int)*pa - (int)*pb; | |
31 ++pa; ++pb; | |
32 } | |
33 } | |
34 return *pa? 1 : *pb? -1 : 0; | |
35 } | |
36 | |
37 #define HEAP_EMPTY 0xffffffffffffffffull | |
38 | |
39 typedef struct { | |
40 int i; | |
41 uint64_t pos, idx; | |
42 bam1_t *b; | |
43 } heap1_t; | |
44 | |
45 #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)))) | |
46 | |
47 static inline int heap_lt(const heap1_t a, const heap1_t b) | |
48 { | |
49 if (g_is_by_qname) { | |
50 int t; | |
51 if (a.b == 0 || b.b == 0) return a.b == 0? 1 : 0; | |
52 t = strnum_cmp(bam1_qname(a.b), bam1_qname(b.b)); | |
53 return (t > 0 || (t == 0 && (a.b->core.flag&0xc0) > (b.b->core.flag&0xc0))); | |
54 } else return __pos_cmp(a, b); | |
55 } | |
56 | |
57 KSORT_INIT(heap, heap1_t, heap_lt) | |
58 | |
59 static void swap_header_targets(bam_header_t *h1, bam_header_t *h2) | |
60 { | |
61 bam_header_t t; | |
62 t.n_targets = h1->n_targets, h1->n_targets = h2->n_targets, h2->n_targets = t.n_targets; | |
63 t.target_name = h1->target_name, h1->target_name = h2->target_name, h2->target_name = t.target_name; | |
64 t.target_len = h1->target_len, h1->target_len = h2->target_len, h2->target_len = t.target_len; | |
65 } | |
66 | |
67 static void swap_header_text(bam_header_t *h1, bam_header_t *h2) | |
68 { | |
69 int tempi; | |
70 char *temps; | |
71 tempi = h1->l_text, h1->l_text = h2->l_text, h2->l_text = tempi; | |
72 temps = h1->text, h1->text = h2->text, h2->text = temps; | |
73 } | |
74 | |
75 #define MERGE_RG 1 | |
76 #define MERGE_UNCOMP 2 | |
77 #define MERGE_LEVEL1 4 | |
78 #define MERGE_FORCE 8 | |
79 | |
80 /*! | |
81 @abstract Merge multiple sorted BAM. | |
82 @param is_by_qname whether to sort by query name | |
83 @param out output BAM file name | |
84 @param headers name of SAM file from which to copy '@' header lines, | |
85 or NULL to copy them from the first file to be merged | |
86 @param n number of files to be merged | |
87 @param fn names of files to be merged | |
88 | |
89 @discussion Padding information may NOT correctly maintained. This | |
90 function is NOT thread safe. | |
91 */ | |
92 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, int level) | |
93 { | |
94 bamFile fpout, *fp; | |
95 heap1_t *heap; | |
96 bam_header_t *hout = 0; | |
97 bam_header_t *hheaders = NULL; | |
98 int i, j, *RG_len = 0; | |
99 uint64_t idx = 0; | |
100 char **RG = 0, mode[8]; | |
101 bam_iter_t *iter = 0; | |
102 | |
103 if (headers) { | |
104 tamFile fpheaders = sam_open(headers); | |
105 if (fpheaders == 0) { | |
106 const char *message = strerror(errno); | |
107 fprintf(stderr, "[bam_merge_core] cannot open '%s': %s\n", headers, message); | |
108 return -1; | |
109 } | |
110 hheaders = sam_header_read(fpheaders); | |
111 sam_close(fpheaders); | |
112 } | |
113 | |
114 g_is_by_qname = by_qname; | |
115 fp = (bamFile*)calloc(n, sizeof(bamFile)); | |
116 heap = (heap1_t*)calloc(n, sizeof(heap1_t)); | |
117 iter = (bam_iter_t*)calloc(n, sizeof(bam_iter_t)); | |
118 // prepare RG tag | |
119 if (flag & MERGE_RG) { | |
120 RG = (char**)calloc(n, sizeof(void*)); | |
121 RG_len = (int*)calloc(n, sizeof(int)); | |
122 for (i = 0; i != n; ++i) { | |
123 int l = strlen(fn[i]); | |
124 const char *s = fn[i]; | |
125 if (l > 4 && strcmp(s + l - 4, ".bam") == 0) l -= 4; | |
126 for (j = l - 1; j >= 0; --j) if (s[j] == '/') break; | |
127 ++j; l -= j; | |
128 RG[i] = calloc(l + 1, 1); | |
129 RG_len[i] = l; | |
130 strncpy(RG[i], s + j, l); | |
131 } | |
132 } | |
133 // read the first | |
134 for (i = 0; i != n; ++i) { | |
135 bam_header_t *hin; | |
136 fp[i] = bam_open(fn[i], "r"); | |
137 if (fp[i] == 0) { | |
138 int j; | |
139 fprintf(stderr, "[bam_merge_core] fail to open file %s\n", fn[i]); | |
140 for (j = 0; j < i; ++j) bam_close(fp[j]); | |
141 free(fp); free(heap); | |
142 // FIXME: possible memory leak | |
143 return -1; | |
144 } | |
145 hin = bam_header_read(fp[i]); | |
146 if (i == 0) { // the first BAM | |
147 hout = hin; | |
148 } else { // validate multiple baf | |
149 int min_n_targets = hout->n_targets; | |
150 if (hin->n_targets < min_n_targets) min_n_targets = hin->n_targets; | |
151 | |
152 for (j = 0; j < min_n_targets; ++j) | |
153 if (strcmp(hout->target_name[j], hin->target_name[j]) != 0) { | |
154 fprintf(stderr, "[bam_merge_core] different target sequence name: '%s' != '%s' in file '%s'\n", | |
155 hout->target_name[j], hin->target_name[j], fn[i]); | |
156 return -1; | |
157 } | |
158 | |
159 // If this input file has additional target reference sequences, | |
160 // add them to the headers to be output | |
161 if (hin->n_targets > hout->n_targets) { | |
162 swap_header_targets(hout, hin); | |
163 // FIXME Possibly we should also create @SQ text headers | |
164 // for the newly added reference sequences | |
165 } | |
166 | |
167 bam_header_destroy(hin); | |
168 } | |
169 } | |
170 | |
171 if (hheaders) { | |
172 // If the text headers to be swapped in include any @SQ headers, | |
173 // check that they are consistent with the existing binary list | |
174 // of reference information. | |
175 if (hheaders->n_targets > 0) { | |
176 if (hout->n_targets != hheaders->n_targets) { | |
177 fprintf(stderr, "[bam_merge_core] number of @SQ headers in '%s' differs from number of target sequences\n", headers); | |
178 if (!reg) return -1; | |
179 } | |
180 for (j = 0; j < hout->n_targets; ++j) | |
181 if (strcmp(hout->target_name[j], hheaders->target_name[j]) != 0) { | |
182 fprintf(stderr, "[bam_merge_core] @SQ header '%s' in '%s' differs from target sequence\n", hheaders->target_name[j], headers); | |
183 if (!reg) return -1; | |
184 } | |
185 } | |
186 | |
187 swap_header_text(hout, hheaders); | |
188 bam_header_destroy(hheaders); | |
189 } | |
190 | |
191 if (reg) { | |
192 int tid, beg, end; | |
193 if (bam_parse_region(hout, reg, &tid, &beg, &end) < 0) { | |
194 fprintf(stderr, "[%s] Malformated region string or undefined reference name\n", __func__); | |
195 return -1; | |
196 } | |
197 for (i = 0; i < n; ++i) { | |
198 bam_index_t *idx; | |
199 idx = bam_index_load(fn[i]); | |
200 iter[i] = bam_iter_query(idx, tid, beg, end); | |
201 bam_index_destroy(idx); | |
202 } | |
203 } | |
204 | |
205 for (i = 0; i < n; ++i) { | |
206 heap1_t *h = heap + i; | |
207 h->i = i; | |
208 h->b = (bam1_t*)calloc(1, sizeof(bam1_t)); | |
209 if (bam_iter_read(fp[i], iter[i], h->b) >= 0) { | |
210 h->pos = ((uint64_t)h->b->core.tid<<32) | (uint32_t)((int32_t)h->b->core.pos+1)<<1 | bam1_strand(h->b); | |
211 h->idx = idx++; | |
212 } | |
213 else h->pos = HEAP_EMPTY; | |
214 } | |
215 if (flag & MERGE_UNCOMP) level = 0; | |
216 else if (flag & MERGE_LEVEL1) level = 1; | |
217 strcpy(mode, "w"); | |
218 if (level >= 0) sprintf(mode + 1, "%d", level < 9? level : 9); | |
219 if ((fpout = strcmp(out, "-")? bam_open(out, "w") : bam_dopen(fileno(stdout), "w")) == 0) { | |
220 fprintf(stderr, "[%s] fail to create the output file.\n", __func__); | |
221 return -1; | |
222 } | |
223 bam_header_write(fpout, hout); | |
224 bam_header_destroy(hout); | |
225 if (!(flag & MERGE_UNCOMP)) bgzf_mt(fpout, n_threads, 256); | |
226 | |
227 ks_heapmake(heap, n, heap); | |
228 while (heap->pos != HEAP_EMPTY) { | |
229 bam1_t *b = heap->b; | |
230 if (flag & MERGE_RG) { | |
231 uint8_t *rg = bam_aux_get(b, "RG"); | |
232 if (rg) bam_aux_del(b, rg); | |
233 bam_aux_append(b, "RG", 'Z', RG_len[heap->i] + 1, (uint8_t*)RG[heap->i]); | |
234 } | |
235 bam_write1_core(fpout, &b->core, b->data_len, b->data); | |
236 if ((j = bam_iter_read(fp[heap->i], iter[heap->i], b)) >= 0) { | |
237 heap->pos = ((uint64_t)b->core.tid<<32) | (uint32_t)((int)b->core.pos+1)<<1 | bam1_strand(b); | |
238 heap->idx = idx++; | |
239 } else if (j == -1) { | |
240 heap->pos = HEAP_EMPTY; | |
241 free(heap->b->data); free(heap->b); | |
242 heap->b = 0; | |
243 } else fprintf(stderr, "[bam_merge_core] '%s' is truncated. Continue anyway.\n", fn[heap->i]); | |
244 ks_heapadjust(heap, 0, n, heap); | |
245 } | |
246 | |
247 if (flag & MERGE_RG) { | |
248 for (i = 0; i != n; ++i) free(RG[i]); | |
249 free(RG); free(RG_len); | |
250 } | |
251 for (i = 0; i != n; ++i) { | |
252 bam_iter_destroy(iter[i]); | |
253 bam_close(fp[i]); | |
254 } | |
255 bam_close(fpout); | |
256 free(fp); free(heap); free(iter); | |
257 return 0; | |
258 } | |
259 | |
260 int bam_merge_core(int by_qname, const char *out, const char *headers, int n, char * const *fn, int flag, const char *reg) | |
261 { | |
262 return bam_merge_core2(by_qname, out, headers, n, fn, flag, reg, 0, -1); | |
263 } | |
264 | |
265 int bam_merge(int argc, char *argv[]) | |
266 { | |
267 int c, is_by_qname = 0, flag = 0, ret = 0, n_threads = 0, level = -1; | |
268 char *fn_headers = NULL, *reg = 0; | |
269 | |
270 while ((c = getopt(argc, argv, "h:nru1R:f@:l:")) >= 0) { | |
271 switch (c) { | |
272 case 'r': flag |= MERGE_RG; break; | |
273 case 'f': flag |= MERGE_FORCE; break; | |
274 case 'h': fn_headers = strdup(optarg); break; | |
275 case 'n': is_by_qname = 1; break; | |
276 case '1': flag |= MERGE_LEVEL1; break; | |
277 case 'u': flag |= MERGE_UNCOMP; break; | |
278 case 'R': reg = strdup(optarg); break; | |
279 case 'l': level = atoi(optarg); break; | |
280 case '@': n_threads = atoi(optarg); break; | |
281 } | |
282 } | |
283 if (optind + 2 >= argc) { | |
284 fprintf(stderr, "\n"); | |
285 fprintf(stderr, "Usage: samtools merge [-nr] [-h inh.sam] <out.bam> <in1.bam> <in2.bam> [...]\n\n"); | |
286 fprintf(stderr, "Options: -n sort by read names\n"); | |
287 fprintf(stderr, " -r attach RG tag (inferred from file names)\n"); | |
288 fprintf(stderr, " -u uncompressed BAM output\n"); | |
289 fprintf(stderr, " -f overwrite the output BAM if exist\n"); | |
290 fprintf(stderr, " -1 compress level 1\n"); | |
291 fprintf(stderr, " -l INT compression level, from 0 to 9 [-1]\n"); | |
292 fprintf(stderr, " -@ INT number of BAM compression threads [0]\n"); | |
293 fprintf(stderr, " -R STR merge file in the specified region STR [all]\n"); | |
294 fprintf(stderr, " -h FILE copy the header in FILE to <out.bam> [in1.bam]\n\n"); | |
295 fprintf(stderr, "Note: Samtools' merge does not reconstruct the @RG dictionary in the header. Users\n"); | |
296 fprintf(stderr, " must provide the correct header with -h, or uses Picard which properly maintains\n"); | |
297 fprintf(stderr, " the header dictionary in merging.\n\n"); | |
298 return 1; | |
299 } | |
300 if (!(flag & MERGE_FORCE) && strcmp(argv[optind], "-")) { | |
301 FILE *fp = fopen(argv[optind], "rb"); | |
302 if (fp != NULL) { | |
303 fclose(fp); | |
304 fprintf(stderr, "[%s] File '%s' exists. Please apply '-f' to overwrite. Abort.\n", __func__, argv[optind]); | |
305 return 1; | |
306 } | |
307 } | |
308 if (bam_merge_core2(is_by_qname, argv[optind], fn_headers, argc - optind - 1, argv + optind + 1, flag, reg, n_threads, level) < 0) ret = 1; | |
309 free(reg); | |
310 free(fn_headers); | |
311 return ret; | |
312 } | |
313 | |
314 /*************** | |
315 * BAM sorting * | |
316 ***************/ | |
317 | |
318 #include <pthread.h> | |
319 | |
320 typedef bam1_t *bam1_p; | |
321 | |
322 static int change_SO(bam_header_t *h, const char *so) | |
323 { | |
324 char *p, *q, *beg = 0, *end = 0, *newtext; | |
325 if (h->l_text > 3) { | |
326 if (strncmp(h->text, "@HD", 3) == 0) { | |
327 if ((p = strchr(h->text, '\n')) == 0) return -1; | |
328 *p = '\0'; | |
329 if ((q = strstr(h->text, "\tSO:")) != 0) { | |
330 *p = '\n'; // change back | |
331 if (strncmp(q + 4, so, p - q - 4) != 0) { | |
332 beg = q; | |
333 for (q += 4; *q != '\n' && *q != '\t'; ++q); | |
334 end = q; | |
335 } else return 0; // no need to change | |
336 } else beg = end = p, *p = '\n'; | |
337 } | |
338 } | |
339 if (beg == 0) { // no @HD | |
340 h->l_text += strlen(so) + 15; | |
341 newtext = malloc(h->l_text + 1); | |
342 sprintf(newtext, "@HD\tVN:1.3\tSO:%s\n", so); | |
343 strcat(newtext, h->text); | |
344 } else { // has @HD but different or no SO | |
345 h->l_text = (beg - h->text) + (4 + strlen(so)) + (h->text + h->l_text - end); | |
346 newtext = malloc(h->l_text + 1); | |
347 strncpy(newtext, h->text, beg - h->text); | |
348 sprintf(newtext + (beg - h->text), "\tSO:%s", so); | |
349 strcat(newtext, end); | |
350 } | |
351 free(h->text); | |
352 h->text = newtext; | |
353 return 0; | |
354 } | |
355 | |
356 static inline int bam1_lt(const bam1_p a, const bam1_p b) | |
357 { | |
358 if (g_is_by_qname) { | |
359 int t = strnum_cmp(bam1_qname(a), bam1_qname(b)); | |
360 return (t < 0 || (t == 0 && (a->core.flag&0xc0) < (b->core.flag&0xc0))); | |
361 } else return (((uint64_t)a->core.tid<<32|(a->core.pos+1)<<1|bam1_strand(a)) < ((uint64_t)b->core.tid<<32|(b->core.pos+1)<<1|bam1_strand(b))); | |
362 } | |
363 KSORT_INIT(sort, bam1_p, bam1_lt) | |
364 | |
365 typedef struct { | |
366 size_t buf_len; | |
367 const char *prefix; | |
368 bam1_p *buf; | |
369 const bam_header_t *h; | |
370 int index; | |
371 } worker_t; | |
372 | |
373 static void write_buffer(const char *fn, const char *mode, size_t l, bam1_p *buf, const bam_header_t *h, int n_threads) | |
374 { | |
375 size_t i; | |
376 bamFile fp; | |
377 fp = strcmp(fn, "-")? bam_open(fn, mode) : bam_dopen(fileno(stdout), mode); | |
378 if (fp == 0) return; | |
379 bam_header_write(fp, h); | |
380 if (n_threads > 1) bgzf_mt(fp, n_threads, 256); | |
381 for (i = 0; i < l; ++i) | |
382 bam_write1_core(fp, &buf[i]->core, buf[i]->data_len, buf[i]->data); | |
383 bam_close(fp); | |
384 } | |
385 | |
386 static void *worker(void *data) | |
387 { | |
388 worker_t *w = (worker_t*)data; | |
389 char *name; | |
390 ks_mergesort(sort, w->buf_len, w->buf, 0); | |
391 name = (char*)calloc(strlen(w->prefix) + 20, 1); | |
392 sprintf(name, "%s.%.4d.bam", w->prefix, w->index); | |
393 write_buffer(name, "w1", w->buf_len, w->buf, w->h, 0); | |
394 free(name); | |
395 return 0; | |
396 } | |
397 | |
398 static int sort_blocks(int n_files, size_t k, bam1_p *buf, const char *prefix, const bam_header_t *h, int n_threads) | |
399 { | |
400 int i; | |
401 size_t rest; | |
402 bam1_p *b; | |
403 pthread_t *tid; | |
404 pthread_attr_t attr; | |
405 worker_t *w; | |
406 | |
407 if (n_threads < 1) n_threads = 1; | |
408 if (k < n_threads * 64) n_threads = 1; // use a single thread if we only sort a small batch of records | |
409 pthread_attr_init(&attr); | |
410 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); | |
411 w = calloc(n_threads, sizeof(worker_t)); | |
412 tid = calloc(n_threads, sizeof(pthread_t)); | |
413 b = buf; rest = k; | |
414 for (i = 0; i < n_threads; ++i) { | |
415 w[i].buf_len = rest / (n_threads - i); | |
416 w[i].buf = b; | |
417 w[i].prefix = prefix; | |
418 w[i].h = h; | |
419 w[i].index = n_files + i; | |
420 b += w[i].buf_len; rest -= w[i].buf_len; | |
421 pthread_create(&tid[i], &attr, worker, &w[i]); | |
422 } | |
423 for (i = 0; i < n_threads; ++i) pthread_join(tid[i], 0); | |
424 free(tid); free(w); | |
425 return n_files + n_threads; | |
426 } | |
427 | |
428 /*! | |
429 @abstract Sort an unsorted BAM file based on the chromosome order | |
430 and the leftmost position of an alignment | |
431 | |
432 @param is_by_qname whether to sort by query name | |
433 @param fn name of the file to be sorted | |
434 @param prefix prefix of the output and the temporary files; upon | |
435 sucessess, prefix.bam will be written. | |
436 @param max_mem approxiate maximum memory (very inaccurate) | |
437 @param full_path the given output path is the full path and not just the prefix | |
438 | |
439 @discussion It may create multiple temporary subalignment files | |
440 and then merge them by calling bam_merge_core(). This function is | |
441 NOT thread safe. | |
442 */ | |
443 void bam_sort_core_ext(int is_by_qname, const char *fn, const char *prefix, size_t _max_mem, int is_stdout, int n_threads, int level, int full_path) | |
444 { | |
445 int ret, i, n_files = 0; | |
446 size_t mem, max_k, k, max_mem; | |
447 bam_header_t *header; | |
448 bamFile fp; | |
449 bam1_t *b, **buf; | |
450 char *fnout = 0; | |
451 char const *suffix = ".bam"; | |
452 if (full_path) suffix += 4; | |
453 | |
454 if (n_threads < 2) n_threads = 1; | |
455 g_is_by_qname = is_by_qname; | |
456 max_k = k = 0; mem = 0; | |
457 max_mem = _max_mem * n_threads; | |
458 buf = 0; | |
459 fp = strcmp(fn, "-")? bam_open(fn, "r") : bam_dopen(fileno(stdin), "r"); | |
460 if (fp == 0) { | |
461 fprintf(stderr, "[bam_sort_core] fail to open file %s\n", fn); | |
462 return; | |
463 } | |
464 header = bam_header_read(fp); | |
465 if (is_by_qname) change_SO(header, "queryname"); | |
466 else change_SO(header, "coordinate"); | |
467 // write sub files | |
468 for (;;) { | |
469 if (k == max_k) { | |
470 size_t old_max = max_k; | |
471 max_k = max_k? max_k<<1 : 0x10000; | |
472 buf = realloc(buf, max_k * sizeof(void*)); | |
473 memset(buf + old_max, 0, sizeof(void*) * (max_k - old_max)); | |
474 } | |
475 if (buf[k] == 0) buf[k] = (bam1_t*)calloc(1, sizeof(bam1_t)); | |
476 b = buf[k]; | |
477 if ((ret = bam_read1(fp, b)) < 0) break; | |
478 if (b->data_len < b->m_data>>2) { // shrink | |
479 b->m_data = b->data_len; | |
480 kroundup32(b->m_data); | |
481 b->data = realloc(b->data, b->m_data); | |
482 } | |
483 mem += sizeof(bam1_t) + b->m_data + sizeof(void*) + sizeof(void*); // two sizeof(void*) for the data allocated to pointer arrays | |
484 ++k; | |
485 if (mem >= max_mem) { | |
486 n_files = sort_blocks(n_files, k, buf, prefix, header, n_threads); | |
487 mem = k = 0; | |
488 } | |
489 } | |
490 if (ret != -1) | |
491 fprintf(stderr, "[bam_sort_core] truncated file. Continue anyway.\n"); | |
492 // output file name | |
493 fnout = calloc(strlen(prefix) + 20, 1); | |
494 if (is_stdout) sprintf(fnout, "-"); | |
495 else sprintf(fnout, "%s%s", prefix, suffix); | |
496 // write the final output | |
497 if (n_files == 0) { // a single block | |
498 char mode[8]; | |
499 strcpy(mode, "w"); | |
500 if (level >= 0) sprintf(mode + 1, "%d", level < 9? level : 9); | |
501 ks_mergesort(sort, k, buf, 0); | |
502 write_buffer(fnout, mode, k, buf, header, n_threads); | |
503 } else { // then merge | |
504 char **fns; | |
505 n_files = sort_blocks(n_files, k, buf, prefix, header, n_threads); | |
506 fprintf(stderr, "[bam_sort_core] merging from %d files...\n", n_files); | |
507 fns = (char**)calloc(n_files, sizeof(char*)); | |
508 for (i = 0; i < n_files; ++i) { | |
509 fns[i] = (char*)calloc(strlen(prefix) + 20, 1); | |
510 sprintf(fns[i], "%s.%.4d%s", prefix, i, suffix); | |
511 } | |
512 bam_merge_core2(is_by_qname, fnout, 0, n_files, fns, 0, 0, n_threads, level); | |
513 for (i = 0; i < n_files; ++i) { | |
514 unlink(fns[i]); | |
515 free(fns[i]); | |
516 } | |
517 free(fns); | |
518 } | |
519 free(fnout); | |
520 // free | |
521 for (k = 0; k < max_k; ++k) { | |
522 if (!buf[k]) continue; | |
523 free(buf[k]->data); | |
524 free(buf[k]); | |
525 } | |
526 free(buf); | |
527 bam_header_destroy(header); | |
528 bam_close(fp); | |
529 } | |
530 | |
531 void bam_sort_core(int is_by_qname, const char *fn, const char *prefix, size_t max_mem) | |
532 { | |
533 bam_sort_core_ext(is_by_qname, fn, prefix, max_mem, 0, 0, -1, 0); | |
534 } | |
535 | |
536 int bam_sort(int argc, char *argv[]) | |
537 { | |
538 size_t max_mem = 768<<20; // 512MB | |
539 int c, is_by_qname = 0, is_stdout = 0, n_threads = 0, level = -1, full_path = 0; | |
540 while ((c = getopt(argc, argv, "fnom:@:l:")) >= 0) { | |
541 switch (c) { | |
542 case 'f': full_path = 1; break; | |
543 case 'o': is_stdout = 1; break; | |
544 case 'n': is_by_qname = 1; break; | |
545 case 'm': { | |
546 char *q; | |
547 max_mem = strtol(optarg, &q, 0); | |
548 if (*q == 'k' || *q == 'K') max_mem <<= 10; | |
549 else if (*q == 'm' || *q == 'M') max_mem <<= 20; | |
550 else if (*q == 'g' || *q == 'G') max_mem <<= 30; | |
551 break; | |
552 } | |
553 case '@': n_threads = atoi(optarg); break; | |
554 case 'l': level = atoi(optarg); break; | |
555 } | |
556 } | |
557 if (optind + 2 > argc) { | |
558 fprintf(stderr, "\n"); | |
559 fprintf(stderr, "Usage: samtools sort [options] <in.bam> <out.prefix>\n\n"); | |
560 fprintf(stderr, "Options: -n sort by read name\n"); | |
561 fprintf(stderr, " -f use <out.prefix> as full file name instead of prefix\n"); | |
562 fprintf(stderr, " -o final output to stdout\n"); | |
563 fprintf(stderr, " -l INT compression level, from 0 to 9 [-1]\n"); | |
564 fprintf(stderr, " -@ INT number of sorting and compression threads [1]\n"); | |
565 fprintf(stderr, " -m INT max memory per thread; suffix K/M/G recognized [768M]\n"); | |
566 fprintf(stderr, "\n"); | |
567 return 1; | |
568 } | |
569 bam_sort_core_ext(is_by_qname, argv[optind], argv[optind+1], max_mem, is_stdout, n_threads, level, full_path); | |
570 return 0; | |
571 } |