user2167741
user2167741

Reputation: 299

Creating a subgraph using Cypher projection

I am trying to create a subgraph of my graph using Cypher projection because I want to use the GDS library. First, I am creating a subgraph using Cypher query which works perfectly fine. Here is the query:

// 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 is a Cypher projection by sending the subgraph nodes and subgraph rels as parameters in the GDS create query and this gives me a null pointer exception:

// All the above lines 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 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

What could be wrong? Thanks.

Upvotes: 1

Views: 487

Answers (1)

Tomaž Bratanič
Tomaž Bratanič

Reputation: 6534

To access start and end node of a relationship, there is a slightly different syntax that you are using:

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

This is what I noticed, hopefully this is the only error.

Upvotes: 1

Related Questions