Commit 9c6fd8f6 authored by Mikaël Salson's avatar Mikaël Salson
Browse files

segmenter.py: Remove everything related to the form

The controller does not manage the form anymore. The controller is
now restricted to its minimum: returning a JSON with the results.

The sequences are sent by the request.
parent 3f68e148
...@@ -4,6 +4,7 @@ import tempfile ...@@ -4,6 +4,7 @@ import tempfile
import shutil import shutil
import defs import defs
from subprocess import Popen, PIPE, STDOUT, os from subprocess import Popen, PIPE, STDOUT, os
import gluon
if request.env.http_origin: if request.env.http_origin:
response.headers['Access-Control-Allow-Origin'] = request.env.http_origin response.headers['Access-Control-Allow-Origin'] = request.env.http_origin
...@@ -23,24 +24,33 @@ limit_max = 10 #max sequence per request ...@@ -23,24 +24,33 @@ limit_max = 10 #max sequence per request
def index(): def index():
form = SQLFORM.factory( if request.vars['sequences'] == None or request.vars['sequences'] == '':
Field('sequences', 'text'), return None
Field('file', 'upload', uploadfolder=os.path.join(request.folder,'uploads')))
form.element('textarea[name=sequences]')['_style'] = 'width:800px; height:200px;' sequences = request.vars['sequences']
return segment_sequences(sequences)
t_result = ""
def segment_sequences(sequences):
'''
Segment the sequences given in parameter (FASTA format)
and return a JSON of the result
'''
text_result = "{}"
if form.process(onvalidation=checkform_segmenter).accepted: check = check_sequences(sequences)
if check is not None:
text_result = '{"error": "%s"}' % check
else:
with TemporaryDirectory() as folder_path: with TemporaryDirectory() as folder_path:
#store sequences in a tmp file #store sequences in a tmp file
file_path = folder_path + "/sequences.txt" file_path = folder_path + "/sequences.txt"
file = open(file_path, 'w') file = open(file_path, 'w')
file.write(form.vars.sequences) file.write(sequences)
file.close() file.close()
#store result in a tmp file #store result in a tmp file
result_path = folder_path + "/result.txt" result_path = folder_path + "/sequences.vidjil"
result = open(result_path, 'w') result = open(result_path, 'w')
## les chemins d'acces a vidjil / aux fichiers de sequences ## les chemins d'acces a vidjil / aux fichiers de sequences
...@@ -63,47 +73,20 @@ def index(): ...@@ -63,47 +73,20 @@ def index():
with open(result_path, 'r') as myfile: with open(result_path, 'r') as myfile:
text_result = myfile.read() text_result = myfile.read()
t_result = '' return response.json(gluon.contrib.simplejson.loads(text_result))
for l in text_result.split('\n'):
pre, post = ('<span style="color: #090; font-weight:bold;">', '</span>') if l.startswith('>') else ('', '')
t_result += pre + l + post + '\n'
response.flash = file_path
elif form.errors:
response.flash = 'form has errors'
#form.errors.sequences = form.vars.sequences
return dict(form=form,
result=XML(t_result))
def check_sequences(sequences):
def checkform_segmenter(form):
#copy file content into form.vars.sequences
if hasattr(form.vars.file, 'file') :
with TemporaryDirectory() as folder_path:
file=request.vars.file.file
shutil.copyfileobj(file,open(folder_path+"/sequences.txt",'wb'))
with open(folder_path+"/sequences.txt", 'r') as myfile:
form.vars.sequences = myfile.read()
form.vars.sequences+= "\n"
#fasta format ? #fasta format ?
if form.vars.sequences[0] is '>': if sequences[0] is '>':
if len(form.vars.sequences.split('>')) > limit_max+1 : if len(sequences.split('>')) > limit_max+1 :
form.errors.sequences = "too much sequences (limit : " + str(limit_max) + ")" return "too many sequences (limit : " + str(limit_max) + ")"
#fastq format ? #fastq format ?
elif form.vars.sequences[0] is '@': elif sequences[0] is '@':
if len(form.vars.sequences.split('\n')) > 4*(limit_max+1) : if len(sequences.split('\n')) > 4*(limit_max+1) :
form.errors.sequences = "too much sequences (limit : " + str(limit_max) + ")" return "too many sequences (limit : " + str(limit_max) + ")"
#unknow format ? #unknow format ?
else : else :
form.errors.sequences = "invalid sequences, please use fasta or fastq format" return "invalid sequences, please use fasta or fastq format"
return None
{{extend 'simple_layout.html'}}
<h2>V(D)J designation tool</h2>
<p>This tool quickly checks the V(D)J designation of a small number of sequences with (possibly incomplete) V(D)J recombinations.
To analyze complete NGS RepSeq datasets, use the <a href="https://app.vidjil.org/">full Vidjil platform</a>.
</p>
<div>
<p>Either enter up to 10 sequences in FASTA format or upload a FASTA file.</p>
{{=form}}
</div>
<div>
{{if result != "":}}
<h3>Result</h3>
<div class="text invalidinput" cols="40" name="result" rows="10" style="width: 100%; height: 2000px; font-family:monospace; white-space: pre;">{{=result}}
</div>
{{pass}}
</div>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment