-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
115 lines (100 loc) · 3.63 KB
/
test.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
# We'll render HTML templates and access data sent by POST
# using the request object from flask. Redirect and url_for
# will be used to redirect the user once the upload is done
# and. We will used subprocess to run command for blast, metrix and analysis
from flask import Flask, request, url_for, render_template, redirect, flash, send_from_directory, jsonify, abort
import os
from werkzeug.utils import secure_filename
from subprocess import Popen, PIPE
import subprocess
from os import rename, listdir
import threading
import sys
import csv
import glob
import urllib2
import subprocess
import pandas as pd
import glob, os, shutil
import numpy
import math
# initialise the Flask application
app = Flask(__name__)
# This is the path to the upload directory
app.config['UPLOAD_FOLDER'] = 'Upload/'
# These are the extension that we are accepting to be uploaded
app.config['ALLOWED_EXTENSIONS'] = set(['fa', 'fasta'])
# For a given file, return whether it's an allowed type or not
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
# Rename files in the upload folder
def rename_file():
path = 'Upload'
files = os.listdir(path)
filenames=(list(files))
G=1
S=1
for file in files:
os.rename(os.path.join(path, file), os.path.join(path, "G"+str(G) + "S" + str(S)+'.fasta'))
S=S+1
if S == 4:
G=G+1
S=1
@app.route('/')
def index():
return render_template("about.html")
# This route will show a form to perform an AJAX request
# jQuery is loaded to execute the request and update the
# value of the operation
@app.route('/blast')
def upload_file():
return render_template("upload.html")
# Route that will process the file upload
@app.route('/<upload>', methods=['POST'])
def upload(upload):
# Get the name of the uploaded files
uploaded_files = request.files.getlist("file[]")
filenames = []
for file in uploaded_files:
# Check if the file is one of the allowed types/extensions
if file and allowed_file(file.filename):
# Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
# Move the file form the temporal folder to the upload
# folder we setup
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Save the filename into a list, we'll use it later
filenames.append(filename)
# Rename files in the Upload folder
rename_file()
# Render blast template when files are succesfully uploaded
return render_template('blast.html', filenames=filenames)
# This route runs blast on all files in the upload folder, create a metrix from the csv files
#and then run analysis.
@app.route('/results')
def blast_file():
#command to run the blast
command= "python localblast.py"
subprocess.call(command, shell=True)
#command to run the matrixproduction
command= "python MatrixProduction.py"
subprocess.call(command, shell=True)
#command to run the analysis
command= "Rscript Analysis.R"
subprocess.call(command, shell=True)
#Render plots and tables generate from the anlysis
return render_template("results.html")
@app.context_processor
def override_url_for():
return dict(url_for=dated_url_for)
def dated_url_for(endpoint, **values):
if endpoint == 'static':
filename = values.get('filename', None)
if filename:
file_path = os.path.join(app.root_path,
endpoint, filename)
values['q'] = int(os.stat(file_path).st_mtime)
return url_for(endpoint, **values)
if __name__ == '__main__':
app.run(debug=True)