Last active
November 13, 2015 01:33
-
-
Save nimezhu/c077098fb553c1de6578 to your computer and use it in GitHub Desktop.
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
request = require("request") | |
var restful="http://127.0.0.1:8081" | |
function list(sp,callback) { | |
request( | |
{url:restful+"/list"+sp+"h" | |
,json:true}, | |
function(err,res,body) { | |
callback(body) | |
} | |
) | |
} | |
function getAln(gene,callback) { | |
request( | |
{url:restful+"/bl2seq_r?gene="+gene ,json:true}, | |
function(err,res,body) { | |
callback(body) | |
}) | |
} | |
function getMotifs(genome,gene,callback) { | |
request( | |
{url:restful+"/"+genome+"_cp_motifs?gene="+gene ,json:true}, | |
function(err,res,body) { | |
callback(body) | |
}) | |
} | |
function getAll(gene,callback,callback2) { | |
var done=0; | |
var hMotifs,mMotifs,Aln; | |
getAln(gene,function(d){ | |
Aln=d; | |
if(d.length>0) { | |
var human_gene=d[0].subject_id | |
getMotifs("hg19",human_gene,function(d){ | |
hMotifs=d.motifs; | |
done+=1; | |
if(done==2) {callback({"aln":Aln,"h":hMotifs,"m":mMotifs},callback2)} | |
}) | |
getMotifs("mm10",gene, | |
function(d) { | |
mMotifs=d.motifs; | |
done+=1; | |
if(done==2) {callback({"aln":Aln,"h":hMotifs,"m":mMotifs},callback2)} | |
} | |
) | |
} else { | |
callback({"aln":[],"h":[],"m":[]},callback2); | |
} | |
} | |
) | |
} | |
function translate(coord_start,coord_end,start,end) { | |
if (coord_end>coord_start) { | |
return [start-coord_start,end-coord_start] | |
} else { | |
return [coord_end-end, coord_end-start] | |
} | |
} | |
function proc(d,callback) { | |
g=[]; | |
if(!d.aln) {callback(g)} | |
else { | |
d.aln.forEach( function(a) { | |
var m_start = a.q_start | |
var m_end = a.q_end | |
var m_max=Math.max(m_start,m_end) | |
var m_min=Math.min(m_start,m_end) | |
var h_start = a.s_start | |
var h_end = a.s_end | |
var h_max=Math.max(h_start,h_end) | |
var h_min=Math.min(h_start,h_end) | |
var m_a=[] | |
var h_a=[] | |
d.m.forEach(function(motif) { | |
if(motif[2]>m_min && motif[1] < m_max) { | |
var x=translate(m_start,m_end,motif[1],motif[2]) | |
s=x[0] | |
e=x[1] | |
m_a.push({"name":motif[0],"s":s,"e":e,"motif":motif}) | |
} | |
}) | |
d.h.forEach(function(motif) { | |
if(motif[2]>h_min && motif[1] < h_max) { | |
var x=translate(h_start,h_end,motif[1],motif[2]) | |
s=x[0] | |
e=x[1] | |
h_a.push({"name":motif[0],"s":s,"e":e,"motif":motif}) | |
} | |
}) | |
var cm=[] | |
h_a.forEach(function(h_m) { | |
m_a.forEach(function(m_m) { | |
if (m_m.name==h_m.name && m_m.s < h_m.e && m_m.e > h_m.s) { | |
cm.push([m_m.name,m_m.s,m_m.e,h_m.s,h_m.e]) | |
} | |
}) | |
}) | |
if(cm.length>1) {g.push(a,cm)} | |
}) | |
callback(g) | |
} | |
} | |
function cb(d) {console.log(JSON.stringify(d));} | |
/* | |
list("mouse",function(genes) { | |
genes.forEach(function(d,i){ | |
console.log("# ",i,d) | |
getAll(d,process); | |
}) | |
}) | |
*/ | |
module.exports = { | |
homoloMotifs : function(gene,callback) { | |
g=getAll(gene,proc,callback); | |
} | |
} | |
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
var fs = require('fs'); | |
var motifWeb = require("./motifWeb"); | |
function readLines(input, func) { | |
var remaining = ''; | |
input.on('data', function(data) { | |
remaining += data; | |
var index = remaining.indexOf('\n'); | |
while (index > -1) { | |
var line = remaining.substring(0, index); | |
remaining = remaining.substring(index + 1); | |
func(line); | |
index = remaining.indexOf('\n'); | |
} | |
}); | |
input.on('end', function() { | |
if (remaining.length > 0) { | |
func(remaining); | |
} | |
}); | |
} | |
var enrichedMotifs={} | |
function func(data) { | |
enrichedMotifs[data]=1; | |
} | |
var input = fs.createReadStream('data/enriched.motif.list'); | |
readLines(input, func); | |
var sqlite3 = require('sqlite3').verbose(); | |
var express = require('express'); | |
var restapi = express(); | |
/***** SECTION FOR mouse tpm ******/ | |
var mouseTpmDb = new sqlite3.Database("data/Mouse_tpm.sqlite3.db") | |
var mouseTpm={}; | |
mouseTpmDb.each("SELECT * from tbl",function(err,row){ | |
mouseTpm[row.gene]=row; | |
}) | |
restapi.get('/mousetpm',function(req,res) { | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
var geneids; | |
if (typeof req.query.genes==="string") { | |
geneids=req.query.genes.split(","); | |
} else { | |
geneids=req.query.genes; | |
} | |
var retv=[]; | |
geneids.forEach(function(geneid){ | |
if (mouseTpm[geneid]) { | |
retv.push(mouseTpm[geneid]); | |
} | |
}) | |
res.json(retv); | |
},function(){ | |
}) | |
/****SECTION FOR HomeGene DB *******/ | |
var homodb = new sqlite3.Database("data/homogene.sqlite3.db") | |
var m2h={} | |
var h2m={} | |
var mouseHomologenes,humanHomologenes; | |
homodb.each("SELECT Mouse,Human from tbl",function(err,row){ | |
m2h[row.Mouse]=row.Human; | |
h2m[row.Human]=row.Mouse; | |
},function(){ | |
mouseHomologenes=Object.keys(m2h) | |
humanHomologenes=Object.keys(h2m) | |
homodb.close(); | |
}); | |
restapi.get('/m2h',function(req,res) { | |
if (req.query.genes) { | |
var retv=translate(req.query.genes,m2h); | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
res.json(retv) | |
} else { | |
res.status(500) | |
} | |
}); | |
restapi.get('/listmouseh',function(req,res) { | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
res.json(mouseHomologenes) | |
}) | |
restapi.get('/listhumanh',function(req,res) { | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
res.json(humanHomologenes) | |
}) | |
restapi.get('/h2m',function(req,res) { | |
if (req.query.genes) { | |
var retv=translate(req.query.genes,h2m); | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
res.json(retv) | |
} else { | |
res.status(500); | |
} | |
}); | |
function translate(genes,map) { | |
if (typeof genes === "string") { | |
genes=genes.split(",") | |
} | |
var retv={"homogene":[],"unknown":[]}; | |
genes.forEach(function(d) { | |
if (map[d]) { | |
retv.homogene.push(map[d]) | |
} | |
else { | |
retv.unknown.push(d); | |
} | |
}) | |
return retv; | |
} | |
/****SECTION FOR:$ | |
* GeneSet DB *******/ | |
var qs = require('querystring'); | |
var dbs = ["test.db","elisa.db"]; | |
var db={}; | |
dbs.forEach(function(d){ | |
db[d] = new sqlite3.Database("data/"+d); | |
db[d].serialize(function() { | |
//db.run("CREATE TABLE IF NOT EXISTS geneset (key TEXT, value INTEGER)"); | |
}); | |
}) | |
restapi.get('/list', function(req, res){ | |
var a=[] | |
var d=req.query.db || "test.db" | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
db[d].each("SELECT id,genesetid,groupid FROM geneset", function(err, row){ | |
a.push(row); | |
}, | |
function() { | |
res.json(a) | |
} | |
) | |
}); | |
restapi.get('/data', function(req, res){ | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
var d=req.query.db || "test.db" | |
db[d].get("SELECT id,genesetid,groupid,geneset,description FROM geneset where id=?",req.query.id, function(err, row){ | |
res.json(row); | |
} | |
) | |
}); | |
restapi.post('/data', function(req, res){ | |
var body="" | |
var d=req.query.db || "test.db" | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
req.on("data",function(data) { | |
body+=data; | |
}) | |
req.on("end",function() { | |
var post = qs.parse(body); | |
var geneset = post["geneset[]"].join(","); | |
var groupid=post.groupid || ""; | |
db[d].run("INSERT INTO geneset(genesetid,groupid,geneset,author,description) VALUES (?,?,?,?,?)", [post.genesetid,groupid,geneset,post.author,post.description] , function(err, row){ | |
if (err){ | |
res.status(500); | |
} | |
else { | |
res.status(202); | |
} | |
res.end(); | |
}) | |
}) | |
}); | |
/* | |
restapi.delete('/data', function(req,res) { | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
console.log("deleting"); | |
res.send("aha insight"); | |
}) | |
*/ | |
restapi.get('/delete', function(req,res) { | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
var d=req.query.db || "test.db" | |
db[d].run("DELETE FROM geneset WHERE id=?",req.query.id,function(err){},function(){ | |
var a={"status":req.query.id+" deleted"} | |
res.json(a); | |
}); | |
}) | |
restapi.post('/put', function(req, res){ | |
var body="" | |
var d=req.query.db || "test.db" | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
req.on("data",function(data) { | |
body+=data; | |
}) | |
req.on("end",function() { | |
var post = qs.parse(body); | |
var geneset = post["geneset[]"].join(","); | |
var groupid=post.groupid || ""; | |
db[d].run('update geneset set genesetid=?,groupid=?,description=?,geneset=? where id=?',[post.genesetid,groupid,post.description,geneset,post.id], function(err, row){ | |
if (err){ | |
console.err(err); | |
res.status(500); | |
} | |
else { | |
res.status(202); | |
} | |
res.end(); | |
}) | |
}) | |
}); | |
/********** SECTION FOR MOTIFS TFS POS *******/ | |
var PosDb = new sqlite3.Database("data/hg19_one_tr_per_gene.motifs.tf.sqlite3.db") | |
function isEnriched(d) { | |
if(enrichedMotifs[d[0]]) {return true} | |
else {return false} | |
} | |
restapi.get('/hg19_cp_motifs', function(req,res) { | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
PosDb.get("SELECT * FROM tbl WHERE gene=?",req.query.gene,function(err,row){ | |
if(err) { | |
res.status(500); | |
} else { | |
var a={}; | |
if(row) { | |
a["gene"]=row.gene | |
if (req.query.enriched) { | |
a["motifs"]=JSON.parse(row.motifs.replace(/'/gi,"\"")).filter(isEnriched) | |
} else { | |
a["motifs"]=JSON.parse(row.motifs.replace(/'/gi,"\"")) | |
} | |
a["TFs"]=JSON.parse(row.TFs.replace(/'/gi,"\"")) | |
} | |
res.json(a); | |
} | |
}); | |
}) | |
var PosDb2 = new sqlite3.Database("data/mm10_one_tr_per_gene.motifs.sqlite3.db") | |
restapi.get('/mm10_cp_motifs', function(req,res) { | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
PosDb2.get("SELECT * FROM tbl WHERE gene=?",req.query.gene,function(err,row){ | |
if(err) { | |
res.status(500); | |
} else{ | |
var a={}; | |
if(row) { | |
a["gene"]=row.gene | |
if (req.query.enriched) { | |
a["motifs"]=JSON.parse(row.motifs.replace(/'/gi,"\"")).filter(isEnriched) | |
} else { | |
a["motifs"]=JSON.parse(row.motifs.replace(/'/gi,"\"")) | |
} | |
} | |
res.json(a); | |
} | |
}); | |
}) | |
/** Section for homologene core promoter bl2seq result*/ | |
var CP_bl2seq = new sqlite3.Database("data/CP.bl2seq.sqlite.db") | |
restapi.get('/bl2seq_r',function(req,res) { | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
var a=[]; | |
CP_bl2seq.each("SELECT * FROM bl2seq_r WHERE query_id=?",req.query.gene, function(err,row) { | |
if(err) { | |
res.status(500); | |
} else { | |
a.push(row) | |
} | |
},function() { | |
res.json(a) | |
}) | |
}) | |
/** Section for Repeats **/ | |
var hg19RepeatDb = new sqlite3.Database("data/hg19_corepromoter_repeats.db") | |
restapi.get('/hg19_cp_repeats', function(req,res) { | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
hg19RepeatDb.get("SELECT * FROM tbl WHERE gene=?",req.query.gene,function(err,row){ | |
if(err) { | |
res.status(500); | |
} else { | |
var a={}; | |
if(row) { | |
a["gene"]=row.gene | |
a["repeats"]=JSON.parse(row.repeats.replace(/'/gi,"\"")) | |
} | |
res.json(a); | |
} | |
}); | |
}) | |
var mm10RepeatDb = new sqlite3.Database("data/mm10_corepromoter_repeats.db") | |
restapi.get('/mm10_cp_repeats', function(req,res) { | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
mm10RepeatDb.get("SELECT * FROM tbl WHERE gene=?",req.query.gene,function(err,row){ | |
if(err) { | |
res.status(500); | |
} else { | |
var a={}; | |
if(row){ | |
a["gene"]=row.gene | |
a["repeats"]=JSON.parse(row.repeats.replace(/'/gi,"\"")) | |
} | |
res.json(a); | |
} | |
}); | |
}) | |
restapi.get('/homolo_motifs',function(req,res) { | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
motifWeb.homoloMotifs(req.query.gene,function(d){ | |
res.json(d); | |
}) | |
}) | |
restapi.listen(8081); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment