def clean(self): cleaned_data = super(MendelianVariantSearchForm, self).clean() if cleaned_data['search_mode'] not in ['standard_inheritance', 'custom_inheritance', 'gene_burden', 'allele_count', 'all_variants']: raise forms.ValidationError("Invalid search mode: {}".format(cleaned_data['search_mode'])) if cleaned_data['search_mode'] == 'standard_inheritance' and not cleaned_data.get('inheritance_mode'): raise forms.ValidationError("Inheritance mode is required for standard search. ") parse_variant_filter(cleaned_data) parse_quality_filter(cleaned_data) parse_genotype_filter(cleaned_data) parse_burden_filter(cleaned_data) parse_allele_count_filter(cleaned_data) search_spec = MendelianVariantSearchSpec() search_spec.search_mode = cleaned_data['search_mode'] search_spec.inheritance_mode = cleaned_data.get('inheritance_mode') search_spec.genotype_inheritance_filter = cleaned_data.get('genotype_filter') search_spec.gene_burden_filter = cleaned_data.get('burden_filter') search_spec.allele_count_filter = cleaned_data.get('allele_count_filter') search_spec.variant_filter = cleaned_data.get('variant_filter') search_spec.quality_filter = cleaned_data.get('quality_filter') cleaned_data['search_spec'] = search_spec return cleaned_data
def clean(self): cleaned_data = super(CohortVariantSearchForm, self).clean() if cleaned_data['search_mode'] not in [ 'custom_inheritance', 'gene_burden' ]: raise forms.ValidationError("Invalid search mode: {}".format( cleaned_data['search_mode'])) parse_variant_filter(cleaned_data) parse_quality_filter(cleaned_data) parse_genotype_filter(cleaned_data) parse_burden_filter(cleaned_data) search_spec = MendelianVariantSearchSpec() search_spec.search_mode = cleaned_data['search_mode'] search_spec.inheritance_mode = cleaned_data.get('inheritance_mode') search_spec.genotype_inheritance_filter = cleaned_data.get( 'genotype_filter') search_spec.gene_burden_filter = cleaned_data.get('gene_burden_filter') search_spec.allele_count_filter = cleaned_data.get( 'allele_count_filter') search_spec.variant_filter = cleaned_data.get('variant_filter') search_spec.genotype_quality_filter = cleaned_data.get( 'quality_filter') cleaned_data['search_spec'] = search_spec return cleaned_data
def clean(self): cleaned_data = super(CohortVariantSearchForm, self).clean() parse_variant_filter(cleaned_data) parse_quality_filter(cleaned_data) search_spec = MendelianVariantSearchSpec() search_spec.search_mode = cleaned_data['search_mode'] search_spec.inheritance_mode = cleaned_data.get('inheritance_mode') search_spec.variant_filter = cleaned_data.get('variant_filter') search_spec.quality_filter = cleaned_data.get('quality_filter') cleaned_data['search_spec'] = search_spec return cleaned_data
def clean(self): cleaned_data = super(MendelianVariantSearchForm, self).clean() if cleaned_data['search_mode'] not in ['standard_inheritance', 'custom_inheritance', 'gene_burden', 'allele_count', 'all_variants']: raise forms.ValidationError("Invalid search mode: {}".format(cleaned_data['search_mode'])) if cleaned_data['search_mode'] == 'standard_inheritance' and not cleaned_data.get('inheritance_mode'): raise forms.ValidationError("Inheritance mode is required for standard search. ") parse_variant_filter(cleaned_data) parse_quality_filter(cleaned_data) parse_genotype_filter(cleaned_data) parse_burden_filter(cleaned_data) parse_allele_count_filter(cleaned_data) search_spec = MendelianVariantSearchSpec() search_spec.search_mode = cleaned_data['search_mode'] search_spec.inheritance_mode = cleaned_data.get('inheritance_mode') search_spec.genotype_inheritance_filter = cleaned_data.get('genotype_filter') search_spec.gene_burden_filter = cleaned_data.get('gene_burden_filter') search_spec.allele_count_filter = cleaned_data.get('allele_count_filter') search_spec.variant_filter = cleaned_data.get('variant_filter') search_spec.genotype_quality_filter = cleaned_data.get('quality_filter') cleaned_data['search_spec'] = search_spec return cleaned_data
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="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
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
def cohort_variant_search_spec(request): project, cohort = get_project_and_cohort_for_user(request.user, request.GET) # TODO: use form search_spec_dict, variants = cache_utils.get_cached_results(project.project_id, request.GET.get('search_hash')) search_spec = MendelianVariantSearchSpec.fromJSON(search_spec_dict) if variants is None: variants = api_utils.calculate_mendelian_variant_search(search_spec, cohort.xfamily()) else: variants = [Variant.fromJSON(v) 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_spec': search_spec.toJSON(), })
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): raise PermissionDenied search_hash = request.GET.get('search_hash') search_spec, genes = cache_utils.get_cached_results(project.project_id, search_hash) search_spec_obj = MendelianVariantSearchSpec.fromJSON(search_spec) if request.GET.get('return_type') != 'csv' or not request.GET.get('group_by_variants'): 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 else: # download results grouped by variant indiv_id_list = [] for family in family_group.get_families(): indiv_id_list.extend(family.indiv_ids_with_variant_data()) response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="results_{}.csv"'.format(search_hash) writer = csv.writer(response) headers = ['genes','chr','pos','ref','alt','worst_annotation' ] headers.extend(project.get_reference_population_slugs()) headers.extend([ 'polyphen','sift','muttaster','fathmm']) for indiv_id in indiv_id_list: headers.append(indiv_id) headers.append(indiv_id+'_gq') headers.append(indiv_id+'_dp') writer.writerow(headers) mall = get_mall(project.project_id) variant_key_to_individual_id_to_variant = defaultdict(dict) variant_key_to_variant = {} for family in family_group.get_families(): for variant in get_variants_with_inheritance_mode( mall, family.xfamily(), search_spec_obj.inheritance_mode, search_spec_obj.variant_filter, search_spec_obj.quality_filter, ): if len(variant.coding_gene_ids) == 0: continue variant_key = (variant.xpos, variant.ref, variant.alt) variant_key_to_variant[variant_key] = variant for indiv_id in family.indiv_ids_with_variant_data(): variant_key_to_individual_id_to_variant[variant_key][indiv_id] = variant for variant_key in sorted(variant_key_to_individual_id_to_variant.keys()): variant = variant_key_to_variant[variant_key] individual_id_to_variant = variant_key_to_individual_id_to_variant[variant_key] genes = [mall.reference.get_gene_symbol(gene_id) for gene_id in variant.coding_gene_ids] fields = [] fields.append(','.join(genes)) fields.extend([ variant.chr, str(variant.pos), variant.ref, variant.alt, variant.annotation.get('vep_group', '.'), ]) for ref_population_slug in project.get_reference_population_slugs(): fields.append(variant.annotation['freqs'][ref_population_slug]) for field_key in ['polyphen', 'sift', 'muttaster', 'fathmm']: fields.append(variant.annotation[field_key]) for indiv_id in indiv_id_list: variant = individual_id_to_variant.get(indiv_id) genotype = None if variant is not None: genotype = variant.get_genotype(indiv_id) if genotype is None: fields.extend(['.', '.', '.']) else: if genotype.num_alt == 0: fields.append("%s/%s" % (variant.ref, variant.ref)) elif genotype.num_alt == 1: fields.append("%s/%s" % (variant.ref, variant.alt)) elif genotype.num_alt == 2: fields.append("%s/%s" % (variant.alt, variant.alt)) else: fields.append("./.") fields.append(str(genotype.gq) if genotype.gq is not None else '.') fields.append(genotype.extras['dp'] if genotype.extras.get('dp') is not None else '.') writer.writerow(fields) return response