From 941542dfbedf4ba4850d3c1d6b96981234fd6560 Mon Sep 17 00:00:00 2001 From: Heng Li Date: Fri, 24 Jul 2009 10:39:26 +0000 Subject: [PATCH] * samtools-0.1.5-12 (r415) * FLAG now can be in HEX --- bam.c | 10 ++++++++-- bam.h | 2 ++ bam_import.c | 6 +++++- bamtk.c | 2 +- sam.c | 4 +++- sam.h | 12 ++++++------ sam_view.c | 7 +++++-- 7 files changed, 30 insertions(+), 13 deletions(-) diff --git a/bam.c b/bam.c index 1ff4a5a..33907f1 100644 --- a/bam.c +++ b/bam.c @@ -236,7 +236,7 @@ int bam_write1(bamFile fp, const bam1_t *b) return bam_write1_core(fp, &b->core, b->data_len, b->data); } -char *bam_format1(const bam_header_t *header, const bam1_t *b) +char *bam_format1_core(const bam_header_t *header, const bam1_t *b, int is_hex) { uint8_t *s = bam1_seq(b), *t = bam1_qual(b); int i; @@ -244,7 +244,8 @@ char *bam_format1(const bam_header_t *header, const bam1_t *b) kstring_t str; str.l = str.m = 0; str.s = 0; - ksprintf(&str, "%s\t%d\t", bam1_qname(b), c->flag); + if (is_hex) ksprintf(&str, "%s\t0x%x\t", bam1_qname(b), c->flag); + else ksprintf(&str, "%s\t%d\t", bam1_qname(b), c->flag); 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); @@ -282,6 +283,11 @@ char *bam_format1(const bam_header_t *header, const bam1_t *b) return str.s; } +char *bam_format1(const bam_header_t *header, const bam1_t *b) +{ + return bam_format1_core(header, b, 0); +} + void bam_view1(const bam_header_t *header, const bam1_t *b) { char *s = bam_format1(header, b); diff --git a/bam.h b/bam.h index 451b65a..a33006d 100644 --- a/bam.h +++ b/bam.h @@ -431,6 +431,8 @@ extern "C" { */ char *bam_format1(const bam_header_t *header, const bam1_t *b); + char *bam_format1_core(const bam_header_t *header, const bam1_t *b, int is_hex); + /*! @typedef @abstract Structure for one alignment covering the pileup position. @field b pointer to the alignment diff --git a/bam_import.c b/bam_import.c index c6fbafc..2cadfcc 100644 --- a/bam_import.c +++ b/bam_import.c @@ -297,7 +297,11 @@ int sam_read1(tamFile fp, bam_header_t *header, bam1_t *b) doff += c->l_qname; } { // flag, tid, pos, qual - ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->flag = atoi(str->s); + long flag; + char *s; + ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; + flag = strtol((char*)str->s, &s, 0); + c->flag = flag; ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->tid = bam_get_tid(header, str->s); if (c->tid < 0 && strcmp(str->s, "*")) { if (header->n_targets == 0) { diff --git a/bamtk.c b/bamtk.c index bbdf82c..5df1407 100644 --- a/bamtk.c +++ b/bamtk.c @@ -4,7 +4,7 @@ #include "bam.h" #ifndef PACKAGE_VERSION -#define PACKAGE_VERSION "0.1.5-11 (r408)" +#define PACKAGE_VERSION "0.1.5-12 (r415)" #endif int bam_taf2baf(int argc, char *argv[]); diff --git a/sam.c b/sam.c index 45cb05c..a3f2998 100644 --- a/sam.c +++ b/sam.c @@ -3,6 +3,7 @@ #define TYPE_BAM 1 #define TYPE_READ 2 +#define TYPE_HEX 4 bam_header_t *bam_header_dup(const bam_header_t *h0) { @@ -75,6 +76,7 @@ samfile_t *samopen(const char *fn, const char *mode, const void *aux) // open file fp->x.tamw = strcmp(fn, "-")? fopen(fn, "w") : stdout; if (fp->x.tamr == 0) goto open_err_ret; + if (strstr(mode, "x")) fp->type |= TYPE_HEX; // write header if (strstr(mode, "h")) { int i; @@ -126,7 +128,7 @@ int samwrite(samfile_t *fp, const bam1_t *b) if (fp == 0 || (fp->type & TYPE_READ)) return -1; // not open for writing if (fp->type & TYPE_BAM) return bam_write1(fp->x.bam, b); else { - char *s = bam_format1(fp->header, b); + char *s = bam_format1_core(fp->header, b, fp->type & TYPE_HEX); int l = strlen(s); fputs(s, fp->x.tamw); fputc('\n', fp->x.tamw); free(s); diff --git a/sam.h b/sam.h index 86b7576..b8a0064 100644 --- a/sam.h +++ b/sam.h @@ -15,7 +15,7 @@ /*! @typedef @abstract SAM/BAM file handler - @field type type of the handler; bit 1 for BAM and bit 2 for reading + @field type type of the handler; bit 1 for BAM, 2 for reading and bit 3 for is_hex @field bam BAM file handler; valid if (type&1) == 1 @field tamr SAM file handler for reading; valid if type == 2 @field tamw SAM file handler for writing; valid if type == 0 @@ -41,11 +41,11 @@ extern "C" { @param fn SAM/BAM file name; "-" is recognized as stdin (for reading) or stdout (for writing). - @param mode open mode /[rw](b?)(u?)(h?)/: 'r' for reading, 'w' for - writing, 'b' for BAM I/O, 'u' for uncompressed BAM output and 'h' - for outputing header in SAM. If 'b' present, it must immediately - follow 'r' or 'w'. Valid modes are "r", "w", "wh", "rb", "wb" and - "wbu" exclusively. + @param mode open mode /[rw](b?)(u?)(h?)(x?)/: 'r' for reading, 'w' + for writing, 'b' for BAM I/O, 'u' for uncompressed BAM output, 'h' + for outputing header in SAM and 'x' for HEX flag. If 'b' present, + it must immediately follow 'r' or 'w'. Valid modes are "r", "w", + "wh", "wx", "whx", "rb", "wb" and "wbu" exclusively. @param aux auxiliary data; if mode[0]=='w', aux points to bam_header_t; if strcmp(mode, "rb")!=0 and @SQ header lines in SAM diff --git a/sam_view.c b/sam_view.c index 02aee3c..f253dbf 100644 --- a/sam_view.c +++ b/sam_view.c @@ -35,13 +35,13 @@ static int usage(void); int main_samview(int argc, char *argv[]) { - int c, is_header = 0, is_header_only = 0, is_bamin = 1, ret = 0, is_uncompressed = 0, is_bamout = 0; + int c, is_header = 0, is_header_only = 0, is_bamin = 1, ret = 0, is_uncompressed = 0, is_bamout = 0, is_hex = 0; samfile_t *in = 0, *out = 0; char in_mode[5], out_mode[5], *fn_out = 0, *fn_list = 0; /* parse command-line options */ strcpy(in_mode, "r"); strcpy(out_mode, "w"); - while ((c = getopt(argc, argv, "Sbt:hHo:q:f:F:ul:r:")) >= 0) { + while ((c = getopt(argc, argv, "Sbt:hHo:q:f:F:ul:r:x")) >= 0) { switch (c) { case 'S': is_bamin = 0; break; case 'b': is_bamout = 1; break; @@ -55,6 +55,7 @@ int main_samview(int argc, char *argv[]) case 'u': is_uncompressed = 1; break; case 'l': g_library = strdup(optarg); break; case 'r': g_rg = strdup(optarg); break; + case 'x': is_hex = 1; break; default: return usage(); } } @@ -64,6 +65,7 @@ int main_samview(int argc, char *argv[]) if (is_bamin) strcat(in_mode, "b"); if (is_header) strcat(out_mode, "h"); if (is_uncompressed) strcat(out_mode, "u"); + if (is_hex && !is_bamout) strcat(out_mode, "x"); if (argc == optind) return usage(); // open file handlers @@ -123,6 +125,7 @@ static int usage() fprintf(stderr, " -H print header only (no alignments)\n"); fprintf(stderr, " -S input is SAM\n"); fprintf(stderr, " -u uncompressed BAM output (force -b)\n"); + fprintf(stderr, " -x output FLAG in HEX (samtools-C specific)\n"); fprintf(stderr, " -t FILE list of reference names and lengths (force -S) [null]\n"); fprintf(stderr, " -o FILE output file name [stdout]\n"); fprintf(stderr, " -f INT required flag, 0 for unset [0]\n"); -- 2.39.2