]> git.donarmstrong.com Git - samtools.git/blobdiff - bam_index.c
A bug fix, "samtools view" is now working again.
[samtools.git] / bam_index.c
index 4a41f5264a67148c75f1f9be6d7e6880e5b848a6..4ff6bd482505172741c5a48ab413f141342e93bc 100644 (file)
@@ -1,8 +1,12 @@
 #include <ctype.h>
+#include <assert.h>
 #include "bam.h"
 #include "khash.h"
 #include "ksort.h"
 #include "bam_endian.h"
+#ifdef _USE_KNETFILE
+#include "knetfile.h"
+#endif
 
 /*!
   @header
@@ -155,7 +159,8 @@ bam_index_t *bam_index_core(bamFile fp)
                        last_tid = c->tid;
                        last_bin = 0xffffffffu;
                } else if (last_coor > c->pos) {
-                       fprintf(stderr, "[bam_index_core] the alignment is not sorted. Abort!\n");
+                       fprintf(stderr, "[bam_index_core] the alignment is not sorted (%s): %u > %u in %d-th chr\n",
+                                       bam1_qname(b), last_coor, c->pos, c->tid+1);
                        exit(1);
                }
                if (b->core.tid >= 0 && b->core.bin < 4681) insert_offset2(&idx->index2[b->core.tid], b, last_off);
@@ -259,30 +264,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");
@@ -332,7 +322,90 @@ 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_load_local(const char *_fn)
+{
+       FILE *fp;
+       char *fnidx, *fn;
+
+       if (strstr(_fn, "ftp://") == _fn || strstr(_fn, "http://") == _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);
+       if (fp) {
+               bam_index_t *idx = bam_index_load_core(fp);
+               fclose(fp);
+               return idx;
+       } else return 0;
+}
+
+#ifdef _USE_KNETFILE
+static void download_from_remote(const char *url)
+{
+       const int buf_size = 1 * 1024 * 1024;
+       char *fn;
+       FILE *fp;
+       uint8_t *buf;
+       knetFile *fp_remote;
+       int l;
+       if (strstr(url, "ftp://") != url && strstr(url, "http://") != url) return;
+       l = strlen(url);
+       for (fn = (char*)url + l - 1; fn >= url; --fn)
+               if (*fn == '/') break;
+       ++fn; // fn now points to the file name
+       fp_remote = knet_open(url, "r");
+       if (fp_remote == 0) {
+               fprintf(stderr, "[download_from_remote] fail to open remote file.\n");
+               return;
+       }
+       if ((fp = fopen(fn, "w")) == 0) {
+               fprintf(stderr, "[download_from_remote] fail to create file in the working directory.\n");
+               knet_close(fp_remote);
+               return;
+       }
+       buf = (uint8_t*)calloc(buf_size, 1);
+       while ((l = knet_read(fp_remote, buf, buf_size)) != 0)
+               fwrite(buf, 1, l, fp);
+       free(buf);
        fclose(fp);
+       knet_close(fp_remote);
+}
+#else
+static void download_from_remote(const char *url)
+{
+       return;
+}
+#endif
+
+bam_index_t *bam_index_load(const char *fn)
+{
+       bam_index_t *idx;
+       idx = bam_index_load_local(fn);
+       if (idx == 0 && (strstr(fn, "ftp://") == fn || strstr(fn, "http://") == fn)) {
+               char *fnidx = calloc(strlen(fn) + 5, 1);
+               strcat(strcpy(fnidx, fn), ".bai");
+               fprintf(stderr, "[bam_index_load] attempting to download the remote index file.\n");
+               download_from_remote(fnidx);
+               idx = bam_index_load_local(fn);
+       }
+       if (idx == 0) fprintf(stderr, "[bam_index_load] fail to load BAM index.\n");
        return idx;
 }
 
@@ -342,7 +415,10 @@ int bam_index_build2(const char *fn, const char *_fnidx)
        FILE *fpidx;
        bamFile fp;
        bam_index_t *idx;
-       assert(fp = bam_open(fn, "r"));
+       if ((fp = bam_open(fn, "r")) == 0) {
+               fprintf(stderr, "[bam_index_build2] fail to open the BAM file.\n");
+               return -1;
+       }
        idx = bam_index_core(fp);
        bam_close(fp);
        if (_fnidx == 0) {
@@ -353,7 +429,7 @@ int bam_index_build2(const char *fn, const char *_fnidx)
        if (fpidx == 0) {
                fprintf(stderr, "[bam_index_build2] fail to create the index file.\n");
                free(fnidx);
-               return 1;
+               return -1;
        }
        bam_index_save(idx, fpidx);
        bam_index_destroy(idx);