From 30a9a40ae3170f0a13efd394ac12e297d3eda03d Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Wed, 8 May 2019 06:01:49 -0500 Subject: rename to ratspub --- gatpub.py | 136 ----------------------------------------------- ratspub.py | 136 +++++++++++++++++++++++++++++++++++++++++++++++ server.py | 43 +++++++-------- templates/cytoscape.html | 46 ++++++++++++++++ templates/layout.html | 8 +-- templates/network.html | 46 ---------------- 6 files changed, 204 insertions(+), 211 deletions(-) delete mode 100755 gatpub.py create mode 100755 ratspub.py create mode 100644 templates/cytoscape.html delete mode 100644 templates/network.html diff --git a/gatpub.py b/gatpub.py deleted file mode 100755 index c853fd2..0000000 --- a/gatpub.py +++ /dev/null @@ -1,136 +0,0 @@ -#!/bin/env python3 -from nltk.tokenize import sent_tokenize -import os -import re -import codecs -import sys - -#gene=sys.argv[1] - -## turn dictionary (synonyms) to regular expression -def undic(dic): - return "|".join(dic.values()) - -def findWholeWord(w): - return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search - -def getSentences(query, gene): - abstracts = os.popen("esearch -db pubmed -query " + query + " | efetch -format uid |fetch-pubmed -path /run/media/hao/PubMed/Archive/ | xtract -pattern PubmedArticle -element MedlineCitation/PMID,ArticleTitle,AbstractText").read() - out=str() - for row in abstracts.split("\n"): - tiab=row.split("\t") - pmid = tiab.pop(0) - tiab= " ".join(tiab) - sentences = sent_tokenize(tiab) - ## keep the sentence only if it contains the gene - for sent in sentences: - if findWholeWord(gene)(sent): - sent=re.sub(r'\b(%s)\b' % gene, r'\1', sent, flags=re.I) - out+=pmid+"\t"+sent+"
\n" - return(out) - -def gene_addiction(gene): - # search gene name & drug name in the context of addiction terms (i.e., exclude etoh affects cancer, or methods to extract cocaine) - q="\"(" + addiction.replace("|", " OR ") + ") AND (" + drug.replace("|", " OR ", ) + ") AND " + gene + "\"" - sents=getSentences(q, gene) - out=str() - for sent in sents.split("\n"): - for drug0 in drug_d: - if findWholeWord(drug_d[drug0])(sent) : - sent=re.sub(r'\b(%s)\b' % drug_d[drug0], r'\1', sent, flags=re.I) - out+=gene+"\t"+"drug\t" + drug0+"\t"+sent+"\n" - for add0 in addiction_d: - if findWholeWord(add0)(sent) : - sent=re.sub(r'\b(%s)\b' % add0, r'\1', sent, flags=re.I) - out+=gene+"\t"+"addiction\t"+add0+"\t"+sent+"\n" - return(out) - -def gene_anatomical(gene): - q="\"(" + brain.replace("|", " OR ") + ") AND " + gene + "\"" - sents=getSentences(q,gene) - out=str() - for sent in sents.split("\n"): - for brain0 in brain_d: - if findWholeWord(brain_d[brain0])(sent) : - sent=re.sub(r'\b(%s)\b' % brain_d[brain0], r'\1', sent, flags=re.I) - out+=gene+"\t"+"brain\t"+brain0+"\t"+sent+"\n" - return(out) - -def gene_functional(gene): - q="\"(" + function.replace("|", " OR ") + ") AND " + gene + "\"" - sents=getSentences(q,gene) - out=str() - for sent in sents.split("\n"): - for bio0 in function_d: - if findWholeWord(function_d[bio0])(sent) : - sent=re.sub(r'\b(%s)\b' % function_d[bio0], r'\1', sent, flags=re.I) - out+=gene+"\t"+"function\t"+bio0+"\t"+sent+"\n" - return(out) - -def generate_nodes(nodes_d, nodecolor): - # include all search terms even if there are no edges, just to show negative result - json0 =str() #"{ data: { id: '" + gene + "'} },\n" - for node in nodes_d: - json0 += "{ data: { id: '" + node + "', nodecolor: '" + nodecolor + "' } },\n" - return(json0) - -def generate_edges(data): - json0=str() - edgeCnts={} - for line in data.split("\n"): - if len(line.strip())!=0: - (source, cat, target, pmid, sent) = line.split("\t") - edgeID=source+"|"+target - if edgeID in edgeCnts: - edgeCnts[edgeID]+=1 - else: - edgeCnts[edgeID]=1 - for edgeID in edgeCnts: - (source,target)=edgeID.split("|") - json0+="{ data: { id: \'" + edgeID + "\', source: \'" + source + "\', target: '" + target + "\', sentCnt: '" + str(edgeCnts[edgeID]) + "' } },\n" - return(json0) - - - -addiction_d = {"reward":"reward|reinforcement|conditioned place preference|CPP|self-administration|self-administered", - "aversion":"aversion|aversive|CTA|withdrawal", - "relapse":"relapse|reinstatement|craving|drug seeking", - "sensitization":"sensitization", - "addiction":"addiction|drug abuse", - "intoxication":"intoxication|binge" - } -addiction=undic(addiction_d) - -drug_d = {"alcohol":"alcohol|alcoholism", - "nicotine":"smoking|nicotine|tobacco", - "amphetamine":"methamphetamine|amphetamine|METH", - "cocaine":"cocaine", - "opioid":"opioid|fentanyl|oxycodone|oxycontin|heroin|morphine", - "cannabinoid":"marijuana|cannabinoid|tetrahydrocannabinol|thc|thc-9" - } -drug=undic(drug_d) - -brain_d ={"cortex":"cortex|pfc|vmpfc|il|pl|prelimbic|infralimbic", - "striatum":"striatum|STR", - "accumbens":"shell|core|NAcc|acbs|acbc", - "hippocampus":"hippocampus|hipp|hip|ca1|ca3|dentate|gyrus", - "amygadala":"amygadala|cea|bla|amy", - "vta":"ventral tegmental|vta|pvta" - } -# brain region has too many short acronyms to just use the undic function, so search PubMed using the following -brain="cortex|accumbens|striatum|amygadala|hippocampus|tegmental|mesolimbic|infralimbic|prelimbic" - -function_d={"plasticity":"LTP|LTD|plasticity|synaptic|epsp|epsc", - "neurotransmission": "neurotransmission|glutamate|GABA|cholinergic|serotoninergic", - "signalling":"signalling|phosphorylation|glycosylation", -# "regulation":"increased|decreased|regulated|inhibited|stimulated", - "transcription":"transcription|methylation|histone|ribosome", - } -function=undic(function_d) - -#https://htmlcolorcodes.com/ -n0=generate_nodes(function_d, "#D7BDE2") -n1=generate_nodes(addiction_d,"#A9CCE3") -n2=generate_nodes(drug_d, "#A3E4D7") -n3=generate_nodes(brain_d, "#F9E79F") -default_nodes=n0+n1+n2+n3 diff --git a/ratspub.py b/ratspub.py new file mode 100755 index 0000000..c853fd2 --- /dev/null +++ b/ratspub.py @@ -0,0 +1,136 @@ +#!/bin/env python3 +from nltk.tokenize import sent_tokenize +import os +import re +import codecs +import sys + +#gene=sys.argv[1] + +## turn dictionary (synonyms) to regular expression +def undic(dic): + return "|".join(dic.values()) + +def findWholeWord(w): + return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search + +def getSentences(query, gene): + abstracts = os.popen("esearch -db pubmed -query " + query + " | efetch -format uid |fetch-pubmed -path /run/media/hao/PubMed/Archive/ | xtract -pattern PubmedArticle -element MedlineCitation/PMID,ArticleTitle,AbstractText").read() + out=str() + for row in abstracts.split("\n"): + tiab=row.split("\t") + pmid = tiab.pop(0) + tiab= " ".join(tiab) + sentences = sent_tokenize(tiab) + ## keep the sentence only if it contains the gene + for sent in sentences: + if findWholeWord(gene)(sent): + sent=re.sub(r'\b(%s)\b' % gene, r'\1', sent, flags=re.I) + out+=pmid+"\t"+sent+"
\n" + return(out) + +def gene_addiction(gene): + # search gene name & drug name in the context of addiction terms (i.e., exclude etoh affects cancer, or methods to extract cocaine) + q="\"(" + addiction.replace("|", " OR ") + ") AND (" + drug.replace("|", " OR ", ) + ") AND " + gene + "\"" + sents=getSentences(q, gene) + out=str() + for sent in sents.split("\n"): + for drug0 in drug_d: + if findWholeWord(drug_d[drug0])(sent) : + sent=re.sub(r'\b(%s)\b' % drug_d[drug0], r'\1', sent, flags=re.I) + out+=gene+"\t"+"drug\t" + drug0+"\t"+sent+"\n" + for add0 in addiction_d: + if findWholeWord(add0)(sent) : + sent=re.sub(r'\b(%s)\b' % add0, r'\1', sent, flags=re.I) + out+=gene+"\t"+"addiction\t"+add0+"\t"+sent+"\n" + return(out) + +def gene_anatomical(gene): + q="\"(" + brain.replace("|", " OR ") + ") AND " + gene + "\"" + sents=getSentences(q,gene) + out=str() + for sent in sents.split("\n"): + for brain0 in brain_d: + if findWholeWord(brain_d[brain0])(sent) : + sent=re.sub(r'\b(%s)\b' % brain_d[brain0], r'\1', sent, flags=re.I) + out+=gene+"\t"+"brain\t"+brain0+"\t"+sent+"\n" + return(out) + +def gene_functional(gene): + q="\"(" + function.replace("|", " OR ") + ") AND " + gene + "\"" + sents=getSentences(q,gene) + out=str() + for sent in sents.split("\n"): + for bio0 in function_d: + if findWholeWord(function_d[bio0])(sent) : + sent=re.sub(r'\b(%s)\b' % function_d[bio0], r'\1', sent, flags=re.I) + out+=gene+"\t"+"function\t"+bio0+"\t"+sent+"\n" + return(out) + +def generate_nodes(nodes_d, nodecolor): + # include all search terms even if there are no edges, just to show negative result + json0 =str() #"{ data: { id: '" + gene + "'} },\n" + for node in nodes_d: + json0 += "{ data: { id: '" + node + "', nodecolor: '" + nodecolor + "' } },\n" + return(json0) + +def generate_edges(data): + json0=str() + edgeCnts={} + for line in data.split("\n"): + if len(line.strip())!=0: + (source, cat, target, pmid, sent) = line.split("\t") + edgeID=source+"|"+target + if edgeID in edgeCnts: + edgeCnts[edgeID]+=1 + else: + edgeCnts[edgeID]=1 + for edgeID in edgeCnts: + (source,target)=edgeID.split("|") + json0+="{ data: { id: \'" + edgeID + "\', source: \'" + source + "\', target: '" + target + "\', sentCnt: '" + str(edgeCnts[edgeID]) + "' } },\n" + return(json0) + + + +addiction_d = {"reward":"reward|reinforcement|conditioned place preference|CPP|self-administration|self-administered", + "aversion":"aversion|aversive|CTA|withdrawal", + "relapse":"relapse|reinstatement|craving|drug seeking", + "sensitization":"sensitization", + "addiction":"addiction|drug abuse", + "intoxication":"intoxication|binge" + } +addiction=undic(addiction_d) + +drug_d = {"alcohol":"alcohol|alcoholism", + "nicotine":"smoking|nicotine|tobacco", + "amphetamine":"methamphetamine|amphetamine|METH", + "cocaine":"cocaine", + "opioid":"opioid|fentanyl|oxycodone|oxycontin|heroin|morphine", + "cannabinoid":"marijuana|cannabinoid|tetrahydrocannabinol|thc|thc-9" + } +drug=undic(drug_d) + +brain_d ={"cortex":"cortex|pfc|vmpfc|il|pl|prelimbic|infralimbic", + "striatum":"striatum|STR", + "accumbens":"shell|core|NAcc|acbs|acbc", + "hippocampus":"hippocampus|hipp|hip|ca1|ca3|dentate|gyrus", + "amygadala":"amygadala|cea|bla|amy", + "vta":"ventral tegmental|vta|pvta" + } +# brain region has too many short acronyms to just use the undic function, so search PubMed using the following +brain="cortex|accumbens|striatum|amygadala|hippocampus|tegmental|mesolimbic|infralimbic|prelimbic" + +function_d={"plasticity":"LTP|LTD|plasticity|synaptic|epsp|epsc", + "neurotransmission": "neurotransmission|glutamate|GABA|cholinergic|serotoninergic", + "signalling":"signalling|phosphorylation|glycosylation", +# "regulation":"increased|decreased|regulated|inhibited|stimulated", + "transcription":"transcription|methylation|histone|ribosome", + } +function=undic(function_d) + +#https://htmlcolorcodes.com/ +n0=generate_nodes(function_d, "#D7BDE2") +n1=generate_nodes(addiction_d,"#A9CCE3") +n2=generate_nodes(drug_d, "#A3E4D7") +n3=generate_nodes(brain_d, "#F9E79F") +default_nodes=n0+n1+n2+n3 diff --git a/server.py b/server.py index d65625e..6a28e0b 100644 --- a/server.py +++ b/server.py @@ -1,6 +1,6 @@ from flask import Flask, render_template, request, redirect import simplejson as json -from gatpub import * +from ratspub import * app=Flask(__name__) app.config['SECRET_KEY'] = '#DtfrL98G5t1dC*4' @@ -13,27 +13,24 @@ def root(): def home(): return render_template('index.html') -@app.route("/network", methods=['GET', 'POST']) -def network(): - edges_list=[] - nodes_list=[] - if request.method == 'POST': - term = request.form - genes=term['query'] - genes=genes.replace(",", " ") - genes=genes.replace(";", " ") - genes=genes.split() - nodes=default_nodes - edges=str() - for gene in genes: - nodes+="{ data: { id: '" + gene + "', nodecolor:'#FADBD8', fontweight:700} },\n" - tmp0=gene_addiction(gene) - e0=generate_edges(tmp0) - tmp1=gene_functional(gene) - e1=generate_edges(tmp1) - tmp2=gene_anatomical(gene) - e2=generate_edges(tmp2) - edges+=e0+e1+e2 - return render_template('network.html', elements=nodes+edges) +@app.route("/search") +def search(): + genes=request.args.get('query') + genes=genes.replace(",", " ") + genes=genes.replace(";", " ") + genes=genes.split() + nodes=default_nodes + edges=str() + for gene in genes: + nodes+="{ data: { id: '" + gene + "', nodecolor:'#FADBD8', fontweight:700} },\n" + tmp0=gene_addiction(gene) + e0=generate_edges(tmp0) + tmp1=gene_functional(gene) + e1=generate_edges(tmp1) + tmp2=gene_anatomical(gene) + e2=generate_edges(tmp2) + edges+=e0+e1+e2 + return render_template('cytoscape.html', elements=nodes+edges) + if __name__ == '__main__': app.run(debug=True) diff --git a/templates/cytoscape.html b/templates/cytoscape.html new file mode 100644 index 0000000..24c18c1 --- /dev/null +++ b/templates/cytoscape.html @@ -0,0 +1,46 @@ +{% extends "layout.html" %} +{% block content %} + + + + +
+ + +{% endblock %} diff --git a/templates/layout.html b/templates/layout.html index 1a81c1a..2887e2f 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -10,7 +10,7 @@ - GatPub: Gene Addictionness through PubMed + RatsPub: Relationship with Addiction Through Searches of PubMed @@ -23,10 +23,7 @@