简体   繁体   English

neo4j中的双向递归

[英]Bidirectional recursion in neo4j

Cannot find answer online for this.无法在线找到答案。 I want to do a recursive query upstream and downstream on protein interaction map. If user enters a protein (protein 'C') and depth N=2, I want to return 2 upstream and 2 downstream proteins in the interaction map and the regulation.我想对蛋白质相互作用 map 进行上游和下游递归查询。如果用户输入蛋白质(蛋白质“C”)并且深度 N=2,我想在相互作用 map 和调节中返回 2 个上游和 2 个下游蛋白质。 However if its upstream then protein 'b' on right side of MATCH needs to come first in the return table and if its downstream direction then protein 'a' on left side of match needs to come first in return table.但是,如果它是上游,则 MATCH 右侧的蛋白质“b”需要在返回表中排在第一位,如果它是下游方向,则匹配左侧的蛋白质“a”需要在返回表中排在第一位。 How can I do this?我怎样才能做到这一点?

For instance this is the bidirection but half of the rows are the wrong order in columns 1 and 3.例如,这是双向的,但一半的行在第 1 列和第 3 列中的顺序是错误的。

MATCH p = (a:Protein { name:'C' })<-[:REGULATES*1..2]->(b:Protein) 
WITH *, relationships(p) as r
RETURN nodes(p)[length(p)-1].name AS Protein1, r[length(p)-1] as Regulates, b.name AS Protein2

I can only get what I want with two calls and switching order or RETURN columns.我只能通过两次调用和切换顺序或 RETURN 列来获得我想要的东西。

MATCH p = (a:Protein { name:'C' })-[:REGULATES*1..2]->(b:Protein) 
WITH *, relationships(p) as r
RETURN nodes(p)[length(p)-1].name AS Protein1, r[length(p)-1] as Regulates, length(p), b.name AS Protein2

MATCH p = (a:Protein { name:'C' })<-[:REGULATES*1..2]-(b:Protein) 
WITH *, relationships(p) as r
RETURN b.name AS Protein1, r[length(p)-1] as Regulates, nodes(p)[length(p)-1].name AS Protein2

Figured it out using functions startNode and endNode .使用函数startNodeendNode 弄清楚了 The last() and head() functions are also handy. last() 和 head() 函数也很方便。

MATCH p = (n:Protein { name:'C' })<-[:REGULATES*1..3]->(b:Protein) 
WITH *, relationships(p) as rs
RETURN startNode(last(rs)).name as Protein1, last(rs).direction as Regulates, endNode(last(rs)).name as Protein2, length(p)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM