Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 14 additions & 2 deletions src/app_neo4j_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,13 +574,25 @@ def get_provenance(neo4j_driver, uuid, depth):
max_level_str = ''
if depth is not None and len(str(depth)) > 0:
max_level_str = f"maxLevel: {depth}, "

delimiter = ', '
entity_properties = ['group_uuid', 'created_by_user_displayname', 'created_by_user_sub', 'created_by_user_email', 'uuid', 'dataset_type', 'hubmap_id', 'entity_type', 'status', 'created_timestamp']
activity_properties = ['hubmap_id', 'created_by_user_displayname', 'created_by_user_sub', 'created_by_user_email', 'creation_action', 'uuid', 'status', 'created_timestamp']
entity_cypher_pairs_list = [f'{prop}: node.{prop}' for prop in entity_properties]
activity_cypher_pairs_list = [f'{prop}: node.{prop}' for prop in activity_properties]
entity_cypher_pairs_string = delimiter.join(entity_cypher_pairs_list)
activity_cypher_pairs_string = delimiter.join(activity_cypher_pairs_list)
# More info on apoc.path.subgraphAll() procedure: https://neo4j.com/labs/apoc/4.0/graph-querying/expand-subgraph/
query = (f"MATCH (n:Entity) "
f"WHERE n.uuid = '{uuid}' "
f"CALL apoc.path.subgraphAll(n, {{ {max_level_str} relationshipFilter:'<ACTIVITY_INPUT|<ACTIVITY_OUTPUT', labelFilter:'-Lab' }}) "
f"YIELD nodes, relationships "
f"WITH [node in nodes | node {{ .*, label:labels(node)[0] }} ] as nodes, "
f"WITH [node in nodes | "
f" CASE "
f" WHEN 'Activity' IN labels(node) THEN node {{{activity_cypher_pairs_string} , label: labels(node)[0] }} "
f" WHEN 'Entity' IN labels(node) THEN node {{{entity_cypher_pairs_string} , label: labels(node)[0] }} "
f" ELSE node {{ label: labels(node)[0] }} "
f" END "
f"] as nodes, "
f"[rel in relationships | rel {{ .*, fromNode: {{ label:labels(startNode(rel))[0], uuid:startNode(rel).uuid }}, toNode: {{ label:labels(endNode(rel))[0], uuid:endNode(rel).uuid }}, rel_data: {{ type: type(rel) }} }} ] as rels "
f"WITH {{ nodes:nodes, relationships:rels }} as json "
f"RETURN json")
Expand Down
2 changes: 1 addition & 1 deletion src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Flask==3.0.3
neo4j==5.20.0
prov==2.0.0
prov==2.0.1
Werkzeug==3.0.3
deepdiff==7.0.1

Expand Down
2 changes: 1 addition & 1 deletion src/schema/schema_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SchemaConstants(object):

DOI_BASE_URL = 'https://doi.org/'

DATASETS_OMITTED_FIELDS = ['ingest_metadata', 'metadata', 'files']
OMITTED_FIELDS = ['ingest_metadata', 'files']

# Define an enumeration to classify an entity's visibility, which can be combined with
# authorization info when verify operations on a request.
Expand Down
31 changes: 23 additions & 8 deletions src/schema/schema_neo4j_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def filter_ancestors_by_type(neo4j_driver, direct_ancestor_uuids, entity_type):
"""
def get_children(neo4j_driver, uuid, property_key = None):
results = []

fields_to_omit = SchemaConstants.OMITTED_FIELDS
if property_key:
query = (f"MATCH (e:Entity)-[:ACTIVITY_INPUT]->(:Activity)-[:ACTIVITY_OUTPUT]->(child:Entity) "
# The target entity can't be a Lab
Expand Down Expand Up @@ -193,7 +193,10 @@ def get_children(neo4j_driver, uuid, property_key = None):
else:
# Convert the list of nodes to a list of dicts
results = nodes_to_dicts(record[record_field_name])

if fields_to_omit:
for node_dict in results:
for field in fields_to_omit:
node_dict.pop(field, None)
return results


Expand All @@ -216,7 +219,7 @@ def get_children(neo4j_driver, uuid, property_key = None):
"""
def get_parents(neo4j_driver, uuid, property_key = None):
results = []

fields_to_omit = SchemaConstants.OMITTED_FIELDS
if property_key:
query = (f"MATCH (e:Entity)<-[:ACTIVITY_OUTPUT]-(:Activity)<-[:ACTIVITY_INPUT]-(parent:Entity) "
# Filter out the Lab entities
Expand Down Expand Up @@ -245,6 +248,10 @@ def get_parents(neo4j_driver, uuid, property_key = None):
else:
# Convert the list of nodes to a list of dicts
results = nodes_to_dicts(record[record_field_name])
if fields_to_omit:
for node_dict in results:
for field in fields_to_omit:
node_dict.pop(field, None)

return results

Expand Down Expand Up @@ -380,7 +387,7 @@ def get_tuplets(neo4j_driver, uuid, property_key=None):
"""
def get_ancestors(neo4j_driver, uuid, property_key = None):
results = []

fields_to_omit = SchemaConstants.OMITTED_FIELDS
if property_key:
query = (f"MATCH (e:Entity)<-[:ACTIVITY_INPUT|ACTIVITY_OUTPUT*]-(ancestor:Entity) "
# Filter out the Lab entities
Expand Down Expand Up @@ -410,6 +417,11 @@ def get_ancestors(neo4j_driver, uuid, property_key = None):
# Convert the list of nodes to a list of dicts
results = nodes_to_dicts(record[record_field_name])

if fields_to_omit:
for node_dict in results:
for field in fields_to_omit:
node_dict.pop(field, None)

return results

"""
Expand All @@ -431,7 +443,7 @@ def get_ancestors(neo4j_driver, uuid, property_key = None):
"""
def get_descendants(neo4j_driver, uuid, property_key = None):
results = []

fields_to_omit = SchemaConstants.OMITTED_FIELDS
if property_key:
query = (f"MATCH (e:Entity)-[:ACTIVITY_INPUT|ACTIVITY_OUTPUT*]->(descendant:Entity) "
# The target entity can't be a Lab
Expand Down Expand Up @@ -460,7 +472,10 @@ def get_descendants(neo4j_driver, uuid, property_key = None):
else:
# Convert the list of nodes to a list of dicts
results = nodes_to_dicts(record[record_field_name])

if fields_to_omit:
for node_dict in results:
for field in fields_to_omit:
node_dict.pop(field, None)
return results


Expand Down Expand Up @@ -1185,7 +1200,7 @@ def get_dataset_upload(neo4j_driver, uuid):
def get_collection_datasets(neo4j_driver, uuid):
results = []

fields_to_omit = SchemaConstants.DATASETS_OMITTED_FIELDS
fields_to_omit = SchemaConstants.OMITTED_FIELDS
query = (f"MATCH (e:Dataset)-[:IN_COLLECTION]->(c:Collection) "
f"WHERE c.uuid = '{uuid}' "
f"RETURN COLLECT(apoc.create.vNode(labels(e), apoc.map.removeKeys(properties(e), {fields_to_omit}))) AS {record_field_name}")
Expand Down Expand Up @@ -1391,7 +1406,7 @@ def unlink_datasets_from_upload(neo4j_driver, upload_uuid, dataset_uuids_list):
"""
def get_upload_datasets(neo4j_driver, uuid, property_key = None):
results = []
fields_to_omit = SchemaConstants.DATASETS_OMITTED_FIELDS
fields_to_omit = SchemaConstants.OMITTED_FIELDS
if property_key:
query = (f"MATCH (e:Dataset)-[:IN_UPLOAD]->(s:Upload) "
f"WHERE s.uuid = '{uuid}' "
Expand Down
Loading