Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Support for javascript (mozilla) #3

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.o
*.so.1
*.a
*~
*.a
bgzip
tabix
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

178 changes: 178 additions & 0 deletions javascript/mozilla/tabix.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
44 changes: 44 additions & 0 deletions javascript/mozilla/test.js
Original file line number Diff line number Diff line change
@@ -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();
2 changes: 1 addition & 1 deletion perl/Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -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..',
);