Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/features/phenov2-1394' into feat…
Browse files Browse the repository at this point in the history
…ures/katsu-permissions
  • Loading branch information
davidlougheed committed Oct 23, 2023
2 parents 1d1132f + 40c2440 commit 3d81d5d
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 29 deletions.
17 changes: 14 additions & 3 deletions chord_metadata_service/chord/ingest/phenopackets.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get_or_create_phenotypic_feature(pf: dict) -> pm.PhenotypicFeature:
description=pf.get("description", ""),
pftype=pf["type"],
excluded=pf.get("excluded", False),
modifier=pf.get("modifier", []), # TODO: Validate ontology term in schema...
modifiers=pf.get("modifiers", []), # TODO: Validate ontology term in schema...
severity=pf.get("severity"),
onset=pf.get("onset"),
evidence=pf.get("evidence"), # TODO: Separate class for evidence?
Expand Down Expand Up @@ -208,11 +208,22 @@ def get_or_create_genomic_interpretation(gen_interp: dict) -> pm.GenomicInterpre
gene_descriptor = _get_or_create_opt("gene_descriptor", gen_interp, get_or_create_gene_descriptor)
variant_interpretation = _get_or_create_opt("variant_interpretation", gen_interp, get_or_create_variant_interp)

# Check if a Biosample or Individual with subject_or_biosample_id exists
subject_or_biosample_id = gen_interp["subject_or_biosample_id"]
is_biosample = pm.Biosample.objects.filter(id=subject_or_biosample_id).exists()
is_subject = pm.Individual.objects.filter(id=subject_or_biosample_id).exists()

# Store the type the ID refers to in extra_properties
element_type = "biosample" if is_biosample and not is_subject else "subject"
extra_properties = gen_interp.get("extra_properties", {})
extra_properties["related_type"] = element_type

gen_obj, _ = pm.GenomicInterpretation.objects.get_or_create(
subject_or_biosample_id=gen_interp["subject_or_biosample_id"],
subject_or_biosample_id=subject_or_biosample_id,
interpretation_status=gen_interp["interpretation_status"],
gene_descriptor=gene_descriptor,
variant_interpretation=variant_interpretation
variant_interpretation=variant_interpretation,
extra_properties=extra_properties,
)
return gen_obj

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"label": "Hematuria"
},
"negated": false,
"modifier": []
"modifiers": []
},
{
"description": "",
Expand All @@ -27,7 +27,7 @@
"id": "HP:0012828",
"label": "Severe"
},
"modifier": []
"modifiers": []
}
],
"diseases": [
Expand Down Expand Up @@ -229,4 +229,4 @@
"is_control_sample": false
}
]
}
}
2 changes: 1 addition & 1 deletion chord_metadata_service/chord/tests/test_ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_create_pf(self):
"label": "Hematuria"
},
"excluded": False,
"modifier": [],
"modifiers": [],
"evidence": []
})

Expand Down
2 changes: 1 addition & 1 deletion chord_metadata_service/patients/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def filter_search(self, qs, name, value):
"phenopackets__phenotypic_features__description",
Cast("phenopackets__phenotypic_features__pftype", TextField()),
Cast("phenopackets__phenotypic_features__severity", TextField()),
Cast("phenopackets__phenotypic_features__modifier", TextField()),
Cast("phenopackets__phenotypic_features__modifiers", TextField()),
Cast("phenopackets__phenotypic_features__onset", TextField()),
Cast("phenopackets__phenotypic_features__evidence", TextField()),
Cast("phenopackets__phenotypic_features__extra_properties", TextField()),
Expand Down
2 changes: 1 addition & 1 deletion chord_metadata_service/phenopackets/descriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def phenotypic_feature(subject="a subject or biosample"):
"type": ontology_class("which describes the phenotype"),
"negated": "Whether the feature is present (false) or absent (true, feature is negated); default is false.",
"severity": ontology_class("that describes the severity of the condition"),
"modifier": { # TODO: Plural?
"modifiers": {
"description": "A list of ontology terms that provide more expressive / precise descriptions of a "
"phenotypic feature, including e.g. positionality or external factors.",
"items": ontology_class("that expounds on the phenotypic feature")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.6 on 2023-10-16 18:02

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('phenopackets', '0008_alter_biosample_procedure_delete_procedure'),
]

operations = [
migrations.RenameField(
model_name='phenotypicfeature',
old_name='modifier',
new_name='modifiers',
),
]
4 changes: 2 additions & 2 deletions chord_metadata_service/phenopackets/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class PhenotypicFeature(BaseTimeStamp, IndexableMixin):
excluded = models.BooleanField(default=False, help_text=rec_help(d.PHENOTYPIC_FEATURE, "negated"))
severity = JSONField(blank=True, null=True, validators=[ontology_validator],
help_text=rec_help(d.PHENOTYPIC_FEATURE, "severity"))
modifier = JSONField(blank=True, null=True, validators=[ontology_list_validator],
help_text=rec_help(d.PHENOTYPIC_FEATURE, "modifier"))
modifiers = JSONField(blank=True, null=True, validators=[ontology_list_validator],
help_text=rec_help(d.PHENOTYPIC_FEATURE, "modifiers"))
onset = JSONField(blank=True, null=True, validators=[JsonSchemaValidator(schema=TIME_ELEMENT_SCHEMA)])
resolution = JSONField(blank=True, null=True, validators=[
JsonSchemaValidator(schema=TIME_ELEMENT_SCHEMA)])
Expand Down
13 changes: 6 additions & 7 deletions chord_metadata_service/phenopackets/search_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from chord_metadata_service.patients.schemas import INDIVIDUAL_SCHEMA
from chord_metadata_service.resources.search_schemas import RESOURCE_SEARCH_SCHEMA
from chord_metadata_service.restapi.schema_utils import (
array_of,
merge_schema_dictionaries,
search_db_fk,
search_db_pk,
Expand Down Expand Up @@ -176,13 +177,11 @@ def _tag_with_database_attrs(schema: dict, db_attrs: dict):
}
}
}),
"negated": {
"excluded": {
"search": search_optional_eq(1),
},
"severity": ONTOLOGY_SEARCH_SCHEMA,
"modifier": { # TODO: Plural?
"items": ONTOLOGY_SEARCH_SCHEMA
},
"modifiers": array_of(ONTOLOGY_SEARCH_SCHEMA),
"onset": TIME_ELEMENT_SEARCH_SCHEMA,
"resolution": TIME_ELEMENT_SEARCH_SCHEMA,
"evidence": EVIDENCE_SEARCH_SCHEMA,
Expand Down Expand Up @@ -301,11 +300,11 @@ def _tag_with_database_attrs(schema: dict, db_attrs: dict):
"onset": DISEASE_ONSET_SEARCH_SCHEMA,
"disease_stage": {
"items": ONTOLOGY_SEARCH_SCHEMA,
"search": {"database": {"type": "array"}}
"search": {"database": {"type": "jsonb"}}
},
"tnm_finding": {
"clinical_tnm_finding": {
"items": ONTOLOGY_SEARCH_SCHEMA,
"search": {"database": {"type": "array"}}
"search": {"database": {"type": "jsonb"}}
},
},
"search": {
Expand Down
13 changes: 9 additions & 4 deletions chord_metadata_service/phenopackets/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,15 @@ class Meta:

def to_representation(self, instance):
response = super().to_representation(instance)
response["gene_descriptor"] = GeneDescriptorSerializer(
instance.gene_descriptor, many=False, required=False).data
response["variant_interpretation"] = VariantInterpretationSerializer(
instance.variant_interpretation, many=False, required=False).data

# May contain a gene_descriptor or a variant_interpretation, not both
if instance.gene_descriptor:
response["gene_descriptor"] = GeneDescriptorSerializer(
instance.gene_descriptor, many=False, required=False).data
elif instance.variant_interpretation:
response["variant_interpretation"] = VariantInterpretationSerializer(
instance.variant_interpretation, many=False, required=False).data

return response


Expand Down
4 changes: 2 additions & 2 deletions chord_metadata_service/phenopackets/tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def valid_phenotypic_feature(biosample=None, phenopacket=None):
"id": "HP: 0012825",
"label": "Mild"
},
modifier=[
modifiers=[
{
"id": "HP: 0012825 ",
"label": "Mild"
Expand Down Expand Up @@ -581,7 +581,7 @@ def invalid_phenotypic_feature():
"id": "HP: 0012825",
"label": "Mild"
},
modifier=[
modifiers=[
{
"label": "Mild"
},
Expand Down
2 changes: 1 addition & 1 deletion chord_metadata_service/restapi/fhir_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def fhir_observation(obj):
)
observation.extension = []
concept_extensions = codeable_concepts_fields(
['severity', 'modifier', 'onset'], 'phenotypic_feature', obj
['severity', 'modifiers', 'onset'], 'phenotypic_feature', obj
)
for ce in concept_extensions:
observation.extension.append(ce)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"title": "Phenotypic Feature",
"url": "http://ga4gh.org/fhir/phenopackets/StructureDefinition/PhenotypicFeature",
"severity": "http://ga4gh.org/fhir/phenopackets/StructureDefinition/phenotypic-feature-severity",
"modifier": "http://ga4gh.org/fhir/phenopackets/StructureDefinition/phenotypic-feature-modifier",
"modifiers": "http://ga4gh.org/fhir/phenopackets/StructureDefinition/phenotypic-feature-modifier",
"onset": "http://ga4gh.org/fhir/phenopackets/StructureDefinition/phenotypic-feature-onset",
"evidence": {
"url": "http://ga4gh.org/fhir/phenopackets/StructureDefinition/evidence",
Expand Down
2 changes: 1 addition & 1 deletion examples/fhir-mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
}
]
},
"modifier": {
"modifiers": {
"url": "http://ga4gh.org/fhir/phenopackets/StructureDefinition/phenotypic-feature-modifier",
"coding": [
{
Expand Down
4 changes: 2 additions & 2 deletions examples/single.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"label": "Hematuria"
},
"negated": false,
"modifier": [],
"modifiers": [],
"evidence": []
},
{
Expand All @@ -27,7 +27,7 @@
"id": "HP:0012828",
"label": "Severe"
},
"modifier": [],
"modifiers": [],
"evidence": []
}
],
Expand Down

0 comments on commit 3d81d5d

Please sign in to comment.