clonedb.py 3.02 KB
Newer Older
1 2 3 4 5 6 7 8
# coding: utf8
import gluon.contrib.simplejson, datetime
import vidjil_utils
import time
import os
import sys
import imp

9 10
from controller_utils import error_message

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
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


def index():
    '''
    The request should receive two parameters:
    - sequences: a list of comma-separated DNA sequences to be searched in the CloneDB
    - sample_set_id: the sample set we're coming from
    '''
    if not auth.user:
        return response.json({'error': 'Access denied'})

    if request.vars['sequences'] == None or request.vars['sequences'] == ''\
       or request.vars['sample_set_id'] == None:
28
        return error_message('Malformed request')
29 30 31 32 33 34

    return search_clonedb(request.vars['sequences'].split(','), int(request.vars['sample_set_id']))

def search_clonedb(sequences, sample_set_id):
    sys.path.insert(1, os.path.abspath(defs.DIR_CLONEDB))
    import grep_clones
35
    clonedb = imp.load_source('clonedb', defs.DIR_CLONEDB+os.path.sep+'clonedb.py')
36 37

    results = []
38
    parent_group = get_default_creation_group(auth)[1]
39 40
    auth.load_permissions(PermissionEnum.read.value, 'sample_set')
    auth.load_permissions(PermissionEnum.anon.value, 'sample_set')
41
    options = clonedb.build_grep_clones_options({'sequence': sequences[0]+' -sample_set:%d' % sample_set_id,
42
                                                     'index': 'clonedb_{}'.format(parent_group)})
43 44 45 46 47 48 49 50
    options += sequences[1:]
    args = grep_clones.parser.parse_args(options)
    try:
        occurrences = grep_clones.launch_search(args)
    except ValueError:
        return error_message('Are you sure your account has an enabled CloneDB?')
    except Exception as e:
        return error_message(e.message)
51 52 53 54
            if 'tags' in occ and 'sample_set' in occ['tags']:
                info = get_info_of_viewable_sample_set([int(sample_id) for sample_id in occ['tags']['sample_set']], int(occ['tags']['config_id'][0]))
                occ['tags']['sample_set_viewable'] = info['viewable']
                occ['tags']['sample_set_name'] = info['name']
aomietanski's avatar
aomietanski committed
55
                occ['tags']['sample_tags'] = info['sample_tags']
56 57 58 59 60 61
                config_db = db.config[occ['tags']['config_id'][0]]
                occ['tags']['config_name'] = [config_db.name if config_db else None]
        results.append(occurrences)
    return response.json(results)

def get_info_of_viewable_sample_set(sample_sets, config):
aomietanski's avatar
aomietanski committed
62
    info = {'viewable': [], 'name': [], 'sample_tags': []}
63 64 65 66 67
    for sample_id in sample_sets:
        viewable = auth.can_view_sample_set(sample_id, auth.user)
        info['viewable'].append(viewable)
        if viewable:
            info['name'].append(get_sample_name(sample_id))
aomietanski's avatar
aomietanski committed
68 69 70
            tags = get_sample_set_tags(sample_id)
            for row in tags:
                info['sample_tags'].append("#" + row.name)
71 72 73
        else:
            info['name'].append(None)
    return info