Skip to content

Commit

Permalink
implement where searches for ids/meta
Browse files Browse the repository at this point in the history
* new syntax might need thinking about
  • Loading branch information
jefferis committed Jan 10, 2022
1 parent cfacabb commit 2646f0a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
44 changes: 35 additions & 9 deletions R/name.R
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ neuprint_get_roiInfo <- function(bodyids, dataset = NULL, all_segments = FALSE,
#'
#' # starts with MBON
#' neuprint_search("type:MBON.*", meta=FALSE)
#'
#' # full access to WHERE cypher queries over nodes (i.e. neurons) in neo4j
#' # NB fields must be prefixed with n. to indicate that they are node properties.
#' # note also that exists(n.somaLocation) is cypher to ensure soma==TRUE
#' neuprint_search("where:exists(n.somaLocation) AND n.post>30000 AND NOT n.cropped")
#' }
#'
#' \dontrun{
Expand Down Expand Up @@ -317,14 +322,22 @@ neuprint_search <- function(search, field = "name", fixed=FALSE, exact=NULL,
if(isFALSE(fixed) && isFALSE(exact))
warning("Ignoring exact=FALSE as regular expression searches are always exact!")
nodetype = ifelse(all_segments,'Segment','Neuron')
fieldtype=neuprint_typeof(field, type = 'neo4j')
if(fieldtype=="STRING") {
search=glue('\\"{search}\\"')
operator=ifelse(fixed, ifelse(exact, "=", "CONTAINS"), "=~")
} else operator="="

if(isTRUE(tolower(field)=='where')) {
# this is a raw CYPHER query
where=search
} else {
fieldtype=neuprint_typeof(field, type = 'neo4j')
if(fieldtype=="STRING") {
search=glue('\\"{search}\\"')
operator=ifelse(fixed, ifelse(exact, "=", "CONTAINS"), "=~")
} else operator="="
where=glue("n.{field} {operator} {search}")
}

cypher = glue("
MATCH (n:`{nodetype}`) \\
WHERE n.{field} {operator} {search} \\
WHERE {where} \\
RETURN n.bodyId")
nc = neuprint_fetch_custom(cypher=cypher, conn=conn, dataset = dataset, ...)
foundbodyids=unlist(nc$data)
Expand All @@ -351,9 +364,9 @@ neuprint_search <- function(search, field = "name", fixed=FALSE, exact=NULL,
#' @return For \code{neuprint_ids}, a character vector of bodyids (of length 0
#' when there are none and \code{mustWork=FALSE}).
#' @export
#' @seealso \code{\link[neuprintr]{neuprint_search}}
#' @section Query syntax: It is probably best just to look at the examples, but
#' the query syntax is as follows where square brackets denote optional parts:
#' @section Standard query syntax: It is probably best just to look at the
#' examples, but the query syntax is as follows where square brackets denote
#' optional parts:
#'
#' \code{[!/][<field>:]<query>}
#'
Expand All @@ -367,6 +380,19 @@ neuprint_search <- function(search, field = "name", fixed=FALSE, exact=NULL,
#' Finally the query itself is a plain text (fixed) or regular expression
#' query.
#'
#'
#' @section Extended query syntax: As a stepping stone to writing full CYPHER
#' queries against Neo4J you can used the special \code{where} keyword to
#' introduce your queries:
#'
#' \code{where:<cypher query>}
#'
#' e.g.
#'
#' \code{"where:exists(n.somaLocation) AND n.post>30000 AND NOT n.cropped"}
#'
#' Note that properties of individual nodes (i.e. neurons) must be prefixed
#' with \code{n.} as would be typical in a CYPHER query.
#' @examples
#' \donttest{
#' # exact match against whole type
Expand Down
29 changes: 24 additions & 5 deletions man/neuprint_search.Rd

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

4 changes: 4 additions & 0 deletions tests/testthat/test-name.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ test_that("test searches on non-string fields", {
expect_is(neuprint_ids("cropped:false"), 'character')
})

test_that("test where searches", {
expect_is(neuprint_ids("where:exists(n.somaLocation) AND n.post>10000 AND NOT n.cropped"), "character")
})

test_that("test bad dataset specification ", {
expect_error(neuprint_search(".*DA2.*", dataset = 'hemibrain1'))
})

0 comments on commit 2646f0a

Please sign in to comment.