Skip to content

Commit

Permalink
ESCKAN-65 - get entities in the journey as part of serializer export
Browse files Browse the repository at this point in the history
  • Loading branch information
D-GopalKrishna committed Sep 18, 2024
1 parent 82c22ad commit 13e2b7a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
7 changes: 7 additions & 0 deletions backend/composer/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ class ConnectivityStatementSerializer(BaseConnectivityStatementSerializer):
)
available_transitions = serializers.SerializerMethodField()
journey = serializers.SerializerMethodField()
entities_journey = serializers.SerializerMethodField()
statement_preview = serializers.SerializerMethodField()
errors = serializers.SerializerMethodField()

Expand All @@ -528,6 +529,10 @@ def get_journey(self, instance):
if 'journey' not in self.context:
self.context['journey'] = instance.get_journey()
return self.context['journey']

def get_entities_journey(self, instance):
self.context['entities_journey'] = instance.get_entities_journey()
return self.context['entities_journey']

def get_statement_preview(self, instance):
if 'journey' not in self.context:
Expand Down Expand Up @@ -630,6 +635,7 @@ class Meta(BaseConnectivityStatementSerializer.Meta):
"projection_phenotype",
"projection_phenotype_id",
"journey",
"entities_journey",
"laterality",
"projection",
"circuit_type",
Expand Down Expand Up @@ -722,6 +728,7 @@ class Meta(ConnectivityStatementSerializer.Meta):
"provenances",
"knowledge_statement",
"journey",
"entities_journey",
"laterality",
"projection",
"circuit_type",
Expand Down
8 changes: 6 additions & 2 deletions backend/composer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
ViaType,
Projection,
)
from .services.graph_service import compile_journey
from .services.graph_service import compile_journey, compile_entities_name
from .utils import doi_uri, pmcid_uri, pmid_uri, create_reference_uri


Expand Down Expand Up @@ -663,7 +663,11 @@ def get_previous_layer_entities(self, via_order):
return set(self.via_set.get(order=via_order - 1).anatomical_entities.all())

def get_journey(self):
return compile_journey(self)
return compile_journey(self)['journey']

def get_entities_journey(self):
entities_journey = compile_journey(self)['entities']
return entities_journey

def get_laterality_description(self):
laterality_map = {
Expand Down
20 changes: 16 additions & 4 deletions backend/composer/services/graph_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def consolidate_paths(paths):

paths = consolidated + [paths[i] for i in range(len(paths)) if i not in used_indices]

return [[((node[0].replace(JOURNEY_DELIMITER, ' or '), node[1]) if (
return paths, [[((node[0].replace(JOURNEY_DELIMITER, ' or '), node[1]) if (
node[1] == 0 or path.index(node) == len(path) - 1) else (
node[0].replace(JOURNEY_DELIMITER, ', '), node[1])) for node in path] for path in paths]

Expand Down Expand Up @@ -132,7 +132,7 @@ def merge_paths(path1, path2):
return merged_path


def compile_journey(connectivity_statement) -> List[str]:
def compile_journey(connectivity_statement) -> dict:
"""
Generates a string of descriptions of journey paths for a given connectivity statement.
Expand All @@ -153,7 +153,16 @@ def compile_journey(connectivity_statement) -> List[str]:

# Generate all paths and then consolidate them
all_paths = generate_paths(origins, vias, destinations)
journey_paths = consolidate_paths(all_paths)
consolidated_paths, journey_paths = consolidate_paths(all_paths)

entities = []
for path in consolidated_paths:
entity = {
'origins': path[0][0].split(JOURNEY_DELIMITER),
'destinations': path[-1][0].split(JOURNEY_DELIMITER),
'vias': [node for node, layer in path if 0 < layer < len(vias) + 1]
}
entities.append(entity)

# Create sentences for each journey path
journey_descriptions = []
Expand All @@ -169,4 +178,7 @@ def compile_journey(connectivity_statement) -> List[str]:

journey_descriptions.append(sentence)

return journey_descriptions
return {
'journey': journey_descriptions,
'entities': entities
}

0 comments on commit 13e2b7a

Please sign in to comment.