Skip to content
Snippets Groups Projects
Commit 12a2097b authored by Chiara Liotta's avatar Chiara Liotta
Browse files

fix docs and resolve step path

parent d90289ad
No related branches found
No related tags found
No related merge requests found
from neo4j import Driver
from graph_creation.utils import create_input_nodes_and_relationships, process_source_relationship
from graph_creation.utils import create_input_nodes_and_relationships, process_source_relationship, resolve_relative_path
from neo4j_queries.node_queries import ensure_component_node, ensure_data_node, ensure_parameter_node
from neo4j_queries.edge_queries import create_data_relationship, create_out_param_relationship
from pathlib import Path
......@@ -66,7 +66,7 @@ def process_cwl_outputs(driver: Driver, cwl_entity: dict) -> None:
for source_id in output['outputSource']:
process_source_relationship(driver, source_id, component_id, param_node_internal_id)
def process_cwl_steps(driver: Driver, cwl_entity: dict, repo_path: str) -> None:
def process_cwl_steps(driver: Driver, cwl_entity: dict) -> None:
"""
Processes the steps of a CWL Workflow component( which we will refer to as outer workflow component).
A step can be a Workflow, CommandLineTool or ExpressionTool.
......@@ -89,12 +89,14 @@ def process_cwl_steps(driver: Driver, cwl_entity: dict, repo_path: str) -> None:
Parameters:
driver (Driver): the driver used to connect to Neo4j
cwl_entity (dict): the dictionary containing the parsed contents of the CWL component
repo_path (str): the path of the repository that contains the CWL component
"""
for step in cwl_entity['steps']:
# Retrieve path of the step
combined_path = Path(repo_path) / step['run']
step_path = str(combined_path)
workflow_folder = Path(cwl_entity['path']).parent
full_step_path = workflow_folder / Path(step['run'])
step_path = str(resolve_relative_path(full_step_path))
# Create the step component node with ID equal to the step
s_node = ensure_component_node(driver, step_path)
s_node_internal_id = s_node[0]
......
from pathlib import Path
from neo4j import Driver
from neo4j_queries.node_queries import ensure_data_node, ensure_parameter_node
from neo4j_queries.edge_queries import create_data_relationship, create_in_param_relationship
......@@ -44,3 +45,30 @@ def process_source_relationship(driver: Driver, source_id: str, component_id: st
data_node = ensure_data_node(driver, source_id, component_id)
data_node_internal_id = data_node[0]
create_data_relationship(driver, param_node_internal_id, data_node_internal_id)
def resolve_relative_path(path: Path)-> Path:
"""
Resolves a relative path by simplifying `.` (current directory)
and `..` (parent directory) components without converting it to an absolute path.
Parameters:
path (Path): the input Path object to be resolved
Returns:
Path: a new object representing the simplified relative path
Example:
>>> resolve_relative_path(Path("x/y/../z"))
Path('x/z')
>>> resolve_relative_path(Path("./a/./b/c/../d"))
Path('a/b/d')
"""
parts = []
for part in path.parts:
if part == "..":
if parts:
parts.pop()
elif part != ".":
parts.append(part)
return Path(*parts)
\ No newline at end of file
......@@ -20,9 +20,9 @@ def create_in_param_relationship(driver: Driver, prefixed_component_id: str, par
component_id = clean_component_id(prefixed_component_id)
query = """
MATCH (c:Component {component_id: $component_id}), (p)
WHERE id(p) = $parameter_internal_id
WHERE elementId(p) = $parameter_internal_id
MERGE (c)-[:DATA]->(p)
RETURN c.id AS component_id, p.parameter_id AS parameter_id
RETURN c.component_id AS component_id, p.parameter_id AS parameter_id
"""
with driver.session() as session:
result = session.run(query, component_id=component_id,
......@@ -49,7 +49,7 @@ def create_out_param_relationship(driver: Driver, prefixed_component_id: str, pa
component_id = clean_component_id(prefixed_component_id)
query = """
MATCH (c:Component {component_id: $component_id}), (p)
WHERE id(p) = $parameter_internal_id
WHERE elementId(p) = $parameter_internal_id
MERGE (c)<-[:DATA]-(p)
RETURN c.component_id AS component_id, p.parameter_id AS parameter_id
"""
......@@ -75,12 +75,13 @@ def create_data_relationship(driver: Driver, from_internal_node_id: int, to_inte
"""
query = """
MATCH (a), (b)
WHERE id(a) = $from_internal_node_id AND id(b) = $to_internal_node_id
WHERE elementId(a) = $from_internal_node_id AND elementId(b) = $to_internal_node_id
MERGE (a)-[:DATA]->(b)
RETURN a.id AS id_1, b.id AS id_2
RETURN elementId(a) AS id_1, elementId(b) AS id_2
"""
with driver.session() as session:
result = session.run(query, from_internal_node_id=from_internal_node_id,
to_internal_node_id=to_internal_node_id)
record = result.single()
return record["id_1"], record["id_2"]
return record["id_1"], record["id_2"]
\ No newline at end of file
......@@ -18,7 +18,7 @@ def ensure_component_node(driver: Driver, prefixed_component_id: str) -> tuple[i
component_id = clean_component_id(prefixed_component_id)
query = """
MERGE (c:Component {component_id: $component_id})
RETURN id(c) AS node_internal_id, c.component_id AS component_id
RETURN elementId(c) AS node_internal_id, c.component_id AS component_id
"""
with driver.session() as session:
result = session.run(query, component_id=component_id)
......@@ -45,7 +45,7 @@ def ensure_parameter_node(driver: Driver, node_id: str, prefixed_component_id: s
component_id = clean_component_id(prefixed_component_id)
query = """
MERGE (n:Parameter {parameter_id: $node_id, component_id: $component_id, parameter_type: $param_type})
RETURN id(n) AS node_internal_id, n.parameter_id AS id_property, n.component_id AS component_id_property,
RETURN elementId(n) AS node_internal_id, n.parameter_id AS id_property, n.component_id AS component_id_property,
n.parameter_type AS parameter_type_property
"""
with driver.session() as session:
......@@ -71,7 +71,7 @@ def ensure_data_node(driver: Driver, node_id: str, prefixed_component_id: str) -
component_id = clean_component_id(prefixed_component_id)
query = """
MERGE (n:Data {data_id: $node_id, component_id: $component_id})
RETURN id(n) AS node_internal_id, n.data_id AS id_property, n.component_id AS component_id_property
RETURN elementId(n) AS node_internal_id, n.data_id AS id_property, n.component_id AS component_id_property
"""
with driver.session() as session:
result = session.run(query, node_id=node_id, component_id=component_id)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment