Skip to content

Commit

Permalink
skip samples with no individual
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Pillot committed Apr 19, 2022
1 parent fc42348 commit a30a41a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions chord_metadata_service/chord/export_cbio.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ 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_obj = {
'individual_id': sample.individual.id,
'id': sample.id
Expand Down
23 changes: 19 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,6 +58,11 @@ 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.
PhModel.Biosample.objects.filter(
id=EXAMPLE_INGEST_PHENOPACKET["biosamples"][-1]["id"]
).update(individual=None)

def stream_to_dict(self, output: TextIO) -> Dict[str, str]:
"""
Utility function. Parses cBioPortal meta data text files (lines of
Expand Down Expand Up @@ -158,6 +163,7 @@ def test_export_cbio_sample_data(self):
output.seek(0)
field_count = None
field_names = []
sample_count = 0
for i, line in enumerate(output):
# 4 first header lines begin with `#`
if i < 4:
Expand All @@ -178,10 +184,19 @@ def test_export_cbio_sample_data(self):
self.assertIn('SAMPLE_ID', pieces)
continue

# TSV body. Inspect first line and break
# TSV body.
self.assertEqual(field_count, len(pieces))
record = dict(zip(field_names, pieces))

self.assertEqual(record["PATIENT_ID"], EXAMPLE_INGEST_PHENOPACKET["subject"]["id"])
self.assertEqual(record["SAMPLE_ID"], EXAMPLE_INGEST_PHENOPACKET["biosamples"][0]["id"])
break
self.assertEqual(
record["PATIENT_ID"],
EXAMPLE_INGEST_PHENOPACKET["biosamples"][sample_count]["individual_id"]
)
self.assertEqual(
record["SAMPLE_ID"],
EXAMPLE_INGEST_PHENOPACKET["biosamples"][sample_count]["id"]
)
sample_count += 1

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

0 comments on commit a30a41a

Please sign in to comment.