Commit 88d0f57e authored by marc's avatar marc
Browse files

segmenter.py : segmenter tool

parent 1931c5c8
# -*- coding: utf-8 -*-
from contextlib import contextmanager
import tempfile
import shutil
import defs
from subprocess import Popen, PIPE, STDOUT, os
@contextmanager
def TemporaryDirectory():
name = tempfile.mkdtemp()
try:
yield name
finally:
shutil.rmtree(name)
limit_max = 10 #max sequence per request
def index():
form = SQLFORM.factory(
Field('sequences', 'text'),
Field('file', 'upload', uploadfolder=os.path.join(request.folder,'uploads')))
form.element('textarea[name=sequences]')['_style'] = 'width:800px; height:200px;'
text_result = ""
if form.process(onvalidation=checkform_segmenter).accepted:
with TemporaryDirectory() as folder_path:
#store sequences in a tmp file
file_path = folder_path + "/sequences.txt"
file = open(file_path, 'w')
file.write(form.vars.sequences)
file.close()
#store result in a tmp file
result_path = folder_path + "/result.txt"
result = open(result_path, 'w')
## les chemins d'acces a vidjil / aux fichiers de sequences
germline_folder = defs.DIR_VIDJIL + '/germline/'
## config de vidjil
config = '-c segment -g germline'
config = config.replace( ' germline' ,germline_folder)
## commande complete
cmd = defs.DIR_VIDJIL + '/vidjil ' + ' -o ' + folder_path
cmd += ' ' + config + ' ' + file_path
## execute la commande vidjil
sys.stdout.flush()
p = Popen(cmd, shell=True, stdin=PIPE, stdout=result, stderr=STDOUT, close_fds=True)
p.wait()
result.close()
with open(result_path, 'r') as myfile:
text_result = myfile.read()
response.flash = file_path
elif form.errors:
response.flash = 'form has errors'
#form.errors.sequences = form.vars.sequences
return dict(form=form,
result=text_result)
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 ?
if form.vars.sequences[0] is '>':
if len(form.vars.sequences.split('>')) > limit_max+1 :
form.errors.sequences = "too much sequences (limit : " + str(limit_max) + ")"
#fastq format ?
elif form.vars.sequences[0] is '@':
if len(form.vars.sequences.split('\n')) > 4*(limit_max+1) :
form.errors.sequences = "too much sequences (limit : " + str(limit_max) + ")"
#unknow format ?
else :
form.errors.sequences = "invalid sequences, please use fasta or fastq format"
{{extend 'simple_layout.html'}}
<h1>Vidjil online segmenter tool</h1>
<div>
{{=form}}
</div>
<div>
{{if result != "":}}
<textarea class="text invalidinput" cols="40" name="result" rows="10" style="width: 100%; height: 2000px; font-family:monospace">
{{=result}}
</textarea>
{{pass}}
</div>
<!--[if HTML5]><![endif]-->
<!DOCTYPE html>
<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->
<!--[if lt IE 7]><html class="ie ie6 ie-lte9 ie-lte8 ie-lte7 no-js" lang="{{=T.accepted_language or 'en'}}"> <![endif]-->
<!--[if IE 7]><html class="ie ie7 ie-lte9 ie-lte8 ie-lte7 no-js" lang="{{=T.accepted_language or 'en'}}"> <![endif]-->
<!--[if IE 8]><html class="ie ie8 ie-lte9 ie-lte8 no-js" lang="{{=T.accepted_language or 'en'}}"> <![endif]-->
<!--[if IE 9]><html class="ie9 ie-lte9 no-js" lang="{{=T.accepted_language or 'en'}}"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class="no-js" lang="{{=T.accepted_language or 'en'}}"> <!--<![endif]-->
<head>
<title>{{=response.title or request.application}}</title>
<!--[if !HTML5]>
<meta http-equiv="X-UA-Compatible" content="IE=edge{{=not request.is_local and ',chrome=1' or ''}}">
<![endif]-->
<!-- www.phpied.com/conditional-comments-block-downloads/ -->
<!-- Always force latest IE rendering engine
(even in intranet) & Chrome Frame
Remove this if you use the .htaccess -->
<meta charset="utf-8" />
<!-- http://dev.w3.org/html5/markup/meta.name.html -->
<meta name="application-name" content="{{=request.application}}" />
<!-- Speaking of Google, don't forget to set your site up:
http://google.com/webmasters -->
<meta name="google-site-verification" content="" />
<!-- Mobile Viewport Fix
j.mp/mobileviewport & davidbcalhoun.com/2010/viewport-metatag
device-width: Occupy full width of the screen in its current orientation
initial-scale = 1.0 retains dimensions instead of zooming out if page height > device height
user-scalable = yes allows the user to zoom in -->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="{{=URL('static','images/favicon.ico')}}" type="image/x-icon">
<link rel="apple-touch-icon" href="{{=URL('static','images/favicon.png')}}">
<script src="{{=URL('static','js/modernizr.custom.js')}}"></script>
<!-- include stylesheets -->
{{
response.files.insert(0,URL('static','css/bootstrap.min.css'))
}}
{{include 'web2py_ajax.html'}}
{{
# using sidebars need to know what sidebar you want to use
left_sidebar_enabled = globals().get('left_sidebar_enabled',False)
right_sidebar_enabled = globals().get('right_sidebar_enabled',False)
middle_columns = {0:'span12',1:'span9',2:'span6'}[
(left_sidebar_enabled and 1 or 0)+(right_sidebar_enabled and 1 or 0)]
}}
</head>
<body>
<div class="container">
<!-- Masthead ================================================== -->
<header class="mastheader row" id="header">
{{if response.title:}}
<div class="span12">
<div class="page-header">
<h1>
{{=response.title}}
<small>{{=response.subtitle or ''}}</small>
</h1>
</div>
</div>
{{pass}}
</header>
<section id="main" class="main row">
{{if left_sidebar_enabled:}}
<div class="span3 left-sidebar">
{{block left_sidebar}}
<h3>Left Sidebar</h3>
<p></p>
{{end}}
</div>
{{pass}}
<div class="{{=middle_columns}}">
{{block center}}
{{include}}
{{end}}
</div>
{{if right_sidebar_enabled:}}
<div class="span3">
{{block right_sidebar}}
<h3>Right Sidebar</h3>
<p></p>
{{end}}
</div>
{{pass}}
</section><!--/main-->
<!-- Footer ================================================== -->
<div class="row">
<footer class="footer span12" id="footer">
<div class="footer-content">
{{block footer}} <!-- this is default footer -->
<div class="copyright pull-left">{{=T('Copyright')}} Vidjil &#169; {{=request.now.year}}</div>
{{end}}
</div>
</footer>
</div>
</div> <!-- /container -->
</body>
</html>
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