]> git.donarmstrong.com Git - samtools.git/blobdiff - bedidx.c
Complain when BAM cannot be open. Severe bug fixed in -m haploid calling.
[samtools.git] / bedidx.c
index 9b76af74ac71302521a8c01b0a100d8d8e1e904d..ec75a1067f5905c69ead868ff42d2073189bb690 100644 (file)
--- a/bedidx.c
+++ b/bedidx.c
@@ -4,6 +4,13 @@
 #include <stdio.h>
 #include <zlib.h>
 
+#ifdef _WIN32
+#define drand48() ((double)rand() / RAND_MAX)
+#endif
+
+#include "ksort.h"
+KSORT_INIT_GENERIC(uint64_t)
+
 #include "kseq.h"
 KSTREAM_INIT(gzFile, gzread, 8192)
 
@@ -26,7 +33,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;
@@ -53,6 +60,7 @@ void bed_index(void *_h)
                if (kh_exist(h, k)) {
                        bed_reglist_t *p = &kh_val(h, k);
                        if (p->idx) free(p->idx);
+                       ks_introsort(uint64_t, p->n, p->a);
                        p->idx = bed_index_core(p->n, p->a, &p->m);
                }
        }
@@ -96,8 +104,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;
@@ -114,8 +123,10 @@ void *bed_read(const char *fn)
                        if (ks_getuntil(ks, 0, str, &dret) > 0 && isdigit(str->s[0])) {
                                beg = atoi(str->s); // begin
                                if (dret != '\n') {
-                                       if (ks_getuntil(ks, 0, str, &dret) > 0 && isdigit(str->s[0]))
+                                       if (ks_getuntil(ks, 0, str, &dret) > 0 && isdigit(str->s[0])) {
                                                end = atoi(str->s); // end
+                                               if (end < beg) end = -1;
+                                       }
                                }
                        }
                }