Imported Upstream version 0.2.1
authorCharles Plessy <plessy@debian.org>
Thu, 19 Aug 2010 06:49:48 +0000 (15:49 +0900)
committerCharles Plessy <plessy@debian.org>
Thu, 19 Aug 2010 06:49:48 +0000 (15:49 +0900)
Makefile
NEWS
index.c
main.c
tabix.h
tabix.py [new file with mode: 0755]

index 03a49969b6d229b49754c3718d4537c2bb8967c6..9013642bacc8a1ec2c93ff03a9e519f5ef202abd 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -28,6 +28,12 @@ all:$(PROG)
 
 lib:libtabix.a
 
 
 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)
 
 libtabix.a:$(LOBJS)
                $(AR) -cru $@ $(LOBJS)
 
@@ -51,6 +57,6 @@ tabix.pdf:tabix.tex
                pdflatex tabix.tex
 
 cleanlocal:
                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
 
 clean:cleanlocal-recur
diff --git a/NEWS b/NEWS
index 290f2d89082a202f1aee9836327548d2b007bdcb..eac654c4cc61d5f5b0ba449e863e351ec4051b82 100644 (file)
--- 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)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 Beta Release 0.2.0 (11 May, 2010)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
diff --git a/index.c b/index.c
index b3479fb7b8846f8506b8de98cea97df3fb17ea48..e5b227c1466eea9d8be901020ffc2726dc33cc9e 100644 (file)
--- 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);    
 }
 
        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;
 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 a2a456566fb9f9a68e3692827cc8fd99943f3f2c..79c37083d444016b651d03ea8408988df2282ab6 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,7 +6,7 @@
 #include "bgzf.h"
 #include "tabix.h"
 
 #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[])
 {
 
 int main(int argc, char *argv[])
 {
diff --git a/tabix.h b/tabix.h
index 95a849158feded5acfc134a1ddef3577546890ad..4390c0918023936c5e8f17016d6f5532ac84b313 100644 (file)
--- 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);
        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 */
        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 (executable)
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 <in.gz> <reg>\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()