def saved_variant_data(request, project_guid, variant_guids=None): project = get_project_and_check_permissions(project_guid, request.user) family_guids = request.GET['families'].split(',') if request.GET.get('families') else None variant_guids = variant_guids.split(',') if variant_guids else None if family_guids: variant_query = SavedVariant.objects.filter(family__guid__in=family_guids) else: variant_query = SavedVariant.objects.filter(family__project=project) if variant_guids: variant_query = variant_query.filter(guid__in=variant_guids) if variant_query.count() < 1: return create_json_response({}, status=404, reason='Variant {} not found'.format(', '.join(variant_guids))) discovery_tags_query = None if user_is_analyst(request.user): discovery_tags_query = Q() for variant in variant_query: discovery_tags_query |= Q(Q(variant_id=variant.variant_id) & ~Q(family_id=variant.family_id)) discovery_tags_query &= Q(family__project__projectcategory__name=ANALYST_PROJECT_CATEGORY) response = get_json_for_saved_variants_with_tags(variant_query, add_details=True, discovery_tags_query=discovery_tags_query) variants = list(response['savedVariantsByGuid'].values()) genes = saved_variant_genes(variants) response['locusListsByGuid'] = _add_locus_lists([project], genes) discovery_tags = response.pop('discoveryTags', None) if discovery_tags: _add_discovery_tags(variants, discovery_tags) response['genesById'] = genes return create_json_response(response)
def saved_variant_data(request, project_guid, variant_guids=None): project = get_project_and_check_permissions(project_guid, request.user) family_guids = request.GET['families'].split(',') if request.GET.get('families') else None variant_guids = variant_guids.split(',') if variant_guids else None if family_guids: variant_query = SavedVariant.objects.filter(family__guid__in=family_guids) else: get_note_only = bool(request.GET.get('includeNoteVariants')) variant_query = SavedVariant.objects.filter(family__project=project, varianttag__isnull=get_note_only).distinct() if variant_guids: variant_query = variant_query.filter(guid__in=variant_guids) if variant_query.count() < 1: return create_json_response({}, status=404, reason='Variant {} not found'.format(', '.join(variant_guids))) response = get_json_for_saved_variants_with_tags(variant_query, add_details=True) discovery_tags = None if user_is_analyst(request.user): discovery_tags, discovery_response = get_json_for_discovery_tags(response['savedVariantsByGuid'].values()) response.update(discovery_response) variants = list(response['savedVariantsByGuid'].values()) genes = saved_variant_genes(variants) response['locusListsByGuid'] = _add_locus_lists([project], genes) if discovery_tags: _add_discovery_tags(variants, discovery_tags) response['genesById'] = genes return create_json_response(response)
def saved_variants_page(request, tag): gene = request.GET.get('gene') if tag == 'ALL': saved_variant_models = SavedVariant.objects.exclude(varianttag=None) else: tag_type = VariantTagType.objects.get(name=tag, project__isnull=True) saved_variant_models = SavedVariant.objects.filter(varianttag__variant_tag_type=tag_type) saved_variant_models = saved_variant_models.filter(family__project__in=get_projects_user_can_view(request.user)) if gene: saved_variant_models = saved_variant_models.filter(saved_variant_json__transcripts__has_key=gene) elif saved_variant_models.count() > MAX_SAVED_VARIANTS: return create_json_response({'error': 'Select a gene to filter variants'}, status=400) prefetch_related_objects(saved_variant_models, 'family__project') response_json = get_json_for_saved_variants_with_tags(saved_variant_models, add_details=True, include_missing_variants=True) project_models_by_guid = {variant.family.project.guid: variant.family.project for variant in saved_variant_models} families = {variant.family for variant in saved_variant_models} individuals = Individual.objects.filter(family__in=families) saved_variants = list(response_json['savedVariantsByGuid'].values()) genes = saved_variant_genes(saved_variants) locus_lists_by_guid = _add_locus_lists(list(project_models_by_guid.values()), genes, include_all_lists=True) projects_json = get_json_for_projects(list(project_models_by_guid.values()), user=request.user, add_project_category_guids_field=False) functional_tag_types = get_json_for_variant_functional_data_tag_types() variant_tag_types = VariantTagType.objects.filter(Q(project__in=project_models_by_guid.values()) | Q(project__isnull=True)) prefetch_related_objects(variant_tag_types, 'project') variant_tags_json = _get_json_for_models(variant_tag_types) tag_projects = {vt.guid: vt.project.guid for vt in variant_tag_types if vt.project} for project_json in projects_json: project_guid = project_json['projectGuid'] project_variant_tags = [ vt for vt in variant_tags_json if tag_projects.get(vt['variantTagTypeGuid'], project_guid) == project_guid] project_json.update({ 'locusListGuids': list(locus_lists_by_guid.keys()), 'variantTagTypes': sorted(project_variant_tags, key=lambda variant_tag_type: variant_tag_type['order'] or 0), 'variantFunctionalTagTypes': functional_tag_types, }) families_json = _get_json_for_families(list(families), user=request.user, add_individual_guids_field=True) individuals_json = _get_json_for_individuals(individuals, add_hpo_details=True, user=request.user) for locus_list in get_json_for_locus_lists(LocusList.objects.filter(guid__in=locus_lists_by_guid.keys()), request.user): locus_lists_by_guid[locus_list['locusListGuid']].update(locus_list) response_json.update({ 'genesById': genes, 'projectsByGuid': {project['projectGuid']: project for project in projects_json}, 'familiesByGuid': {family['familyGuid']: family for family in families_json}, 'individualsByGuid': {indiv['individualGuid']: indiv for indiv in individuals_json}, 'locusListsByGuid': locus_lists_by_guid, }) return create_json_response(response_json)
def _process_variants(variants, families, user): if not variants: return {'searchedVariants': variants} prefetch_related_objects(families, 'project') genes = saved_variant_genes(variants) projects = {family.project for family in families} locus_lists_by_guid = _add_locus_lists(projects, genes) response_json, _ = _get_saved_variants(variants, families, include_discovery_tags=user_is_analyst(user)) response_json.update({ 'searchedVariants': variants, 'genesById': genes, 'locusListsByGuid': locus_lists_by_guid, }) return response_json