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) {
@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;
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;
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) {
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) {
// 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];
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) {
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': {
fprintf(stderr, "\n");
fprintf(stderr, "Usage: samtools sort [options] <in.bam> <out.prefix>\n\n");
fprintf(stderr, "Options: -n sort by read name\n");
+ fprintf(stderr, " -f use <out.prefix> 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");
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;
}