Skip to content

Commit

Permalink
SCKAN-248 fix: Add check for anatomical entity before adding it
Browse files Browse the repository at this point in the history
  • Loading branch information
afonsobspinto committed Feb 7, 2024
1 parent 0e84bf7 commit 14d56e5
Showing 1 changed file with 24 additions and 9 deletions.
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)

0 comments on commit 14d56e5

Please sign in to comment.