Skip to content

Commit

Permalink
Merge pull request #4198 from broadinstitute/aip-family-mapping
Browse files Browse the repository at this point in the history
Aip family mapping
  • Loading branch information
hanars authored Jun 27, 2024
2 parents a09e0c7 + afceec7 commit 64b0520
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
22 changes: 17 additions & 5 deletions seqr/views/apis/summary_data_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,31 @@ def bulk_update_family_external_analysis(request):

def _load_aip_data(data: dict, user: User):
category_map = data['metadata']['categories']
projects = data['metadata'].get('projects')
results = data['results']

family_id_map = dict(Individual.objects.filter(
family__project__in=get_internal_projects(), individual_id__in=results.keys(),
).values_list('individual_id', 'family_id'))
if not projects:
raise ErrorsWarningsException(['No projects specified in the metadata'])

family_id_map = defaultdict(list)
for individual_id, family_id in Individual.objects.filter(
family__project__in=get_internal_projects().filter(name__in=projects), individual_id__in=results.keys(),
).values_list('individual_id', 'family_id'):
family_id_map[individual_id].append(family_id)
errors = []
missing_individuals = set(results.keys()) - set(family_id_map.keys())
if missing_individuals:
raise ErrorsWarningsException([f'Unable to find the following individuals: {", ".join(sorted(missing_individuals))}'])
errors.append(f'Unable to find the following individuals: {", ".join(sorted(missing_individuals))}')
multi_family_individuals = {individual_id for individual_id, families in family_id_map.items() if len(families) > 1}
if multi_family_individuals:
errors.append(f'The following individuals are found in multiple families: {", ".join(sorted(multi_family_individuals))}')
if errors:
raise ErrorsWarningsException(errors)

family_variant_data = {}
for family_id, variant_pred in results.items():
family_variant_data.update({
(family_id_map[family_id], variant_id): pred for variant_id, pred in variant_pred.items()
(family_id_map[family_id][0], variant_id): pred for variant_id, pred in variant_pred.items()
})

today = datetime.now().strftime('%Y-%m-%d')
Expand Down
12 changes: 11 additions & 1 deletion seqr/views/apis/summary_data_api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,19 @@ def test_bulk_update_family_external_analysis(self, mock_load_uploaded_file, moc
body['dataType'] = 'AIP'
response = self.client.post(url, content_type='application/json', data=json.dumps(body))
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()['errors'], ['No projects specified in the metadata'])

aip_upload['metadata']['projects'] = ['1kg project nåme with uniçøde', 'Test Reprocessed Project']
response = self.client.post(url, content_type='application/json', data=json.dumps(body))
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()['errors'], ['Unable to find the following individuals: SAM_123'])

aip_upload['results']['NA20889'] = aip_upload['results'].pop('SAM_123')
aip_upload['results']['NA20870'] = aip_upload['results'].pop('SAM_123')
response = self.client.post(url, content_type='application/json', data=json.dumps(body))
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()['errors'], ['The following individuals are found in multiple families: NA20870'])

aip_upload['results']['NA20889'] = aip_upload['results'].pop('NA20870')
response = self.client.post(url, content_type='application/json', data=json.dumps(body))
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()['errors'], [
Expand Down

0 comments on commit 64b0520

Please sign in to comment.