From: Charles Plessy Date: Thu, 19 Aug 2010 06:49:48 +0000 (+0900) Subject: Imported Upstream version 0.2.1 X-Git-Url: http://woldlab.caltech.edu/gitweb/?p=tabix.git;a=commitdiff_plain;h=07678d3f22da823d211cb5687fe51335ed0498fd Imported Upstream version 0.2.1 --- diff --git a/Makefile b/Makefile index 03a4996..9013642 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,12 @@ all:$(PROG) lib:libtabix.a +libtabix.so.1:$(LOBJS) + $(CC) -shared -Wl,-soname,libtabix.so -o $@ $(LOBJS) -lc -lz + +libtabix.1.dylib:$(LOBJS) + libtool -dynamic $(LOBJS) -o $@ -lc -lz + libtabix.a:$(LOBJS) $(AR) -cru $@ $(LOBJS) @@ -51,6 +57,6 @@ tabix.pdf:tabix.tex pdflatex tabix.tex cleanlocal: - rm -fr gmon.out *.o a.out *.dSYM $(PROG) *~ *.a tabix.aux tabix.log tabix.pdf *.class + rm -fr gmon.out *.o a.out *.dSYM $(PROG) *~ *.a tabix.aux tabix.log tabix.pdf *.class libtabix.*.dylib libtabix.so* clean:cleanlocal-recur diff --git a/NEWS b/NEWS index 290f2d8..eac654c 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,15 @@ +Beta Release 0.2.1 (3 June, 2010) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Notable changes: + + * Allow shared library to be compiled. Added python binding to the + shared library. + +(0.2.1: 3 June 2010, r582) + + + Beta Release 0.2.0 (11 May, 2010) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/index.c b/index.c index b3479fb..e5b227c 100644 --- a/index.c +++ b/index.c @@ -929,6 +929,15 @@ ti_iter_t ti_queryi(tabix_t *t, int tid, int beg, int end) return ti_iter_query(t->idx, tid, beg, end); } +ti_iter_t ti_querys(tabix_t *t, const char *reg) +{ + int tid, beg, end; + if (reg == 0) return ti_iter_first(); + if (ti_lazy_index_load(t) != 0) return 0; + if (ti_parse_region(t->idx, reg, &tid, &beg, &end) < 0) return 0; + return ti_iter_query(t->idx, tid, beg, end); +} + ti_iter_t ti_query(tabix_t *t, const char *name, int beg, int end) { int tid; diff --git a/main.c b/main.c index a2a4565..79c3708 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ #include "bgzf.h" #include "tabix.h" -#define PACKAGE_VERSION "0.1.6 (r565)" +#define PACKAGE_VERSION "0.2.1 (r582)" int main(int argc, char *argv[]) { diff --git a/tabix.h b/tabix.h index 95a8491..4390c09 100644 --- a/tabix.h +++ b/tabix.h @@ -73,6 +73,7 @@ extern "C" { void ti_close(tabix_t *t); ti_iter_t ti_query(tabix_t *t, const char *name, int beg, int end); ti_iter_t ti_queryi(tabix_t *t, int tid, int beg, int end); + ti_iter_t ti_querys(tabix_t *t, const char *reg); const char *ti_read(tabix_t *t, ti_iter_t iter, int *len); /* Destroy the iterator */ diff --git a/tabix.py b/tabix.py new file mode 100755 index 0000000..8b5c7c8 --- /dev/null +++ b/tabix.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python + +# Author: Heng Li and Aaron Quinlan +# License: MIT/X11 + +import sys +from ctypes import * +from ctypes.util import find_library +import glob, platform + +def load_shared_library(lib, _path='.', ver='*'): + """Search for and load the tabix library. The + expectation is that the library is located in + the current directory (ie. "./") + """ + # find from the system path + path = find_library(lib) + if (path == None): # if fail, search in the custom directory + s = platform.system() + if (s == 'Darwin'): suf = ver+'.dylib' + elif (s == 'Linux'): suf = '.so'+ver + candidates = glob.glob(_path+'/lib'+lib+suf); + if (len(candidates) == 1): path = candidates[0] + else: return None + cdll.LoadLibrary(path) + return CDLL(path) + +def tabix_init(): + """Initialize and return a tabix reader object + for subsequent tabix_get() calls. + """ + tabix = load_shared_library('tabix') + if (tabix == None): return None + tabix.ti_read.restype = c_char_p + # on Mac OS X 10.6, the following declarations are required. + tabix.ti_open.restype = c_void_p + tabix.ti_querys.argtypes = [c_void_p, c_char_p] + tabix.ti_querys.restype = c_void_p + tabix.ti_query.argtypes = [c_void_p, c_char_p, c_int, c_int] + tabix.ti_query.restype = c_void_p + tabix.ti_read.argtypes = [c_void_p, c_void_p, c_void_p] + tabix.ti_iter_destroy.argtypes = [c_void_p] + tabix.ti_close.argtypes = [c_void_p] + # FIXME: explicit declarations for APIs not used in this script + return tabix + +# OOP interface +class Tabix: + def __init__(self, fn, fnidx=0): + self.tabix = tabix_init(); + if (self.tabix == None): + sys.stderr.write("[Tabix] Please make sure the shared library is compiled and available.\n") + return + self.fp = self.tabix.ti_open(fn, fnidx); + + def __del__(self): + if (self.tabix): self.tabix.ti_close(self.fp) + + def fetch(self, chr, start=-1, end=-1): + """Generator function that will yield each interval + within the requested range from the requested file. + """ + if (self.tabix == None): return + if (start < 0): iter = self.tabix.ti_querys(self.fp, chr) # chr looks like: "chr2:1,000-2,000" or "chr2" + else: iter = self.tabix.ti_query(self.fp, chr, start, end) # chr must be a sequence name + if (iter == None): + sys.stderr.write("[Tabix] Malformatted query or wrong sequence name.\n") + return + while (1): # iterate + s = self.tabix.ti_read(self.fp, iter, 0) + if (s == None): break + yield s + self.tabix.ti_iter_destroy(iter) + +# command-line interface +def main(): + if (len(sys.argv) < 3): + sys.stderr.write("Usage: tabix.py \n") + sys.exit(1) + + # report the features in the requested interval + tabix = Tabix(sys.argv[1]) + for line in tabix.fetch(sys.argv[2]): + print line + +if __name__ == '__main__': + main()