clanofsol
clanofsol

Reputation: 93

Cypher query with consecutive matches returning unwanted relationships

I have a Neo4j database about drugs. I need to run a query in Cypher with a somewhat complex pattern, so I have to use multiple match statements. I want to merge a couple of relationships, but end up obtaining unwanted relationships.

I reviewed some similar questions, but nobody else seemed to have this problem. I also had to use a similar query with a slightly simpler pattern that didn't suffer from this problem. My code is the following:

MATCH (x:Drug)-[:BLOCKS|:INHIBITS]->(z:Protein)-[:FACILITATES]->(process:Process)-[:IMPAIRS]->(effect:Effect)<-[:HAS_EFFECT]-(y:Drug)
WHERE x <> y
MATCH (x)<-[:HAS_PARTICIPANT]-(interaction:Interaction)-[:HAS_PARTICIPANT]->(y)
MATCH (interaction)-[:HAS_EFFECT]->(anotherEffect:Effect)

MERGE (x)-[r1:DECREASES]->(process)-[r2:IS_DECREASED_BY]->(x)
SET r1.checked = true, r2.checked = true
MERGE (x)-[r3:INCREASES]->(effect)-[r4:IS_INCREASED_BY]->(x)
SET r3.checked = true, r4.checked = true

FOREACH (ignoreMe in CASE WHEN 'TherapeuticEffect' IN LABELS(effect) THEN [1] ELSE [] END |
[...]

That is what I look forward to get (only x related to the remaining nodes, effect to the left and process to the right):

Right graph

What I'm actually getting (both x and y related to the other nodes):

Wrong graph

What is wrong with my query and how can I solve it?

Upvotes: 0

Views: 305

Answers (1)

František Hartman
František Hartman

Reputation: 15086

It very much looks like your query matches in both directions. Hard to tell without having the data.

What you can try is to debug the MATCH part first and see if it returns what you expect:

MATCH (x:Drug)-[:BLOCKS|:INHIBITS]->(z:Protein)-[:FACILITATES]->(process:Process)-[:IMPAIRS]->(effect:Effect)<-[:HAS_EFFECT]-(y:Drug)
WHERE x <> y
MATCH (x)<-[:HAS_PARTICIPANT]-(interaction:Interaction)-[:HAS_PARTICIPANT]->(y)
MATCH (interaction)-[:HAS_EFFECT]->(anotherEffect:Effect)
RETURN x.preferredLabel,y.preferredLabel // or others like return count(*)
``

Upvotes: 1

Related Questions