コード例 #1
0
ファイル: views.py プロジェクト: ericminikel/xbrowse
def mendelian_variant_search_spec(request):

    project, family = get_project_and_family_for_user(request.user, request.GET)

    # TODO: use form

    search_hash = request.GET.get('search_hash')
    search_spec_dict, variants = cache_utils.get_cached_results(project.project_id, search_hash)
    search_spec = MendelianVariantSearchSpec.fromJSON(search_spec_dict)
    if variants is None:
        variants = api_utils.calculate_mendelian_variant_search(search_spec, family.xfamily())
    else:
        variants = [Variant.fromJSON(v) for v in variants]
    add_extra_info_to_variants_family(get_reference(), family, variants)
    return_type = request.GET.get('return_type')
    if return_type == 'json' or not return_type:
        return JSONResponse({
            'is_error': False,
            'variants': [v.toJSON() for v in variants],
            'search_spec': search_spec_dict,
        })
    elif request.GET.get('return_type') == 'csv':
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="xbrowse_results_{}.csv"'.format(search_hash)
        writer = csv.writer(response)
        indiv_ids = family.indiv_ids_with_variant_data()
        headers = xbrowse_displays.get_variant_display_headers(indiv_ids)
        writer.writerow(headers)
        for variant in variants:
            fields = xbrowse_displays.get_display_fields_for_variant(variant, get_reference(), indiv_ids)
            writer.writerow(fields)
        return response
コード例 #2
0
ファイル: family_group_views.py プロジェクト: dmyung/xbrowse
def family_group_gene(request, project_id, family_group_slug, gene_id):

    project = get_object_or_404(Project, project_id=project_id)
    family_group = get_object_or_404(FamilyGroup,
                                     project=project,
                                     slug=family_group_slug)
    if not project.can_view(request.user):
        return HttpResponse('unauthorized')

    gene_id = get_gene_id_from_str(gene_id, get_reference())
    gene = get_reference().get_gene(gene_id)

    varfilter = get_default_variant_filter(
        'all_coding',
        mall.get_annotator().reference_population_slugs)
    variants_by_family = family_group_analysis.get_variants_in_gene(
        family_group, gene_id, variant_filter=varfilter)

    return render(
        request, 'family_group/family_group_gene.html', {
            'project': project,
            'family_group': family_group,
            'family_group_json': json.dumps(family_group.toJSON()),
            'gene_json': json.dumps(gene),
            'gene': gene,
            'variants_by_family_json': json.dumps(variants_by_family),
        })
コード例 #3
0
ファイル: utils.py プロジェクト: ericminikel/xbrowse
def calculate_mendelian_variant_search(search_spec, xfamily):

    variants = None

    if search_spec.search_mode == 'standard_inheritance':

        variants = list(get_variants_with_inheritance_mode(
            get_mall(),
            xfamily,
            search_spec.inheritance_mode,
            variant_filter=search_spec.variant_filter,
            quality_filter=search_spec.genotype_quality_filter,
        ))

    elif search_spec.search_mode == 'custom_inheritance':

        variants = list(get_variants_family(
            get_datastore(),
            xfamily,
            genotype_filter=search_spec.genotype_inheritance_filter,
            variant_filter=search_spec.variant_filter,
            quality_filter=search_spec.genotype_quality_filter,
        ))

    elif search_spec.search_mode == 'gene_burden':

        gene_stream = get_genes_family(
            get_datastore(),
            get_reference(),
            xfamily,
            burden_filter=search_spec.gene_burden_filter,
            variant_filter=search_spec.variant_filter,
            quality_filter=search_spec.genotype_quality_filter,
        )

        variants = list(stream_utils.gene_stream_to_variant_stream(gene_stream, get_reference()))

    elif search_spec.search_mode == 'allele_count':

        variants = list(get_variants_allele_count(
            get_datastore(),
            xfamily,
            search_spec.allele_count_filter,
            variant_filter=search_spec.variant_filter,
            quality_filter=search_spec.genotype_quality_filter,
        ))

    elif search_spec.search_mode == 'all_variants':
        variants = list(get_variants_family(
            get_datastore(),
            xfamily,
            variant_filter=search_spec.variant_filter,
            quality_filter=search_spec.genotype_quality_filter,
        ))

    return variants
コード例 #4
0
ファイル: load_resources.py プロジェクト: batsal/xbrowse
    def handle(self, *args, **options):

        if settings.CUSTOM_ANNOTATOR_SETTINGS is not None:
            print("Load dbNSFP.. ")

            # note that you could use mall.get_custom_annotator() here too
            custom_annotator = CustomAnnotator(settings.CUSTOM_ANNOTATOR_SETTINGS)
            custom_annotator.load()

        get_reference().load()
        mall.get_annotator().load()
コード例 #5
0
ファイル: reference_views.py プロジェクト: batsal/xbrowse
def gene_info(request, gene_str):

    real_gene_id = get_gene_id_from_str(gene_str, get_reference())
    gene = get_reference().get_gene(real_gene_id)
    gene['expression'] = get_reference().get_tissue_expression_display_values(real_gene_id)
    gene_json = json.dumps(gene)

    return render(request, 'gene_info.html', {
        'gene_json': gene_json,
        'gene_symbol': gene['symbol'],
    })
コード例 #6
0
ファイル: family_views.py プロジェクト: jdagilliland/seqr
def family_coverage_gene(request, family, gene_id):

    project_id = family.project.project_id
    gene = get_reference().get_gene(gene_id)
    gene_structure = get_reference().get_gene_structure(gene_id)
    individuals = family.get_individuals()
    indiv_ids = [i.indiv_id for i in individuals]
    num_individuals = len(indiv_ids)

    coding_regions = []
    for c in get_coding_regions_from_gene_structure(gene_id, gene_structure):
        coding_region = {}
        coding_region['start'] = genomeloc.get_chr_pos(c.xstart)[1]
        coding_region['stop'] = genomeloc.get_chr_pos(c.xstop)[1]
        coding_region['gene_id'] = c.gene_id
        coding_region['size'] = c.xstop - c.xstart + 1
        coding_regions.append(coding_region)

    coverages = {}
    for individual in individuals:
        coverages[
            individual.indiv_id] = get_coverage_store().get_coverage_for_gene(
                str(individual.pk), gene['gene_id'])

    whole_gene = Counter({'callable': 0, 'low_coverage': 0, 'poor_mapping': 0})
    for coverage_spec in coverages.values():
        whole_gene['callable'] += coverage_spec['gene_totals']['callable']
        whole_gene['low_coverage'] += coverage_spec['gene_totals'][
            'low_coverage']
        whole_gene['poor_mapping'] += coverage_spec['gene_totals'][
            'poor_mapping']
    gene_coding_size = 0
    for c in coding_regions:
        gene_coding_size += c['stop'] - c['start'] + 1
    totalsize = gene_coding_size * num_individuals
    whole_gene['ratio_callable'] = whole_gene['callable'] / float(totalsize)
    whole_gene['ratio_low_coverage'] = whole_gene['low_coverage'] / float(
        totalsize)
    whole_gene['ratio_poor_mapping'] = whole_gene['poor_mapping'] / float(
        totalsize)
    whole_gene['gene_coding_size'] = gene_coding_size

    return render(
        request, 'coverage/family_coverage_gene.html', {
            'project': family.project,
            'family': family,
            'gene': gene,
            'coverages_json': json.dumps(coverages),
            'whole_gene_json': json.dumps(whole_gene),
            'coding_regions_json': json.dumps(coding_regions),
            'indiv_ids_json': json.dumps(indiv_ids),
            'individuals': individuals,
            'whole_gene': whole_gene,
        })
コード例 #7
0
def gene_info(request, gene_str):

    real_gene_id = get_gene_id_from_str(gene_str, get_reference())
    gene = get_reference().get_gene(real_gene_id)
    gene['expression'] = get_reference().get_tissue_expression_display_values(
        real_gene_id)
    gene_json = json.dumps(gene)

    return render(request, 'gene_info.html', {
        'gene_json': gene_json,
        'gene_symbol': gene['symbol'],
    })
コード例 #8
0
ファイル: utils.py プロジェクト: mattsolo1/seqr
def calculate_mendelian_variant_search(search_spec, xfamily):
    sys.stderr.write("     mendelian_variant_search for %s - search mode: %s  %s\n" % (xfamily.project_id, search_spec.search_mode, search_spec.__dict__))

    variants = None
    if search_spec.search_mode == 'standard_inheritance':
        variants = list(get_variants_with_inheritance_mode(
            get_mall(xfamily.project_id),
            xfamily,
            search_spec.inheritance_mode,
            variant_filter=search_spec.variant_filter,
            quality_filter=search_spec.quality_filter,
        ))

    elif search_spec.search_mode == 'custom_inheritance':
        variants = list(get_variants_family(
            get_datastore(xfamily.project_id),
            xfamily,
            genotype_filter=search_spec.genotype_inheritance_filter,
            variant_filter=search_spec.variant_filter,
            quality_filter=search_spec.quality_filter,
        ))

    elif search_spec.search_mode == 'gene_burden':
        gene_stream = get_genes_family(
            get_datastore(xfamily.project_id),
            get_reference(),
            xfamily,
            burden_filter=search_spec.gene_burden_filter,
            variant_filter=search_spec.variant_filter,
            quality_filter=search_spec.quality_filter,
        )

        variants = list(stream_utils.gene_stream_to_variant_stream(gene_stream, get_reference()))

    elif search_spec.search_mode == 'allele_count':
        variants = list(get_variants_allele_count(
            get_datastore(xfamily.project_id),
            xfamily,
            search_spec.allele_count_filter,
            variant_filter=search_spec.variant_filter,
            quality_filter=search_spec.quality_filter,
        ))

    elif search_spec.search_mode == 'all_variants':
        variants = list(get_variants_family(
            get_datastore(xfamily.project_id),
            xfamily,
            variant_filter=search_spec.variant_filter,
            quality_filter=search_spec.quality_filter,
            indivs_to_consider=xfamily.indiv_id_list(),
        ))

    return variants
コード例 #9
0
ファイル: load_resources.py プロジェクト: rpete/seqr
    def handle(self, *args, **options):

        if settings.CUSTOM_ANNOTATOR_SETTINGS is not None:
            print("Load dbNSFP.. ")

            # note that you could use mall.get_custom_annotator() here too
            custom_annotator = CustomAnnotator(
                settings.CUSTOM_ANNOTATOR_SETTINGS)
            custom_annotator.load()

        get_reference().load()
        mall.get_annotator().load()
コード例 #10
0
ファイル: project_views.py プロジェクト: dmyung/xbrowse
def gene_quicklook(request, project_id, gene_id):
    """
    Summary of a gene in a project
    """
    project = get_object_or_404(Project, project_id=project_id)
    if not project.can_view(request.user):
        return HttpResponse("Unauthorized")
    gene_id = get_gene_id_from_str(gene_id, get_reference())
    gene = get_reference().get_gene(gene_id)
    sys.stderr.write(project_id + " - staring gene search for: %s %s \n" %
                     (gene_id, gene))
    variant_filter = get_default_variant_filter(
        'all_coding',
        mall.get_annotator().reference_population_slugs)
    num_indivs = len(
        [i for i in project.get_individuals() if i.has_variant_data()])
    aac_threshold = (.2 * num_indivs) + 5
    rare_variants = []
    for variant in project_analysis.get_variants_in_gene(
            project, gene_id, variant_filter=variant_filter):
        aac = get_alt_allele_count(variant)
        max_af = max(variant.annotation['freqs'].values())
        if aac <= aac_threshold and max_af < .01:
            rare_variants.append(variant)

    add_extra_info_to_variants_project(get_reference(), project, rare_variants)

    knockouts = []
    knockout_ids, variation = get_knockouts_in_gene(project, gene_id)
    for kid in knockout_ids:
        variants = variation.get_relevant_variants_for_indiv_ids([kid])
        add_extra_info_to_variants_project(get_reference(), project, variants)
        knockouts.append({
            'indiv_id': kid,
            'variants': [v.toJSON() for v in variants],
        })

    sys.stderr.write("Retrieved %s variants \n" % len(rare_variants))
    return render(
        request, 'project/gene_quicklook.html', {
            'gene':
            gene,
            'gene_json':
            json.dumps(gene),
            'project':
            project,
            'rare_variants_json':
            json.dumps([v.toJSON() for v in rare_variants]),
            'individuals_json':
            json.dumps([i.get_json_obj() for i in project.get_individuals()]),
            'knockouts_json':
            json.dumps(knockouts),
        })
コード例 #11
0
ファイル: views.py プロジェクト: rpete/seqr
def gene_info(request, gene_id):

    gene = get_reference().get_gene(gene_id)
    gene['expression'] = get_reference().get_tissue_expression_display_values(gene_id)

    ret = {
        'gene': gene,
        'is_error': False, 
        'found_gene': gene is not None, 
    }

    return JSONResponse(ret)
コード例 #12
0
ファイル: views.py プロジェクト: frichter/seqr
def gene_info(request, gene_id):

    gene = get_reference().get_gene(gene_id)
    gene['expression'] = get_reference().get_tissue_expression_display_values(gene_id)

    ret = {
        'gene': gene,
        'is_error': False, 
        'found_gene': gene is not None, 
    }

    return JSONResponse(ret)
コード例 #13
0
ファイル: reference_views.py プロジェクト: macarthur-lab/seqr
def gene_info(request, gene_str):

    real_gene_id = get_gene_id_from_str(gene_str, get_reference())
    gene = get_reference().get_gene(real_gene_id)
    gene['expression'] = get_reference().get_tissue_expression_display_values(real_gene_id)
    add_notes_to_genes([gene], request.user)
    gene_json = json.dumps(gene)

    return render(request, 'gene_info.html', {
        'gene_json': gene_json,
        'gene_symbol': gene['symbol'],
        'new_page_url': '/gene_info/{}'.format(gene_str),
    })
コード例 #14
0
ファイル: family_views.py プロジェクト: burkesquires/xbrowse
def family_coverage_gene(request, family, gene_id):

    project_id = family.project.project_id
    gene = get_reference().get_gene(gene_id)
    gene_structure = get_reference().get_gene_structure(gene_id)
    individuals = family.get_individuals()
    indiv_ids = [i.indiv_id for i in individuals]
    num_individuals = len(indiv_ids)

    coding_regions = []
    for c in get_coding_regions_from_gene_structure(gene_id, gene_structure):
        coding_region = {}
        coding_region['start'] = genomeloc.get_chr_pos(c.xstart)[1]
        coding_region['stop'] = genomeloc.get_chr_pos(c.xstop)[1]
        coding_region['gene_id'] = c.gene_id
        coding_region['size'] = c.xstop-c.xstart+1
        coding_regions.append(coding_region)

    coverages = {}
    for individual in individuals:
        coverages[individual.indiv_id] = get_coverage_store().get_coverage_for_gene(
            str(individual.pk),
            gene['gene_id']
        )

    whole_gene = Counter({'callable': 0, 'low_coverage': 0, 'poor_mapping': 0})
    for coverage_spec in coverages.values():
        whole_gene['callable'] += coverage_spec['gene_totals']['callable']
        whole_gene['low_coverage'] += coverage_spec['gene_totals']['low_coverage']
        whole_gene['poor_mapping'] += coverage_spec['gene_totals']['poor_mapping']
    gene_coding_size = 0
    for c in coding_regions:
        gene_coding_size += c['stop']-c['start']+1
    totalsize = gene_coding_size*num_individuals
    whole_gene['ratio_callable'] = whole_gene['callable'] / float(totalsize)
    whole_gene['ratio_low_coverage'] = whole_gene['low_coverage'] / float(totalsize)
    whole_gene['ratio_poor_mapping'] = whole_gene['poor_mapping'] / float(totalsize)
    whole_gene['gene_coding_size'] = gene_coding_size

    return render(request, 'coverage/family_coverage_gene.html', {
        'project': family.project,
        'family': family,
        'gene': gene,
        'coverages_json': json.dumps(coverages),
        'whole_gene_json': json.dumps(whole_gene),
        'coding_regions_json': json.dumps(coding_regions),
        'indiv_ids_json': json.dumps(indiv_ids),
        'individuals': individuals,
        'whole_gene': whole_gene,
    })
コード例 #15
0
ファイル: reference_views.py プロジェクト: zmcv/seqr
def gene_info(request, gene_str):

    real_gene_id = get_gene_id_from_str(gene_str, get_reference())
    gene = get_reference().get_gene(real_gene_id)
    gene['expression'] = get_reference().get_tissue_expression_display_values(
        real_gene_id)
    add_notes_to_genes([gene], request.user)
    gene_json = json.dumps(gene)

    return render(
        request, 'gene_info.html', {
            'gene_json': gene_json,
            'gene_symbol': gene['symbol'],
            'new_page_url': '/gene_info/{}'.format(gene_str),
        })
コード例 #16
0
ファイル: views.py プロジェクト: frichter/seqr
def cohort_gene_search_variants(request):

    # TODO: this view not like the others - refactor to forms

    error = None

    project, cohort = get_project_and_cohort_for_user(request.user, request.GET)
    if not project.can_view(request.user):
        return PermissionDenied

    form = api_forms.CohortGeneSearchVariantsForm(request.GET)
    if form.is_valid():
        gene_id = form.cleaned_data['gene_id']
        inheritance_mode = form.cleaned_data['inheritance_mode']
        variant_filter = form.cleaned_data['variant_filter']
        quality_filter = form.cleaned_data['quality_filter']
    else:
        error = server_utils.form_error_string(form)

    if not error:

        indivs_with_inheritance, gene_variation = cohort_search.get_individuals_with_inheritance_in_gene(
            get_datastore(project.project_id),
            get_reference(),
            cohort.xcohort(),
            inheritance_mode,
            gene_id,
            variant_filter=variant_filter,
            quality_filter=quality_filter
        )

        relevant_variants = gene_variation.get_relevant_variants_for_indiv_ids(cohort.indiv_id_list())

        api_utils.add_extra_info_to_variants_family(get_reference(), cohort, relevant_variants)

        ret = {
            'is_error': False, 
            'variants': [v.toJSON() for v in relevant_variants],
            'gene_info': get_reference().get_gene(gene_id),
        }
        return JSONResponse(ret)

    else: 
        ret = {
            'is_error': True, 
            'error': error
        }
        return JSONResponse(ret)
コード例 #17
0
def control_comparison(control_cohort, gene_id, sample_hits, sample_size, inheritance_mode, variant_filter, quality_filter):
    """
    Compare the results of num_hits, total against the reference population
    Return dict of 'num_hits', 'fisher_2sided_palue',
    """
    cohort = get_population_datastore().get_control_cohort(control_cohort)
    indivs_with_inheritance, gene_variation = get_individuals_with_inheritance_in_gene(
        get_population_datastore(),
        get_reference(),
        cohort,
        inheritance_mode,
        gene_id,
        variant_filter=variant_filter,
        quality_filter=quality_filter
        )
    control_hits = len(indivs_with_inheritance)
    fisher_results = fisher.pvalue(
        sample_hits,
        sample_size,
        control_hits,
        get_population_datastore().get_control_cohort_size(settings.DEFAULT_CONTROL_COHORT)
    )
    return {
        'control_hits': control_hits,
        'fisher_2sided_pvalue': fisher_results.two_tail,
    }
コード例 #18
0
ファイル: views.py プロジェクト: rpete/seqr
def mendelian_variant_search_spec(request):

    project, family = get_project_and_family_for_user(request.user, request.GET)

    search_hash = request.GET.get('search_hash')
    search_spec_dict, variants = cache_utils.get_cached_results(project.project_id, search_hash)
    search_spec = MendelianVariantSearchSpec.fromJSON(search_spec_dict)
    if variants is None:
        variants = api_utils.calculate_mendelian_variant_search(search_spec, family.xfamily())
    else:
        variants = [Variant.fromJSON(v) for v in variants]
    add_extra_info_to_variants_family(get_reference(), family, variants)
    return_type = request.GET.get('return_type')
    if return_type == 'json' or not return_type:
        return JSONResponse({
            'is_error': False,
            'variants': [v.toJSON() for v in variants],
            'search_spec': search_spec_dict,
        })
    elif request.GET.get('return_type') == 'csv':
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="results_{}.csv"'.format(search_hash)
        writer = csv.writer(response)
        indiv_ids = family.indiv_ids_with_variant_data()
        headers = xbrowse_displays.get_variant_display_headers(get_mall(project.project_id), project, indiv_ids)
        writer.writerow(headers)
        for variant in variants:
            fields = xbrowse_displays.get_display_fields_for_variant(get_mall(project.project_id), project, variant, indiv_ids)
            writer.writerow(fields)
        return response
コード例 #19
0
ファイル: project_views.py プロジェクト: tianyunwang/seqr
def causal_variants(request, project_id):

    project = get_object_or_404(Project, project_id=project_id)
    if not project.can_view(request.user):
        raise PermissionDenied

    variants = get_causal_variants_for_project(project)
    add_extra_info_to_variants_project(get_reference(),
                                       project,
                                       variants,
                                       add_family_tags=True,
                                       add_populations=True)

    return render(
        request, 'project/causal_variants.html', {
            'project':
            project,
            'variants_json':
            json.dumps([v.toJSON() for v in variants]),
            'families_json':
            json.dumps({
                family.family_id: family.get_json_obj()
                for family in project.get_families()
            })
        })
コード例 #20
0
ファイル: views.py プロジェクト: rpete/seqr
def cohort_gene_search(request):

    project, cohort = get_project_and_cohort_for_user(request.user, request.GET)
    sys.stderr.write("cohort_gene_search %s  %s: starting ... \n" % (project.project_id, cohort.cohort_id))
    form = api_forms.CohortGeneSearchForm(request.GET)
    if form.is_valid():
        search_spec = form.cleaned_data['search_spec']
        search_spec.cohort_id = cohort.cohort_id
        sys.stderr.write("cohort_gene_search %s  %s: search spec: %s \n" % (project.project_id, cohort.cohort_id, str(search_spec.toJSON())))
        genes = api_utils.calculate_cohort_gene_search(cohort, search_spec)
        sys.stderr.write("cohort_gene_search %s  %s: get %s genes \n" % (project.project_id, cohort.cohort_id, len(genes)))
        search_hash = cache_utils.save_results_for_spec(project.project_id, search_spec.toJSON(), genes)
        api_utils.add_extra_info_to_genes(project, get_reference(), genes)
        sys.stderr.write("cohort_gene_search %s  %s: done adding extra info \n" % (project.project_id, cohort.cohort_id))
        return JSONResponse({
            'is_error': False,
            'genes': genes,
            'search_hash': search_hash,
        })

    else:
        return JSONResponse({
            'is_error': True,
            'error': server_utils.form_error_string(form)
        })
コード例 #21
0
ファイル: project_views.py プロジェクト: burkesquires/xbrowse
def saved_variants(request, project_id):

    project = get_object_or_404(Project, project_id=project_id)
    if not project.can_view(request.user):
        return HttpResponse('unauthorized')
    
    variants = get_all_saved_variants_for_project(project)
    if 'family' in request.GET:
        requested_family_id = request.GET.get('family')
        variants = filter(lambda v: v.extras['family_id'] == requested_family_id, variants)
        
    variants = sorted(variants, key=lambda v: (v.extras['family_id'], v.xpos))
    grouped_variants = itertools.groupby(variants, key=lambda v: v.extras['family_id'])
    for family_id, family_variants in grouped_variants:
        family = Family.objects.get(project=project, family_id=family_id)
        family_variants = list(family_variants)

        add_extra_info_to_variants_family(get_reference(), family, family_variants)

    return render(request, 'project/saved_variants.html', {
        'project': project,
        'tag': None,
        'variants_json': json.dumps([v.toJSON() for v in variants]),
        'families_json': json.dumps({family.family_id: family.get_json_obj() for family in project.get_families()})
    })
コード例 #22
0
ファイル: family_views.py プロジェクト: burkesquires/xbrowse
def family_coverage_gene_list(request, family, gene_list):
    """
    Table of summary coverages for each gene in the gene list
    """
    sample_id_list = [str(individual.pk) for individual in family.get_individuals()]

    cache_key = ('family_coverage_gene_list', family.project.project_id, family.family_id, gene_list.slug)
    cached_results = server_utils.get_cached_results(cache_key)
    if cached_results:
        gene_coverages = cached_results
    else:
        gene_coverages = []
        for gene_id in gene_list.gene_id_list():
            d = {
                'gene_id': gene_id,
                'gene_name': get_reference().get_gene_symbol(gene_id),
                'totals': get_coverage_store().get_coverage_totals_for_gene(gene_id, sample_id_list)
            }
            d['coding_size'] = sum(d['totals'].values())
            try:
                d['percent'] = float(d['totals']['callable'])*100 / d['coding_size']
            except ZeroDivisionError:
                d['percent'] = 0
            gene_coverages.append(d)
        server_utils.save_results_cache(cache_key, gene_coverages)

    return render(request, 'coverage/family_coverage_gene_list.html', {
        'project': family.project,
        'family': family,
        'gene_list': gene_list,
        'gene_coverages': gene_coverages,
    })
コード例 #23
0
ファイル: views.py プロジェクト: zmcv/seqr
def breakpoint_search(request, project_id, family_id):

    log.info("Showing main breakpoint search page")

    project = get_object_or_404(Project, project_id=project_id)
    family = get_object_or_404(Family, project=project, family_id=family_id)
    if not project.can_view(request.user):
        raise PermissionDenied

    if not family.has_data('breakpoints'):
        return render(
            request, 'analysis_unavailable.html',
            {'reason': 'This family does not have any breakpoint data.'})

    gene_lists = project.get_gene_list_map()

    gene_list_json = dict([(get_reference().get_gene_symbol(g),
                            [gl.name for gl in gll])
                           for g, gll in gene_lists.iteritems()])

    bam_file_paths = dict(
        (ind.indiv_id,
         os.path.join(settings.READ_VIZ_BAM_PATH, ind.bam_file_path))
        for ind in family.get_individuals())

    return render(
        request, 'breakpoint_search.html', {
            'project': project,
            'family': family,
            'gene_lists_json': json.dumps(gene_list_json),
            'bam_files_json': json.dumps(bam_file_paths),
        })
コード例 #24
0
ファイル: utilities.py プロジェクト: rpete/seqr
def fetch_project_individuals_data(project_id):
    """
    Notes:
      1. ONLY project-authorized user has access to this individual
    """
    project = get_object_or_404(Project, project_id=project_id)
    variants = get_causal_variants_for_project(project)
    variants = sorted(variants, key=lambda v: (v.extras['family_id'], v.xpos))
    grouped_variants = itertools.groupby(variants, key=lambda v: v.extras['family_id'])
    for family_id, family_variants in grouped_variants:
        family = Family.objects.get(project=project, family_id=family_id)
        family_variants = list(family_variants)
        add_extra_info_to_variants_family(get_reference(), family, family_variants)

    family_data = [v.toJSON() for v in variants]
    variant_data = {family.family_id: family.get_json_obj() for family in project.get_families()}

    phenotype_entry_counts = gather_phenotype_data_for_project(project_id, variant_data)

    status_description_map = {}
    for abbrev, details in ANALYSIS_STATUS_CHOICES:
        status_description_map[abbrev] = details[0]
    families_json = json_displays.family_list(project.get_families())
    family_statuses = {}
    for f in families_json:
        family_statuses[f['family_id']] = status_description_map[f['analysis_status']['status']]

    return family_data, variant_data, phenotype_entry_counts, family_statuses
コード例 #25
0
ファイル: model_utils.py プロジェクト: bnouyou/seqr
def deprecated_retrieve_saved_variants_json(project, variant_tuples,
                                            create_if_missing):
    project_id = project.deprecated_project_id
    xbrowse_project = BaseProject.objects.get(project_id=project_id)
    user = User.objects.filter(is_staff=True).first(
    )  # HGMD annotations are only returned for staff users

    variants = get_variants_from_variant_tuples(xbrowse_project,
                                                variant_tuples,
                                                user=user)
    if not create_if_missing:
        variants = [
            var for var in variants if not var.get_extra('created_variant')
        ]
    add_extra_info_to_variants_project(get_reference(),
                                       xbrowse_project,
                                       variants,
                                       add_populations=True)

    family_guids_by_id = {
        f.family_id: f.guid
        for f in Family.objects.filter(project=project)
    }
    individual_guids_by_id = {
        i.individual_id: i.guid
        for i in Individual.objects.filter(family__project=project)
    }
    return [
        _variant_details(variant.toJSON(), family_guids_by_id,
                         individual_guids_by_id) for variant in variants
    ]
コード例 #26
0
ファイル: views.py プロジェクト: rpete/seqr
def diagnostic_search(request):

    project, family = utils.get_project_and_family_for_user(request.user, request.GET)
    if not project.can_view(request.user):
        raise PermissionDenied

    form = api_forms.DiagnosticSearchForm(family, request.GET)
    if form.is_valid():

        search_spec = form.cleaned_data['search_spec']
        search_spec.family_id = family.family_id

        gene_list = form.cleaned_data['gene_list']
        diagnostic_info_list = []
        for gene_id in gene_list.gene_id_list():
            diagnostic_info = get_gene_diangostic_info(family, gene_id, search_spec.variant_filter)
            add_extra_info_to_variants_family(get_reference(), family, diagnostic_info._variants)
            diagnostic_info_list.append(diagnostic_info)



        return JSONResponse({
            'is_error': False,
            'gene_diagnostic_info_list': [d.toJSON() for d in diagnostic_info_list],
            'gene_list_info': gene_list.toJSON(details=True),
            'data_summary': family.get_data_summary(),
        })

    else:
        return JSONResponse({
            'is_error': True,
            'error': server_utils.form_error_string(form)
        })
コード例 #27
0
def _set_saved_variant_json(new_variant_tag_or_note, source_variant_tag_or_note, new_family):
    if new_family is None:
        return

    project_id = new_family.project.deprecated_project_id
    project = Project.objects.get(project_id=project_id)
    try:
        variant_info = get_datastore(project).get_single_variant(
            project_id,
            new_family.family_id,
            source_variant_tag_or_note.xpos,
            source_variant_tag_or_note.ref,
            source_variant_tag_or_note.alt)
    except Exception as e:
        logger.error("Unable to retrieve variant annotations for %s %s: %s" % (
            new_family, source_variant_tag_or_note, e))
        return

    if variant_info:
        add_extra_info_to_variants_project(get_reference(), project, [variant_info], add_family_tags=True,
                                           add_populations=True)
        variant_json = variant_info.toJSON()

        new_variant_tag_or_note.saved_variant_json = json.dumps(variant_json)
        new_variant_tag_or_note.save()
コード例 #28
0
ファイル: views.py プロジェクト: rpete/seqr
def combine_mendelian_families_variants(request):

    project, family_group = utils.get_project_and_family_group_for_user(request.user, request.GET)

    form = api_forms.CombineMendelianFamiliesVariantsForm(request.GET)
    if form.is_valid():
        variants_grouped = get_variants_by_family_for_gene(
            get_mall(project.project_id),
            [f.xfamily() for f in form.cleaned_data['families']],
            form.cleaned_data['inheritance_mode'],
            form.cleaned_data['gene_id'],
            variant_filter=form.cleaned_data['variant_filter'],
            quality_filter=form.cleaned_data['quality_filter']
        )
        variants_by_family = []
        for family in form.cleaned_data['families']:
            variants = variants_grouped[(family.project.project_id, family.family_id)]
            add_extra_info_to_variants_family(get_reference(), family, variants)
            variants_by_family.append({
                'project_id': family.project.project_id,
                'family_id': family.family_id,
                'family_name': str(family),
                'variants': [v.toJSON() for v in variants],
            })
        return JSONResponse({
            'is_error': False,
            'variants_by_family': variants_by_family,
        })

    else:
        return JSONResponse({
            'is_error': True,
            'error': server_utils.form_error_string(form)
        })
コード例 #29
0
ファイル: views.py プロジェクト: rpete/seqr
def combine_mendelian_families(request):

    project, family_group = utils.get_project_and_family_group_for_user(request.user, request.GET)
    if not project.can_view(request.user):
        return PermissionDenied

    form = api_forms.CombineMendelianFamiliesForm(request.GET)
    if form.is_valid():

        search_spec = form.cleaned_data['search_spec']
        search_spec.family_group_id = family_group.slug

        genes = api_utils.calculate_combine_mendelian_families(family_group, search_spec)
        search_hash = cache_utils.save_results_for_spec(project.project_id, search_spec.toJSON(), genes)
        api_utils.add_extra_info_to_genes(project, get_reference(), genes)

        return JSONResponse({
            'is_error': False,
            'genes': genes,
            'search_hash': search_hash,
        })

    else:
        return JSONResponse({
            'is_error': True,
            'error': server_utils.form_error_string(form)
        })
コード例 #30
0
ファイル: views.py プロジェクト: ericminikel/xbrowse
def cohort_variant_search(request):

    project, cohort = get_project_and_cohort_for_user(request.user, request.GET)
    if not project.can_view(request.user):
        return HttpResponse('unauthorized')

    form = api_forms.CohortVariantSearchForm(request.GET)
    if form.is_valid():
        search_spec = form.cleaned_data['search_spec']
        search_spec.family_id = cohort.cohort_id

        variants = api_utils.calculate_mendelian_variant_search(search_spec, cohort.xfamily())
        search_hash = cache_utils.save_results_for_spec(project.project_id, search_spec.toJSON(), [v.toJSON() for v in variants])
        api_utils.add_extra_info_to_variants_cohort(get_reference(), cohort, variants)

        return JSONResponse({
            'is_error': False,
            'variants': [v.toJSON() for v in variants],
            'search_hash': search_hash,
        })

    else:
        return JSONResponse({
            'is_error': True,
            'error': server_utils.form_error_string(form)
        })
コード例 #31
0
ファイル: family_views.py プロジェクト: messiaen/seqr
def family_variant_view(request, project_id, family_id):

    project = get_object_or_404(Project, project_id=project_id)
    family = get_object_or_404(Family, project=project, family_id=family_id)
    if not project.can_view(request.user):
        raise PermissionDenied

    try:
        xpos = int(request.GET.get('xpos'))
        ref = request.GET.get('ref')
        alt = request.GET.get('alt')
    except:
        return HttpResponse('Invalid View')

    variant = get_datastore(project).get_single_variant(
        project_id, family_id, xpos, ref, alt)
    add_extra_info_to_variants_project(get_reference(),
                                       project, [variant],
                                       add_family_tags=True,
                                       add_populations=True)

    return render(
        request, 'family/family_variant_view.html', {
            'project': project,
            'family': family,
            'variant_json': json.dumps(variant.toJSON()),
        })
コード例 #32
0
ファイル: views.py プロジェクト: frichter/seqr
def cohort_variant_search(request):

    project, cohort = get_project_and_cohort_for_user(request.user, request.GET)
    if not project.can_view(request.user):
        return PermissionDenied

    form = api_forms.CohortVariantSearchForm(request.GET)
    if form.is_valid():
        search_spec = form.cleaned_data['search_spec']
        search_spec.family_id = cohort.cohort_id

        sys.stderr.write("cohort_variant_search - starting: %s  %s\n" % (json.dumps(search_spec.toJSON()), cohort.xfamily().family_id))
        variants = api_utils.calculate_mendelian_variant_search(search_spec, cohort.xfamily())

        list_of_variants = [v.toJSON() for v in variants]
        sys.stderr.write("cohort_variant_search - done calculate_mendelian_variant_search: %s  %s %s\n" % (json.dumps(search_spec.toJSON()), cohort.xfamily().family_id, len(list_of_variants)))
        search_hash = cache_utils.save_results_for_spec(project.project_id, search_spec.toJSON(), list_of_variants)

        sys.stderr.write("cohort_variant_search - done save_results_for_spec: %s  %s\n" % (json.dumps(search_spec.toJSON()), cohort.xfamily().family_id))
        api_utils.add_extra_info_to_variants_cohort(get_reference(), cohort, variants)

        sys.stderr.write("cohort_variant_search - done add_extra_info_to_variants_cohort: %s  %s\n" % (json.dumps(search_spec.toJSON()), cohort.xfamily().family_id))
        return JSONResponse({
            'is_error': False,
            'variants': [v.toJSON() for v in variants],
            'search_hash': search_hash,
        })

    else:
        return JSONResponse({
            'is_error': True,
            'error': server_utils.form_error_string(form)
        })
コード例 #33
0
ファイル: project_views.py プロジェクト: dmyung/xbrowse
def variants_with_tag(request, project_id, tag):

    project = get_object_or_404(Project, project_id=project_id)
    if not project.can_view(request.user):
        return HttpResponse('unauthorized')

    project_tag = get_object_or_404(ProjectTag, project=project, tag=tag)

    variants = get_variants_by_tag(project, tag)
    variants = sorted(variants, key=lambda v: v.extras['family_id'])
    grouped_variants = itertools.groupby(variants,
                                         key=lambda v: v.extras['family_id'])
    for family_id, family_variants in grouped_variants:
        family = Family.objects.get(project=project, family_id=family_id)
        family_variants = list(family_variants)
        add_extra_info_to_variants_family(get_reference(), family,
                                          family_variants)

    return render(
        request, 'project/saved_variants.html', {
            'project':
            project,
            'variants_json':
            json.dumps([v.toJSON() for v in variants]),
            'families_json':
            json.dumps({
                family.family_id: family.get_json_obj()
                for family in project.get_families()
            })
        })
コード例 #34
0
ファイル: views.py プロジェクト: frichter/seqr
def combine_mendelian_families_variants(request):

    project, family_group = utils.get_project_and_family_group_for_user(request.user, request.GET)

    form = api_forms.CombineMendelianFamiliesVariantsForm(request.GET)
    if form.is_valid():
        variants_grouped = get_variants_by_family_for_gene(
            get_mall(project.project_id),
            [f.xfamily() for f in form.cleaned_data['families']],
            form.cleaned_data['inheritance_mode'],
            form.cleaned_data['gene_id'],
            variant_filter=form.cleaned_data['variant_filter'],
            quality_filter=form.cleaned_data['quality_filter']
        )
        variants_by_family = []
        for family in form.cleaned_data['families']:
            variants = variants_grouped[(family.project.project_id, family.family_id)]
            add_extra_info_to_variants_family(get_reference(), family, variants)
            variants_by_family.append({
                'project_id': family.project.project_id,
                'family_id': family.family_id,
                'family_name': str(family),
                'variants': [v.toJSON() for v in variants],
            })
        return JSONResponse({
            'is_error': False,
            'variants_by_family': variants_by_family,
        })

    else:
        return JSONResponse({
            'is_error': True,
            'error': server_utils.form_error_string(form)
        })
コード例 #35
0
ファイル: family_group.py プロジェクト: rpete/seqr
def get_variants_in_gene(family_group,
                         gene_id,
                         variant_filter=None,
                         quality_filter=None):
    """

    """
    variants_by_family = []
    for family in family_group.get_families():
        variant_list = list(
            get_mall(
                family.project.project_id).variant_store.get_variants_in_gene(
                    family.project.project_id,
                    family.family_id,
                    gene_id,
                    variant_filter=variant_filter))
        variant_list = search_utils.filter_gene_variants_by_variant_filter(
            variant_list, gene_id, variant_filter)
        add_extra_info_to_variants_family(get_reference(), family,
                                          variant_list)
        variants_by_family.append({
            'variants': [v.toJSON() for v in variant_list],
            'family_id':
            family.family_id,
            'project_id':
            family.project.project_id,
            'family_name':
            str(family),
        })
    return variants_by_family
コード例 #36
0
ファイル: family_views.py プロジェクト: evrimulgen/seqr
def family_coverage_gene_list(request, family, gene_list):
    """
    Table of summary coverages for each gene in the gene list
    """
    sample_id_list = [str(individual.pk) for individual in family.get_individuals()]

    cache_key = ('family_coverage_gene_list', family.project.project_id, family.family_id, gene_list.slug)
    cached_results = server_utils.get_cached_results(cache_key)
    if cached_results:
        gene_coverages = cached_results
    else:
        gene_coverages = []
        for gene_id in gene_list.gene_id_list():
            d = {
                'gene_id': gene_id,
                'gene_name': get_reference().get_gene_symbol(gene_id),
                'totals': get_coverage_store().get_coverage_totals_for_gene(gene_id, sample_id_list)
            }
            d['coding_size'] = sum(d['totals'].values())
            try:
                d['percent'] = float(d['totals']['callable'])*100 / d['coding_size']
            except ZeroDivisionError:
                d['percent'] = 0
            gene_coverages.append(d)
        server_utils.save_results_cache(cache_key, gene_coverages)

    return render(request, 'coverage/family_coverage_gene_list.html', {
        'project': family.project,
        'family': family,
        'gene_list': gene_list,
        'gene_coverages': gene_coverages,
    })
コード例 #37
0
ファイル: views.py プロジェクト: batsal/xbrowse
def combine_mendelian_families_spec(request):

    project, family_group = utils.get_project_and_family_group_for_user(request.user, request.GET)
    if not project.can_view(request.user):
        return HttpResponse('unauthorized')

    search_hash = request.GET.get('search_hash')
    search_spec, genes = cache_utils.get_cached_results(project.project_id, search_hash)
    if genes is None:
        genes = api_utils.calculate_combine_mendelian_families(family_group, search_spec)
    api_utils.add_extra_info_to_genes(project, get_reference(), genes)

    if request.GET.get('return_type', '') != 'csv':
        return JSONResponse({
            'is_error': False,
            'genes': genes,
            'search_spec': search_spec,
        })
    else:
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="family_group_results_{}.csv"'.format(search_hash)
        writer = csv.writer(response)
        writer.writerow(["gene", "# families", "family list", "chrom", "start", "end"])
        for gene in genes:
            family_id_list = [family_id for (project_id, family_id) in gene["family_id_list"]]
            writer.writerow(map(str, [gene["gene_name"], len(family_id_list), " ".join(family_id_list), gene["chr"], gene["start"], gene["end"], ""]))
        return response
コード例 #38
0
ファイル: views.py プロジェクト: rpete/seqr
def cohort_variant_search(request):

    project, cohort = get_project_and_cohort_for_user(request.user, request.GET)
    if not project.can_view(request.user):
        return PermissionDenied

    form = api_forms.CohortVariantSearchForm(request.GET)
    if form.is_valid():
        search_spec = form.cleaned_data['search_spec']
        search_spec.family_id = cohort.cohort_id

        sys.stderr.write("cohort_variant_search - starting: %s  %s\n" % (json.dumps(search_spec.toJSON()), cohort.xfamily().family_id))
        variants = api_utils.calculate_mendelian_variant_search(search_spec, cohort.xfamily())

        list_of_variants = [v.toJSON() for v in variants]
        sys.stderr.write("cohort_variant_search - done calculate_mendelian_variant_search: %s  %s %s\n" % (json.dumps(search_spec.toJSON()), cohort.xfamily().family_id, len(list_of_variants)))
        search_hash = cache_utils.save_results_for_spec(project.project_id, search_spec.toJSON(), list_of_variants)

        sys.stderr.write("cohort_variant_search - done save_results_for_spec: %s  %s\n" % (json.dumps(search_spec.toJSON()), cohort.xfamily().family_id))
        api_utils.add_extra_info_to_variants_cohort(get_reference(), cohort, variants)

        sys.stderr.write("cohort_variant_search - done add_extra_info_to_variants_cohort: %s  %s\n" % (json.dumps(search_spec.toJSON()), cohort.xfamily().family_id))
        return JSONResponse({
            'is_error': False,
            'variants': [v.toJSON() for v in variants],
            'search_hash': search_hash,
        })

    else:
        return JSONResponse({
            'is_error': True,
            'error': server_utils.form_error_string(form)
        })
コード例 #39
0
ファイル: views.py プロジェクト: frichter/seqr
def mendelian_variant_search(request):

    # TODO: how about we move project getter into the form, and just test for authX here?
    # esp because error should be described in json, not just 404
    project, family = get_project_and_family_for_user(request.user, request.GET)

    form = api_forms.MendelianVariantSearchForm(request.GET)
    if form.is_valid():

        search_spec = form.cleaned_data['search_spec']
        search_spec.family_id = family.family_id

        variants = api_utils.calculate_mendelian_variant_search(search_spec, family.xfamily())
        search_hash = cache_utils.save_results_for_spec(project.project_id, search_spec.toJSON(), [v.toJSON() for v in variants])
        add_extra_info_to_variants_family(get_reference(), family, variants)

        return_type = request.GET.get('return_type', 'json')
        if return_type == 'json':
            return JSONResponse({
                'is_error': False,
                'variants': [v.toJSON() for v in variants],
                'search_hash': search_hash,
            })
        elif return_type == 'csv':
            return ''
        else:
            return HttpResponse("Return type not implemented")

    else:
        return JSONResponse({
            'is_error': True,
            'error': server_utils.form_error_string(form)
        })
コード例 #40
0
def saved_variants(request, project_id, family_id):

    project = get_object_or_404(Project, project_id=project_id)
    family = get_object_or_404(Family, project=project, family_id=family_id)
    if not project.can_view(request.user):
        raise PermissionDenied

    variants, couldntfind = get_saved_variants_for_family(family)

    # TODO: first this shouldnt be in API - base should never depend on api
    # TODO: also this should have better naming
    add_extra_info_to_variants_project(get_reference(),
                                       project,
                                       variants,
                                       add_family_tags=True,
                                       add_populations=True)

    return render(
        request, 'family/saved_family_variants.html', {
            'project':
            project,
            'family':
            family,
            'variants_json':
            json.dumps([v.toJSON() for v in variants]),
            'new_page_url':
            '/project/{0}/family_page/{1}'.format(
                family.seqr_family.project.guid, family.seqr_family.guid)
            if family.seqr_family else None,
        })
コード例 #41
0
ファイル: views.py プロジェクト: frichter/seqr
def combine_mendelian_families(request):

    project, family_group = utils.get_project_and_family_group_for_user(request.user, request.GET)
    if not project.can_view(request.user):
        return PermissionDenied

    form = api_forms.CombineMendelianFamiliesForm(request.GET)
    if form.is_valid():

        search_spec = form.cleaned_data['search_spec']
        search_spec.family_group_id = family_group.slug

        genes = api_utils.calculate_combine_mendelian_families(family_group, search_spec)
        search_hash = cache_utils.save_results_for_spec(project.project_id, search_spec.toJSON(), genes)
        api_utils.add_extra_info_to_genes(project, get_reference(), genes)

        return JSONResponse({
            'is_error': False,
            'genes': genes,
            'search_hash': search_hash,
        })

    else:
        return JSONResponse({
            'is_error': True,
            'error': server_utils.form_error_string(form)
        })
コード例 #42
0
ファイル: views.py プロジェクト: frichter/seqr
def diagnostic_search(request):

    project, family = utils.get_project_and_family_for_user(request.user, request.GET)
    if not project.can_view(request.user):
        raise PermissionDenied

    form = api_forms.DiagnosticSearchForm(family, request.GET)
    if form.is_valid():

        search_spec = form.cleaned_data['search_spec']
        search_spec.family_id = family.family_id

        gene_list = form.cleaned_data['gene_list']
        diagnostic_info_list = []
        for gene_id in gene_list.gene_id_list():
            diagnostic_info = get_gene_diangostic_info(family, gene_id, search_spec.variant_filter)
            add_extra_info_to_variants_family(get_reference(), family, diagnostic_info._variants)
            diagnostic_info_list.append(diagnostic_info)



        return JSONResponse({
            'is_error': False,
            'gene_diagnostic_info_list': [d.toJSON() for d in diagnostic_info_list],
            'gene_list_info': gene_list.toJSON(details=True),
            'data_summary': family.get_data_summary(),
        })

    else:
        return JSONResponse({
            'is_error': True,
            'error': server_utils.form_error_string(form)
        })
コード例 #43
0
def get_knockouts_in_gene(project, gene_id, quality_filter=None):
    """
    Get all the variants in a gene, but filter out quality_filter genotypes
    """
    indiv_id_list = [i.indiv_id for i in project.get_individuals()]

    # filter out variants > 0.01 AF in any of the reference populations
    reference_populations = mall.get_annotator().reference_population_slugs
    variant_filter = get_default_variant_filter('moderate_impact',
                                                reference_populations)
    variant_list = get_project_datastore(
        project.project_id).get_project_variants_in_gene(
            project.project_id,
            gene_id,
            variant_filter=variant_filter,
        )
    variant_list = search_utils.filter_gene_variants_by_variant_filter(
        variant_list, gene_id, variant_filter)
    variation = CohortGeneVariation(
        get_reference(),
        gene_id,
        variant_list,
        indiv_id_list,
        quality_filter={},
    )
    knockouts = get_individuals_with_inheritance('recessive', variation,
                                                 indiv_id_list)
    return knockouts, variation
コード例 #44
0
ファイル: project_views.py プロジェクト: obonyojimmy/seqr
def causal_variants(request, project_id):

    project = get_object_or_404(Project, project_id=project_id)
    if not project.can_view(request.user):
        raise PermissionDenied

    variants = get_causal_variants_for_project(project)
    variants = sorted(variants, key=lambda v: (v.extras['family_id'], v.xpos))
    grouped_variants = itertools.groupby(variants,
                                         key=lambda v: v.extras['family_id'])
    for family_id, family_variants in grouped_variants:
        family = Family.objects.get(project=project, family_id=family_id)
        family_variants = list(family_variants)
        add_extra_info_to_variants_family(get_reference(), family,
                                          family_variants)

    return render(
        request, 'project/causal_variants.html', {
            'project':
            project,
            'variants_json':
            json.dumps([v.toJSON() for v in variants]),
            'families_json':
            json.dumps({
                family.family_id: family.get_json_obj()
                for family in project.get_families()
            })
        })
コード例 #45
0
ファイル: views.py プロジェクト: frichter/seqr
def cohort_gene_search(request):

    project, cohort = get_project_and_cohort_for_user(request.user, request.GET)
    sys.stderr.write("cohort_gene_search %s  %s: starting ... \n" % (project.project_id, cohort.cohort_id))
    form = api_forms.CohortGeneSearchForm(request.GET)
    if form.is_valid():
        search_spec = form.cleaned_data['search_spec']
        search_spec.cohort_id = cohort.cohort_id
        sys.stderr.write("cohort_gene_search %s  %s: search spec: %s \n" % (project.project_id, cohort.cohort_id, str(search_spec.toJSON())))
        genes = api_utils.calculate_cohort_gene_search(cohort, search_spec)
        sys.stderr.write("cohort_gene_search %s  %s: get %s genes \n" % (project.project_id, cohort.cohort_id, len(genes)))
        search_hash = cache_utils.save_results_for_spec(project.project_id, search_spec.toJSON(), genes)
        api_utils.add_extra_info_to_genes(project, get_reference(), genes)
        sys.stderr.write("cohort_gene_search %s  %s: done adding extra info \n" % (project.project_id, cohort.cohort_id))
        return JSONResponse({
            'is_error': False,
            'genes': genes,
            'search_hash': search_hash,
        })

    else:
        return JSONResponse({
            'is_error': True,
            'error': server_utils.form_error_string(form)
        })
コード例 #46
0
ファイル: views.py プロジェクト: macarthur-lab/seqr
def breakpoint_search(request, project_id, family_id):

    log.info("Showing main breakpoint search page")

    project = get_object_or_404(Project, project_id=project_id)
    family = get_object_or_404(Family, project=project, family_id=family_id)
    if not project.can_view(request.user):
        raise PermissionDenied

    if not family.has_data('breakpoints'):
        return render(request, 'analysis_unavailable.html', {
            'reason': 'This family does not have any breakpoint data.'
        })

    gene_lists = project.get_gene_list_map()

    gene_list_json = dict([ (get_reference().get_gene_symbol(g),[ gl.name for gl in gll ]) for g,gll in gene_lists.iteritems() ])

    bam_file_paths = dict((ind.indiv_id, os.path.join(settings.READ_VIZ_BAM_PATH,ind.bam_file_path)) for ind in family.get_individuals())

    return render(request, 'breakpoint_search.html', {
        'project': project,
        'family': family,
        'gene_lists_json': json.dumps(gene_list_json),
        'bam_files_json': json.dumps(bam_file_paths),
    })
コード例 #47
0
ファイル: views.py プロジェクト: dmyung/xbrowse
def cohort_gene_search_variants(request):

    # TODO: this view not like the others - refactor to forms

    error = None

    project, cohort = get_project_and_cohort_for_user(request.user,
                                                      request.GET)
    if not project.can_view(request.user):
        return HttpResponse('unauthorized')

    form = api_forms.CohortGeneSearchVariantsForm(request.GET)
    if form.is_valid():
        gene_id = form.cleaned_data['gene_id']
        inheritance_mode = form.cleaned_data['inheritance_mode']
        variant_filter = form.cleaned_data['variant_filter']
        quality_filter = form.cleaned_data['quality_filter']
    else:
        error = server_utils.form_error_string(form)

    if not error:

        indivs_with_inheritance, gene_variation = cohort_search.get_individuals_with_inheritance_in_gene(
            get_datastore(project.project_id),
            get_reference(),
            cohort.xcohort(),
            inheritance_mode,
            gene_id,
            variant_filter=variant_filter,
            quality_filter=quality_filter)

        relevant_variants = gene_variation.get_relevant_variants_for_indiv_ids(
            cohort.indiv_id_list())

        api_utils.add_extra_info_to_variants_family(get_reference(), cohort,
                                                    relevant_variants)

        ret = {
            'is_error': False,
            'variants': [v.toJSON() for v in relevant_variants],
            'gene_info': get_reference().get_gene(gene_id),
        }
        return JSONResponse(ret)

    else:
        ret = {'is_error': True, 'error': error}
        return JSONResponse(ret)
コード例 #48
0
def add_family_search_flag(request):

    error = None

    for key in [
            'project_id', 'family_id', 'xpos', 'ref', 'alt', 'note',
            'flag_type', 'flag_inheritance_mode'
    ]:
        if request.GET.get(key, None) == None:
            error = "%s is requred" % key

    if not error:
        project = get_object_or_404(Project,
                                    project_id=request.GET.get('project_id'))
        family = get_object_or_404(Family,
                                   project=project,
                                   family_id=request.GET.get('family_id'))
        if not project.can_edit(request.user):
            raise PermissionDenied

    if not error:
        xpos = int(request.GET['xpos'])
        ref = request.GET.get('ref')
        alt = request.GET['alt']
        note = request.GET.get('note')
        flag_type = request.GET.get('flag_type')
        flag_inheritance_mode = request.GET.get('flag_inheritance_mode')

        # todo: more validation - is variant valid?

        flag = FamilySearchFlag(
            user=request.user,
            family=family,
            xpos=int(request.GET['xpos']),
            ref=ref,
            alt=alt,
            note=note,
            flag_type=flag_type,
            suggested_inheritance=flag_inheritance_mode,
            date_saved=timezone.now(),
        )

    if not error:
        flag.save()
        variant = get_datastore(project.project_id).get_single_variant(
            family.project.project_id, family.family_id, xpos, ref, alt)
        api_utils.add_extra_info_to_variant(get_reference(), family, variant)

        ret = {
            'is_error': False,
            'variant': variant.toJSON(),
        }

    else:
        ret = {
            'is_error': True,
            'error': error,
        }
    return JSONResponse(ret)
コード例 #49
0
    def handle(self, *args, **options):
        #genomicFeatures section
        self.all_gene_lists = defaultdict(set)
        self.gene_to_gene_lists = defaultdict(set)
        for gene_list in GeneList.objects.all():
            print('gene list: [%s]' % gene_list.name)
            self.all_gene_lists[gene_list.name] = set(
                g.gene_id for g in gene_list.genelistitem_set.all())
            for g in gene_list.genelistitem_set.all():
                self.gene_to_gene_lists[g.gene_id].add(gene_list.name)

        print("starting... ")
        gene_to_projects = defaultdict(set)
        gene_to_variants = defaultdict(set)
        gene_to_families = defaultdict(set)
        gene_to_variant_tags = defaultdict(set)

        Key = namedtuple('Key', 'gene_id, gene_name')
        project_ids = defaultdict(int)
        for variant_tag in tqdm(VariantTag.objects.filter(), unit=' variants'):
            project_tag = variant_tag.project_tag
            project_id = project_tag.project.project_id
            project_ids[project_id] += 1
            tag_name = project_tag.tag.lower()

            variant = get_datastore(project_id).get_single_variant(
                project_id,
                variant_tag.family.family_id,
                variant_tag.xpos,
                variant_tag.ref,
                variant_tag.alt,
            )

            # print(gene_to_projects)
            if variant is None:
                #print("Variant %s no longer called in this family (did the callset version change?)" % (variant_tag.toJSON()))
                continue

            #print(project_id,variant.toJSON()['gene_ids'])
            if variant.gene_ids is not None:
                for gene_id in variant.gene_ids:
                    gene_name = get_reference().get_gene_symbol(gene_id)
                    key = Key._make([gene_id, gene_name])
                    gene_to_variants[key].add(
                        "%s-%s-%s-%s" %
                        (variant.chr, variant.pos, variant.ref, variant.alt))
                    if variant_tag.family:
                        gene_to_families[key].add(variant_tag.family.family_id)
                    gene_to_variant_tags[key].add(tag_name)
                    gene_to_projects[key].add(project_id.lower())

            if len(gene_to_projects) % 50 == 0:
                self.print_out(gene_to_projects, gene_to_families,
                               gene_to_variants, gene_to_variant_tags)

        self.print_out(gene_to_projects, gene_to_families, gene_to_variants,
                       gene_to_variant_tags)
コード例 #50
0
ファイル: views.py プロジェクト: frichter/seqr
def family_gene_lookup(request):
    project, family = utils.get_project_and_family_for_user(request.user, request.GET)
    if not project.can_view(request.user):
        raise PermissionDenied
    gene_id = request.GET.get('gene_id')
    if not get_reference().is_valid_gene_id(gene_id):
        return JSONResponse({
            'is_error': True,
            'error': 'Invalid gene',
        })
    family_gene_data = get_gene_diangostic_info(family, gene_id)
    add_extra_info_to_variants_family(get_reference(), family, family_gene_data._variants)
    return JSONResponse({
        'is_error': False,
        'family_gene_data': family_gene_data.toJSON(),
        'data_summary': family.get_data_summary(),
        'gene': get_reference().get_gene(gene_id),
    })
コード例 #51
0
ファイル: views.py プロジェクト: rpete/seqr
def family_gene_lookup(request):
    project, family = utils.get_project_and_family_for_user(request.user, request.GET)
    if not project.can_view(request.user):
        raise PermissionDenied
    gene_id = request.GET.get('gene_id')
    if not get_reference().is_valid_gene_id(gene_id):
        return JSONResponse({
            'is_error': True,
            'error': 'Invalid gene',
        })
    family_gene_data = get_gene_diangostic_info(family, gene_id)
    add_extra_info_to_variants_family(get_reference(), family, family_gene_data._variants)
    return JSONResponse({
        'is_error': False,
        'family_gene_data': family_gene_data.toJSON(),
        'data_summary': family.get_data_summary(),
        'gene': get_reference().get_gene(gene_id),
    })
コード例 #52
0
def _deprecated_retrieve_saved_variants_json(project, variant_tuples, create_if_missing):
    project_id = project.deprecated_project_id
    xbrowse_project = BaseProject.objects.get(project_id=project_id)
    user = User.objects.filter(is_staff=True).first()  # HGMD annotations are only returned for staff users

    variants = get_variants_from_variant_tuples(xbrowse_project, variant_tuples, user=user)
    if not create_if_missing:
        variants = [var for var in variants if not var.get_extra('created_variant')]
    add_extra_info_to_variants_project(get_reference(), xbrowse_project, variants, add_populations=True)
    return [variant.toJSON() for variant in variants]
コード例 #53
0
ファイル: variant_utils.py プロジェクト: macarthur-lab/seqr
def _deprecated_retrieve_saved_variants_json(project, variant_tuples, create_if_missing):
    project_id = project.deprecated_project_id
    xbrowse_project = BaseProject.objects.get(project_id=project_id)
    user = User.objects.filter(is_staff=True).first()  # HGMD annotations are only returned for staff users

    variants = get_variants_from_variant_tuples(xbrowse_project, variant_tuples, user=user)
    if not create_if_missing:
        variants = [var for var in variants if not var.get_extra('created_variant')]
    add_extra_info_to_variants_project(get_reference(), xbrowse_project, variants, add_populations=True)
    return [variant.toJSON() for variant in variants]
コード例 #54
0
ファイル: forms.py プロジェクト: dmyung/xbrowse
    def clean(self):
        cleaned_data = super(CombineMendelianFamiliesVariantsForm, self).clean()
        parse_variant_filter(cleaned_data)
        parse_quality_filter(cleaned_data)
        parse_family_tuple_list(cleaned_data)

        if not get_reference().is_valid_gene_id(cleaned_data['gene_id']):
            raise forms.ValidationError("{} is not a valid gene ID".format(cleaned_data['gene_id']))

        return cleaned_data
コード例 #55
0
    def handle(self, *args, **options):
        #genomicFeatures section
        self.all_gene_lists = defaultdict(set)
        self.gene_to_gene_lists = defaultdict(set)
        for gene_list in GeneList.objects.all():
            print('gene list: [%s]' % gene_list.name)
            self.all_gene_lists[gene_list.name] = set(g.gene_id for g in gene_list.genelistitem_set.all())
            for g in gene_list.genelistitem_set.all():
                self.gene_to_gene_lists[g.gene_id].add(gene_list.name)

        print("starting... ")
        gene_to_projects = defaultdict(set)
        gene_to_variants = defaultdict(set)
        gene_to_families = defaultdict(set)
        gene_to_variant_tags = defaultdict(set)
        gene_to_variant_and_families = defaultdict(lambda: defaultdict(set))

        Key = namedtuple('Key', 'gene_id, gene_name')
        project_ids = defaultdict(int)
        for variant_tag in tqdm(VariantTag.objects.filter(), unit=' variants'):
            project_tag = variant_tag.project_tag
            project_id = project_tag.project.project_id
            project_ids[project_id] += 1
            tag_name = project_tag.tag.lower()

            variant = get_datastore(project_tag.project).get_single_variant(
                project_id,
                variant_tag.family.family_id,
                variant_tag.xpos,
                variant_tag.ref,
                variant_tag.alt,
            )

            # print(gene_to_projects)
            if variant is None:
                #print("Variant %s no longer called in this family (did the callset version change?)" % (variant_tag.toJSON()))
                continue

            #print(project_id,variant.toJSON()['gene_ids'])
            if variant.gene_ids is not None:
                for gene_id in variant.gene_ids:
                    gene_name = get_reference().get_gene_symbol(gene_id)
                    key = Key._make([gene_id, gene_name])
                    variant_id = "%s-%s-%s-%s" % (variant.chr, variant.pos, variant.ref, variant.alt)
                    gene_to_variants[key].add(variant_id)
                    if variant_tag.family:
                        gene_to_families[key].add(variant_tag.family)
                    gene_to_variant_tags[key].add(tag_name)
                    gene_to_projects[key].add(project_id.lower())
                    gene_to_variant_and_families[key][variant_id].add(variant_tag.family.family_id)
            
            if len(gene_to_projects) % 50 == 0:
                self.print_out(gene_to_projects, gene_to_families, gene_to_variants, gene_to_variant_tags, gene_to_variant_and_families)

        self.print_out(gene_to_projects, gene_to_families, gene_to_variants, gene_to_variant_tags, gene_to_variant_and_families)
コード例 #56
0
ファイル: utils.py プロジェクト: macarthur-lab/seqr
def calculate_combine_mendelian_families(family_group, search_spec, user=None):
    """
    Calculate search results from the params in search_spec
    Should be called after cache is checked - this does all the computation
    Returns (is_error, genes) tuple
    """
    xfamilygroup = family_group.xfamilygroup()

    genes = []
    for gene_id, family_id_list in get_families_by_gene(
        get_mall(family_group.project),
        xfamilygroup,
        search_spec.inheritance_mode,
        search_spec.variant_filter,
        search_spec.quality_filter,
        user=user,
    ):

        xgene = get_reference().get_gene(gene_id)
        if xgene is None:
            continue

        try:
            start_pos, end_pos = get_reference().get_gene_bounds(gene_id)
            chr, start = genomeloc.get_chr_pos(start_pos)
            end = genomeloc.get_chr_pos(end_pos)[1]
        except KeyError:
            chr, start, end = None, None, None

        gene = {
            'gene_info': xgene,
            'gene_id': gene_id,
            'gene_name': xgene['symbol'],
            'chr': chr,
            'start': start,
            'end': end,
            'family_id_list': family_id_list,
        }

        genes.append(gene)

    return genes
コード例 #57
0
ファイル: utils.py プロジェクト: ericminikel/xbrowse
def add_gene_databases_to_variants(variants):
    """
    Adds variant['gene_databases'] - a list of *gene* databases that this variant's gene(s) are seen in
    """
    for variant in variants:
        variant.set_extra('in_disease_gene_db', False)
        for gene_id in variant.coding_gene_ids:
            gene = get_reference().get_gene(gene_id)
            # TODO: should be part of reference cache
            if gene and (len(gene['phenotype_info']['orphanet_phenotypes']) or len(gene['phenotype_info']['mim_phenotypes'])):
                variant.set_extra('in_disease_gene_db', True)
コード例 #58
0
ファイル: views.py プロジェクト: frichter/seqr
def add_family_search_flag(request):

    # TODO: this view not like the others - refactor to forms

    error = None

    for key in ['project_id', 'family_id', 'xpos', 'ref', 'alt', 'note', 'flag_type', 'flag_inheritance_mode']:
        if request.GET.get(key, None) == None:
            error = "%s is requred" % key

    if not error:
        project = get_object_or_404(Project, project_id=request.GET.get('project_id'))
        family = get_object_or_404(Family, project=project, family_id=request.GET.get('family_id'))
        if not project.can_edit(request.user):
            return PermissionDenied

    if not error:
        xpos = int(request.GET['xpos'])
        ref=request.GET.get('ref')
        alt=request.GET['alt']
        note=request.GET.get('note')
        flag_type=request.GET.get('flag_type')
        flag_inheritance_mode=request.GET.get('flag_inheritance_mode')

        # todo: more validation - is variant valid?

        flag = FamilySearchFlag(user=request.user,
            family=family,
            xpos=int(request.GET['xpos']),
            ref=ref,
            alt=alt,
            note=note,
            flag_type=flag_type,
            suggested_inheritance=flag_inheritance_mode,
            date_saved = datetime.datetime.now(),
        )

    if not error:
        flag.save()
        variant = get_datastore(project.project_id).get_single_variant(family.project.project_id, family.family_id,
            xpos, ref, alt )
        api_utils.add_extra_info_to_variant(get_reference(), family, variant)

        ret = {
            'is_error': False,
            'variant': variant.toJSON(),
        }

    else:
        ret = {
            'is_error': True,
            'error': error,
        }
    return JSONResponse(ret)
コード例 #59
0
ファイル: project_views.py プロジェクト: dmyung/xbrowse
def gene_quicklook(request, project_id, gene_id):
    """
    Summary of a gene in a project
    """
    project = get_object_or_404(Project, project_id=project_id)
    if not project.can_view(request.user):
        return HttpResponse("Unauthorized")
    gene_id = get_gene_id_from_str(gene_id, get_reference())
    gene = get_reference().get_gene(gene_id)
    sys.stderr.write(project_id + " - staring gene search for: %s %s \n" % (gene_id, gene))
    variant_filter = get_default_variant_filter('all_coding', mall.get_annotator().reference_population_slugs)
    num_indivs = len([i for i in project.get_individuals() if i.has_variant_data()])
    aac_threshold = (.2 * num_indivs) + 5
    rare_variants = []
    for variant in project_analysis.get_variants_in_gene(project, gene_id, variant_filter=variant_filter):
        aac = get_alt_allele_count(variant)
        max_af = max(variant.annotation['freqs'].values())
        if aac <= aac_threshold and max_af < .01:
            rare_variants.append(variant)

    add_extra_info_to_variants_project(get_reference(), project, rare_variants)

    knockouts = []
    knockout_ids, variation = get_knockouts_in_gene(project, gene_id)
    for kid in knockout_ids:
        variants = variation.get_relevant_variants_for_indiv_ids([kid])
        add_extra_info_to_variants_project(get_reference(), project, variants)
        knockouts.append({
            'indiv_id': kid,
            'variants': [v.toJSON() for v in variants],
        })

    sys.stderr.write("Retrieved %s variants \n" % len(rare_variants))
    return render(request, 'project/gene_quicklook.html', {
        'gene': gene,
        'gene_json': json.dumps(gene),
        'project': project,
        'rare_variants_json': json.dumps([v.toJSON() for v in rare_variants]),
        'individuals_json': json.dumps([i.get_json_obj() for i in project.get_individuals()]),
        'knockouts_json': json.dumps(knockouts),
    })