segmenter.py 2.71 KB
Newer Older
marc's avatar
marc committed
1 2 3 4 5
# -*- coding: utf-8 -*-
from contextlib import contextmanager
import tempfile
import shutil
import defs
6
import gluon
marc's avatar
marc committed
7

8 9 10 11 12
if request.env.http_origin:
    response.headers['Access-Control-Allow-Origin'] = request.env.http_origin
    response.headers['Access-Control-Allow-Credentials'] = 'true'
    response.headers['Access-Control-Max-Age'] = 86400

marc's avatar
marc committed
13 14 15 16 17 18 19 20 21 22 23 24 25
@contextmanager
def TemporaryDirectory():
    name = tempfile.mkdtemp()
    try:
        yield name
    finally:
        shutil.rmtree(name)

        
limit_max = 10 #max sequence per request
        

def index():
26 27 28 29 30 31 32 33 34 35 36 37
    if request.vars['sequences'] == None or request.vars['sequences'] == '':
        return None

    sequences = request.vars['sequences']
    return segment_sequences(sequences)

def segment_sequences(sequences):
    '''
    Segment the sequences given in parameter (FASTA format)
    and return a JSON of the result
    '''
    text_result = "{}"
marc's avatar
marc committed
38
    
39 40 41 42
    check = check_sequences(sequences)
    if check is not None:
        text_result = '{"error": "%s"}' % check
    else:
marc's avatar
marc committed
43 44 45 46
        with TemporaryDirectory() as folder_path:
            
            #store sequences in a tmp file
            file_path = folder_path + "/sequences.txt"
47 48 49
            fasta = open(file_path, 'w')
            fasta.write(sequences)
            fasta.close()
marc's avatar
marc committed
50 51
            
            #store result in a tmp file
52
            result_path = folder_path + "/sequences.vidjil"
marc's avatar
marc committed
53 54 55 56 57
            
            ## les chemins d'acces a vidjil / aux fichiers de sequences
            germline_folder = defs.DIR_VIDJIL + '/germline/'

            ## config de vidjil
58
            config = '-c segment -i -2 -3 -g germline'
marc's avatar
marc committed
59 60 61
            config = config.replace( ' germline' ,germline_folder)

            ## commande complete
62
            cmd = defs.DIR_VIDJIL + '/vidjil-algo ' + ' -o  ' + folder_path 
marc's avatar
marc committed
63 64 65
            cmd += ' ' + config + ' ' + file_path

            ## execute la commande vidjil
66 67
            os.system(cmd)

68 69 70 71 72
            if os.path.isfile(result_path):
                with open(result_path, 'r') as myfile:
                    text_result = myfile.read()
            else:
                return response.json({'error': 'Error while processing the file'})
73

74
    return response.json(gluon.contrib.simplejson.loads(text_result))
marc's avatar
marc committed
75

76
def check_sequences(sequences):
marc's avatar
marc committed
77
    #fasta format ?
78 79 80 81
    if sequences[0] is '>':
        if len(sequences.split('>')) > limit_max+1 :
            return "too many sequences (limit : " + str(limit_max) + ")"

marc's avatar
marc committed
82
    #fastq format ?
83 84 85 86
    elif sequences[0] is '@':
        if len(sequences.split('\n')) > 4*(limit_max+1) :
            return "too many sequences (limit : " + str(limit_max) + ")"

marc's avatar
marc committed
87 88
    #unknow format ?
    else :
89 90
        return "invalid sequences, please use fasta or fastq format"
    return None