]> git.donarmstrong.com Git - samtools.git/commitdiff
* samtools-0.1.4-17 (r361)
authorHeng Li <lh3@live.co.uk>
Thu, 25 Jun 2009 20:03:10 +0000 (20:03 +0000)
committerHeng Li <lh3@live.co.uk>
Thu, 25 Jun 2009 20:03:10 +0000 (20:03 +0000)
 * if a file is given on FTP, search locally for the BAM index

bam_index.c
bamtk.c

index 347189d63e397d4d433cc42783aea3a07545e9e2..1c0b29b09c9e6b063549b08d56cf5941d6ffccac 100644 (file)
@@ -260,30 +260,15 @@ void bam_index_save(const bam_index_t *idx, FILE *fp)
        fflush(fp);
 }
 
-bam_index_t *bam_index_load(const char *fn)
+static bam_index_t *bam_index_load_core(FILE *fp)
 {
-       bam_index_t *idx;
-       FILE *fp;
        int i;
-       char *fnidx, magic[4];
-
-       fnidx = (char*)calloc(strlen(fn) + 5, 1);
-       strcpy(fnidx, fn); strcat(fnidx, ".bai");
-       fp = fopen(fnidx, "r");
-       if (fp == 0) { // try "{base}.bai"
-               char *s = strstr(fn, "bam");
-               if (s == fn + strlen(fn) - 3) {
-                       strcpy(fnidx, fn);
-                       fnidx[strlen(fn)-1] = 'i';
-                       fp = fopen(fnidx, "r");
-               }
-       }
+       char magic[4];
+       bam_index_t *idx;
        if (fp == 0) {
-               fprintf(stderr, "[bam_index_load] the alignment is not indexed. Please run `index' command first. Abort!\n");
-               exit(1);
+               fprintf(stderr, "[bam_index_load_core] fail to load index.\n");
+               return 0;
        }
-       free(fnidx);
-
        fread(magic, 1, 4, fp);
        if (strncmp(magic, "BAI\1", 4)) {
                fprintf(stderr, "[bam_index_load] wrong magic number.\n");
@@ -333,6 +318,44 @@ bam_index_t *bam_index_load(const char *fn)
                if (bam_is_be)
                        for (j = 0; j < index2->n; ++j) bam_swap_endian_8p(&index2->offset[j]);
        }
+       return idx;
+}
+
+bam_index_t *bam_index_load2(const char *fnidx)
+{
+       bam_index_t *idx;
+       FILE *fp = fopen(fnidx, "r");
+       idx = bam_index_load_core(fp);
+       fclose(fp);
+       return idx;
+}
+
+bam_index_t *bam_index_load(const char *_fn)
+{
+       bam_index_t *idx;
+       FILE *fp;
+       char *fnidx, *fn;
+
+       if (strstr(_fn, "ftp://") == _fn) {
+               const char *p;
+               int l = strlen(_fn);
+               for (p = _fn + l - 1; p >= _fn; --p)
+                       if (*p == '/') break;
+               fn = strdup(p + 1);
+       } else fn = strdup(_fn);
+       fnidx = (char*)calloc(strlen(fn) + 5, 1);
+       strcpy(fnidx, fn); strcat(fnidx, ".bai");
+       fp = fopen(fnidx, "r");
+       if (fp == 0) { // try "{base}.bai"
+               char *s = strstr(fn, "bam");
+               if (s == fn + strlen(fn) - 3) {
+                       strcpy(fnidx, fn);
+                       fnidx[strlen(fn)-1] = 'i';
+                       fp = fopen(fnidx, "r");
+               }
+       }
+       free(fnidx); free(fn);
+       idx = bam_index_load_core(fp);
        fclose(fp);
        return idx;
 }
diff --git a/bamtk.c b/bamtk.c
index a1e87c20eb7aaeeef2aa7cc51c7147182d7a2be3..9474e83ea7a16de8412b59e2b1a8c73b89e70145 100644 (file)
--- a/bamtk.c
+++ b/bamtk.c
@@ -3,7 +3,7 @@
 #include "bam.h"
 
 #ifndef PACKAGE_VERSION
-#define PACKAGE_VERSION "0.1.4-16 (r360)"
+#define PACKAGE_VERSION "0.1.4-17 (r361)"
 #endif
 
 int bam_taf2baf(int argc, char *argv[]);