Server-side javascript: translating a DNA with Node.js
(wikipedia) Node.js is an evented I/O framework for the V8 JavaScript engine on Unix-like platforms. It is intended for writing scalable (javascript-based) network programs such as web servers.
In the following post I will create a javascript server translating a DNA to a protein.
Installing Node.js
I've downloaded the sources for Node.js from http://nodejs.org/#download. It compiled (configure+make) and ran without any problem.The script
The following script contains a class handling a GeneticCode and the server TranslateDna translating the DNA to a protein, it handles both the POST and the GET http methods. It no parameter is found it displays a simple HTML form, else the form data are decoded and the DNA is translated. The protein is returned as a JSON structure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Author: | |
* Pierre Lindenbaum | |
* Mail: | |
* plindenbaum@yahoo.fr | |
* WWW: | |
* http://plindenbaum.blogspot.com | |
* Motivation: | |
* my first Node.js script . translate a DNA to a protein | |
* Usage: | |
* ${nodejs.dir}/node script.js | |
*/ | |
var sys = require('sys'); | |
querystring = require('querystring'); | |
/** class GeneticCode */ | |
function GeneticCode(name,ncbiString) | |
{ | |
this.name=name; | |
this.ncbiString=ncbiString; | |
}; | |
/** std genetic code */ | |
GeneticCode.STANDARD=new GeneticCode( | |
"standard", | |
"FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG" | |
); | |
GeneticCode.prototype.base2index=function(c) | |
{ | |
switch(c) | |
{ | |
case 'T':case 't': return 0; | |
case 'C':case 'c': return 1; | |
case 'A':case 'a': return 2; | |
case 'G':case 'g': return 3; | |
default: return -1; | |
} | |
}; | |
GeneticCode.prototype.translate=function( b1, b2, b3) | |
{ | |
if(arguments.length==1) | |
{ | |
var prot=""; | |
for(var i=0;i+2 < b1.length;i+=3) | |
{ | |
prot+= this.translate(b1[i],b1[i+1],b1[i+2]); | |
} | |
return prot; | |
} | |
var base1= this.base2index(b1); | |
var base2= this.base2index(b2); | |
var base3= this.base2index(b3); | |
if(base1==-1 || base2==-1 || base3==-1) | |
{ | |
return '?'; | |
} | |
else | |
{ | |
return this.ncbiString[base1*16+base2*4+base3]; | |
} | |
} | |
function TranslateDna(req,res) | |
{ | |
if(req.method == 'GET') | |
{ | |
var params = require('url').parse(req.url,true); | |
if(params && params.query && params.query.dna) | |
{ | |
res.writeHead(200, {'Content-type': 'text/plain'}); | |
res.write(JSON.stringify({'protein':GeneticCode.STANDARD.translate(params.query.dna.replace(/\s/g,"")),'query':params.query.dna})); | |
res.end(); | |
return; | |
} | |
} | |
else if(req.method=='POST') | |
{ | |
var query = ""; | |
req.addListener('data', function (chunk) { | |
query+=chunk; | |
}); | |
req.addListener('end', function (chunk) | |
{ | |
var params = querystring.parse(query); | |
if(params && params.dna) | |
{ | |
res.writeHead(200, {'Content-type': 'text/plain'}); | |
res.write(JSON.stringify({'protein':GeneticCode.STANDARD.translate(params.dna.replace(/\s/g,"")),'query': params.dna})); | |
res.end(); | |
return; | |
} | |
}); | |
return; | |
} | |
res.writeHead(200, {'Content-type': 'text/html'}); | |
res.end( | |
'<html><body><form action="/" method=\"'+ | |
(Math.random()<0.5?"GET":"POST") | |
+'\">'+ | |
'<h1>DNA</h1>'+ | |
'<textarea name="dna"></textarea><br/>' + | |
'<input type="submit" value="Submit">' + | |
'</form></body></html>' | |
); | |
} | |
http = require('http'); | |
var nodeserver = http.createServer(TranslateDna); | |
nodeserver.listen(8080); | |
console.log('Server running at http://127.0.0.1:8080/'); |
Running the server
> node-v0.2.5/node translate.js
Server running at http://127.0.0.1:8080
Server running at http://127.0.0.1:8080
Test
> curl "http://localhost:8080/"
<html><body><form action="/" method="GET"><h1>DNA</h1><textarea name="dna"></textarea><br/><input type="submit" value="Submit"></form></body></html>
> curl "http://localhost:8080/?dna=ATGAACTATCGATGCTACGACTGATCG"
{"protein":"MNYRCYD*S","query":"ATGAACTATCGATGCTACGACTGATCG"}
That's it,
Pierre
1 comment:
Hi
Can node.js be used to find bone marrow transplant donors from complete human genomes databases on AWS?
Or are we better off using
c/c++/java/python/ruby ?
Are there existing libraries ?
Thanks
Post a Comment