Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCKAN-248 fix: Add check for anatomical entity before adding it #218

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions backend/composer/services/cs_ingestion/cs_ingestion_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,11 @@ def add_origins(connectivity_statement: ConnectivityStatement, statement: Dict):
origin_uris = statement[ORIGINS].anatomical_entities
origins = []
for uri in origin_uris:
anatomical_entities = AnatomicalEntity.objects.filter(ontology_uri=uri)
origins.append(anatomical_entities.first())
anatomical_entity = AnatomicalEntity.objects.filter(ontology_uri=uri).first()
if anatomical_entity:
origins.append(anatomical_entity)
else:
assert connectivity_statement.state == CSState.INVALID

if origins:
connectivity_statement.origins.add(*origins)
Expand All @@ -429,29 +432,41 @@ def add_vias(connectivity_statement: ConnectivityStatement, statement: Dict):

for via_instance, via_data in zip(created_vias, statement[VIAS]):
for uri in via_data.anatomical_entities:
anatomical_entities = AnatomicalEntity.objects.filter(ontology_uri=uri)
via_instance.anatomical_entities.add(anatomical_entities.first())
anatomical_entity = AnatomicalEntity.objects.filter(ontology_uri=uri).first()
if anatomical_entity:
via_instance.anatomical_entities.add(anatomical_entity)
else:
assert connectivity_statement.state == CSState.INVALID

for uri in via_data.from_entities:
from_entity = AnatomicalEntity.objects.filter(ontology_uri=uri).first()
via_instance.from_entities.add(from_entity)
if from_entity:
via_instance.from_entities.add(from_entity)
else:
assert connectivity_statement.state == CSState.INVALID


def add_destinations(connectivity_statement: ConnectivityStatement, statement: Dict):
destinations_data = [
Destination(connectivity_statement=connectivity_statement, type=dest.type)
for dest in statement[DESTINATIONS]
]

created_destinations = Destination.objects.bulk_create(destinations_data)

for destination_instance, dest_data in zip(created_destinations, statement[DESTINATIONS]):
for uri in dest_data.anatomical_entities:
anatomical_entity = AnatomicalEntity.objects.filter(ontology_uri=uri).first()
destination_instance.anatomical_entities.add(anatomical_entity)
if anatomical_entity:
destination_instance.anatomical_entities.add(anatomical_entity)
else:
assert connectivity_statement.state == CSState.INVALID

for uri in dest_data.from_entities:
from_entity = AnatomicalEntity.objects.filter(ontology_uri=uri).first()
destination_instance.from_entities.add(from_entity)
if from_entity:
destination_instance.from_entities.add(from_entity)
else:
assert connectivity_statement.state == CSState.INVALID


def add_notes(connectivity_statement: ConnectivityStatement, statement: Dict):
Expand Down Expand Up @@ -541,4 +556,4 @@ def propagate_invalid_state(connectivity_statement: ConnectivityStatement, inval
connectivity_statements_invalid_reasons[backward_cs.reference_uri][1].append(current_reason)

# Recursively propagate invalid state
propagate_invalid_state(backward_cs, invalid_visited, connectivity_statements_invalid_reasons, current_reason)
propagate_invalid_state(backward_cs, invalid_visited, connectivity_statements_invalid_reasons, current_reason)