Skip to content

Commit

Permalink
Updated functions to produce neuroglancer links for tracing workflows…
Browse files Browse the repository at this point in the history
… from seatable

* added hidden function crantr:::crant_table_update_tracing to add ngl_link to rows with 'TRACING_ISSUE' as a status
* fixed crant_scene by pointing it to the right layer for CRANT root IDs
* helper function crant_ngl_segments to get CRANT root IDs from CRANT scenes
  • Loading branch information
alexanderbates committed Oct 27, 2024
1 parent dc96eb2 commit 1f5b590
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 256 deletions.
50 changes: 50 additions & 0 deletions R/ant-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,54 @@ crant_table_annotate <- function(root_ids,
invisible()
}

# crant table update tracing
# hidden
crant_table_update_tracing <- function(table = "CRANTb_meta",
base = "CRANTb"){

# Get current table
cat('reading CRANT meta seatable...\n')
ac <- crant_table_query(sql = sprintf('select _id, root_id, status, ngl_link from %s',table),
base = base) %>%
dplyr::select(!!rlang::sym("root_id"),
!!rlang::sym("status"),
!!rlang::sym("ngl_link"),
!!rlang::sym("_id"))
ac[ac=="0"] <- NA
ac[ac==""] <- NA

# Only add ngl_link for a certain set of statuses
cat('generating neuroglancer links ...\n')
n_rows <- nrow(ac %>% dplyr::filter(grepl("TRACING_ISSUE$|PROOFREADING_ISSUE$|PARTIALLY_PROOFREAD$", status)))
p <- dplyr::progress_estimated(n_rows)
ac.new <- ac %>%
dplyr::filter(grepl("TRACING_ISSUE$|PROOFREADING_ISSUE$|PARTIALLY_PROOFREAD$", !!rlang::sym("status"))) %>%
dplyr::rowwise() %>%
dplyr::mutate(
ngl_id = {
p$tick()$print() # Update progress bar
tryCatch(paste(crant_ngl_segments(!!rlang::sym("ngl_link")),collapse="_"), error = function(e) NA)
}
) %>%
dplyr::mutate(ngl_link = dplyr::case_when(
is.na(!!rlang::sym("ngl_link")) ~ crant_scene(!!rlang::sym("root_id"), shorten_url = TRUE),
!is.na(!!rlang::sym("ngl_id")) & !grepl(!!rlang::sym("root_id"),!!rlang::sym("ngl_id")) ~ crant_scene(!!rlang::sym("root_id"), shorten_url = TRUE),
TRUE ~ ngl_link
)) %>%
dplyr::ungroup() %>%
dplyr::select(-!!rlang::sym("ngl_id")) %>%
as.data.frame()

# Update
cat('updating CRANTb_meta seatable...\n')
ac.new[is.na(ac.new)] <- ''
crant_table_update_rows(df = ac.new,
base = "CRANTb",
table = "CRANTb_meta",
append_allowed = FALSE,
chunksize = 1000)
cat('done.')

# Return
invisible()
}
22 changes: 14 additions & 8 deletions R/crant.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' but could point to another dataset layer.
#' @param open Whether to open the URL in your browser (see
#' \code{\link{browseURL}})
#' @param shorten_url logical, whether or not to produce a shortened URL.
#' @return A character vector containing a single Neuroglancer URL (invisibly
#' when \code{open=TRUE}).
#' @export
Expand All @@ -18,16 +19,17 @@
#' }
crant_scene <- function(ids=NULL,
open=FALSE,
layer = NULL,
shorten_url = FALSE,
layer = "proofreadable seg",
url=paste0("https://spelunker.cave-explorer.org/#!middleauth+",
"https://global.daf-apis.com/nglstate/api/v1/",
"5733498854834176")) {
url=sub("#!middleauth+", "?", url, fixed = T)
parts=unlist(strsplit(url, "?", fixed = T))
json=try(fafbseg::flywire_fetch(parts[2], token=fafbseg::chunkedgraph_token(), return = 'text', cache = TRUE)) # token = crant_token()
url <- sub("#!middleauth+", "?", url, fixed = T)
parts <- unlist(strsplit(url, "?", fixed = T))
json <- try(fafbseg::flywire_fetch(parts[2], token=fafbseg::chunkedgraph_token(), return = 'text', cache = TRUE)) # NOT token = crant_token()
if(inherits(json, 'try-error')) {
badtoken=paste0("You have a token but it doesn't seem to be authorised for CAVE or global.daf-apis.com.\n",
"Have you definitely used `crant_set_token()` to make a token for the CAVE datasets?")
"Have you definitely used `flywire_set_token()` to make a token for the CAVE datasets?")
if(grepl(500, json))
stop("There seems to be a (temporary?) problem with the zetta server!")
else if(grepl(401, json))
Expand All @@ -39,14 +41,18 @@ crant_scene <- function(ids=NULL,
else
stop(badtoken)
}
u=fafbseg::ngl_encode_url(json, baseurl = parts[1])
u <- fafbseg::ngl_encode_url(json, baseurl = parts[1])
if(!is.null(ids)){
banc_ngl_segments(u, layer=layer) <- crant_ids(ids)
crant_ngl_segments(u, layer=layer) <- crant_ids(ids)
}
if(open) {
browseURL(u)
invisible(u)
} else (u)
} else if(shorten_url){
crant_shorturl(u)
}else{
u
}
}

#' Set the token to be used to authenticate to CRANT autosegmentation resources
Expand Down
Loading

0 comments on commit 1f5b590

Please sign in to comment.