def add(request, project_id): project = get_object_or_404(Project, project_id=project_id) if not project.can_admin(request.user): raise PermissionDenied if request.method == 'POST': form = base_forms.AddCohortForm(project, request.POST) if form.is_valid(): cohort = Cohort.objects.create( project=project, cohort_id=form.cleaned_data['cohort_id'], display_name=form.cleaned_data['name'], short_description=form.cleaned_data['description'], ) for indiv in form.cleaned_data['individuals']: cohort.individuals.add(indiv) cohort.save() # TODO figure out a way to launch variant loading in the background #xbrowse_controls.load_variants_for_cohort_list(project, [cohort]) return JSONResponse({'is_error': False, 'next_page': reverse('cohort_home', args=(project.project_id, cohort.cohort_id))}) else: return JSONResponse({'is_error': True, 'error': server_utils.form_error_string(form)}) individuals_json = json_displays.individual_list(project.get_individuals()) return render(request, 'cohort/add.html', { 'project': project, 'individuals_json': json.dumps(individuals_json), })
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) })
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) })
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) })
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) })
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) })
def add_individual(request): """ Adds given individual to the local database Args: submission information of a single patient is expected in the POST data Returns: Submission status information """ affected_patient = json.loads( request.POST.get("patient_data", "wasn't able to parse patient_data in POST!")) seqr_id = request.POST.get( "localId", "wasn't able to parse Id (as seqr knows it) in POST!") family_id = request.POST.get("familyId", "wasn't able to parse family Id in POST!") project_id = request.POST.get("projectId", "wasn't able to parse project Id in POST!") submission = json.dumps({'patient': affected_patient}) headers = { 'X-Auth-Token': settings.MME_NODE_ADMIN_TOKEN, 'Accept': settings.MME_NODE_ACCEPT_HEADER, 'Content-Type': settings.MME_CONTENT_TYPE_HEADER } result = requests.post(url=settings.MME_ADD_INDIVIDUAL_URL, headers=headers, data=submission) #if successfully submitted to MME, persist info if result.status_code == 200: settings.SEQR_ID_TO_MME_ID_MAP.insert({ 'submitted_data': { 'patient': affected_patient }, 'seqr_id': seqr_id, 'family_id': family_id, 'project_id': project_id, 'insertion_date': datetime.datetime.now() }) print result.status_code, ">" if result.status_code == 401: return JSONResponse({ 'http_result': { "message": "sorry, authorization failed, I wasn't able to insert that individual" }, 'status_code': result.status_code, }) return JSONResponse({ 'http_result': result.json(), 'status_code': result.status_code, })
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 request_dict = request.GET or request.POST project, family = get_project_and_family_for_user(request.user, request_dict) form = api_forms.MendelianVariantSearchForm(request_dict) if form.is_valid(): search_spec = form.cleaned_data['search_spec'] search_spec.family_id = family.family_id try: variants = api_utils.calculate_mendelian_variant_search( search_spec, family.xfamily()) except Exception as e: return JSONResponse({ 'is_error': True, 'error': str(e.args[0]) if e.args else str(e) }) sys.stderr.write("done fetching %s variants. Adding extra info..\n" % len(variants)) hashable_search_params = search_spec.toJSON() hashable_search_params['family_id'] = family.family_id search_hash = cache_utils.save_results_for_spec( project.project_id, hashable_search_params, [v.toJSON() for v in variants]) add_extra_info_to_variants_family(get_reference(), family, variants) sys.stderr.write( "done adding extra info to %s variants. Sending response..\n" % len(variants)) return_type = request_dict.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) })
def get_family_submissions(request, project_id, family_id): """ Gets all submission information for this family """ project = get_object_or_404(Project, project_id=project_id) if not project.can_view(request.user): raise PermissionDenied else: #find latest submission submission_records = settings.SEQR_ID_TO_MME_ID_MAP.find({ 'project_id': project_id, 'family_id': family_id }).sort('insertion_date', -1).limit(1) family_submissions = [] for submission in submission_records: family_submissions.append({ 'submitted_data': submission['submitted_data'], 'seqr_id': submission['seqr_id'], 'family_id': submission['family_id'], 'project_id': submission['project_id'], 'insertion_date': submission['insertion_date'].strftime("%b %d %Y %H:%M:%S"), }) return JSONResponse({"family_submissions": family_submissions})
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_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)
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)
def insert_individual_into_phenotips(request, eid, project_id): """ Insert these phenotypes of this individual into PhenoTips Args: eid is an external id to PhenoTips. (A sample ID in seqr) project_id the project this individual belongs to Returns: A JSON response with insertion status """ project = get_object_or_404(Project, project_id=project_id) if not project.can_edit(request.user): raise PermissionDenied #this is the phenotypes that were uploaded and needs to be inserted in uploaded_phenotype_data = json.loads( request.POST.get("phenotypes", "no data found in POST")) #finding phenotips ID via sample ID, which is in phenotype_data['external_id'], and fetching existing #phenotips data with that indiv = Individual.objects.get( project=project, indiv_id=uploaded_phenotype_data['external_id']) phenotips_id = indiv.phenotips_id existing_phenotypes = get_phenotypes_entered_for_individual( project_id, phenotips_id) #using the external ID, and ID from existing record in PhenoTips external_id = phenotips_id #merge uploaded data into what'a already there merged_phenotypes = merge_phenotype_data(uploaded_phenotype_data, existing_phenotypes) username, passwd = (settings.PHENOTIPS_ADMIN_UNAME, settings.PHENOTIPS_ADMIN_PWD) url = settings.PHENOTIPS_UPLOAD_EXTERNAL_PHENOTYPE_URL + '/' + external_id response = requests.put(url, data=json.dumps(merged_phenotypes), auth=(username, passwd)) #do some validation to find out what went in (some values tend to drop in upload process VALID_UPLOAD = 204 validation = "" if response.status_code == VALID_UPLOAD: phenotypes_now_avalable = get_phenotypes_entered_for_individual( project_id, external_id) validation = validate_phenotips_upload(phenotypes_now_avalable, merged_phenotypes) update_xbrowse_model( indiv, phenotips_data=json.dumps(phenotypes_now_avalable)) if response.status_code != VALID_UPLOAD: logger.error("ERROR: %s %s" % (response.status_code, response.reason)) return JSONResponse({ 'response': response.text, 'status_code': response.status_code, 'validation': validation })
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), })
def gene_autocomplete(request): query = request.GET.get('q', '') genes = [{ 'value': item['gene_id'], 'label': item['symbol'], } for k, item in GENE_ITEMS.items() if k.startswith(query.lower())][:20] return JSONResponse(genes)
def export_project_individuals(request, project_id): ''' Notes: 1. ONLY project-authorized user has access to this individual ''' family_data, variant_data, phenotype_entry_counts, family_statuses = fetch_project_individuals_data(project_id) return JSONResponse({ 'variant': variant_data, 'family_data': family_data, 'phenotype_entry_counts': phenotype_entry_counts })
def gene_autocomplete(request): query = request.GET.get('q', '') #sys.stderr.write("Gene autocomplete for: " + str(query) + "\n") gene_items = sorted([(k, item) for k, item in GENE_ITEMS.items() if k.startswith(query.lower())], key=lambda i: len(i[0])) genes = [{ 'value': item['gene_id'], 'label': item['symbol'], } for k, item in gene_items[:20]] return JSONResponse(genes)
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)
def projects(request): """ List the projects that this user has access to """ user_projects = user_controls.get_projects_for_user(request.user) project_ids = [p.project_id for p in user_projects] response_format = request.GET.get('format', 'json') if response_format == 'json': return JSONResponse({'projects': project_ids}) elif response_format == 'tsv': return HttpResponse('\n'.join(project_ids)) else: raise Exception("Invalid format")
def cohort_gene_search_spec(request): project, cohort = get_project_and_cohort_for_user(request.user, request.GET) search_spec, genes = cache_utils.get_cached_results(project.project_id, request.GET.get('search_hash')) if genes is None: genes = api_utils.calculate_cohort_gene_search(cohort, search_spec) api_utils.add_extra_info_to_genes(project, get_reference(), genes) return JSONResponse({ 'is_error': False, 'genes': genes, 'search_spec': search_spec, })
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) })
def export_project_variants(request, project_id): """ Export all variants associated to this project Args: Project id Returns: A JSON object of variant information """ project = get_object_or_404(Project, project_id=project_id) if not project.can_view(request.user): raise PermissionDenied status_description_map = {} for abbrev, details in ANALYSIS_STATUS_CHOICES: status_description_map[abbrev] = details[0] variants = [] project_tags = ProjectTag.objects.filter(project__project_id=project_id) for project_tag in project_tags: variant_tags = VariantTag.objects.filter(project_tag=project_tag) for variant_tag in variant_tags: variant = get_datastore(project.project_id).get_single_variant( project.project_id, variant_tag.family.family_id if variant_tag.family else '', variant_tag.xpos, variant_tag.ref, variant_tag.alt, ) variant_json = variant.toJSON() if variant is not None else { 'xpos': variant_tag.xpos, 'ref': variant_tag.ref, 'alt': variant_tag.alt } family_status = '' if variant_tag.family: family_status = status_description_map.get( variant_tag.family.analysis_status, 'unknown') variants.append({ "variant": variant_json, "tag": project_tag.tag, "description": project_tag.title, "family": variant_tag.family.toJSON(), "family_status": family_status }) return JSONResponse(variants)
def delete_variant_note(request, note_id): ret = { 'is_error': False, } notes = VariantNote.objects.filter(id=note_id) if not notes: ret['is_error'] = True ret['error'] = 'note id %s not found' % note_id else: note = list(notes)[0] if not note.project.can_edit(request.user): raise PermissionDenied note.delete() return JSONResponse(ret)
def get_project_individuals(request, project_id): """ Get a list of individuals with their family IDs of this project Args: project_id Returns: map of individuals and their family """ project = get_object_or_404(Project, project_id=project_id) if not project.can_view(request.user): raise PermissionDenied indivs = [] for indiv in project.get_individuals(): strct = {'guid': indiv.guid} for k, v in indiv.to_dict().iteritems(): if k not in ['phenotypes']: strct[k] = v indivs.append(strct) return JSONResponse({"individuals": indivs})
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 get_submission_candidates(request, project_id, family_id): """ Gathers submission candidate individuals from this family Args: individual_id: an individual ID project_id: project this individual belongs to Returns: Status code """ project = get_object_or_404(Project, project_id=project_id) if not project.can_view(request.user): raise PermissionDenied else: id_maps, affected_patients, id_map = get_all_clinical_data_for_family( project_id, family_id) return JSONResponse({ "submission_candidates": affected_patients, "id_maps": id_maps })
def add_variant_note(request): """ """ family = None if 'family_id' in request.GET: project, family = get_project_and_family_for_user( request.user, request.GET) else: project = utils.get_project_for_user(request.user, request.GET) form = api_forms.VariantNoteForm(project, request.GET) if form.is_valid(): note = VariantNote.objects.create( user=request.user, date_saved=datetime.datetime.now(), project=project, note=form.cleaned_data['note_text'], xpos=form.cleaned_data['xpos'], ref=form.cleaned_data['ref'], alt=form.cleaned_data['alt'], ) if family: note.family = family note.save() variant = get_datastore(project.project_id).get_single_variant( project.project_id, family.family_id, form.cleaned_data['xpos'], form.cleaned_data['ref'], form.cleaned_data['alt'], ) add_extra_info_to_variants_family(get_reference(), family, [ variant, ]) ret = { 'is_error': False, 'variant': variant.toJSON(), } else: ret = {'is_error': True, 'error': server_utils.form_error_string(form)} return JSONResponse(ret)
def export_project_individuals_phenotypes(request, project_id): """ Export all HPO terms entered for this project individuals. A direct proxy from PhenoTips API Args: project_id Returns: A JSON string of HPO terms entered """ project = get_object_or_404(Project, project_id=project_id) if not project.can_view(request.user): raise PermissionDenied project = get_object_or_404(Project, project_id=project_id) result = {} for individual in project.get_individuals(): ui_display_name = individual.indiv_id ext_id = individual.phenotips_id result[ui_display_name] = phenotype_entry_metric_for_individual( project_id, ext_id)['raw'] return JSONResponse(result)
def family_variant_annotation(request): # TODO: this view not like the others - refactor to forms error = None for key in ['project_id', 'family_id', 'xpos', 'ref', 'alt']: if request.GET.get(key) is 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_view(request.user): return PermissionDenied if not error: variant = get_datastore(project.project_id).get_single_variant( family.project.project_id, family.family_id, int(request.GET['xpos']), request.GET['ref'], request.GET['alt'] ) if not variant: error = "Variant does not exist" if not error: ret = { 'variant': variant.toJSON(), 'is_error': False, } else: ret = { 'is_error': True, 'error': error, } return JSONResponse(ret)
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