X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bam_sort.c;h=c46bce3b3f4ab2fbc5e5166b4bfaf2ff12b013a9;hb=efc19a36de7236c21b3ae1fe7601e0503bc9c4b7;hp=0ad9cff80ad2a7d6ae9c5c21397622f124c5ed78;hpb=81b4ff832414380351f72ca733ca6a4dfdaed9ff;p=samtools.git diff --git a/bam_sort.c b/bam_sort.c index 0ad9cff..c46bce3 100644 --- a/bam_sort.c +++ b/bam_sort.c @@ -319,6 +319,40 @@ int bam_merge(int argc, char *argv[]) typedef bam1_t *bam1_p; +static int change_SO(bam_header_t *h, const char *so) +{ + char *p, *q, *beg = 0, *end = 0, *newtext; + if (h->l_text > 3) { + if (strncmp(h->text, "@HD", 3) == 0) { + if ((p = strchr(h->text, '\n')) == 0) return -1; + *p = '\0'; + if ((q = strstr(h->text, "\tSO:")) != 0) { + *p = '\n'; // change back + if (strncmp(q + 4, so, p - q - 4) != 0) { + beg = q; + for (q += 4; *q != '\n' && *q != '\t'; ++q); + end = q; + } else return 0; // no need to change + } else beg = end = p, *p = '\n'; + } + } + if (beg == 0) { // no @HD + h->l_text += strlen(so) + 15; + newtext = malloc(h->l_text + 1); + sprintf(newtext, "@HD\tVN:1.3\tSO:%s\n", so); + strcat(newtext, h->text); + } else { // has @HD but different or no SO + h->l_text = (beg - h->text) + (4 + strlen(so)) + (h->text + h->l_text - end); + newtext = malloc(h->l_text + 1); + strncpy(newtext, h->text, beg - h->text); + sprintf(newtext + (beg - h->text), "\tSO:%s", so); + strcat(newtext, end); + } + free(h->text); + h->text = newtext; + return 0; +} + static inline int bam1_lt(const bam1_p a, const bam1_p b) { if (g_is_by_qname) { @@ -400,12 +434,13 @@ static int sort_blocks(int n_files, size_t k, bam1_p *buf, const char *prefix, c @param prefix prefix of the output and the temporary files; upon sucessess, prefix.bam will be written. @param max_mem approxiate maximum memory (very inaccurate) + @param full_path the given output path is the full path and not just the prefix @discussion It may create multiple temporary subalignment files and then merge them by calling bam_merge_core(). This function is NOT thread safe. */ -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) +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) { int ret, i, n_files = 0; size_t mem, max_k, k, max_mem; @@ -413,6 +448,8 @@ void bam_sort_core_ext(int is_by_qname, const char *fn, const char *prefix, size bamFile fp; bam1_t *b, **buf; char *fnout = 0; + char const *suffix = ".bam"; + if (full_path) suffix += 4; if (n_threads < 2) n_threads = 1; g_is_by_qname = is_by_qname; @@ -425,6 +462,8 @@ void bam_sort_core_ext(int is_by_qname, const char *fn, const char *prefix, size return; } header = bam_header_read(fp); + if (is_by_qname) change_SO(header, "queryname"); + else change_SO(header, "coordinate"); // write sub files for (;;) { if (k == max_k) { @@ -436,6 +475,11 @@ void bam_sort_core_ext(int is_by_qname, const char *fn, const char *prefix, size if (buf[k] == 0) buf[k] = (bam1_t*)calloc(1, sizeof(bam1_t)); b = buf[k]; if ((ret = bam_read1(fp, b)) < 0) break; + if (b->data_len < b->m_data>>2) { // shrink + b->m_data = b->data_len; + kroundup32(b->m_data); + b->data = realloc(b->data, b->m_data); + } mem += sizeof(bam1_t) + b->m_data + sizeof(void*) + sizeof(void*); // two sizeof(void*) for the data allocated to pointer arrays ++k; if (mem >= max_mem) { @@ -448,7 +492,7 @@ void bam_sort_core_ext(int is_by_qname, const char *fn, const char *prefix, size // output file name fnout = calloc(strlen(prefix) + 20, 1); if (is_stdout) sprintf(fnout, "-"); - else sprintf(fnout, "%s.bam", prefix); + else sprintf(fnout, "%s%s", prefix, suffix); // write the final output if (n_files == 0) { // a single block char mode[8]; @@ -463,7 +507,7 @@ void bam_sort_core_ext(int is_by_qname, const char *fn, const char *prefix, size fns = (char**)calloc(n_files, sizeof(char*)); for (i = 0; i < n_files; ++i) { fns[i] = (char*)calloc(strlen(prefix) + 20, 1); - sprintf(fns[i], "%s.%.4d.bam", prefix, i); + sprintf(fns[i], "%s.%.4d%s", prefix, i, suffix); } bam_merge_core2(is_by_qname, fnout, 0, n_files, fns, 0, 0, n_threads, level); for (i = 0; i < n_files; ++i) { @@ -486,15 +530,16 @@ void bam_sort_core_ext(int is_by_qname, const char *fn, const char *prefix, size 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, 0, -1); + bam_sort_core_ext(is_by_qname, fn, prefix, max_mem, 0, 0, -1, 0); } int bam_sort(int argc, char *argv[]) { size_t max_mem = 768<<20; // 512MB - int c, is_by_qname = 0, is_stdout = 0, n_threads = 0, level = -1; - while ((c = getopt(argc, argv, "nom:@:l:")) >= 0) { + int c, is_by_qname = 0, is_stdout = 0, n_threads = 0, level = -1, full_path = 0; + while ((c = getopt(argc, argv, "fnom:@:l:")) >= 0) { switch (c) { + case 'f': full_path = 1; break; case 'o': is_stdout = 1; break; case 'n': is_by_qname = 1; break; case 'm': { @@ -513,6 +558,7 @@ int bam_sort(int argc, char *argv[]) fprintf(stderr, "\n"); fprintf(stderr, "Usage: samtools sort [options] \n\n"); fprintf(stderr, "Options: -n sort by read name\n"); + fprintf(stderr, " -f use as full file name instead of prefix\n"); fprintf(stderr, " -o final output to stdout\n"); fprintf(stderr, " -l INT compression level, from 0 to 9 [-1]\n"); fprintf(stderr, " -@ INT number of sorting and compression threads [1]\n"); @@ -520,6 +566,6 @@ int bam_sort(int argc, char *argv[]) fprintf(stderr, "\n"); return 1; } - bam_sort_core_ext(is_by_qname, argv[optind], argv[optind+1], max_mem, is_stdout, n_threads, level); + bam_sort_core_ext(is_by_qname, argv[optind], argv[optind+1], max_mem, is_stdout, n_threads, level, full_path); return 0; }