]> git.donarmstrong.com Git - samtools.git/blobdiff - bam_import.c
* samtools-0.1.2-16
[samtools.git] / bam_import.c
index 6b3b4bc69352a92d0c9e74379fe8ddd2e4564c43..6697a9ffb4e89e4d67e00e8a5917c1c71ea3b405 100644 (file)
@@ -44,29 +44,27 @@ struct __tamFile_t {
        uint64_t n_lines;
 };
 
-char **bam_load_pos(const char *fn, int *_n)
+char **__bam_get_lines(const char *fn, int *_n) // for bam_plcmd.c only
 {
        char **list = 0, *s;
-       int n = 0, dret, m = 0, c;
+       int n = 0, dret, m = 0;
        gzFile fp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(fn, "r");
        kstream_t *ks;
        kstring_t *str;
        str = (kstring_t*)calloc(1, sizeof(kstring_t));
        ks = ks_init(fp);
-       while (ks_getuntil(ks, 0, str, &dret) > 0) {
+       while (ks_getuntil(ks, '\n', str, &dret) > 0) {
                if (n == m) {
                        m = m? m << 1 : 16;
                        list = (char**)realloc(list, m * sizeof(char*));
                }
-               s = list[n++] = (char*)calloc(str->l + 5, 1);
+               if (str->s[str->l-1] == '\r')
+                       str->s[--str->l] = '\0';
+               s = list[n++] = (char*)calloc(str->l + 1, 1);
                strcpy(s, str->s);
-               s += str->l + 1;
-               ks_getuntil(ks, 0, str, &dret);
-               *((uint32_t*)s) = atoi(str->s);
-               if (dret != '\n')
-                       while ((c = ks_getc(fp)) >= 0 && c != '\n');
        }
        ks_destroy(ks);
+       gzclose(fp);
        free(str->s); free(str);
        *_n = n;
        return list;
@@ -209,7 +207,7 @@ int sam_read1(tamFile fp, bam_header_t *header, bam1_t *b)
                        if (*s) parse_error(fp->n_lines, "unmatched CIGAR operation");
                        c->bin = bam_reg2bin(c->pos, bam_calend(c, bam1_cigar(b)));
                        doff += c->n_cigar * 4;
-               }
+               } else c->bin = bam_reg2bin(c->pos, c->pos + 1);
        }
        { // mtid, mpos, isize
                ret = ks_getuntil(ks, 0, str, &dret); c->mtid = strcmp(str->s, "=")? bam_get_tid(header, str->s) : c->tid;
@@ -245,9 +243,9 @@ int sam_read1(tamFile fp, bam_header_t *header, bam1_t *b)
                        type = str->s[3];
                        s = alloc_data(b, doff + 3) + doff;
                        s[0] = key[0]; s[1] = key[1]; s += 2; doff += 2;
-                       if (type == 'A' || type == 'a') {
+                       if (type == 'A' || type == 'a' || type == 'c' || type == 'C') { // c and C for backward compatibility
                                s = alloc_data(b, doff + 2) + doff;
-                               *s++ = type; *s = str->s[5];
+                               *s++ = 'A'; *s = str->s[5];
                                doff += 2;
                        } else if (type == 'I' || type == 'i') {
                                long long x;
@@ -341,7 +339,7 @@ static void taf2baf_core(const char *fntaf, const char *fnbaf, bam_header_t *hea
        int ret;
 
        b = (bam1_t*)calloc(1, sizeof(bam1_t));
-       fpbaf = bam_open(fnbaf, "w");
+       fpbaf = (strcmp(fnbaf, "-") == 0)? bam_dopen(fileno(stdout), "w") : bam_open(fnbaf, "w");
        fp = sam_open(fntaf);
        ret = sam_read1(fp, header, b);
        bam_header_write(fpbaf, header);