Attention une mise à jour du service Gitlab va être effectuée le mardi 30 novembre entre 17h30 et 18h00. Cette mise à jour va générer une interruption du service dont nous ne maîtrisons pas complètement la durée mais qui ne devrait pas excéder quelques minutes. Cette mise à jour intermédiaire en version 14.0.12 nous permettra de rapidement pouvoir mettre à votre disposition une version plus récente.

segmenter.py 3.09 KB
Newer Older
marc's avatar
marc committed
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
# -*- 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
45
            config = '-c segment -i -2 -3 -g germline'
marc's avatar
marc committed
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
            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"