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/normalise query fields #38

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
27 changes: 27 additions & 0 deletions R/ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,33 @@ is_key <- function(x, compound=FALSE) {
is.character(x) & grepl("^[a-z0-9]+:[0-9]{5,20}$", x)
}

id_spec_type <- function(x) {
if(is.numeric(x))
return("id")
if(is.character(x)) {
if(length(x) == 1) {
if(grepl("^http[s]{0,1}://", x))
return("url")
else if(isTRUE(substr(x,1,1)=="/"))
return("query")
else if(grepl("^\\s*([a-z:]{3}[0-9,\\s]+)+$", x, perl = T))
return("key")
else if(grepl("^\\s*([0-9,\\s]+)+$", x, perl = T))
return("id")
else
return("query")
}
if(all(grepl("^[a-z0-9]+:[0-9]{5,20}$", x)))
return("key")
if(all(grepl("^[0-9]{5,20}$", x)))
return("id")
}
if(inherits(x, "ngscene"))
return('ngscene')
return(NA_character_)
}


#' Specify ids for fly connectome datasets
#'
#' @param query A query (e.g. cell type name or regular expression)
Expand Down
79 changes: 79 additions & 0 deletions R/normalisation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
normalise_query <- function(x, dataset=NULL) {
if(!isTRUE(id_spec_type(x)=='query'))
return(x)

if(!isTRUE(substr(x,1,1)=="/") || length(x)!=1)
return(x)

# first add default query field if missing
query_field=stringr::str_match(x, '^/([A-z][A-z0-9]*):.+')[,2]
if(is.na(query_field)) {
query_field='type'
x=paste0('/type:', substr(x,2,nchar(x)))
}
query_field2=translate_fields(query_field, dataset = dataset, direction = 'out')
query_expr=stringr::str_match(x, '^[^:]+:(.+)')[,2]
if(is.na(query_expr)) {
warning('unable to parse query expression!')
return(x)
}
x=paste0("/", query_field2, ":", query_expr)
x
}

# this private function; expects a character vector and returns one of same length
# out means from coconatfly to the external data source
# in means from the external data source to coconatfly
translate_fields <- function(x, dataset, direction=c("out", "in")) {
FUN=field_translater(dataset=dataset, direction = direction)
FUN(x)
}

# this is a function generator, the resultant functions are what dplyr::rename
# would like
field_translater <- function(dataset, direction=c("out", "in")) {
dataset=match_datasets(dataset)
direction=match.arg(direction)
field_table <- switch(dataset,
flywire=c(
id='root_id',
type="cell_type",
class="super_class",
subclass="cell_class",
subsubclass="cell_sub_class"),
hemibrain=c(
id='bodyid',
lineage="cellBodyFiber"
),
manc=c(
id='bodyid',
lineage="hemilineage"
),
malecns=c(
id='bodyid',
class="superclass",
subclass="class",
subsubclass="subclass"
),
opticlobe=c(
id='bodyid'
),
character()
)

FUN=function(x) {
if(length(field_table)==0) return(x)
# out means that we are translating coconatfly -> external
# in means that we are translating external -> coconatfly
if(direction=='out') {
y=field_table[match(x, names(field_table))]
y[is.na(y)]=x[is.na(y)]
} else {
y=names(field_table)[match(x, field_table)]
y[is.na(y)]=x[is.na(y)]
}
unname(y)
}
FUN
}

13 changes: 13 additions & 0 deletions tests/testthat/test-normalisation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test_that("field translation works", {
cf=c("subclass", "class", "type", "rhubarb")
fw=c("cell_class", "super_class", "cell_type", "rhubarb")
expect_equal(translate_fields(fw, dataset = 'fly', direction = 'in'), cf)
expect_equal(translate_fields(cf, dataset = 'fly', direction = 'out'), fw)

l=as.list(letters[seq_along(cf)])
names(l)=cf
df=data.frame(l)
expect_type(tofw <- field_translater(dataset = "flywire", direction = "out"), 'closure')
df2=dplyr::rename_with(df, .fn=tofw)
expect_equal(colnames(df2), fw)
})
Loading