cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

Creating a subgraph using Cypher projection

kasthuri
Node Clone

I am trying to create a subgraph of my graph using Cypher projection. First, I am creating a subgraph using Cypher query which works perfectly fine. The query is this:

// Filter for only recurrent events
WITH [path=(m:IDHcodel)--(n:Tissue)
WHERE (m.node_category = 'molecular' AND n.event_class = 'Recurrence')
	AND NOT EXISTS((m)--(:Tissue{event_class:'Primary'})) | m] AS recur_events

// Obtain the sub-network with 2 or more patients in edges
MATCH p=(m1)-[r:hasIDHcodelPatients]->(m2) 
WHERE (m1 IN recur_events AND m2 IN recur_events AND r.total_common_patients >= 2)
WITH COLLECT(p) AS all_paths
WITH [p IN all_paths | nodes(p)] AS path_nodes, [p IN all_paths | relationships(p)]  AS path_rels
RETURN apoc.coll.toSet(apoc.coll.flatten(path_nodes)) AS subgraph_nodes, apoc.coll.flatten(path_rels) AS subgraph_rels

So far so good. Now all I am trying to do a Cypher projection by sending the subgraph nodes and subgraph rels as parameters and this gives me null pointer exception:

// Filter for only recurrent events
WITH [path=(m:IDHcodel)--(n:Tissue)
WHERE (m.node_category = 'molecular' AND n.event_class = 'Recurrence')
	AND NOT EXISTS((m)--(:Tissue{event_class:'Primary'})) | m] AS recur_events

// Obtain the sub-network with 2 or more patients in edges
MATCH p=(m1)-[r:hasIDHcodelPatients]->(m2) 
WHERE (m1 IN recur_events AND m2 IN recur_events AND r.total_common_patients >= 2)
WITH COLLECT(p) AS all_paths
WITH [p IN all_paths | nodes(p)] AS path_nodes, [p IN all_paths | relationships(p)]  AS path_rels
WITH apoc.coll.toSet(apoc.coll.flatten(path_nodes)) AS subgraph_nodes, apoc.coll.flatten(path_rels) AS subgraph_rels

// Call gds library to create a graph by sending subgraph_nodes and subgraph_rels as parameters
CALL gds.graph.create.cypher(
    'example',
    'MATCH (n) where n in $sn RETURN id(n) as id',
    'MATCH ()-[r]-() where r in $sr RETURN r.start as source , r.end as target',
	{parameters: {sn: subgraph_nodes, sr: subgraph_rels} } 
) YIELD graphName AS graph, nodeQuery, nodeCount AS nodes, relationshipQuery, relationshipCount AS rels

RETURN graph

Any help?

Thanks!

1 REPLY 1

kasthuri
Node Clone

Ok, I got the solution from Stack Overflow. It's a different syntax for obtaining the source and target nodes in the gds.graph.create. Here is a solution and it works.

// All the lines in obtaining subgraph except using WITH instead of RETRUN in the last line. ie.,

...

WITH apoc.coll.toSet(apoc.coll.flatten(path_nodes)) AS subgraph_nodes, apoc.coll.flatten(path_rels) AS subgraph_rels

// Call gds library to create a graph by sending subgraph_nodes and subgraph_rels as parameters
CALL gds.graph.create.cypher(
    'example',
    'MATCH (n) where n in $sn RETURN id(n) as id',
    'MATCH ()-[r]-() where r in $sr RETURN id(startNode(r)) as source , id(endNode(r)) as target',
    {parameters: {sn: subgraph_nodes, sr: subgraph_rels} } 
) YIELD graphName AS graph, nodeQuery, nodeCount AS nodes, relationshipQuery, relationshipCount AS rels

RETURN graph