]> git.donarmstrong.com Git - samtools.git/commitdiff
* synchronize bgzf in tabix and in samtools
authorHeng Li <lh3@live.co.uk>
Thu, 31 Mar 2011 20:38:57 +0000 (20:38 +0000)
committerHeng Li <lh3@live.co.uk>
Thu, 31 Mar 2011 20:38:57 +0000 (20:38 +0000)
 * fixed a severe bug in bedidx.c

bedidx.c
bgzf.c
bgzf.h

index 9b76af74ac71302521a8c01b0a100d8d8e1e904d..92978318d4ccfbbabeeddd150458245f669062d8 100644 (file)
--- a/bedidx.c
+++ b/bedidx.c
@@ -26,7 +26,7 @@ int *bed_index_core(int n, uint64_t *a, int *n_idx)
        m = *n_idx = 0; idx = 0;
        for (i = 0; i < n; ++i) {
                int beg, end;
-               beg = a[i]>>32; end = (uint32_t)a[i];
+               beg = a[i]>>32 >> LIDX_SHIFT; end = ((uint32_t)a[i]) >> LIDX_SHIFT;
                if (m < end + 1) {
                        int oldm = m;
                        m = end + 1;
@@ -96,8 +96,9 @@ void *bed_read(const char *fn)
        int dret;
        kstring_t *str;
        // read the list
-       str = calloc(1, sizeof(kstring_t));
        fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
+       if (fp == 0) return 0;
+       str = calloc(1, sizeof(kstring_t));
        ks = ks_init(fp);
        while (ks_getuntil(ks, 0, str, &dret) >= 0) { // read the chr name
                int beg = -1, end = -1;
diff --git a/bgzf.c b/bgzf.c
index db970c83f621bac471457bc58821e69e186e03ee..216cd04527f1ad187420cf1fab781e1d72e485b8 100644 (file)
--- a/bgzf.c
+++ b/bgzf.c
@@ -111,6 +111,32 @@ report_error(BGZF* fp, const char* message) {
     fp->error = message;
 }
 
+int bgzf_check_bgzf(const char *fn)
+{
+    BGZF *fp;
+    uint8_t buf[10],magic[10]="\037\213\010\4\0\0\0\0\0\377";
+    int n;
+
+    if ((fp = bgzf_open(fn, "r")) == 0) 
+    {
+        fprintf(stderr, "[bgzf_check_bgzf] failed to open the file: %s\n",fn);
+        return -1;
+    }
+
+#ifdef _USE_KNETFILE
+    n = knet_read(fp->x.fpr, buf, 10);
+#else
+    n = fread(buf, 1, 10, fp->file);
+#endif
+    bgzf_close(fp);
+
+    if ( n!=10 ) 
+        return -1;
+
+    if ( !memcmp(magic, buf, 10) ) return 1;
+    return 0;
+}
+
 static BGZF *bgzf_read_init()
 {
        BGZF *fp;
diff --git a/bgzf.h b/bgzf.h
index 9dc7b5fdb600087c5b4c44ff202c05f914626a23..7295f37425df320199aef87bb7bb80e482cc38bd 100644 (file)
--- a/bgzf.h
+++ b/bgzf.h
@@ -128,6 +128,7 @@ int bgzf_check_EOF(BGZF *fp);
 int bgzf_read_block(BGZF* fp);
 int bgzf_flush(BGZF* fp);
 int bgzf_flush_try(BGZF *fp, int size);
+int bgzf_check_bgzf(const char *fn);
 
 #ifdef __cplusplus
 }