Created
December 31, 2010 12:46
-
-
Save lindenb/760980 to your computer and use it in GitHub Desktop.
My first Node.js script . translate a DNA to a protein
This file contains hidden or 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/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment