Skip to content

Commit 5cd7bb1

Browse files
committed
create_path_return_clause: id_lists option
Refs greenelab/connectivity-search-backend#31 (comment) Also switchs to neo4j cypher list comprehension
1 parent 0568e3f commit 5cd7bb1

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

hetio/neo4j.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -289,27 +289,34 @@ def construct_unique_nodes_clause(metarels, unique_nodes):
289289

290290
def create_path_return_clause(path_style='list', return_property='name'):
291291
"""
292-
Create a Cypher query clause to return paths either as a list or as a string.
292+
Create a Cypher query clause to return paths. If path_style is 'list' or 'string',
293+
then the return_property is extracted for each node along the path. If path_style
294+
is 'id_lists', then two lists (node_ids and rel_ids) are returned of neo4j databse ids.
293295
As formatting the output as a string is less efficient in terms of database
294-
hits, 'list' is the default style
296+
hits, 'list' is the default style.
295297
296298
Parameters
297299
----------
298300
path_style : str
299301
the way the user wants the path returned. Currently supported options
300-
are 'list' and 'string'
302+
are 'list', 'string', and 'id_lists'
301303
return_property : str
302304
which node property to use to describe the path
303305
"""
304306
if path_style == 'string':
305307
return "substring(reduce(s = '', node IN nodes(path)| s + '–' + node.{property}), 1) AS path,".format(property=return_property)
306308
elif path_style == 'list':
307-
return "extract(n in nodes(path) | n.{property}) AS path,".format(property=return_property)
309+
return "[node in nodes(path) | node.{property}] AS path,".format(property=return_property)
310+
elif path_style == 'id_lists':
311+
return (
312+
'[node IN nodes(path) | id(node)] AS node_ids,\n'
313+
'[rel IN relationships(path) | id(rel)] AS rel_ids,'
314+
)
308315
else:
309-
err_string = ("{style} is not a style currently implemented by "
310-
"create_path_return_clause. Valid styles are "
311-
"'list' and 'string'").format(style=path_style)
312-
316+
err_string = (
317+
"{style} is not a style currently implemented by create_path_return_clause. "
318+
"Valid styles are 'list', 'string', and 'id_lists'."
319+
).format(style=path_style)
313320
raise ValueError(err_string)
314321

315322
def construct_dwpc_query(metarels, property='name', join_hint='midpoint', index_hint=False, unique_nodes=True):

0 commit comments

Comments
 (0)