-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_app.py
342 lines (261 loc) · 9.75 KB
/
new_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Dinesh_Ravindra_Raju
"""
import numpy as np
from flask import Flask, request, jsonify, render_template, session
from flask_session import Session
import pickle
import pypgx
from pypgx.api import utils
import pandas as pd
import subprocess
import argparse
import os
import shlex
from werkzeug.utils import secure_filename
import os
import logging
from flask_login import current_user
#from flask_login import
import uuid
from flask import (
Flask,
request,
redirect,
url_for,
render_template,
send_from_directory,
)
from werkzeug.utils import secure_filename
import matplotlib
matplotlib.use('agg')
def tbi(vcf):
# pass
os.chdir(UPLOAD_FOLDER)
if vcf.endswith(".vcf.gz"):
#print("zipped file ")
print(vcf.split(".gz")[0])
os.system("gunzip {vcf}".format(vcf=vcf))
os.system("bgzip -c {vcf_un} > {vcf2}".format(vcf_un=vcf.split(".gz")[0] , vcf2 = vcf))
os.system("tabix -p vcf {vcf}".format(vcf=vcf))
elif vcf.endswith(".vcf"):
print(vcf+".gz")
print("Zipping using bgzip")
os.system("bgzip -c {vcf} > {vcf2}".format(vcf=vcf,vcf2=vcf +".gz"))
os.system("tabix -p vcf {vcf2}".format(vcf2 = vcf+".gz"))
# print("index made")
#vcf = str(vcf)+".gz"
elif vcf.endswith(".txt.gz"):
f = vcf.split(".txt.gz")[0]+".vcf"
print("------------------------------------------------------------------")
print(f)
os.system("gzip -d {vcf}".format(vcf=vcf))
os.system("bcftools convert --tsv2vcf {v} -f ../hs37d5.fa -s {v} -Ov -o {f}".format(v= vcf.split(".gz")[0], f = f))
os.system("bgzip -c {f} > {vcf2}".format(f=f, vcf2=f +".gz"))
os.system("tabix -p vcf {vcf2}".format(vcf2 = f+".gz"))
else:
#f = vcf.split("uploads/")[-1].split(".txt")[0]+".vcf"
f = vcf.split(".txt")[0]+".vcf"
print("-------------------------------------------------------------------")
print("F is ",f)
os.system("bcftools convert --tsv2vcf {v} -f ../hs37d5.fa -s {v} -Ov -o {f}".format(v= vcf, f = f))
print("ran!")
os.system("bgzip -c {f} > {vcf2}".format(f=f, vcf2=f +".gz"))
os.system("rm -r {f}".format(f=f))
os.system("rm -r {v}".format(v=vcf))
os.system("tabix -p vcf {vcf2}".format(vcf2 = f+".gz"))
#os.system("mv {vcf2} ./uploads".format(vcf2=f +".gz"))
#os.system("mv {tabix} ./uploads".format(tabix=f +".gz.tbi"))
os.chdir(DIR_PATH)
dpe = []
final_features = []
sequence = ""
dp_features = []
dpe = []
UPLOAD_FOLDER = os.path.dirname(os.path.abspath(__file__)) + "/uploads/"
app = Flask(__name__, static_url_path="/static")
app.secret_key = "pimadi"
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
#sess = Session()
#sess.init_app(app)
#sess.init_app(app)
ALLOWED_EXTENSIONS = {"gz", "vcf","txt"}
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
app.config["MAX_CONTENT_LENGTH"] = 100 * 1024 * 1024
@app.route("/")
def home():
#print(current_user.id)
#print(session.sid)
return render_template("home.html")
@app.route("/predictor/")
def predictor():
return render_template("predictor.html")
@app.route("/about/")
def about():
return render_template("contact.html")
@app.route("/predict", methods=["POST"])
def predict():
# Add code to make .vcf.gz.tbi (V.IMP)
try:
ph_result = []
recommend = None
df = pypgx.load_recommendation_table()
print("This will work")
drug = request.form.get("drug")
vcf = request.files.get("file")
id = str(uuid.uuid4())
session["username"] = id
print(request.files)
# print(sess.sid)
print(request.form)
print(vcf)
# saving the file
filename = secure_filename(vcf.filename)
# print(app.config["UPLOAD_FOLDER"])
#path_upload = os.path.join("uploads/", filename)
#print(path_upload)
vcf.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
print(os.getcwd())
tbi("{name}".format(name=filename))
# tbi("eee")
name_ = filename.split(".")[0]+".vcf.gz"
new_file_path = "./uploads/{name}".format(name=name_)
print("new_file path is:",new_file_path)
cmd = f"python3 run_pypgx.py -vcf {new_file_path} -j {id} -d {drug}"
os.system(cmd)
focus_df = df.loc[(df["Drug"] == str(drug).lower())]
groups = focus_df.groupby(["Drug"])
print(groups)
genes = []
for ind, row in focus_df.iterrows():
genes.append(row["Gene1"])
print(genes)
unqiue_genes = list(set(genes))
print(unqiue_genes)
"""
if len(unqiue_genes) == 1:
print("Running api")
print(id)
print(filename)
print("going in",os.path.join(UPLOAD_FOLDER, name_))
pypgx.api.pipeline.run_ngs_pipeline(
str(unqiue_genes[0]).upper(),
id,
variants= new_file_path
)
print("ran!")
print(id)
"""
os.chdir(id)
#print("unzip ")
subprocess.run(
shlex.split("unzip results.zip".format(dir=id))
) # change this and shutils
#print("unzipped")
file = [f for f in os.listdir("./") if f.startswith("tmp")]
# file = list(filter(lambda x : x.startswith("tmp"), list(os.listdir("./"))))
print(f"file : {file[0]}")
df2 = pd.read_csv("{dir}/data.tsv".format(dir=file[0]), sep="\t")
print(df2.head())
genotype = str(df2["Genotype"])
phenotype = df2["Phenotype"][0]
print(phenotype)
print(genotype)
# try to avoid this
os.chdir("../")
# recommendation = str(df.loc[(df['Drug'] == str(drug).lower())])
recommendation = focus_df[focus_df["Phenotype1"] == phenotype][
"Recommendation"
]
rec = recommendation.to_frame()
print(rec.columns)
df3 = rec.drop_duplicates(
keep="first"
) # Removing duplicates and just keeping the first hit
recommend = df3["Recommendation"].tolist()
ph_result = []
if phenotype.startswith("Normal Metabolizer") or phenotype.startswith(
"Rapid Metabolizer"
):
ph_result.append("Take it!")
else:
ph_result.append("Leave it!")
# print(df3)
# df3.to_csv('rec2.csv')
# return df3.to_json()
except Exception as e:
logging.error(str(e))
#return {"msg": str(e)}
return render_template('error.html', message=str(e))
os.system("rm -r {vcf}".format(vcf=new_file_path))
os.system("rm -r {vcf_tbi}".format(vcf_tbi = new_file_path+".tbi"))
os.system("rm -r {dir}".format(dir=id))
return render_template("show.html", data=recommend, varchar=drug, diag=ph_result)
def allowed_file(filename):
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/submit", methods=["GET", "POST"])
def submit():
if request.method == "POST":
if "file" not in request.files:
# print('No file attached in request')
return render_template("predictor.html")
return redirect(request.url)
file = request.files["file"]
if file.filename == "":
# flash('No selected file')
return render_template("predictor.html")
return redirect(request.url)
if file and allowed_file(file.filename):
# session["username"] = file.filename
filename = secure_filename(file.filename)
file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
data, varchar = predict(
os.path.join(app.config["UPLOAD_FOLDER"], filename), filename
)
# return redirect(url_for('uploaded_file', filename=filename))
session.pop('username', None)
# return render_template('predictor.html')
# return render_template("show.html", data=data, varchar=varchar)
"""
def process_file(path, filename):
drug = request.form.get("drug")
#vcf = args.vcf
#id = args.jobid
focus_df = df.loc[(df['Drug'] == str(drug).lower())]
groups = (focus_df.groupby(["Drug"]))
genes=[]
for ind , row in focus_df.iterrows():
genes.append(row["Gene1"])
unqiue_genes = list(set(genes))
if len(unqiue_genes)==1:
pypgx.api.pipeline.run_ngs_pipeline(str(unqiue_genes[0]).upper() ,id , variants=vcf)
#os.chdir("./{dir}".format(dir=id))
#print("pipeline done !")
os.chdir(id)
subprocess.run(shlex.split("unzip results.zip".format(dir=id)))
print("unzipped")
file = [f for f in os.listdir("./") if f.startswith("tmp") ]
print(file[0])
df2 = pd.read_csv("{dir}/data.tsv".format(dir=file[0]),sep='\t')
genotype = str(df2["Genotype"])
phenotype = (df2["Phenotype"][0])
print(phenotype)
os.chdir("../")
#recommendation = str(df.loc[(df['Drug'] == str(drug).lower())])
recommendation = focus_df[focus_df["Phenotype1"]==phenotype]["Recommendation"]
rec=recommendation.to_frame()
print(rec.columns)
df3 = rec.drop_duplicates(keep='first') #Removing duplicates and just keeping the first hit
print(df3)
df3.to_csv('rec2.csv')
return(str,kratikal)
#return render_template("show.html", data=str, varchar=kratikal)
"""
def allowed_file(filename):
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
if __name__ == "__main__":
app.run(host="0.0.0.0",debug=True)