Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graph explore #48

Merged
merged 15 commits into from
Nov 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
updates to cypher_query and cypher_query_df to support multiple queri…
…es in a batch
oneilsh committed Sep 5, 2024
commit b446b33ae6030de50339102c6a1c4943e83dd166
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -54,6 +54,7 @@ importFrom(kableExtra,column_spec)
importFrom(kableExtra,kable)
importFrom(kableExtra,kable_styling)
importFrom(neo2R,cypher)
importFrom(neo2R,multicypher)
importFrom(purrr,map_chr)
importFrom(readr,col_character)
importFrom(readr,cols)
@@ -66,6 +67,7 @@ importFrom(stringr,str_wrap)
importFrom(tibble,tibble)
importFrom(tidygraph,activate)
importFrom(tidygraph,as_tibble)
importFrom(tidygraph,graph_join)
importFrom(tidygraph,tbl_graph)
importFrom(utils,capture.output)
importFrom(utils,download.file)
5 changes: 2 additions & 3 deletions R/cypher_query.R
Original file line number Diff line number Diff line change
@@ -3,9 +3,8 @@
#' This function takes a Cypher query and parameters, executes the query using the given engine, and returns the result as a tbl_kgx graph.
#'
#' @param engine A neo4j KG engine
#' @param query A string representing the Cypher query.
#' @param query A string representing the Cypher query. Multiple queries may be passed as a vector; if so, Neo2R::multicypher if used and the result is returned as a single joined graph.
#' @param parameters A list of parameters for the Cypher query. Default is an empty list.
#' @param queries A vector of queries as used in Neo2R::multicypher. If provided, multicypher is used, query is ignored, and the result is returned as a single joined graph.
#' @param ... Additional arguments passed to the function.
#' @return The result of the Cypher query as a tbl_kgx graph.
#' @export
@@ -19,7 +18,7 @@
#' result <- cypher_query(engine, query, parameters)
#' print(result)
#' @importFrom neo2R cypher
cypher_query <- function(engine, query, parameters = NULL, queries = NULL, ...) {
cypher_query <- function(engine, query, parameters = NULL, ...) {
UseMethod("cypher_query")
}

15 changes: 10 additions & 5 deletions R/cypher_query.neo4j_engine.R
Original file line number Diff line number Diff line change
@@ -29,8 +29,12 @@ stitch_vectors <- function(x) {

#' Process neo2R cypher to tbl_kgx
#'
#' Given a result from neo2R::cypher returning KGX-formatted nodes and edges
#' (or )
#' Given a result from neo2R::cypher returning KGX-formatted nodes and edges,
#' parse the result to generate a tbl_kgx object, attaching the provided engine.
#'
#' @param res The result from neo2R::cypher with result = "graph"
#' @param engine The engine to attach to the returned graph
#' @return A tbl_kgx
neo2r_to_kgx <- function(res, engine) {
relationship_ids_contained <- as.integer(unlist(res$paths))

@@ -124,14 +128,15 @@ neo2r_to_kgx <- function(res, engine) {

#' @export
#' @importFrom neo2R cypher
#' @importFrom neo2R multicypher
#' @importFrom tibble tibble
#' @importFrom tidygraph graph_join
cypher_query.neo4j_engine <- function(engine, query, parameters = NULL, queries = NULL, ...) { #
if(is.null(queries)) {
cypher_query.neo4j_engine <- function(engine, query, parameters = NULL, ...) { #
if(length(query) == 1) {
res <- neo2R::cypher(engine$graph_conn, query = query, parameters = parameters, result = "graph")
return(neo2r_to_kgx(res, engine = engine))
} else {
res <- neo2R::multicypher(engine$graph_conn, queries = queries, parameters = parameters, result = "graph")
res <- neo2R::multicypher(engine$graph_conn, queries = query, parameters = parameters, result = "graph")
graphs <- lapply(res, neo2r_to_kgx, engine = engine)
g <- tbl_kgx(nodes = data.frame())
for(g2 in graphs) {
8 changes: 4 additions & 4 deletions R/cypher_query_df.R
Original file line number Diff line number Diff line change
@@ -3,10 +3,10 @@
#' This function takes a Cypher query and parameters, executes the query using the given engine, and returns the result as a data frame.
#'
#' @param engine A neo4j_engine() or derivative providing access to a Neo4j database.
#' @param query A string representing the Cypher query, which should return a table.
#' @param parameters A list of parameters for the Cypher query. Default is an empty list.
#' @param query A string representing the Cypher query, which should return a table. Multiple queries may be passed as a vector; if so, Neo2R::multicypher if used and the result is returned as a list of data frames.
#' @param parameters A list of parameters for the Cypher query, if required.
#' @param ... Additional arguments passed to the function.
#' @return The result of the Cypher query as a data frame.
#' @return The result of the Cypher query as a data frame, or a list of data frames if multiple queries are passed.
#' @export
#' @examplesIf monarch_engine_check()
#' engine <- monarch_engine()
@@ -17,6 +17,6 @@
#' result <- cypher_query_df(engine, query, parameters)
#' print(result)
#' @importFrom neo2R cypher
cypher_query_df <- function(engine, query, parameters = list(), ...) {
cypher_query_df <- function(engine, query, parameters = NULL, ...) {
UseMethod("cypher_query_df")
}
9 changes: 7 additions & 2 deletions R/cypher_query_df.neo4j_engine.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#' @export
#' @importFrom neo2R cypher
cypher_query_df.neo4j_engine <- function(engine, query, parameters = list(), ...) {
#' @importFrom neo2R multicypher
cypher_query_df.neo4j_engine <- function(engine, query, parameters = NULL, ...) {
if(length(query) == 1) {
result <- neo2R::cypher(engine$graph_conn, query = query, parameters = parameters, result = "row", arraysAsStrings = FALSE)
} else {
result <- neo2R::multicypher(engine$graph_conn, queries = query, parameters = parameters, result = "row", arraysAsStrings = FALSE)
}

return(result)
return(result)
}
2 changes: 1 addition & 1 deletion man/cypher_query.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/cypher_query_df.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/neo2r_to_kgx.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-cypher_query.R
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ test_that("cypher_query works with multicypher queries", {
queries <- c("MATCH (n {id: 'MONDO:0007947'}) RETURN n",
"MATCH (n {id: 'MONDO:0017309'}) RETURN n",
"MATCH (n {id: 'MONDO:0020066'}) RETURN n")
g <- cypher_query(e, queries = queries)
g <- cypher_query(e, query = queries)
expect_s3_class(g, "tbl_kgx")
nodes_df <- data.frame(tidygraph::activate(g, nodes))
edges_df <- data.frame(tidygraph::activate(g, edges))