diff options
author | hakangunturkun | 2020-09-07 22:35:17 -0500 |
---|---|---|
committer | hakangunturkun | 2020-09-07 22:35:17 -0500 |
commit | a637889f13c2151303998e411dc81f3196a974f6 (patch) | |
tree | f593bb635e759889109504405d2abc3e31af9008 /server.py | |
parent | 35179dec9aa1926102ad2ddbbd5b8ad2882be92e (diff) | |
download | genecup-a637889f13c2151303998e411dc81f3196a974f6.tar.gz |
last version
Diffstat (limited to 'server.py')
-rwxr-xr-x | server.py | 658 |
1 files changed, 405 insertions, 253 deletions
@@ -6,6 +6,7 @@ import shutil from flask import jsonify from datetime import datetime import bcrypt +import hashlib import tempfile import random import string @@ -23,21 +24,22 @@ from collections import Counter import numpy as np from numpy import array import tensorflow -import keras -from keras.models import Model -from keras.preprocessing.text import Tokenizer -from keras.preprocessing.sequence import pad_sequences -from keras.layers import * -from keras.models import Sequential -from keras.layers import Dense -from keras.layers import Flatten -from keras.layers import Embedding -from keras import metrics -from keras import optimizers +import tensorflow.keras +from tensorflow.keras.models import Model +from tensorflow.keras.preprocessing.text import Tokenizer +from tensorflow.keras.preprocessing.sequence import pad_sequences +from tensorflow.keras.layers import * +from tensorflow.keras.models import Sequential +from tensorflow.keras.layers import Dense +from tensorflow.keras.layers import Flatten +from tensorflow.keras.layers import Embedding +from tensorflow.keras import metrics +from tensorflow.keras import optimizers import pickle app=Flask(__name__) -datadir="/export/ratspub/" +#datadir="/export/ratspub/" +datadir = "." app.config['SECRET_KEY'] = '#DtfrL98G5t1dC*4' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+datadir+'userspub.sqlite' db = SQLAlchemy(app) @@ -72,6 +74,12 @@ with open('./nlp/tokenizer.pickle', 'rb') as handle: with open('./nlp/vocabulary.txt', 'r') as vocab: vocab = vocab.read() +def tf_auc_score(y_true, y_pred): + return tensorflow.metrics.auc(y_true, y_pred)[1] + +from tensorflow.keras import backend as K +K.clear_session() + # create the CNN model def create_model(vocab_size, max_length): model = Sequential() @@ -81,8 +89,8 @@ def create_model(vocab_size, max_length): model.add(Flatten()) model.add(Dense(10, activation='relu')) model.add(Dense(1, activation='sigmoid')) - opt = keras.optimizers.Adamax(learning_rate=0.002, beta_1=0.9, beta_2=0.999) - model.compile(loss='binary_crossentropy', optimizer=opt, metrics=[keras.metrics.AUC()]) + opt = tensorflow.keras.optimizers.Adamax(lr=0.002, beta_1=0.9, beta_2=0.999) + model.compile(loss='binary_crossentropy', optimizer=opt, metrics=[tf_auc_score]) return model @app.route("/") @@ -98,6 +106,8 @@ def login(): found_user = users.query.filter_by(email=email).first() if (found_user and (bcrypt.checkpw(password.encode('utf8'), found_user.password))): session['email'] = found_user.email + print(bcrypt.hashpw(session['email'].encode('utf8'), bcrypt.gensalt())) + session['hashed_email'] = hashlib.md5(session['email'] .encode('utf-8')).hexdigest() session['name'] = found_user.name session['id'] = found_user.id else: @@ -117,11 +127,13 @@ def signup(): flash("Already registered, but wrong password!", "loginout") return render_template('signup.html') session['email'] = email + session['hashed_email'] = hashlib.md5(session['email'] .encode('utf-8')).hexdigest() session['name'] = name password = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt()) user = users(name=name, email=email, password = password) if found_user: session['email'] = found_user.email + session['hashed_email'] = hashlib.md5(session['email'] .encode('utf-8')).hexdigest() session['id'] = found_user.id found_user.name = name db.session.commit() @@ -147,6 +159,7 @@ def signin(): found_user = users.query.filter_by(email=email).first() if (found_user and (bcrypt.checkpw(password.encode('utf8'), found_user.password))): session['email'] = found_user.email + session['hashed_email'] = hashlib.md5(session['email'] .encode('utf-8')).hexdigest() session['name'] = found_user.name session['id'] = found_user.id flash("Login Succesful!", "loginout") @@ -168,6 +181,7 @@ def profile(nm_passwd): if request.method == "POST": password = request.form['password'] session['email'] = found_user.email + session['hashed_email'] = hashlib.md5(session['email'] .encode('utf-8')).hexdigest() session['name'] = found_user.name session['id'] = found_user.id password = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt()) @@ -220,22 +234,22 @@ def progress(): genes=genes.replace(",", " ") genes=genes.replace(";", " ") genes=re.sub(r'\bLOC\d*?\b', "", genes, flags=re.I) - genes=genes.split() - if len(genes)>=100: - message="<span class='text-danger'>Up to 100 terms can be searched at a time</span>" + genes1 = [f[1:-1] for f in re.findall('".+?"', genes)] + genes2 = [p for p in re.findall(r'([^""]+)',genes) if p not in genes1] + genes2_str = ''.join(genes2) + genes2 = genes2_str.split() + genes3 = genes1 + genes2 + genes = [re.sub("\s+", '-', s) for s in genes3] + + if len(genes)>=30: + message="<span class='text-danger'>Up to 30 terms can be searched at a time</span>" return render_template('index.html', message=message) elif len(genes)==0: message="<span class='text-danger'>Please enter a search term </span>" return render_template('index.html', message=message) tf_path=tempfile.gettempdir() - session['path']=tf_path+"/tmp" + ''.join(random.choice(string.ascii_letters) for x in range(6)) - # put the query in session cookie - session['query']=genes - return render_template('progress.html', url_in="search", url_out="cytoscape") + #tf_path = "/tmp/" -@app.route("/search") -def search(): - genes=session['query'] genes_for_folder_name ="" if len(genes) == 1: marker = "" @@ -254,25 +268,53 @@ def search(): timestamp = datetime.utcnow().replace(microsecond=0) timestamp = timestamp.replace(tzinfo=pytz.utc) timestamp = timestamp.astimezone(pytz.timezone("America/Chicago")) - session['timestamp'] = timestamp timeextension = str(timestamp) timeextension = timeextension.replace(':', '_') timeextension = timeextension.replace('-', '_') timeextension = timeextension.replace(' ', '_') timeextension = timeextension.replace('_06_00', '') + session['timeextension'] = timeextension user_login=0 #create a folder for the search if ('email' in session): - user_login=1 - os.makedirs(datadir+"user/"+str(session['email']+"/"+timeextension+"_0_"+genes_for_folder_name+marker),exist_ok=True) - session['user_folder'] = datadir+"user/"+str(session['email']) - user_folder=session['user_folder'] - session['path'] = datadir+"user/"+str(session['email'])+"/"+timeextension+"_0_"+genes_for_folder_name+marker+"/"+timeextension + os.makedirs(datadir + "/user/"+str(session['hashed_email'])+"/"+str(timeextension)+"_0_"+genes_for_folder_name+marker,exist_ok=True) + session['user_folder'] = datadir+"/user/"+str(session['hashed_email']) + session['path_user'] = datadir+"/user/"+str(session['hashed_email'])+"/"+str(timeextension)+"_0_"+genes_for_folder_name+marker+"/" + session['rnd'] = timeextension+"_0_"+genes_for_folder_name+marker + rnd = session['rnd'] + else: + rnd = "tmp" + ''.join(random.choice(string.ascii_letters) for x in range(6)) + session['path']=tf_path+ "/" + rnd + #os.makedirs(datadir+ session['path']) + os.makedirs(session['path']) + + genes_session = '' + for gen in genes: + genes_session += str(gen) + "_" + genes_session = genes_session[:-1] + session['query']=genes + + return render_template('progress.html', url_in="search", url_out="cytoscape?rnd="+rnd+"&genequery="+genes_session) + +@app.route("/search") +def search(): + genes=session['query'] + timeextension=session['timeextension'] percent=round(100/(len(genes)*6),1) # 6 categories - snt_file=session['path']+"_snt" - cysdata=open(session['path']+"_cy","w+") + if ('email' in session): + sessionpath = session['path_user'] + timeextension + path_user=session['path_user'] + user_login=1 + else: + user_login=0 + sessionpath = session['path'] + #path_user=datadir+session['path']+"/" + path_user=session['path']+"/" + + snt_file=sessionpath+"_snt" + cysdata=open(sessionpath+"_cy","w+") sntdata=open(snt_file,"w+") - zeroLinkNode=open(session['path']+"_0link","w+") + zeroLinkNode=open(sessionpath+"_0link","w+") search_type = session['search_type'] #consider the types got from checkbox temp_nodes = "" @@ -300,143 +342,164 @@ def search(): json_nodes += nj6 json_nodes = json_nodes[:-2] json_nodes =json_nodes+"]}" + def generate(genes, tf_name): - sentences=str() - edges=str() - nodes = temp_nodes - progress=0 - searchCnt=0 - nodesToHide=str() - json_edges = str() - for gene in genes: - gene=gene.replace("-"," ") - # report progress immediately - progress+=percent - yield "data:"+str(progress)+"\n\n" - #addiction terms must present with at least one drug - addiction=undic(addiction_d) +") AND ("+undic(drug_d) - sent0=gene_category(gene, addiction_d, addiction, "addiction") - e0=generate_edges(sent0, tf_name) - ej0=generate_edges_json(sent0, tf_name) - # drug - drug=undic(drug_d) - sent1=gene_category(gene, drug_d, drug, "drug") - progress+=percent - yield "data:"+str(progress)+"\n\n" - e1=generate_edges(sent1, tf_name) - ej1=generate_edges_json(sent1, tf_name) - # function - function=undic(function_d) - sent2=gene_category(gene, function_d, function, "function") - progress+=percent - yield "data:"+str(progress)+"\n\n" - e2=generate_edges(sent2, tf_name) - ej2=generate_edges_json(sent2, tf_name) - # brain has its own query terms that does not include the many short acronyms - sent3=gene_category(gene, brain_d, brain_query_term, "brain") - progress+=percent - e3=generate_edges(sent3, tf_name) - ej3=generate_edges_json(sent3, tf_name) - # stress - stress=undic(stress_d) - sent4=gene_category(gene, stress_d, stress, "stress") - progress+=percent - yield "data:"+str(progress)+"\n\n" - e4=generate_edges(sent4, tf_name) - ej4=generate_edges_json(sent4, tf_name) - # psychiatric - psychiatric=undic(psychiatric_d) - sent5=gene_category(gene, psychiatric_d, psychiatric, "psychiatric") - progress+=percent - yield "data:"+str(progress)+"\n\n" - e5=generate_edges(sent5, tf_name) - ej5=generate_edges_json(sent5, tf_name) - # GWAS - e6=searchArchived('GWAS', gene, 'cys') - ej6=searchArchived('GWAS', gene , 'json') - #consider the types got from checkbox - geneEdges = "" - if ("addiction" in search_type): - geneEdges += e0 - json_edges += ej0 - if ("drug" in search_type): - geneEdges += e1 - json_edges += ej1 - if ("function" in search_type): - geneEdges += e2 - json_edges += ej2 - if ("brain" in search_type): - geneEdges += e3 - json_edges += ej3 - if ("stress" in search_type): - geneEdges += e4 - json_edges += ej4 - if ("psychiatric" in search_type): - geneEdges += e5 - json_edges += ej5 - if ("GWAS" in search_type): - geneEdges += e6 - json_edges += ej6 - ## there is a bug here. zero link notes are not excluded anymore - if len(geneEdges) >1: - edges+=geneEdges - nodes+="{ data: { id: '" + gene + "', nodecolor:'#E74C3C', fontweight:700, url:'/startGeneGene?forTopGene="+gene+"'} },\n" - else: - nodesToHide+=gene + " " - sentences+=sent0+sent1+sent2+sent3+sent4+sent5 - sent0=None - sent1=None - sent2=None - sent3=None - sent4=None - sent5=None - #save data before the last yield - searchCnt+=1 - if (searchCnt==len(genes)): - progress=100 - sntdata.write(sentences) - sntdata.close() - cysdata.write(nodes+edges) - cysdata.close() - zeroLinkNode.write(nodesToHide) - zeroLinkNode.close() - yield "data:"+str(progress)+"\n\n" - #edges in json format - json_edges="{\"data\":["+json_edges - json_edges = json_edges[:-2] - json_edges =json_edges+"]}" - #write edges to txt file in json format - with open(datadir+"edges.json", 'w') as edgesjson: - edgesjson.write(json_edges) - #write edges to txt file in json format also in user folder - if (user_login == 1): - with open(user_folder+"/"+timeextension+"_0_"+genes_for_folder_name+marker+"/edges.json", "w") as temp_file_edges: - temp_file_edges.write(json_edges) - #write nodes to txt file in json format - with open(datadir+"nodes.json", 'w') as nodesjson: - #if (userlogin) == 1: - nodesjson.write(json_nodes) - #write nodes to txt file in json format also in user folder - if ('email' in session): - with open(datadir+"user/"+str(session['email'])+"/"+timeextension+"_0_"+genes_for_folder_name+marker+"/nodes.json", "w") as temp_file_nodes: - temp_file_nodes.write(json_nodes) + with app.test_request_context(): + sentences=str() + edges=str() + nodes = temp_nodes + progress=0 + searchCnt=0 + nodesToHide=str() + json_edges = str() + for gene in genes: + gene=gene.replace("-"," ") + # report progress immediately + progress+=percent + yield "data:"+str(progress)+"\n\n" + #addiction terms must present with at least one drug + addiction=undic(addiction_d) +") AND ("+undic(drug_d) + sent0=gene_category(gene, addiction_d, addiction, "addiction") + e0=generate_edges(sent0, tf_name) + ej0=generate_edges_json(sent0, tf_name) + # drug + drug=undic(drug_d) + sent1=gene_category(gene, drug_d, drug, "drug") + progress+=percent + yield "data:"+str(progress)+"\n\n" + e1=generate_edges(sent1, tf_name) + ej1=generate_edges_json(sent1, tf_name) + # function + function=undic(function_d) + sent2=gene_category(gene, function_d, function, "function") + progress+=percent + yield "data:"+str(progress)+"\n\n" + e2=generate_edges(sent2, tf_name) + ej2=generate_edges_json(sent2, tf_name) + # brain has its own query terms that does not include the many short acronyms + sent3=gene_category(gene, brain_d, brain_query_term, "brain") + progress+=percent + e3=generate_edges(sent3, tf_name) + ej3=generate_edges_json(sent3, tf_name) + # stress + stress=undic(stress_d) + sent4=gene_category(gene, stress_d, stress, "stress") + progress+=percent + yield "data:"+str(progress)+"\n\n" + e4=generate_edges(sent4, tf_name) + ej4=generate_edges_json(sent4, tf_name) + # psychiatric + psychiatric=undic(psychiatric_d) + sent5=gene_category(gene, psychiatric_d, psychiatric, "psychiatric") + progress+=percent + yield "data:"+str(progress)+"\n\n" + e5=generate_edges(sent5, tf_name) + ej5=generate_edges_json(sent5, tf_name) + # GWAS + e6=searchArchived('GWAS', gene, 'cys') + ej6=searchArchived('GWAS', gene , 'json') + #consider the types got from checkbox + geneEdges = "" + if ("addiction" in search_type): + geneEdges += e0 + json_edges += ej0 + if ("drug" in search_type): + geneEdges += e1 + json_edges += ej1 + if ("function" in search_type): + geneEdges += e2 + json_edges += ej2 + if ("brain" in search_type): + geneEdges += e3 + json_edges += ej3 + if ("stress" in search_type): + geneEdges += e4 + json_edges += ej4 + if ("psychiatric" in search_type): + geneEdges += e5 + json_edges += ej5 + if ("GWAS" in search_type): + geneEdges += e6 + json_edges += ej6 + if len(geneEdges) >1: + edges+=geneEdges + nodes+="{ data: { id: '" + gene + "', nodecolor:'#E74C3C', fontweight:700, url:'/synonyms?node="+gene+"'} },\n" + else: + nodesToHide+=gene + " " + sentences+=sent0+sent1+sent2+sent3+sent4+sent5 + sent0=None + sent1=None + sent2=None + sent3=None + sent4=None + sent5=None + #save data before the last yield + searchCnt+=1 + if (searchCnt==len(genes)): + progress=100 + sntdata.write(sentences) + sntdata.close() + cysdata.write(nodes+edges) + cysdata.close() + zeroLinkNode.write(nodesToHide) + zeroLinkNode.close() + yield "data:"+str(progress)+"\n\n" + #edges in json format + json_edges="{\"data\":["+json_edges + json_edges = json_edges[:-2] + json_edges =json_edges+"]}" + #write edges to txt file in json format also in user folder + with open(path_user+"edges.json", "w") as temp_file_edges: + temp_file_edges.write(json_edges) + with open(path_user+"nodes.json", "w") as temp_file_nodes: + temp_file_nodes.write(json_nodes) return Response(generate(genes, snt_file), mimetype='text/event-stream') -@app.route("/tableview") +@app.route("/tableview/") def tableview(): - with open(datadir+"nodes.json") as jsonfile: - jnodes = json.load(jsonfile) - jedges ='' - file_edges = open(datadir+'edges.json', 'r') - for line in file_edges.readlines(): - if ':' not in line: - nodata_temp = 1 - else: - nodata_temp = 0 - with open(datadir+"edges.json") as edgesjsonfile: - jedges = json.load(edgesjsonfile) - break - genename=session['query'] + genes_url=request.args.get('genequery') + rnd_url=request.args.get('rnd') + tf_path=tempfile.gettempdir() + if ('email' in session): + filename = rnd_url.split("_0_")[0] + genes_session_tmp = datadir+"/user/"+str(session['hashed_email'])+"/"+rnd_url+"/"+filename + gene_url_tmp = "/user/"+str(session['hashed_email'])+"/"+rnd_url + try: + with open(datadir+gene_url_tmp+"/nodes.json") as jsonfile: + jnodes = json.load(jsonfile) + except FileNotFoundError: + flash("You logged out!") + return render_template('index.html') + jedges ='' + file_edges = open(datadir+gene_url_tmp +'/edges.json', 'r') + for line in file_edges.readlines(): + if ':' not in line: + nodata_temp = 1 + else: + nodata_temp = 0 + with open(datadir+gene_url_tmp +"/edges.json") as edgesjsonfile: + jedges = json.load(edgesjsonfile) + break + else: + genes_session_tmp=tf_path+"/"+rnd_url + gene_url_tmp = genes_session_tmp + try: + with open(gene_url_tmp+"/nodes.json") as jsonfile: + jnodes = json.load(jsonfile) + except FileNotFoundError: + flash("You logged out!") + return render_template('index.html') + jedges ='' + file_edges = open(gene_url_tmp +'/edges.json', 'r') + for line in file_edges.readlines(): + if ':' not in line: + nodata_temp = 1 + else: + nodata_temp = 0 + with open(gene_url_tmp +"/edges.json") as edgesjsonfile: + jedges = json.load(edgesjsonfile) + break + genename=genes_url.split("_") if len(genename)>3: genename = genename[0:3] added = ",..." @@ -447,24 +510,56 @@ def tableview(): gene_name=gene_name.replace("'","") gene_name = gene_name+added num_gene = gene_name.count(',')+1 - message3="<b> Actions: </b><li> <font color=\"#E74C3C\">Click on the abstract count to read sentences linking the keyword and the gene</font> <li> Click on a gene to search its relations with top 200 addiction genes. <li> Click on a keyword to see the terms included in the search. <li>View the results in <a href='cytoscape'><b> a graph.</b></a><li>All results will appear in a new Browser window (or tab)" - return render_template('tableview.html', nodata_temp=nodata_temp, num_gene=num_gene,session_path = session['path'], jedges=jedges, jnodes=jnodes,gene_name=gene_name, message3=message3) -@app.route("/tableview0") + message3="<b> Actions: </b><li> <font color=\"#E74C3C\">Click on the abstract count to read sentences linking the keyword and the gene</font> <li> Click on a gene to search its relations with top 200 addiction genes. <li> Click on a keyword to see the terms included in the search. <li>View the results in <a href='\\cytoscape/?rnd={}&genequery={}'\ ><b> a graph.</b></a>".format(rnd_url,genes_url) + return render_template('tableview.html', genes_session_tmp = genes_session_tmp, nodata_temp=nodata_temp, num_gene=num_gene, jedges=jedges, jnodes=jnodes,gene_name=gene_name, message3=message3, rnd_url=rnd_url, genes_url=genes_url) + +@app.route("/tableview0/") def tableview0(): - with open(datadir+"nodes.json") as jsonfile: - jnodes = json.load(jsonfile) - jedges ='' - file_edges = open(datadir+'edges.json', 'r') - for line in file_edges.readlines(): - if ':' not in line: - nodata_temp = 1 - else: - nodata_temp = 0 - with open(datadir+"edges.json") as edgesjsonfile: - jedges = json.load(edgesjsonfile) - break - genename=session['query'] + genes_url=request.args.get('genequery') + rnd_url=request.args.get('rnd') + tf_path=tempfile.gettempdir() + if ('email' in session): + filename = rnd_url.split("_0_")[0] + genes_session_tmp = datadir+"/user/"+str(session['hashed_email'])+"/"+rnd_url+"/"+filename + gene_url_tmp = "/user/"+str(session['hashed_email'])+"/"+rnd_url + try: + with open(datadir+gene_url_tmp+"/nodes.json") as jsonfile: + jnodes = json.load(jsonfile) + except FileNotFoundError: + flash("You logged out!") + return render_template('index.html') + jedges ='' + file_edges = open(datadir+gene_url_tmp+'/edges.json', 'r') + for line in file_edges.readlines(): + if ':' not in line: + nodata_temp = 1 + else: + nodata_temp = 0 + with open(datadir+gene_url_tmp+"/edges.json") as edgesjsonfile: + jedges = json.load(edgesjsonfile) + break + else: + genes_session_tmp=tf_path+"/"+rnd_url + gene_url_tmp = genes_session_tmp + try: + with open(gene_url_tmp+"/nodes.json") as jsonfile: + jnodes = json.load(jsonfile) + except FileNotFoundError: + flash("You logged out!") + return render_template('index.html') + jedges ='' + file_edges = open(gene_url_tmp+'/edges.json', 'r') + for line in file_edges.readlines(): + if ':' not in line: + nodata_temp = 1 + else: + nodata_temp = 0 + with open(gene_url_tmp+"/edges.json") as edgesjsonfile: + jedges = json.load(edgesjsonfile) + break + genes_url=request.args.get('genequery') + genename=genes_url.split("_") if len(genename)>3: genename = genename[0:3] added = ",..." @@ -475,16 +570,20 @@ def tableview0(): gene_name=gene_name.replace("'","") gene_name = gene_name+added num_gene = gene_name.count(',')+1 - message4="<b> Notes: </b><li> These are the keywords that have <b>zero</b> abstract counts. <li>View all the results in <a href='cytoscape'><b> a graph.</b></a>" - return render_template('tableview0.html',nodata_temp=nodata_temp, num_gene=num_gene,session_path = session['path'], jedges=jedges, jnodes=jnodes,gene_name=gene_name, message4=message4) + message4="<b> Notes: </b><li> These are the keywords that have <b>zero</b> abstract counts. <li>View all the results in <a href='\\cytoscape/?rnd={}&genequery={}'><b> a graph.</b></a>".format(rnd_url,genes_url) + return render_template('tableview0.html',nodata_temp=nodata_temp, num_gene=num_gene, jedges=jedges, jnodes=jnodes,gene_name=gene_name, message4=message4) @app.route("/userarchive") def userarchive(): - if os.path.exists(datadir+"user/"+str(session['email'])) == False: - flash("Search history doesn't exist!") - return render_template('index.html') if ('email' in session): - session['user_folder'] = datadir+"user/"+str(session['email']) + if os.path.exists(datadir+"/user/"+str(session['hashed_email'])) == False: + flash("Search history doesn't exist!") + return render_template('index.html') + else: + session['user_folder'] = datadir+"/user/"+str(session['hashed_email']) + else: + flash("You logged out!") + return render_template('index.html') session_id=session['id'] def sorted_alphanumeric(data): convert = lambda text: int(text) if text.isdigit() else text.lower() @@ -512,81 +611,105 @@ def userarchive(): # delete this search @app.route('/remove', methods=['GET', 'POST']) def remove(): - remove_folder = request.args.get('remove_folder') - shutil.rmtree(datadir+"user/"+str(session['email']+"/"+remove_folder), ignore_errors=True) - return redirect(url_for('userarchive')) + if('email' in session): + remove_folder = request.args.get('remove_folder') + shutil.rmtree(datadir+"/user/"+str(session['hashed_email']+"/"+remove_folder), ignore_errors=True) + return redirect(url_for('userarchive')) + else: + flash("You logged out!") + return render_template('index.html') @app.route('/date', methods=['GET', 'POST']) def date(): select_date = request.args.get('selected_date') #open the cache folder for the user - tf_path=datadir+"user" + tf_path=datadir+"/user" if ('email' in session): time_extension = str(select_date) time_extension = time_extension.split('_0_')[0] gene_name1 = str(select_date).split('_0_')[1] time_extension = time_extension.replace(':', '_') time_extension = time_extension.replace('-', '_') - session['path'] = tf_path+"/"+str(session['email'])+"/"+select_date+"/"+time_extension - session['user_folder'] = tf_path+"/"+str(session['email']) - else: - tf_path=tempfile.gettempdir() - session['path']=tf_path+"/tmp" + ''.join(random.choice(string.ascii_letters) for x in range(6)) - with open(tf_path+"/"+str(session['email'])+"/"+select_date+"/edges.json", "r") as archive_file: - with open(datadir+"edges.json", "w") as temp_file: - for line in archive_file: - temp_file.write(line) - with open(tf_path+"/"+str(session['email'])+"/"+select_date+"/nodes.json", "r") as archive_file: - with open(datadir+"nodes.json", "w") as temp_file: - for line in archive_file: - temp_file.write(line) - with open(datadir+"nodes.json", "r") as jsonfile: - jnodes = json.load(jsonfile) - - jedges ='' - file_edges = open(datadir+'edges.json', 'r') - for line in file_edges.readlines(): - if ':' not in line: - nodata_temp = 1 + session['user_folder'] = tf_path+"/"+str(session['hashed_email']) + genes_session_tmp = tf_path+"/"+str(session['hashed_email'])+"/"+select_date+"/"+time_extension + with open(tf_path+"/"+str(session['hashed_email'])+"/"+select_date+"/nodes.json", "r") as jsonfile: + jnodes = json.load(jsonfile) + jedges ='' + file_edges = open(tf_path+"/"+str(session['hashed_email'])+"/"+select_date+"/edges.json", "r") + for line in file_edges.readlines(): + if ':' not in line: + nodata_temp = 1 + else: + nodata_temp = 0 + with open(tf_path+"/"+str(session['hashed_email'])+"/"+select_date+"/edges.json", "r") as edgesjsonfile: + jedges = json.load(edgesjsonfile) + break + gene_list_all=[] + gene_list=[] + if nodata_temp == 0: + for p in jedges['data']: + if p['source'] not in gene_list: + gene_list_all.append(p['source']) + gene_list.append(p['source']) + if len(gene_list)>3: + gene_list = gene_list[0:3] + added = ",..." + else: + added = "" + gene_name = str(gene_list)[1:] + gene_name=gene_name[:-1] + gene_name=gene_name.replace("'","") + gene_name = gene_name+added + num_gene = gene_name.count(',')+1 else: - nodata_temp = 0 - with open(datadir+"edges.json") as edgesjsonfile: - jedges = json.load(edgesjsonfile) - break - gene_list=[] - if nodata_temp == 0: - for p in jedges['data']: - if p['source'] not in gene_list: - gene_list.append(p['source']) - if len(gene_list)>3: - gene_list = gene_list[0:3] - added = ",..." - else: - added = "" - gene_name = str(gene_list)[1:] - gene_name=gene_name[:-1] - gene_name=gene_name.replace("'","") - gene_name = gene_name+added - num_gene = gene_name.count(',')+1 - else: - gene_name1 = gene_name1.replace("_", ", ") - gene_name = gene_name1 - num_gene = gene_name1.count(',')+1 - for i in range(0,num_gene): - gene_list.append(gene_name1.split(',')[i]) - session['query'] = gene_list - message3="<b> Actions: </b><li><font color=\"#E74C3C\">Click on the keywords to see the indicated number of abstracts </font><li> Click on a gene to search its relations with top 200 addiction genes<li>Click on a keyword to see the terms included in the search<li>Hover your pointer over a node to hide other links <li>Move nodes around to adjust visibility, reload the page to restore the default layout<li> View the results in <a href='cytoscape'><b>a graph.</b></a>" - return render_template('tableview.html', title='',nodata_temp=nodata_temp, date=select_date, num_gene=num_gene,session_path = session['path'], jedges=jedges, jnodes=jnodes,gene_name=gene_name, message3=message3) - -@app.route('/cytoscape') + gene_name1 = gene_name1.replace("_", ", ") + gene_name = gene_name1 + num_gene = gene_name1.count(',')+1 + for i in range(0,num_gene): + gene_list.append(gene_name1.split(',')[i]) + genes_session = '' + for gen in gene_list_all: + genes_session += str(gen) + "_" + genes_session = genes_session[:-1] + else: + flash("You logged out!") + return render_template('index.html') + message3="<b> Actions: </b><li> <font color=\"#E74C3C\">Click on the abstract count to read sentences linking the keyword and the gene</font> <li> Click on a gene to search its relations with top 200 addiction genes. <li> Click on a keyword to see the terms included in the search. <li>View the results in <a href='\\cytoscape/?rnd={}&genequery={}'\ ><b> a graph.</b></a>".format(select_date,genes_session) + return render_template('tableview.html',nodata_temp=nodata_temp, num_gene=num_gene,genes_session_tmp = genes_session_tmp, rnd_url=select_date ,jedges=jedges, jnodes=jnodes,gene_name=gene_name, genes_url=genes_session, message3=message3) + +@app.route('/cytoscape/') def cytoscape(): - message2="<b> Actions: </b><li><font color=\"#E74C3C\">Click on a line to see the indicated number of abstracts </font> <li> Click on a gene to search its relations with top 200 addiction genes<li>Click on a keyword to see the terms included in the search<li>Hover your pointer over a node to hide other links <li>Move nodes around to adjust visibility, reload the page to restore the default layout<li>View the results in <a href='tableview'><b>a table. </b></a> <li>All results will appear in a new Browser window (or tab)" - with open(session['path']+"_cy","r") as f: - elements=f.read() - with open(session['path']+"_0link","r") as z: - zeroLink=z.read() - if (len(zeroLink)>0): - message2+="<span style=\"color:darkred;\">No result was found for these genes: " + zeroLink + "</span>" + genes_url=request.args.get('genequery') + rnd_url=request.args.get('rnd') + tf_path=tempfile.gettempdir() + genes_session_tmp=tf_path + "/" + genes_url + + message2="<b> Actions: </b><li><font color=\"#E74C3C\">Click on a line to see the indicated number of abstracts </font> <li> Click on a gene to search its relations with top 200 addiction genes<li>Click on a keyword to see the terms included in the search<li>Hover your pointer over a node to hide other links <li>Move nodes around to adjust visibility, reload the page to restore the default layout<li>View the results in <a href='\\tableview/?rnd={}&genequery={}'\ ><b>a table. </b></a> <li>All results will appear in a new Browser window (or tab)".format(rnd_url,genes_url) + if ('email' in session): + filename = rnd_url.split("_0_")[0] + rnd_url_tmp = datadir+"/user/"+str(session['hashed_email'])+"/"+rnd_url+"/"+filename + try: + with open(rnd_url_tmp+"_cy","r") as f: + elements=f.read() + except FileNotFoundError: + flash("You logged out!") + return render_template('index.html') + with open(rnd_url_tmp+"_0link","r") as z: + zeroLink=z.read() + if (len(zeroLink)>0): + message2+="<span style=\"color:darkred;\">No result was found for these genes: " + zeroLink + "</span>" + else: + rnd_url_tmp=tf_path +"/" + rnd_url + try: + with open(rnd_url_tmp+"_cy","r") as f: + elements=f.read() + except FileNotFoundError: + flash("You logged out!") + return render_template('index.html') + with open(rnd_url_tmp+"_0link","r") as z: + zeroLink=z.read() + if (len(zeroLink)>0): + message2+="<span style=\"color:darkred;\">No result was found for these genes: " + zeroLink + "</span>" return render_template('cytoscape.html', elements=elements, message2=message2) @app.route("/sentences") @@ -607,6 +730,7 @@ def sentences(): else: return 'pos' pmid_list=[] + pmid_string='' edge=request.args.get('edgeID') (tf_name, gene0, cat0)=edge.split("|") if(cat0=='stress'): @@ -617,7 +741,7 @@ def sentences(): out_neg = "" num_abstract = 0 stress_cellular = "<br><br><br>"+"</ol><b>Sentence(s) describing celluar stress (classified using a deep learning model):</b><hr><ol>" - stress_systemic = "<b>Sentence(s) describing systemic stress (classified using a deep learning model):</b><hr>" + stress_systemic = "<b></ol>Sentence(s) describing systemic stress (classified using a deep learning model):</b><hr><ol>" with open(tf_name, "r") as df: all_sents=df.read() for sent in all_sents.split("\n"): @@ -627,6 +751,7 @@ def sentences(): out3+= "<li> "+ text + " <a href=\"https://www.ncbi.nlm.nih.gov/pubmed/?term=" + pmid +"\" target=_new>PMID:"+pmid+"<br></a>" num_abstract += 1 if(pmid+cat0 not in pmid_list): + pmid_string = pmid_string + ' ' + pmid pmid_list.append(pmid+cat0) if(cat0=='stress'): out4 = predict_sent(text) @@ -638,9 +763,9 @@ def sentences(): out_neg += out_pred_neg out1="<h3>"+gene0 + " and " + cat0 + "</h3>\n" if len(pmid_list)>1: - out2 = str(num_abstract) + ' sentences in ' + str(len(pmid_list)) + ' studies' + "<br><br>" + out2 = str(num_abstract) + ' sentences in ' + " <a href=\"https://www.ncbi.nlm.nih.gov/pubmed/?term=" + pmid_string +"\" target=_new>"+ str(len(pmid_list)) + ' studies' +"<br></a>" + "<br><br>" else: - out2 = str(num_abstract) + ' sentence(s) in ' + str(len(pmid_list)) + ' study' "<br><br>" + out2 = str(num_abstract) + ' sentence(s) in '+ " <a href=\"https://www.ncbi.nlm.nih.gov/pubmed/?term=" + pmid_string +"\" target=_new>"+ str(len(pmid_list)) + ' study' +"<br></a>" "<br><br>" if(out_neg == "" and out_pos == ""): out= out1+ out2 +out3 elif(out_pos != "" and out_neg!=""): @@ -649,6 +774,7 @@ def sentences(): out= out1+ out2 + stress_systemic + out_pos elif(out_neg != "" and out_pos == ""): out = out1 +out2+stress_cellular+out_neg + K.clear_session() return render_template('sentences.html', sentences="<ol>"+out+"</ol><p>") ## show the cytoscape graph for one gene from the top gene list @app.route("/showTopGene") @@ -665,6 +791,32 @@ def shownode(): out="<p>"+node.upper()+"<hr><li>"+ allnodes[node].replace("|", "<li>") return render_template('sentences.html', sentences=out+"<p>") +@app.route("/synonyms") +def synonyms(): + node=request.args.get('node') + node=node.upper() + allnodes={**genes} + try: + synonym_list = list(allnodes[node].split("|")) + session['synonym_list'] = synonym_list + session['main_gene'] = node.upper() + out="<hr><li>"+ allnodes[node].replace("|", "<li>") + synonym_list_str = ';'.join([str(syn) for syn in synonym_list]) + synonym_list_str +=';' + node + case = 1 + return render_template('genenames.html', case = case, gene = node.upper(), synonym_list = synonym_list, synonym_list_str=synonym_list_str) + except: + try: + #if(node in session['synonym_list']): + synonym_list = session['synonym_list'] + synonym_list_str = ';'.join([str(syn) for syn in synonym_list]) + synonym_list_str +=';' + node + case = 1 + return render_template('genenames.html', case=case, gene = session['main_gene'] , synonym_list = synonym_list, synonym_list_str=synonym_list_str) + except: + case = 2 + return render_template('genenames.html', gene = node, case = case) + @app.route("/startGeneGene") def startGeneGene(): session['forTopGene']=request.args.get('forTopGene') |