From d0f188e968b5d6e889d33fde3fc0b7127cf2aa82 Mon Sep 17 00:00:00 2001 From: John Marshall Date: Fri, 25 Jan 2013 15:56:45 +0000 Subject: [PATCH 1/2] Reorder link libraries Move -lz after libtabix.a, as it is needed to resolve names used in libtabix.a. Modern linkers have started requiring user care in library ordering again. --- perl/Makefile.PL | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/perl/Makefile.PL b/perl/Makefile.PL index 3ea6e8d..496a727 100644 --- a/perl/Makefile.PL +++ b/perl/Makefile.PL @@ -2,7 +2,7 @@ use ExtUtils::MakeMaker; WriteMakefile( NAME => 'Tabix', VERSION_FROM => 'Tabix.pm', - LIBS => ['-lz -L.. -ltabix'], + LIBS => ['-L.. -ltabix -lz'], DEFINE => '-D_FILE_OFFSET_BITS=64 -D_USE_KNETFILE', INC => '-I..', ); From 34599c630bad9100de9037420ff2bb4fcc46b3de Mon Sep 17 00:00:00 2001 From: Pierre Lindenbaum Date: Wed, 29 May 2013 22:01:36 +0200 Subject: [PATCH 2/2] added support for javascript/mozilla (see http://plindenbaum.blogspot.fr/2013/05/binding-c-library-with-javascript.html ). Added a README.md and a .gitignore --- .gitignore | 7 ++ README.md | 14 +++ javascript/mozilla/tabix.js | 178 ++++++++++++++++++++++++++++++++++++ javascript/mozilla/test.js | 44 +++++++++ 4 files changed, 243 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 javascript/mozilla/tabix.js create mode 100644 javascript/mozilla/test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb10fb7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.o +*.so.1 +*.a +*~ +*.a +bgzip +tabix diff --git a/README.md b/README.md new file mode 100644 index 0000000..4463a5d --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +Tabix +===== + +How to cite: +------------ + +Tabix: fast retrieval of sequence features from generic TAB-delimited files + +Li H. + +Bioinformatics. 2011 Mar 1;27(5):718-9. + +http://www.ncbi.nlm.nih.gov/pubmed/21208982 + diff --git a/javascript/mozilla/tabix.js b/javascript/mozilla/tabix.js new file mode 100644 index 0000000..1b26c69 --- /dev/null +++ b/javascript/mozilla/tabix.js @@ -0,0 +1,178 @@ +/** + JAVASCRIPT BINDINGS for Tabix. + + Author: Pierre Lindenbaum PhD @yokofakun http://plindenbaum.blogspot.com + Date: May 2013 + Reference: http://plindenbaum.blogspot.fr/2013/05/binding-c-library-with-javascript.html + + */ + +/* import js-ctypes */ +Components.utils.import("resource://gre/modules/ctypes.jsm") + +/** iterator constructor */ +function TabixIterator(owner,ptr) + { + this.owner=owner; + this.ptr=ptr; + } + +/* tabix file constructor */ +function TabixFile(filename) + { + this.init(); + this.log("Opening "+filename); + this.ptr=TabixFile.DLOpen(filename,0); + this.iter=null; + if(this.ptr.isNull()) throw "I/O ERROR: Cannot open \""+filename+"\""; + }; + + +/* TabixFile prototype methods below */ +TabixFile.prototype.LOADED=false; +TabixFile.prototype= { + LOADED : false, + DEBUG : false, + log: function(msg) + { + if(!this.DEBUG) return; + print("FileReader: " + msg); + }, + /* release the dynamic libraries */ + dispose: function() + { + if(!this.LOADED) return; + this.lib.close(); + this.LOADED=false; + }, + /* initialize the dynamic libraries */ + init : function() + { + if(this.LOADED) return; + try + { + this.lib = ctypes.open("libtabix.so.1"); + this.log("OK loaded"); + /* https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/js-ctypes_reference/Library#declare%28%29 */ + TabixFile.DLOpen= this.lib.declare("ti_open", + ctypes.default_abi, + ctypes.voidptr_t, + ctypes.char.ptr, + ctypes.int32_t + ); + TabixFile.DLClose= this.lib.declare("ti_close", + ctypes.default_abi, + ctypes.void_t, + ctypes.voidptr_t + ); + TabixFile.DLQueryS = this.lib.declare("ti_querys", + ctypes.default_abi, + ctypes.voidptr_t, + ctypes.voidptr_t, + ctypes.char.ptr + ); + + TabixFile.DLQuery = this.lib.declare("ti_query", + ctypes.default_abi, + ctypes.voidptr_t, + ctypes.voidptr_t, + ctypes.int32_t, + ctypes.int32_t, + ctypes.int32_t + ); + + TabixFile.DLIterFirst = this.lib.declare("ti_iter_first", + ctypes.default_abi, + ctypes.voidptr_t, + ctypes.voidptr_t + ); + + TabixFile.DLIterDestroy = this.lib.declare("ti_iter_destroy", + ctypes.default_abi, + ctypes.void_t, + ctypes.voidptr_t + ); + + TabixFile.DLIterRead = this.lib.declare("ti_read", + ctypes.default_abi, + ctypes.char.ptr, + ctypes.voidptr_t, + ctypes.voidptr_t, + ctypes.int32_t.ptr + ); + this.LOADED=true; + } + catch(e) + { + this.log(e); + throw e; + } + }, + /** close this TabixFile */ + close: function() + { + this.log("closing"); + if(this.iter!=null) this.iter.close(); + if(this.ptr==null) return; + TabixFile.DLClose(this.ptr); + this.ptr=null; + }, + /** + returns a TABIX iterator + + if argument is a string : returns an interator to the specified region : "chr:start-end" + if argument.length==3 : invokes ti_query(ptr,arg[0],arg[1],arg[2]) + if there is no argument : return an iterator to the whole tabix file. + + */ + query : function() + { + if(this.ptr==null) return; + if(this.iter!=null) + { + this.log("Closing iterator"); + this.iter.close(); + } + + var r=null; + switch(arguments.length) + { + case 0 : r=TabixFile.DLIterFirst(this.ptr); break; + case 1 : r=TabixFile.DLQueryS(this.ptr,arguments[0]); break; + case 3 : r=TabixFile.DLQuery( + this.ptr,arguments[0], + arguments[1], + arguments[2] + );break; + default : break; + } + this.log("query. "+arguments.length); + this.iter=new TabixIterator(this,r); + return this.iter; + } + }; + +/** Iterator protorype */ +TabixIterator.prototype={ + /** returns the next line or NULL */ + next: function() + { + if(this.ptr==null || this.owner.iter !== this) return null; + /* https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Using_js-ctypes/Declaring_types */ + var len=ctypes.int32_t(0); + var line=TabixFile.DLIterRead(this.owner.ptr,this.ptr,len.address()); + + if(line.isNull()) + { + this.close(); + return; + } + return line.readString(); + }, + /** release that iterator */ + close : function() + { + if(this.ptr!=null) TabixFile.DLIterDestroy(this.ptr); + this.ptr=null; + } + } diff --git a/javascript/mozilla/test.js b/javascript/mozilla/test.js new file mode 100644 index 0000000..ad7b617 --- /dev/null +++ b/javascript/mozilla/test.js @@ -0,0 +1,44 @@ +/* +to run that test: + + * download mozilla xulrunner-sdk : https://developer.mozilla.org/en-US/docs/XULRunner + * compile the tabix ldynamic ibrary using `make libtabix.so.1` + * run the test: + + +$ LD_LIBRARY_PATH=/path/to/xulrunner/bin:/path/to/tabix.dir \ + /path/to/xulrunner-sdk/bin/xpcshell -f test.js + +*/ + +/** query a location */ +load("tabix.js"); + + +var tabix=new TabixFile("../../example.gtf.gz"); + +print("#iteration using query(range)##################"); + +var iter=tabix.query("chr2:32800-35441"); +while((line=iter.next())!=null) + { + print(line); + } + + +iter=tabix.query("chr1:1873-2090"); +while((line=iter.next())!=null) + { + print(line); + } +iter.close(); + +print("#iteration using query()##################"); + /** get the 3 first lines */ +iter=tabix.query(); +for(var i=0; i< 3 && ((line=iter.next())!=null) ; ++i) + { + print(line); + } + +tabix.close();