Skip to content

Commit

Permalink
Make flywire_basics visibile
Browse files Browse the repository at this point in the history
* Function to get some basic information to describe a flywire neuron
* Also flywire_rewrite function now deletes duplicated entries from FAFB tab of matching sheet
  • Loading branch information
alexanderbates committed Dec 10, 2020
1 parent 7a9a2f6 commit 2d18933
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 38 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export(fafb_set_transmitter)
export(flow_centrality)
export(flycircuit_matching_rewrite)
export(flycircuit_neurons)
export(flywire_basics)
export(flywire_contributions)
export(flywire_failed)
export(flywire_ids)
Expand Down
29 changes: 16 additions & 13 deletions R/flywire_googledrive.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
#' without loading the entire, large \code{neuronlist} into memory. You will need access to the hemibrain Google Team Drive and
#' have it mounted with Google filestream.The function \code{flywire_neurons_update} can be used to update the available data.
#' If you want to flag flywire neurons that should be added to the Google drive, without doing this yourself, you can use
#' \code{flywire_request}.
#' \code{flywire_request}. The \code{flywire_basics} function will calculate some useful meta-data, namely
#' a point in the primary neurite tract that can be used as a stable reference for this neuron.
#'
#' @param brain the brainspace in which hemibrain neurons have been registered. Defaults to raw voxel space for the FlyWire project.
#' @param local \code{FALSE} or path. By default (\code{FALSE}) data is read from \code{options()$Drive_hemibrain_data}), but the user can specify an alternative path.
#' @param mirror logical, whether or not to read neurons that have been mirrored (i.e. flipped to the 'other' brain hemisphere).
#' @param flywire.neurons a \code{neuronlist} of flywire neurons in FlyWire space. The \code{flywire_basics} function will calculate some useful meta-data, namely
#' a point in the primary neurite tract that can be used as a stable reference for this neuron.
#' @param x flywire IDs to update, for the saved Google drive \code{neuronlistfh} objects called with \code{flywire_neurons}.
#' @param request a neuronlist, matrix of x,y,z position or flywire ID to add to a
#' \href{https://docs.google.com/spreadsheets/d/1rzG1MuZYacM-vbW7100aK8HeA-BY6dWAVXQ7TB6E2cQ/edit#gid=0}{Google sheet} that records flywire positions
Expand Down Expand Up @@ -270,15 +273,15 @@ flywire_nblast_update <- function(x = NULL,

}


# hidden
flywire_basics <- function(x, ...){
if(!nat::is.neuronlist(x)){
stop("x must be a neuronlist")
#' @rdname flywire_neurons
#' @export
flywire_basics <- function(flywire.neurons, ...){
if(!nat::is.neuronlist(flywire.neurons)){
stop("flywire.neurons must be a neuronlist")
}

# Get xyz for primary branch points
simp = nat::nlapply(x,nat::simplify_neuron,n=1)
simp = nat::nlapply(flywire.neurons,nat::simplify_neuron,n=1)
branchpoints = sapply(simp, function(y) nat::xyzmatrix(y)[ifelse(length(nat::branchpoints(y)),nat::branchpoints(y),max(nat::endpoints(y))),])
branchpoints = t(branchpoints)
flywire.nm.xyz = apply(branchpoints, 1, paste_coords)
Expand All @@ -293,12 +296,12 @@ flywire_basics <- function(x, ...){
#FAFB.xyz = ""

# Add
x[,"flywire.id"] = names(x)
x[,"flywire.xyz"] = flywire.xyz
x[,"FAFB.xyz"] = FAFB.xyz
x[,"dataset"] = "flywire"
x[,"id"] = NULL
x
flywire.neurons[,"flywire.id"] = names(flywire.neurons)
flywire.neurons[,"flywire.xyz"] = flywire.xyz
flywire.neurons[,"FAFB.xyz"] = FAFB.xyz
flywire.neurons[,"dataset"] = "flywire"
flywire.neurons[,"id"] = NULL
flywire.neurons

}

Expand Down
72 changes: 49 additions & 23 deletions R/flywire_matching.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@
#' @export
flywire_matching_rewrite <- function(flywire.ids = names(flywire_neurons()),
meta = flywire_neurons()[,],
selected_file = options()$hemibrainr_matching_gsheet,
catmaid.update = TRUE,
selected_file = options()$hemibrainr_matching_gsheet, # 1_RXfVRw2nVjzk6yXOKiOr_JKwqk2dGSVG_Xe7_v8roU
...){
# Get the FAFB matching Google sheet
gs = hemibrain_match_sheet(sheet = "FAFB", selected_file = selected_file)
skids = unique(gs$skid)

# Update side information
if(!is.null(meta$side)){
side.indices = match(gs$flywire.xyz,meta$flywire.xyz)
sides = meta$side[side.indices]
gs$side[!is.na(side.indices)] = sides[!is.na(side.indices)]
}

# Get FAFBv14 coordinates
if(length(skids)){
if(length(skids) & catmaid.update){
cats = nat::neuronlist()
batches = split(1:length(skids), round(seq(from = 1, to = 100, length.out = length(skids))))
all.ids = c()
for(i in 1:10){
# Read CATMAID neurons
message("Batch:", i, "/10")
cat = tryCatch(catmaid::read.neurons.catmaid(skids[batches[[i]]], OmitFailures = TRUE), error = function(e) NULL)
search = skids[batches[[i]]]
cat = tryCatch(catmaid::read.neurons.catmaid(search, OmitFailures = TRUE), error = function(e) NULL)
if(!length(cat)){
next
}
Expand All @@ -39,27 +48,20 @@ flywire_matching_rewrite <- function(flywire.ids = names(flywire_neurons()),
flywire.xyz = apply(branchpoints.flywire.raw, 1, paste_coords)

# Add
indices = match(rownames(branchpoints),gs$skid)
flywire.xyz.na = is.na(gs[indices,]$flywire.xyz)
indices.na = indices[flywire.xyz.na]
indices = match(rownames(branchpoints),names(cat))
if(length(indices)){
gs[indices,]$FAFB.xyz = FAFB.xyz
}
if(length(indices.na)){
gs[indices.na,]$flywire.xyz = flywire.xyz
gs[indices.na,]$flywire.id = fw.ids
}
if(!is.null(meta$side)){
side.indices = match(gs$flywire.xyz,meta$flywire.xyz)
sides = meta$side[side.indices]
gs$side[!is.na(side.indices)] = sides[!is.na(side.indices)]
if(length(indices)){
gs[indices,]$flywire.xyz = flywire.xyz
gs[indices,]$flywire.id = fw.ids
}
all.ids=unique(c(all.ids,fw.ids))
}
}

# Update
write.cols = setdiff(colnames(gs),c("FAFB.xyz", "flywire.xyz", "flywire.id", "side"))
if(length(all.ids) & length(write.cols)){
write.cols = intersect(c("FAFB.xyz", "flywire.xyz", "flywire.id", "side"),colnames(gs))
if(length(write.cols)){
for(column in write.cols){
letter = LETTERS[match(column,colnames(gs))]
range = paste0(letter,2,":",letter,nrow(gs)+1)
Expand All @@ -70,17 +72,41 @@ flywire_matching_rewrite <- function(flywire.ids = names(flywire_neurons()),
sheet = "FAFB",
col_names = FALSE)
}
# Add flywire match to the hemibrain and LM sheets
## Read the FAFB Google Sheet
fg = hemibrain_match_sheet(sheet = "FAFB", selected_file = selected_file)
}else{
fg = gs
}

# Add flywire match to the hemibrain and LM sheets
## Read the FAFB Google Sheet
fg = hemibrain_match_sheet(sheet = "FAFB", selected_file = selected_file)
}else{
all.ids = c()
fg = gs
# Figure out duplicate entries
fg$index = 1:nrow(fg)+1
dupes = unique(fg$flywire.id[duplicated(fg$flywire.id)])
dupes = dupes[!is.na(dupes)]
dupes = dupes[!dupes%in%c("NA"," ","")]
for(dupe in dupes){
sub = subset(fg, fg$flywire.id == dupe)
skd = unique(sub$skid)
skd = skd[!is.na(skd)]
skd = skd[!skd%in%c("NA"," ","")]
if(length(skd)>1){
next
}
best = which.max(apply(sub, 1, function(r) sum(is.na(r[c("hemibrain.match", "hemibrain.match.quality",
"LM.match", "LM.match.quality",
"FAFB.hemisphere.match", "FAFB.hemisphere.match.quality")]))))
remove = sub[-best,]
for(r in remove$index){
range.del = googlesheets4::cell_rows(r)
googlesheets4::range_delete(ss = selected_file,
range = range.del,
sheet = "FAFB")
message("Removing a row for: ", dupe)
}
}

# Add missing flywire information
all.ids = unique(gs$flywire.id)
missing = setdiff(flywire.ids, all.ids)
hemibrain_matching_add(ids = missing, meta = meta, dataset="flywire", selected_file = selected_file, ...)

Expand Down
1 change: 1 addition & 0 deletions R/hemibrain_matching.R
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,7 @@ lm_matches <- function(priority = c("hemibrain","lm"), selected_file = options()
#' @param flycircuit.ids flycircuit IDs to add to Google sheet if not already present.
#' @param meta meta data for the given flycircuit IDs.
#' @param top.nblast logical. Whether or not to also give the top NBLAST match for each entry.
#' @param catmaid.update logical. Whether or not to update \code{flywire.xyz} and \code{flywire.id} columns, based on e CATMAID neuron specified by a \code{skid} column.
#'
#' @param ... arguments passed to methods for, for example, \code{neuprintr::neuprint_get_meta} and \code{elmr::fafb_get_meta}.
#'
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ reference:
- 'flywire_neurons'
- 'flywire_googledrive_data'
- 'flywire_ids_update'
- '`flywire_basics`'
- title: Access light-level google drive data
desc: ~
contents:
Expand Down
2 changes: 1 addition & 1 deletion data-raw/information.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ usethis::use_data(hemibrain_glomeruli_summary, overwrite = TRUE)
### Supplementary data for Schlegel and Bates 2021:
supp.cols = c("bodyid", "pre", "post", "upstream", "downstream",
"status", "name", "voxels", "soma", "side",
"connectivity.type", "cell.type", "anatomy.group", "class", "cellBodyFiber",
"connectivity.type", "cell.type", "group","anatomy.group", "class", "cellBodyFiber",
# "ItoLee_Lineage", "ItoLee_Hemilineage", "Hartenstein_Lineage", "Hartenstein_Hemilineage",
"putative.classic.transmitter", "putative.other.transmitter", "glomerulus", "presyn.glom",
"FAFB.match", "FAFB.match.quality", "layer", "ct.layer",
Expand Down
9 changes: 8 additions & 1 deletion man/flywire_neurons.Rd

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

3 changes: 3 additions & 0 deletions man/hemibrain_add_made_matches.Rd

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

0 comments on commit 2d18933

Please sign in to comment.