Pierre
Pierre

Reputation: 953

How to find all relationships in cypher

Having a graph where molecules are attached to a common scaffold with [:substructureOf] relationships and where similar molecules are connected to eachothers with a [:isSimilarTo] relationship, is the a way to return all [:isSimilarTo] relationships for a specific subset of molecules?

In pseudo cypher code, considering m as a collection (subset of molecules), I would like to assess that m1 and m2 of each sim relationship is part of m

MATCH (:Scaffold {Name: 'A'}) <-[:usbstructureOf]- (m:Molecule)
WITH m MATCH (m1:Molecule) -[sim:isSimilarTo]- (m2:Molecule) 
WHERE m1 IN m AND m2 IN m

Is there a proper cypher way to write this? Example dataset below.

CREATE (:Scaffold {Name: 'A'});
CREATE (:Scaffold {Name: 'B'});
MATCH (s:Scaffold {Name: 'A'}) MERGE (s) -[:substructureOf]->(:Molecule {Name: 'm1'});
MATCH (s:Scaffold {Name: 'A'}) MERGE (s) -[:substructureOf]->(:Molecule {Name: 'm2'});
MATCH (s:Scaffold {Name: 'A'}) MERGE (s) -[:substructureOf]->(:Molecule {Name: 'm3'});
MATCH (s:Scaffold {Name: 'A'}) MERGE (s) -[:substructureOf]->(:Molecule {Name: 'm4'});
MATCH (s:Scaffold {Name: 'B'}) MERGE (s) -[:substructureOf]->(:Molecule {Name: 'm5'});
MATCH (m:Molecule {Name: 'm1'}), (n:Molecule {Name: 'm2'}) CREATE (m) -[isSimilarTo]-> (n);
MATCH (m:Molecule {Name: 'm1'}), (n:Molecule {Name: 'm3'}) CREATE (m) -[isSimilarTo]-> (n);
MATCH (m:Molecule {Name: 'm2'}), (n:Molecule {Name: 'm3'}) CREATE (m) -[isSimilarTo]-> (n);
MATCH (m:Molecule {Name: 'm3'}), (n:Molecule {Name: 'm4'}) CREATE (m) -[isSimilarTo]-> (n);
MATCH (m:Molecule {Name: 'm4'}), (n:Molecule {Name: 'm5'}) CREATE (m) -[isSimilarTo]-> (n);

Upvotes: 0

Views: 115

Answers (2)

Pierre
Pierre

Reputation: 953

Alternative answer:

MATCH (s:Scaffold {Name: 'A'})-[:substructureOf*]->(m:Molecule) 
WITH collect(m) as mols MATCH p=(:Molecule) -[sim:isSimilarTo]- (:Molecule)    
WHERE startNode(r) IN mols and endNode(r) IN mols
RETURN p

The overall path is shorter.

Upvotes: 0

Martin Preusse
Martin Preusse

Reputation: 9369

I still don't fully understand what you want, a network scheme/drawing might help next time.

But I think what you want is similar molecules sharing the same scaffold. I.e. all Molecule pairs connected by isSimilarTo edges, where both Molecules are linked to a defined Scaffold.

You can get this by matching the complete path:

(Scaffold)--(Molecule)--(similar Molecule)--(same Scaffold)

In Cypher:

MATCH (s:Scaffold {Name: 'A'})<-[:isbstructureOf]-(m1:Molecule)
       -[sim:isSimilarTo]-(m2:Molecule)-[:isbstructureOf]->(s)
// Return the relationships
RETURN DISTINCT m1.name, sim.value, m2.name
// Return count of relationships
RETURN count(DISTINCT sim)

Upvotes: 1

Related Questions