]> git.donarmstrong.com Git - samtools.git/blobdiff - bam_sort.c
works
[samtools.git] / bam_sort.c
index 0ad9cff80ad2a7d6ae9c5c21397622f124c5ed78..7d00cd1a83321729c1384cf56f066d2c83f65548 100644 (file)
@@ -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) {
@@ -425,6 +459,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 +472,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) {