Skip to content

Commit

Permalink
fallback: get patient id from phenopackets
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Pillot committed Apr 19, 2022
1 parent a30a41a commit f0e2b8a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
17 changes: 13 additions & 4 deletions chord_metadata_service/chord/export_cbio.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,21 @@ def sample_export(results, file_handle: TextIO):

samples = []
for sample in results:
# sample.inidividual can be null. Skip the sample in that case.
if sample.individual is None:
continue

# sample.inidividual may be null: use Phenopacket model Subject field
# instead if available or skip.
subject_id = None
if sample.individual is not None:
subject_id = sample.individual
else:
phnpkt = pm.Phenopacket.objects.filter(biosamples=sample).first()
if phnpkt.subject is not None:
subject_id = phnpkt.subject.id
else:
continue

sample_obj = {
'individual_id': sample.individual.id,
'individual_id': subject_id,
'id': sample.id
}
if sample.sampled_tissue:
Expand Down
9 changes: 5 additions & 4 deletions chord_metadata_service/chord/tests/test_export_cbio.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def setUp(self) -> None:

self.p = WORKFLOW_INGEST_FUNCTION_MAP[WORKFLOW_PHENOPACKETS_JSON](EXAMPLE_INGEST_OUTPUTS, self.t.identifier)

# Update the last sample to remove reference to any individual.
# Update the last sample to remove direct reference to any individual.
# In that case, Sample and Individual are cross referenced through the
# Phenopacket model.
PhModel.Biosample.objects.filter(
id=EXAMPLE_INGEST_PHENOPACKET["biosamples"][-1]["id"]
).update(individual=None)
Expand Down Expand Up @@ -184,7 +186,7 @@ def test_export_cbio_sample_data(self):
self.assertIn('SAMPLE_ID', pieces)
continue

# TSV body.
# TSV body: 1 row per sample
self.assertEqual(field_count, len(pieces))
record = dict(zip(field_names, pieces))

Expand All @@ -198,5 +200,4 @@ def test_export_cbio_sample_data(self):
)
sample_count += 1

# samples not attached to an individual are not exported
self.assertEqual(sample_count, samples.filter(individual_id__isnull=False).count())
self.assertEqual(sample_count, samples.count())

0 comments on commit f0e2b8a

Please sign in to comment.