Пример #1
0
def query_variants_handler(request, search_hash):
    """Search variants.
    """
    page = int(request.GET.get('page') or 1)
    per_page = int(request.GET.get('per_page') or 100)
    sort = request.GET.get('sort') or XPOS_SORT_KEY
    if sort == PATHOGENICTY_SORT_KEY and request.user.is_staff:
        sort = PATHOGENICTY_HGMD_SORT_KEY

    try:
        results_model = _get_or_create_results_model(search_hash, json.loads(request.body or '{}'), request.user)
    except Exception as e:
        logger.error(e)
        return create_json_response({'error': e.message}, status=400, reason=e.message)

    _check_results_permission(results_model, request.user)

    try:
        variants, total_results = get_es_variants(results_model, sort=sort, page=page, num_results=per_page)
    except InvalidIndexException as e:
        logger.error('InvalidIndexException: {}'.format(e))
        return create_json_response({'error': e.message}, status=400, reason=e.message)
    except ConnectionTimeout:
        return create_json_response({}, status=504, reason='Query Time Out')

    response = _process_variants(variants or [], results_model.families.all(), request.user)
    response['search'] = _get_search_context(results_model)
    response['search']['totalResults'] = total_results

    return create_json_response(response)
Пример #2
0
def query_variants_handler(request, search_hash):
    """Search variants.
    """
    page = int(request.GET.get('page') or 1)
    per_page = int(request.GET.get('per_page') or 100)
    sort = request.GET.get('sort') or XPOS_SORT_KEY
    if sort == PATHOGENICTY_SORT_KEY and user_is_analyst(request.user):
        sort = PATHOGENICTY_HGMD_SORT_KEY

    search_context = json.loads(request.body or '{}')
    try:
        results_model = _get_or_create_results_model(search_hash,
                                                     search_context,
                                                     request.user)
    except Exception as e:
        return create_json_response({'error': str(e)},
                                    status=400,
                                    reason=str(e))

    _check_results_permission(results_model, request.user)
    is_all_project_search = _is_all_project_family_search(search_context)

    variants, total_results = get_es_variants(
        results_model,
        sort=sort,
        page=page,
        num_results=per_page,
        skip_genotype_filter=is_all_project_search,
        user=request.user)

    response_context = {}
    if is_all_project_search and len(variants) == total_results:
        # For all project search only save the relevant families
        family_guids = set()
        for variant in variants:
            family_guids.update(variant['familyGuids'])
        families = results_model.families.filter(guid__in=family_guids)
        results_model.families.set(families)

        projects = Project.objects.filter(family__in=families).distinct()
        response_context = _get_projects_details(projects, request.user)

    response = _process_variants(variants or [], results_model.families.all(),
                                 request.user)
    response['search'] = _get_search_context(results_model)
    response['search']['totalResults'] = total_results
    response.update(response_context)

    return create_json_response(response)
Пример #3
0
def export_variants_handler(request, search_hash):
    results_model = VariantSearchResults.objects.get(search_hash=search_hash)

    _check_results_permission(results_model, request.user)

    families = results_model.families.all()
    family_ids_by_guid = {family.guid: family.family_id for family in families}

    variants, _ = get_es_variants(results_model, page=1, load_all=True)
    variants = _flatten_variants(variants)

    json, variants_to_saved_variants = _get_saved_variants(variants, families)

    max_families_per_variant = max([len(variant['familyGuids']) for variant in variants])
    max_samples_per_variant = max([len(variant['genotypes']) for variant in variants])

    rows = []
    for variant in variants:
        row = [_get_field_value(variant, config) for config in VARIANT_EXPORT_DATA]
        for i in range(max_families_per_variant):
            family_guid = variant['familyGuids'][i] if i < len(variant['familyGuids']) else ''
            variant_guid = variants_to_saved_variants.get(variant['variantId'], {}).get(family_guid, '')
            family_tags = {
                'family_id': family_ids_by_guid.get(family_guid),
                'tags': [tag for tag in json['variantTagsByGuid'].values() if variant_guid in tag['variantGuids']],
                'notes': [note for note in json['variantNotesByGuid'].values() if variant_guid in note['variantGuids']],
            }
            row += [_get_field_value(family_tags, config) for config in VARIANT_FAMILY_EXPORT_DATA]
        genotypes = variant['genotypes'].values()
        for i in range(max_samples_per_variant):
            if i < len(genotypes):
                row.append('{sampleId}:{numAlt}:{gq}:{ab}'.format(**genotypes[i]))
            else:
                row.append('')
        rows.append(row)

    header = [config['header'] for config in VARIANT_EXPORT_DATA]
    for i in range(max_families_per_variant):
        header += ['{}_{}'.format(config['header'], i+1) for config in VARIANT_FAMILY_EXPORT_DATA]
    header += ['sample_{}:num_alt_alleles:gq:ab'.format(i+1) for i in range(max_samples_per_variant)]

    file_format = request.GET.get('file_format', 'tsv')

    return export_table('search_results_{}'.format(search_hash), header, rows, file_format, titlecase_header=False)