Skip to content

Commit

Permalink
annotate biosample queryset with phenopacket subject id
Browse files Browse the repository at this point in the history
  • Loading branch information
zxenia committed Apr 20, 2022
1 parent f0e2b8a commit 2130e4b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 6 additions & 6 deletions chord_metadata_service/chord/export_cbio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import csv
from typing import TextIO, Callable
from django.db.models import F

from .export_utils import ExportError

Expand Down Expand Up @@ -56,7 +57,8 @@ def study_export(getPath: Callable[[str], str], dataset_id: str):

# Export samples
with open(getPath(SAMPLE_DATA_FILENAME), 'w') as file_sample:
sampl = pm.Biosample.objects.filter(phenopacket__table__ownership_record__dataset_id=dataset.identifier)
sampl = pm.Biosample.objects.filter(phenopacket__table__ownership_record__dataset_id=dataset.identifier)\
.annotate(phenopacket_subject_id=F("phenopacket__subject"))
sample_export(sampl, file_sample)

with open(getPath(SAMPLE_META_FILENAME), 'w') as file_sample_meta:
Expand Down Expand Up @@ -181,12 +183,10 @@ def sample_export(results, file_handle: TextIO):
subject_id = None
if sample.individual is not None:
subject_id = sample.individual
elif sample.phenopacket_subject_id is not None:
subject_id = sample.phenopacket_subject_id
else:
phnpkt = pm.Phenopacket.objects.filter(biosamples=sample).first()
if phnpkt.subject is not None:
subject_id = phnpkt.subject.id
else:
continue
continue

sample_obj = {
'individual_id': subject_id,
Expand Down
4 changes: 3 additions & 1 deletion chord_metadata_service/chord/tests/test_export_cbio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Dict, TextIO
from os import walk, path

from django.db.models import F
from django.test import TestCase

from chord_metadata_service.chord.export_cbio import (
Expand Down Expand Up @@ -158,7 +159,8 @@ def test_export_cbio_patient_data(self):
break

def test_export_cbio_sample_data(self):
samples = PhModel.Biosample.objects.filter(phenopacket=self.p)
samples = PhModel.Biosample.objects.filter(phenopacket=self.p)\
.annotate(phenopacket_subject_id=F("phenopacket__subject"))
with io.StringIO() as output:
sample_export(samples, output)
# Check header
Expand Down

0 comments on commit 2130e4b

Please sign in to comment.