Beispiel #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

    results_model = VariantSearchResults.objects.filter(
        search_hash=search_hash).first()
    if not results_model:
        if not request.body:
            return create_json_response(
                {},
                status=400,
                reason='Invalid search hash: {}'.format(search_hash))

        search_context = json.loads(request.body)

        project_families = search_context.get('projectFamilies')
        if not project_families:
            return create_json_response(
                {},
                status=400,
                reason='Invalid search: no projects/ families specified')

        search_dict = search_context.get('search', {})
        search_model = VariantSearch.objects.filter(search=search_dict).filter(
            Q(created_by=request.user) | Q(name__isnull=False)).first()
        if not search_model:
            search_model = VariantSearch.objects.create(
                created_by=request.user, search=search_dict)

        results_model = VariantSearchResults.objects.create(
            search_hash=search_hash, variant_search=search_model)

        all_families = set()
        for project_family in project_families:
            all_families.update(project_family['familyGuids'])
        results_model.families.set(
            Family.objects.filter(guid__in=all_families))

    _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:
        return create_json_response({}, status=400, reason=e.message)
    except ConnectionTimeout as e:
        return create_json_response({}, status=504, reason='Query Time Out')

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

    return create_json_response(response)
Beispiel #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 request.user.is_staff:
        sort = PATHOGENICTY_HGMD_SORT_KEY

    results_model = VariantSearchResults.objects.filter(
        search_hash=search_hash).first()
    if not results_model:
        if not request.body:
            return create_json_response(
                {},
                status=400,
                reason='Invalid search hash: {}'.format(search_hash))

        search_context = json.loads(request.body)

        project_families = search_context.get('projectFamilies')
        if not project_families:
            return create_json_response(
                {},
                status=400,
                reason='Invalid search: no projects/ families specified')

        search_model = VariantSearch.objects.create(created_by=request.user,
                                                    search=search_context.get(
                                                        'search', {}))

        results_model = VariantSearchResults.objects.create(
            search_hash=search_hash, variant_search=search_model, sort=sort)

        # TODO handle multiple projects
        results_model.families.set(
            Family.objects.filter(guid__in=project_families[0]['familyGuids']))

    elif results_model.sort != sort:
        families = results_model.families.all()
        results_model, created = VariantSearchResults.objects.get_or_create(
            search_hash=search_hash,
            variant_search=results_model.variant_search,
            sort=sort)
        if created:
            results_model.families.set(families)

    _check_results_permission(results_model, request.user)

    try:
        variants = get_es_variants(results_model,
                                   page=page,
                                   num_results=per_page)
    except InvalidIndexException as e:
        return create_json_response({}, status=400, reason=e.message)

    response = _process_variants(variants, results_model.families)
    response['search'] = _get_search_context(results_model)

    return create_json_response(response)
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

    results_model = VariantSearchResults.objects.filter(search_hash=search_hash).first()
    if not results_model:
        if not request.body:
            return create_json_response({}, status=400, reason='Invalid search hash: {}'.format(search_hash))

        search_context = json.loads(request.body)

        project_families = search_context.get('projectFamilies')
        if not project_families:
            return create_json_response({}, status=400, reason='Invalid search: no projects/ families specified')

        search_dict = search_context.get('search', {})
        search_model = VariantSearch.objects.filter(search=search_dict).filter(Q(created_by=request.user) | Q(name__isnull=False)).first()
        if not search_model:
            search_model = VariantSearch.objects.create(created_by=request.user, search=search_dict)

        results_model = VariantSearchResults.objects.create(search_hash=search_hash, variant_search=search_model)

        all_families = set()
        for project_family in project_families:
            all_families.update(project_family['familyGuids'])
        results_model.families.set(Family.objects.filter(guid__in=all_families))

    _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:
        return create_json_response({}, status=400, reason=e.message)
    except ConnectionTimeout as e:
        return create_json_response({}, status=504, reason='Query Time Out')

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

    return create_json_response(response)
Beispiel #4
0
def export_variants_handler(request, search_hash):
    results_model = VariantSearchResults.objects.get(search_hash=search_hash)

    _check_results_permission(results_model, request.user)

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

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

    saved_variants_by_guid = _get_saved_variants(variants)
    saved_variants_by_family = defaultdict(dict)
    for var in saved_variants_by_guid.values():
        for family_guid in var['familyGuids']:
            saved_variants_by_family[family_guid]['{}-{}-{}'.format(var['xpos'], var['ref'], var['alt'])] = var

    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 ''
            family_tags = saved_variants_by_family[family_guid].get('{}-{}-{}'.format(variant['xpos'], variant['ref'], variant['alt'])) or {}
            family_tags['family_id'] = family_ids_by_guid.get(family_guid)
            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):
            genotype = genotypes[i] if i < len(genotypes) else {}
            row += [_get_field_value(genotype, config) for config in VARIANT_GENOTYPE_EXPORT_DATA]
        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]
    for i in range(max_samples_per_variant):
        header += ['{}_{}'.format(config['header'], i+1) for config in VARIANT_GENOTYPE_EXPORT_DATA]

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

    return export_table('search_results_{}'.format(search_hash), header, rows, file_format, titlecase_header=False)
Beispiel #5
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:
        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:
        return create_json_response({'error': e.message},
                                    status=400,
                                    reason=e.message)
    except ConnectionTimeout as e:
        return create_json_response({}, status=504, reason='Query Time Out')

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

    return create_json_response(response)
Beispiel #6
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)

    saved_variants_by_guid, 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 ''
            family_tags = saved_variants_by_guid.get(
                variants_to_saved_variants.get(variant['variantId'], {}).get(
                    family_guid, '')) or {}
            family_tags['family_id'] = family_ids_by_guid.get(family_guid)
            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)