Quick question on a cypher query to arrange nodes by centrality

Hi,

I'm working with a graph containing > 50 types of nodes, and want to i) find all nodes of type "Gene" that are connected to node named "cardiomyopathy" of type "Disease", and ii) arrange the nodes found in order of their degree centrality.

For i) I do the following:

MATCH (c:Disease {name:"cardiomyopathy"})-[]-(g:Gene)
RETURN g.name AS Gene

but I can't figure out how to arrange the Gene nodes obtained above by their degree centrality.

For example, the following doesn't work,

MATCH (c:Disease {name: "cardiomyopathy"})-[]-(g:Gene)
WITH g, size((g)--()) as degreeCentrality
RETURN g.name AS Gene, degreeCentrality
ORDER BY degreeCentrality DESC

and throws an error

A pattern expression should only be used in order to test the existence of a pattern. It should therefore only be used in contexts that evaluate to a boolean, e.g. inside the function exists() or in a WHERE-clause. No other uses are allowed, instead they should be replaced by a pattern comprehension. (line 2, column 14 (offset: 69))
"WITH g, size((g)--()) as degreeCentrality"

I'm using neo4j 5.6 within neo4j desktop. I also have the gds and apoc plugins available.

Thanks,
Tarak

Use the new count subquery

MATCH (c:Disease {name: "cardiomyopathy"})-[]-(g:Gene)
WITH g, count{(g)--()} as degreeCentrality
RETURN g.name AS Gene, degreeCentrality
ORDER BY degreeCentrality DESC
2 Likes

Hi @glilienfield, thank you very much! Can you please suggest if the above calculation of degree centrality be done using inbuilt functions or with the graph data science library? I'm interested in more centrality measures, as well as other graph metrics that may be cumbersome to write using basic cypher queries.

For example, what's the easiest way to re-write the cypher query to calculate PageRank for all the relevant nodes?

Thanks,
Tarak