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

Feature/search numeric #152

Merged
merged 4 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ Imports:
tibble,
Matrix,
checkmate,
magrittr
magrittr,
glue
Remotes:
natverse/drvid,
natverse/nat
Encoding: UTF-8
Language: en-GB
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
URL: https://natverse.org/neuprintr, https://github.com/natverse/neuprintr
BugReports: https://github.com/natverse/neuprintr/issues
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export(progress_natprogress)
importFrom(Matrix,sparseMatrix)
importFrom(checkmate,assert_integer)
importFrom(drvid,read.neuron.dvid)
importFrom(glue,glue)
importFrom(httr,parse_url)
importFrom(magrittr,"%>%")
importFrom(memoise,memoise)
Expand Down
45 changes: 39 additions & 6 deletions R/name.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ neuprint_get_neuron_names <- function(bodyids, dataset = NULL, all_segments = TR
#' # or simpler
#' neuprint_get_meta('DA2')
#' }
#' \dontrun{
#' neuprint_get_meta('cropped:false')
#' }
neuprint_get_meta <- function(bodyids, dataset = NULL, all_segments = TRUE,
conn = NULL, chunk=TRUE, progress=FALSE,
possibleFields=NULL, ...){
Expand Down Expand Up @@ -313,12 +316,16 @@ neuprint_search <- function(search, field = "name", fixed=FALSE, exact=NULL,
exact=FALSE
if(isFALSE(fixed) && isFALSE(exact))
warning("Ignoring exact=FALSE as regular expression searches are always exact!")
all_segments.cypher = ifelse(all_segments,"Segment","Neuron")
cypher = sprintf("MATCH (n:`%s`) WHERE n.%s %s \\\"%s\\\" RETURN n.bodyId",
all_segments.cypher,
field,
ifelse(fixed, ifelse(exact, "=", "CONTAINS"), "=~"),
search)
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="="
cypher = glue("
MATCH (n:`{nodetype}`) \\
WHERE n.{field} {operator} {search} \\
RETURN n.bodyId")
nc = neuprint_fetch_custom(cypher=cypher, conn=conn, dataset = dataset, ...)
foundbodyids=unlist(nc$data)
if(meta && isTRUE(length(foundbodyids)>0)){
Expand Down Expand Up @@ -439,3 +446,29 @@ dfFields <- function(field_name) {
}
newnames
}

#' @importFrom glue glue
neuprint_typeof <- function(field, type=c("r", "neo4j"), cache=TRUE,
conn=NULL, dataset=NULL, ...) {
type=match.arg(type)
if(length(field)>1) {
ff=sapply(field, neuprint_typeof, type=type, cache=cache, conn=conn, dataset=dataset, ...)
return(ff)
}
q <- if(type=='r') {"
MATCH (n:Neuron)
WHERE exists(n.`{field}`)
RETURN n.{field} AS {field}
LIMIT 1
" } else {"
MATCH (n:Neuron)
WHERE exists(n.`{field}`)
RETURN apoc.meta.type(n.`{field}`)
LIMIT 1
"}
q=glue(gsub("\\s+", " ", q))
r=try(neuprintr::neuprint_fetch_custom(q, include_headers = FALSE, cache = cache, conn=NULL, dataset=NULL, ...))
if(inherits(r, 'try-error')) NA_character_
else if(type=="r") mode(unlist(r$data, use.names = F))
else unlist(r$data, use.names = F)
}
3 changes: 3 additions & 0 deletions man/neuprint_get_meta.Rd

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

11 changes: 11 additions & 0 deletions tests/testthat/test-name.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ test_that("test name searches ", {
structure(c(NA_character_, ndup), .Names=c(1,iddup)))
})

test_that("test searches on non-string fields", {
expect_equal(neuprint_typeof('bodyId', 'r'), 'numeric')
expect_equal(neuprint_typeof('cropped', 'r'), 'logical')
expect_equal(neuprint_typeof('bodyId', 'neo4j'), 'INTEGER')
expect_equal(neuprint_typeof('cropped', 'neo4j'), 'BOOLEAN')
expect_equal(neuprint_typeof('type', 'neo4j'), 'STRING')

expect_equal(neuprint_ids("bodyId:202916528"), "202916528")
expect_is(neuprint_ids("cropped:false"), 'character')
})

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