]> git.donarmstrong.com Git - samtools.git/commitdiff
* samtools-0.1.7-8 (r585)
authorHeng Li <lh3@live.co.uk>
Fri, 11 Jun 2010 16:22:06 +0000 (16:22 +0000)
committerHeng Li <lh3@live.co.uk>
Fri, 11 Jun 2010 16:22:06 +0000 (16:22 +0000)
 * speed up "view"

bam.c
bamtk.c
kstring.h

diff --git a/bam.c b/bam.c
index 86cf3f3b71f4ecedbb8e80a2033c664cd7ae3b27..aa96bb58a1c53973031c311c2a8cf9459c0372d4 100644 (file)
--- a/bam.c
+++ b/bam.c
@@ -235,8 +235,8 @@ char *bam_format1_core(const bam_header_t *header, const bam1_t *b, int of)
        kstring_t str;
        str.l = str.m = 0; str.s = 0;
 
-       ksprintf(&str, "%s\t", bam1_qname(b));
-       if (of == BAM_OFDEC) ksprintf(&str, "%d\t", c->flag);
+       kputsn(bam1_qname(b), c->l_qname-1, &str); kputc('\t', &str);
+       if (of == BAM_OFDEC) { kputw(c->flag, &str); kputc('\t', &str); }
        else if (of == BAM_OFHEX) ksprintf(&str, "0x%x\t", c->flag);
        else { // BAM_OFSTR
                for (i = 0; i < 16; ++i)
@@ -244,41 +244,43 @@ char *bam_format1_core(const bam_header_t *header, const bam1_t *b, int of)
                                kputc(bam_flag2char_table[i], &str);
                kputc('\t', &str);
        }
-       if (c->tid < 0) kputs("*\t", &str);
-       else ksprintf(&str, "%s\t", header->target_name[c->tid]);
-       ksprintf(&str, "%d\t%d\t", c->pos + 1, c->qual);
+       if (c->tid < 0) kputsn("*\t", 2, &str);
+       else { kputs(header->target_name[c->tid], &str); kputc('\t', &str); }
+       kputw(c->pos + 1, &str); kputc('\t', &str); kputw(c->qual, &str); kputc('\t', &str);
        if (c->n_cigar == 0) kputc('*', &str);
        else {
-               for (i = 0; i < c->n_cigar; ++i)
-                       ksprintf(&str, "%d%c", bam1_cigar(b)[i]>>BAM_CIGAR_SHIFT, "MIDNSHP"[bam1_cigar(b)[i]&BAM_CIGAR_MASK]);
+               for (i = 0; i < c->n_cigar; ++i) {
+                       kputw(bam1_cigar(b)[i]>>BAM_CIGAR_SHIFT, &str);
+                       kputc("MIDNSHP"[bam1_cigar(b)[i]&BAM_CIGAR_MASK], &str);
+               }
        }
        kputc('\t', &str);
-       if (c->mtid < 0) kputs("*\t", &str);
-       else if (c->mtid == c->tid) kputs("=\t", &str);
-       else ksprintf(&str, "%s\t", header->target_name[c->mtid]);
-       ksprintf(&str, "%d\t%d\t", c->mpos + 1, c->isize);
+       if (c->mtid < 0) kputsn("*\t", 2, &str);
+       else if (c->mtid == c->tid) kputsn("=\t", 2, &str);
+       else { kputs(header->target_name[c->mtid], &str); kputc('\t', &str); }
+       kputw(c->mpos + 1, &str); kputc('\t', &str); kputw(c->isize, &str); kputc('\t', &str);
        if (c->l_qseq) {
                for (i = 0; i < c->l_qseq; ++i) kputc(bam_nt16_rev_table[bam1_seqi(s, i)], &str);
                kputc('\t', &str);
                if (t[0] == 0xff) kputc('*', &str);
                else for (i = 0; i < c->l_qseq; ++i) kputc(t[i] + 33, &str);
-       } else ksprintf(&str, "*\t*");
+       } else kputsn("*\t*", 3, &str);
        s = bam1_aux(b);
        while (s < b->data + b->data_len) {
                uint8_t type, key[2];
                key[0] = s[0]; key[1] = s[1];
                s += 2; type = *s; ++s;
-               ksprintf(&str, "\t%c%c:", key[0], key[1]);
-               if (type == 'A') { ksprintf(&str, "A:%c", *s); ++s; }
-               else if (type == 'C') { ksprintf(&str, "i:%u", *s); ++s; }
-               else if (type == 'c') { ksprintf(&str, "i:%d", *(int8_t*)s); ++s; }
-               else if (type == 'S') { ksprintf(&str, "i:%u", *(uint16_t*)s); s += 2; }
-               else if (type == 's') { ksprintf(&str, "i:%d", *(int16_t*)s); s += 2; }
-               else if (type == 'I') { ksprintf(&str, "i:%u", *(uint32_t*)s); s += 4; }
-               else if (type == 'i') { ksprintf(&str, "i:%d", *(int32_t*)s); s += 4; }
+               kputc('\t', &str); kputsn((char*)key, 2, &str); kputc(':', &str);
+               if (type == 'A') { kputsn("A:", 2, &str); kputc(*s, &str); ++s; }
+               else if (type == 'C') { kputsn("i:", 2, &str); kputw(*s, &str); ++s; }
+               else if (type == 'c') { kputsn("i:", 2, &str); kputw(*(int8_t*)s, &str); ++s; }
+               else if (type == 'S') { kputsn("i:", 2, &str); kputw(*(uint16_t*)s, &str); s += 2; }
+               else if (type == 's') { kputsn("i:", 2, &str); kputw(*(int16_t*)s, &str); s += 2; }
+               else if (type == 'I') { kputsn("i:", 2, &str); kputuw(*(uint32_t*)s, &str); s += 4; }
+               else if (type == 'i') { kputsn("i:", 2, &str); kputw(*(int32_t*)s, &str); s += 4; }
                else if (type == 'f') { ksprintf(&str, "f:%g", *(float*)s); s += 4; }
                else if (type == 'd') { ksprintf(&str, "d:%lg", *(double*)s); s += 8; }
-               else if (type == 'Z' || type == 'H') { ksprintf(&str, "%c:", type); while (*s) kputc(*s++, &str); ++s; }
+               else if (type == 'Z' || type == 'H') { kputc(type, &str); kputc(':', &str); while (*s) kputc(*s++, &str); ++s; }
        }
        return str.s;
 }
diff --git a/bamtk.c b/bamtk.c
index 22c56483d1852e7baacc4d63925724d1ba311ed0..ec91abc76c37e227da1d39064bdc163ed3bf59dd 100644 (file)
--- a/bamtk.c
+++ b/bamtk.c
@@ -9,7 +9,7 @@
 #endif
 
 #ifndef PACKAGE_VERSION
-#define PACKAGE_VERSION "0.1.7-7 (r584)"
+#define PACKAGE_VERSION "0.1.7-8 (r585)"
 #endif
 
 int bam_taf2baf(int argc, char *argv[]);
index 918b020fb7f238f41ebd4c78027bb9711ed79f20..925117a38646ab4c1b82db01e46091f9e073eefd 100644 (file)
--- a/kstring.h
+++ b/kstring.h
@@ -75,6 +75,23 @@ static inline int kputw(int c, kstring_t *s)
        return 0;
 }
 
+static inline int kputuw(unsigned c, kstring_t *s)
+{
+       char buf[16];
+       int l, i;
+       unsigned x;
+       if (c == 0) return kputc('0', s);
+       for (l = 0, x = c; x > 0; x /= 10) buf[l++] = x%10 + '0';
+       if (s->l + l + 1 >= s->m) {
+               s->m = s->l + l + 2;
+               kroundup32(s->m);
+               s->s = (char*)realloc(s->s, s->m);
+       }
+       for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
+       s->s[s->l] = 0;
+       return 0;
+}
+
 static inline int *ksplit(kstring_t *s, int delimiter, int *n)
 {
        int max = 0, *offsets = 0;