Skip to content

Commit

Permalink
Various bug fixes
Browse files Browse the repository at this point in the history
* direct flywire_neurons() correctly
* Add function to add flycircuit IDs to matching sheet
* Updated matching sheet helper functions
* Updated dn.ids
* Added hemibrain image to README
  • Loading branch information
alexanderbates committed Nov 20, 2020
1 parent ffeaa2b commit 0de66c2
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
^data-raw$
^hemibrain_data$
^Meta$
^hemibrainr\.pptx$
^\\~\\$hemibrainr\.pptx$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ data-raw/annotations/
vignettes/future/*
data-raw/metrics/
doc/
hemibrainr.pptx
~$hemibrainr.pptx
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export(fafb_matching_rewrite)
export(fafb_set_hemilineage)
export(fafb_set_transmitter)
export(flow_centrality)
export(flycircuit_matching_rewrite)
export(flycircuit_neurons)
export(flywire_contributions)
export(flywire_ids)
Expand Down
4 changes: 2 additions & 2 deletions R/flywire_googledrive.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ flywire_neurons <- function(local = FALSE,

# Get Google drive folder
savedir = good_savedir(local = local)
neuronsdir = paste0(savedir,"flywire_neurons/")
fhdir = paste0(neuronsdir,brain,"/")
neuronsdir = file.path(savedir,"flywire_neurons/")
fhdir = file.path(neuronsdir,brain,"/")

# Read
filelist = list.files(path = fhdir, pattern = ".rds", full.names = TRUE)
Expand Down
106 changes: 89 additions & 17 deletions R/hemibrain_matching.R
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,10 @@ fafb_matching <- function(ids = NULL,
#'
#' @param priority whether to use FAFB->hemibrain matches (FAFB) or hemibrain->FAFB matches (hemibrain) in order to ascribe
#' cell type names to FAFB neurons. In both cases, cell type names are attached to hemibrain bodyids, and propagated to their FAFB matches.
#' @param selected_file the Google Sheet database to read and write from. For
#' now, defaults to this
#' \href{https://docs.google.com/spreadsheets/d/1OSlDtnR3B1LiB5cwI5x5Ql6LkZd8JOS5bBr-HTi0pOw/edit#gid=0}{Google
#' Sheet}.
#'
#' @return a \code{data.frame} which includes neuron's ID (either its CATMAID skeleton ID or neuprint body ID), the data set from which it comes,
#' its putative cell type and connectivity type, and its match in the other dataset.
Expand All @@ -944,11 +948,11 @@ fafb_matching <- function(ids = NULL,
#' @export
#' @seealso \code{\link{hemibrain_matching}}
#' @importFrom stats ave
hemibrain_matches <- function(priority = c("FAFB","hemibrain")){
hemibrain_matches <- function(priority = c("FAFB","hemibrain"),
selected_file = options()$hemibrainr_matching_gsheet){
priority = match.arg(priority)

# Get matches
selected_file = options()$hemibrainr_matching_gsheet
hemibrain.matches = gsheet_manipulation(FUN = googlesheets4::read_sheet,
ss = selected_file,
sheet = "hemibrain",
Expand Down Expand Up @@ -1239,6 +1243,8 @@ lm_matches <- function(priority = c("hemibrain","lm")){
#' assigned a number of neurons to match up. In order to add yourself as a user,
#' simply open this Google Sheet in your browser and add your initials to neurons of your choosing on the rightmost column 'Users'.
#' @param flywire.ids flywire IDs to add to Google sheet if not already present.
#' @param flycircuit.ids flycircuit IDs to add to Google sheet if not already present.
#' @param meta meta data for the given flycircuit IDs.
#'
#' @param ... arguments passed to methods for, for example, \code{neuprintr::neuprint_get_meta} and \code{elmr::fafb_get_meta}.
#'
Expand All @@ -1256,7 +1262,7 @@ lm_matches <- function(priority = c("hemibrain","lm")){
#'
#'
#' }}
#' @seealso \code{\link{hemibrain_matching}}
#' @seealso \code{\link{hemibrain_matching}}, \code{\link{fafb_matching}}
#' @rdname hemibrain_add_made_matches
hemibrain_add_made_matches <- function(df,
direction = c("both","hemibrain-FAFB","FAFB-hemibrain"),
Expand Down Expand Up @@ -1346,7 +1352,7 @@ hemibrain_add_made_matches <- function(df,

# Get correct GSheet
hemibrain_match_sheet <- function(selected_file = options()$hemibrainr_matching_gsheet,
sheet = c("hemibrain","FAFB","CATMAID","flywire")){
sheet = c("hemibrain","FAFB","CATMAID","flywire","lm")){
# Which sheet
sheet = match.arg(sheet)
sheet[sheet=="hemibrain"] = "hemibrain"
Expand All @@ -1357,21 +1363,26 @@ hemibrain_match_sheet <- function(selected_file = options()$hemibrainr_matching_
id.field = "flywire.id"
sheet = "FAFB"
}else if (sheet=="CATMAID"){
id.field = "skid"
sheet = "FAFB"
}else{
}else if (sheet=="FAFB"){
id.field = "skid"
}else{
id.field = "id"
}
# Read sheet
gs = gsheet_manipulation(FUN = googlesheets4::read_sheet,
ss = selected_file,
sheet = sheet,
return = TRUE)
gs[[id.field]] = correct_id(gs[[id.field]])
ids = gs[[id.field]]
ids[is.na(ids)] = paste0("missing_",1:sum(is.na(ids)))
ids = paste0(ids,"#",ave(ids,ids,FUN= seq.int))
ids = gsub("#1$","",ids)
rownames(gs) = ids
if(nrow(gs)){
gs[[id.field]] = correct_id(gs[[id.field]])
ids = gs[[id.field]]
ids[is.na(ids)] = paste0("missing_",1:sum(is.na(ids)))
ids = paste0(ids,"#",ave(ids,ids,FUN= seq.int))
ids = gsub("#1$","",ids)
rownames(gs) = ids
}
gs
}

Expand Down Expand Up @@ -1581,7 +1592,7 @@ update_gsheet <- function(update,
gs,
selected_file = options()$hemibrainr_matching_gsheet,
tab,
match = c("hemibrain", "LM", "FAFB", "flywire"),
match = c("hemibrain", "lm", "FAFB", "flywire"),
id){
match = match.arg(match)
if(match=="flywire"){
Expand Down Expand Up @@ -1620,14 +1631,15 @@ fafb_matching_rewrite <- function(selected_file = options()$hemibrainr_matching
n2 = subset(n1, n1$skid %in% n$skid)
n[match(n2$skid,n$skid),c("skid","ItoLee_Hemilineage", "Hartenstein_Hemilineage", "cell_body_fiber")] = n2[,c("skid","ItoLee_Hemilineage", "Hartenstein_Hemilineage", "cell_body_fiber")]
ids.missing = setdiff(n1$skid,n$skid)
n3 = elmr::fafb_get_meta(unique(ids.missing), batch = TRUE, ...)
n = plyr::rbind.fill(n, n3[,c("skid","ItoLee_Hemilineage", "Hartenstein_Hemilineage", "cell_body_fiber")])
if(length(ids.missing)){
n3 = elmr::fafb_get_meta(unique(ids.missing), batch = TRUE, ...)
n = plyr::rbind.fill(n, n3[,c("skid","ItoLee_Hemilineage", "Hartenstein_Hemilineage", "cell_body_fiber")])
}
n = n[!duplicated(n),]
n$cell.type = matches[as.character(n$skid),"connectivity.type"]
lskids = as.character(catmaid::catmaid_skids("annotation:side: left", ...))
n$side = "right"
n[n$skid%in%lskids,"side"] = "left"
n$User[is.na(n$User)] = "flyconnectome"
nblast = tryCatch(hemibrain_nblast('hemibrain-fafb14'), error = function(e) NULL)
if(!is.null(nblast)){
nblast = hemibrain_nblast('hemibrain-fafb14')
Expand Down Expand Up @@ -1666,8 +1678,12 @@ hemibrain_matching_rewrite <- function(ids = NULL,
}
meta1 = hemibrain_get_meta(unique(ids), ...)
ids.missing = setdiff(gs$bodyid,meta1$bodyid)
meta2 = hemibrain_get_meta(unique(ids.missing), ...)
meta = rbind(meta1,meta2)
if(length(ids.missing)){
meta2 = hemibrain_get_meta(unique(ids.missing), ...)
meta = rbind(meta1,meta2)
}else{
meta = meta1
}
meta$cell.type = meta$type
meta = meta[,c("bodyid","ItoLee_Hemilineage","Hartenstein_Hemilineage","cellBodyFiber","cell.type","layer","ct.layer")]
meta$FAFB.match = gs$FAFB.match[match(meta$bodyid,gs$bodyid)]
Expand All @@ -1678,6 +1694,23 @@ hemibrain_matching_rewrite <- function(ids = NULL,
meta = meta[order(meta$bodyid),]
meta = meta[order(meta$cell.type),]
meta = meta[order(meta$ItoLee_Hemilineage),]
nblast = tryCatch(hemibrain_nblast('hemibrain-fafb14'), error = function(e) NULL)
if(!is.null(nblast)){
nblast.top =nblast[,match(meta$bodyid,colnames(nblast))]
tops = apply(nblast.top,1,function(r) which.max(r))
top = rownames(nblast)[unlist(tops)]
top[!meta$bodyid%in%colnames(nblast)] = NA
meta$nblast.catmaid.top = top
}
nblast = tryCatch(hemibrain_nblast('hemibrain-flywire'), error = function(e) NULL)
fw.neurons = tryCatch(flywire_neurons(), error = function(e) NULL)
if(!is.null(nblast) & !is.null(fw.neurons)){
nblast.top =nblast[,match(meta$bodyid,colnames(nblast))]
tops = apply(nblast.top,1,function(r) which.max(r))
top = rownames(nblast)[unlist(tops)]
top[!meta$bodyid%in%colnames(nblast)] = NA
meta$nblast.flywire.top = fw.neurons[unlist(top),"flywire.xyz"]
}
batches = split(1:nrow(meta), ceiling(seq_along(1:nrow(meta))/500))
gsheet_manipulation(FUN = googlesheets4::write_sheet,
data = meta[0,],
Expand All @@ -1691,6 +1724,45 @@ hemibrain_matching_rewrite <- function(ids = NULL,
}
}

#' @rdname hemibrain_add_made_matches
#' @export
flycircuit_matching_rewrite <- function(flycircuit.ids = names(flycircuit_neurons()),
meta = flycircuit_neurons[,],
selected_file = options()$hemibrainr_matching_gsheet){

# Get the LM matching Google sheet
gs = hemibrain_match_sheet(sheet = "lm", selected_file = selected_file)
ids = unique(gs$id)

# Get meta
fc.meta = meta[,intersect(colnames(fc.meta), colnames(gs))]
fc.meta = fc.meta[,colnames(gs)]
fc.meta = subset(fc.meta, fc.meta$id %in% flycircuit.ids)

# gs merge
if(nrow(gs)){
gs = merge(gs, fc.meta, all.x = TRUE, all.y = TRUE)
}else{
gs = fc.meta
}

# Update
rownames(gs) = NULL
googlesheets4::write_sheet(gs[0,],
ss = selected_file,
sheet = "lm")
batches = split(1:nrow(gs), ceiling(seq_along(1:nrow(gs))/500))
for(i in batches){
gsheet_manipulation(FUN = googlesheets4::sheet_append,
data = gs[min(i):max(i),],
ss = selected_file,
sheet = "lm")
}
}




# hidden
id_selector <- function(gs,
ids = NULL,
Expand Down
6 changes: 3 additions & 3 deletions R/hemibrain_read_neurons.R
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,13 @@ hemibrain_neurons <- function(local = FALSE,
folder = "hemibrain_neurons/"){
brain = match.arg(brain)
savedir = good_savedir(local = local)
neuronsdir = paste0(savedir,folder)
neuronsdir = file.path(savedir,folder)
if(dotprops){
message("Vector cloud object only available for JRCFIB2018F")
brain = "JRCFIB2018F"
fhdir = paste0(neuronsdir,brain,"/dotprops/")
fhdir = file.path(neuronsdir,brain,"dotprops/")
}else{
fhdir = paste0(neuronsdir,brain,"/")
fhdir = file.path(neuronsdir,brain,"/")
}
filelist = list.files(path = fhdir, pattern = ".rds", full.names = TRUE)
filelist = filelist[grepl("mirror",filelist)==mirror]
Expand Down
6 changes: 6 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ knitr::opts_chunk$set(

The goal of *hemibrainr* is to provide useful code for preprocessing and analysing data from the [Janelia FlyEM hemibrain](https://www.janelia.org/project-team/flyem) project. It makes use of the [natverse](https://github.com/natverse) R package, [neuprintr](https://github.com/natverse/neuprintr) to get hemibrain data from their connectome analysis and data hosting service [neuprint](https://github.com/connectome-neuprint/neuPrint). The dataset has been described [here]((https://www.biorxiv.org/content/10.1101/2020.01.21.911859v1)). Using this R package in concert with the [natverse](https://github.com/natverse/natverse) ecosystem is highly recommended.

The hemibrain connectome comprises the region of the fly brain depicted below. It is ~21,662 ~full neurons, 9.5 million synapses and is about ~35% complete in this region:

<center>
![hemibrain](https://raw.githubusercontent.com/flyconnectome/hemibrainr/master/inst/images/hemibrain.png)
</center>

## Installation

```{r install, eval = FALSE}
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<!-- README.md is generated from README.Rmd. Please edit that file -->
<!-- badges: start -->

<img src="man/figures/logo.svg" align="right" height="139" /> [![Travis
build
status](https://travis-ci.org/flyconnectome/hemibrainr.svg?branch=master)](https://travis-ci.org/flyconnectome/hemibrainr)
Expand All @@ -26,6 +25,13 @@ Using this R package in concert with the
[natverse](https://github.com/natverse/natverse) ecosystem is highly
recommended.

The hemibrain connectome comprises the region of the fly brain depicted
below. It is \~21,662 \~full neurons, 9.5 million synapses and is about
\~35% complete in this region:

<center>
![hemibrain](https://raw.githubusercontent.com/flyconnectome/hemibrainr/master/inst/images/hemibrain.png)
</center>
Installation
------------

Expand Down
Binary file modified data/dn.ids.rda
Binary file not shown.
Binary file added inst/images/birthday.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/images/hemibrain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion man/classed.ids.Rd

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

13 changes: 12 additions & 1 deletion man/hemibrain_add_made_matches.Rd

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

10 changes: 9 additions & 1 deletion man/hemibrain_matches.Rd

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

4 changes: 2 additions & 2 deletions vignettes/google_filestream.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ Let us have a look at the default `hemibrainr` settings, letting it to know wher
library(hemibrainr)
# Else, it wants to see it on the mounted team drive, here
options()$Gdrive_hemibrain_data
options("Gdrive_hemibrain_data")
# Get hemibrain skeletons
db <- hemibrain_neurons(local=FALSE)
## Will not work without google filestream
# You can also specify certain Google sheets
## Used for neuron-neuron matching
options()$hemibrainr_matching_gsheet
options("hemibrainr_matching_gsheet")
```

### Set your drive location
Expand Down

0 comments on commit 0de66c2

Please sign in to comment.