def _parse_mme_results(individual, saved_results, user, additional_genes=None, response_json=None): results = [] contact_institutions = set() for result_model in saved_results: result = result_model.result_data result['matchStatus'] = _get_json_for_model(result_model) results.append(result) contact_institutions.add(result['patient']['contact'].get( 'institution', '').strip().lower()) hpo_terms_by_id, genes_by_id, gene_symbols_to_ids = get_mme_genes_phenotypes( results + [individual.mme_submitted_data], additional_genes=additional_genes) parsed_results = [ _parse_mme_result(result, hpo_terms_by_id, gene_symbols_to_ids, individual.guid) for result in results ] parsed_results_gy_guid = { result['matchStatus']['matchmakerResultGuid']: result for result in parsed_results } contact_notes = { note.institution: _get_json_for_model(note, user=user) for note in MatchmakerContactNotes.objects.filter( institution__in=contact_institutions) } response = { 'mmeResultsByGuid': parsed_results_gy_guid, 'mmeContactNotes': contact_notes, 'individualsByGuid': { individual.guid: { 'mmeResultGuids': parsed_results_gy_guid.keys(), 'mmeSubmittedData': parse_mme_patient(individual.mme_submitted_data, hpo_terms_by_id, gene_symbols_to_ids, individual.guid), 'mmeSubmittedDate': individual.mme_submitted_date, 'mmeDeletedDate': individual.mme_deleted_date, } }, 'genesById': genes_by_id, } if response_json: response.update(response_json) return create_json_response(response)
def _parse_mme_results(submission, saved_results, user, additional_genes=None, response_json=None): results = [] contact_institutions = set() prefetch_related_objects(saved_results, 'originating_submission__individual__family__project') for result_model in saved_results: result = result_model.result_data result['matchStatus'] = _get_json_for_model(result_model) if result_model.originating_submission: originating_family = result_model.originating_submission.individual.family if has_project_permissions(originating_family.project, user): result['originatingSubmission'] = { 'originatingSubmissionGuid': result_model.originating_submission.guid, 'familyGuid': originating_family.guid, 'projectGuid': originating_family.project.guid, } results.append(result) contact_institutions.add(result['patient']['contact'].get('institution', '').strip().lower()) additional_hpo_ids = {feature['id'] for feature in (submission.features or []) if feature.get('id')} if not additional_genes: additional_genes = set() additional_genes.update({gene_feature['gene']['id'] for gene_feature in (submission.genomic_features or [])}) hpo_terms_by_id, genes_by_id, gene_symbols_to_ids = get_mme_genes_phenotypes_for_results( results, additional_genes=additional_genes, additional_hpo_ids=additional_hpo_ids) parsed_results = [_parse_mme_result(res, hpo_terms_by_id, gene_symbols_to_ids, submission.guid) for res in results] parsed_results_gy_guid = {result['matchStatus']['matchmakerResultGuid']: result for result in parsed_results} contact_notes = {note.institution: _get_json_for_model(note, user=user) for note in MatchmakerContactNotes.objects.filter(institution__in=contact_institutions)} submission_json = get_json_for_matchmaker_submission( submission, individual_guid=submission.individual.guid, additional_model_fields=['contact_name', 'contact_href', 'submission_id'] ) submission_json.update({ 'mmeResultGuids': list(parsed_results_gy_guid.keys()), 'phenotypes': parse_mme_features(submission.features, hpo_terms_by_id), 'geneVariants': parse_mme_gene_variants(submission.genomic_features, gene_symbols_to_ids), }) response = { 'mmeResultsByGuid': parsed_results_gy_guid, 'mmeContactNotes': contact_notes, 'mmeSubmissionsByGuid': {submission.guid: submission_json}, 'individualsByGuid': {submission.individual.guid: {'mmeSubmissionGuid': submission.guid}}, 'genesById': genes_by_id, } if response_json: response.update(response_json) return create_json_response(response)
def get_gene(gene_id, user): gene = GeneInfo.objects.get(gene_id=gene_id) gene_json = _get_json_for_model(gene, get_json_for_models=_get_json_for_genes, user=user, gene_fields=ALL_GENE_FIELDS) return gene_json
def _process_result(result, gene): for field, (_, result_func) in gene_fields.items(): if field == NOTES: updates = {'notes': gene_notes_json.get(result['geneId'], [])} elif field == CONSTRAINT: constraint = _get_gene_model(gene, 'geneconstraint') updates = { 'constraints': _get_json_for_model( constraint, process_result=_add_total_constraint_count) if constraint else {} } else: updates = result_func(gene) result.update(updates)
def update_mme_result_status(request, matchmaker_result_guid): """ Looks for matches for the given individual. Expects a single patient (MME spec) in the POST data field under key "patient_data" Args: project_id,indiv_id and POST all data in POST under key "patient_data" Returns: Status code and results """ result = MatchmakerResult.objects.get(guid=matchmaker_result_guid) check_mme_permissions(result.submission, request.user) request_json = json.loads(request.body) update_model_from_json(result, request_json, allow_unknown_keys=True) return create_json_response({ 'mmeResultsByGuid': {matchmaker_result_guid: {'matchStatus': _get_json_for_model(result)}}, })
def update_mme_contact_note(request, institution): """ Looks for matches for the given individual. Expects a single patient (MME spec) in the POST data field under key "patient_data" Args: project_id,indiv_id and POST all data in POST under key "patient_data" Returns: Status code and results """ institution = institution.strip().lower() note, _ = MatchmakerContactNotes.objects.get_or_create(institution=institution) request_json = json.loads(request.body) note.comments = request_json.get('comments', '') note.save() return create_json_response({ 'mmeContactNotes': {institution: _get_json_for_model(note, user=request.user)}, })
def send_mme_contact_email(request, matchmaker_result_guid): """ Sends the given email and updates the contacted status for the match Args: matchmaker_result_guid Returns: Status code and results """ result = MatchmakerResult.objects.get(guid=matchmaker_result_guid) project = result.individual.family.project check_permissions(project, request.user) request_json = json.loads(request.body) email_message = EmailMessage( subject=request_json['subject'], body=request_json['body'], to=map(lambda s: s.strip(), request_json['to'].split(',')), from_email='*****@*****.**', ) try: email_message.send() except Exception as e: message = e.message json_body = {} if hasattr(e, 'response'): message = e.response.content try: json_body = e.response.json() except Exception: pass return create_json_response(json_body, status=getattr(e, 'status_code', 400), reason=message) update_model_from_json(result, {'weContacted': True}) return create_json_response({ 'mmeResultsByGuid': { matchmaker_result_guid: { 'matchStatus': _get_json_for_model(result) } }, })
def update_mme_result_status(request, matchmaker_result_guid): """ Looks for matches for the given individual. Expects a single patient (MME spec) in the POST data field under key "patient_data" Args: project_id,indiv_id and POST all data in POST under key "patient_data" Returns: Status code and results """ result = MatchmakerResult.objects.get(guid=matchmaker_result_guid) project = result.individual.family.project check_permissions(project, request.user) request_json = json.loads(request.body) update_model_from_json(result, request_json, allow_unknown_keys=True) return create_json_response({ 'mmeResultsByGuid': {matchmaker_result_guid: {'matchStatus': _get_json_for_model(result)}}, })
def _parse_mme_results(individual, saved_results): results = [] for result_model in saved_results: result = result_model.result_data result['matchStatus'] = _get_json_for_model(result_model) results.append(result) hpo_terms_by_id, genes_by_id, gene_symbols_to_ids = get_mme_genes_phenotypes(results + [individual.mme_submitted_data]) parsed_results = [_parse_mme_result(result, hpo_terms_by_id, gene_symbols_to_ids) for result in results] parsed_results_gy_guid = {result['matchStatus']['matchmakerResultGuid']: result for result in parsed_results} return create_json_response({ 'mmeResultsByGuid': parsed_results_gy_guid, 'individualsByGuid': {individual.guid: { 'mmeResultGuids': parsed_results_gy_guid.keys(), 'mmeSubmittedData': parse_mme_patient(individual.mme_submitted_data, hpo_terms_by_id, gene_symbols_to_ids), 'mmeSubmittedDate': individual.mme_submitted_date, 'mmeDeletedDate': individual.mme_deleted_date, }}, 'genesById': genes_by_id, })
def send_mme_contact_email(request, matchmaker_result_guid): """ Sends the given email and updates the contacted status for the match Args: matchmaker_result_guid Returns: Status code and results """ result = MatchmakerResult.objects.get(guid=matchmaker_result_guid) check_mme_permissions(result.submission, request.user) request_json = json.loads(request.body) email_message = EmailMessage( subject=request_json['subject'], body=request_json['body'], to=[s.strip() for s in request_json['to'].split(',')], from_email=MME_DEFAULT_CONTACT_EMAIL, ) try: email_message.send() except Exception as e: message = str(e) json_body = {} if hasattr(e, 'response'): message = e.response.content try: json_body = e.response.json() except Exception: json_body = {'message':message} return create_json_response(json_body, status=getattr(e, 'status_code', 400), reason=message) update_model_from_json(result, {'weContacted': True}) return create_json_response({ 'mmeResultsByGuid': {matchmaker_result_guid: {'matchStatus': _get_json_for_model(result)}}, })
def _add_dbnsfp(gene): model = _get_gene_model(gene, 'dbnsfpgene') if model: return _get_json_for_model(model) else: return _get_empty_json_for_model(dbNSFPGene)
def _add_gene_model_func(gene): model = _get_gene_model(gene, field) return {return_key: _get_json_for_model(model) if model else default()}