Intro

You can select synapses within a spatial region and then carrying out a random sub-sampling. This might be useful as part of a strategy for defining connectivity without reconstructing all connections.

Setup

First load main packages

library(elmr)
## Loading required package: catmaid
## Loading required package: httr
## Loading required package: nat
## Loading required package: rgl
## Registered S3 method overwritten by 'nat':
##   method             from
##   as.mesh3d.ashape3d rgl
## 
## Attaching package: 'nat'
## The following object is masked from 'package:rgl':
## 
##     wire3d
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, union
## Loading required package: nat.flybrains
## Loading required package: nat.templatebrains
## Loading required package: nat.nblast
library(catmaid)
library(nat)

In order to run some of the examples we need to ensure that we:

  • can login to the FAFB CATMAID server

We will most our examples run conditionally based on this.

cl=try(catmaid_login())
catmaid_available=inherits(cl, "catmaid_connection")
run_vignette=catmaid_available

# set up for 3d plots based on rgl package
rgl::setupKnitr()
# frontal view
view3d(userMatrix=rgl::rotationMatrix(angle = pi, 1,0,0), zoom=0.6)

Selecting synapses

We’re going to look at synapses downstream of DA1 PNs

cda1=catmaid_get_connectors_between('name:PN Glomerulus DA1')

If we wanted to get the upstream synapses, we could do that as follows:

cda1.in=catmaid_get_connectors_between(post_skids = 'name:PN Glomerulus DA1')

We then want to select synapses within a neuropil region, the lateral horn. The elmr package now has a neuropil surface object FAFBNP13.surf that we can use for this purpose. To look for points inside the right LH, we can extract just that region from the surface object containing all neuropils as follows:

# convert subset of regions to rgl::mesh3d object
lhr=as.mesh3d(FAFBNP.surf, 'LH_R')

Now we can select just those synapses within the LH volume, by piping the XYZ positions of the presynaptic connector node to the pointsinside function.

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:nat':
## 
##     intersect, setdiff, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# make a new logical column indicating if connector is inside LH
cda1$inlh=select(cda1, connector_x:connector_z) %>%
  pointsinside(surf = lhr)
cda1 %>%
  filter(inlh) ->
  cda1.lh

Now let’s take a random 10% sample of the post-synaptic connector nodes for just one PN:

cda1.lh %>%
  filter(pre_skid==61221) %>%
  sample_frac(size=0.1) ->
random_syn

We can then construct CATMAID URLs for all those synapses:

synapse_urls=sapply(1:nrow(random_syn),
       function(i) open_fafb(random_syn[i, c("post_node_x", "post_node_y", "post_node_z")],
            active_skeleton_id = random_syn$post_skid[i],
            active_node_id = random_syn$post_node_id[i], open = F)
)

That was a little bit clunky but we basically feed the open_fafb function the data one row at a time centering the URL on the postsynaptic node and selecting that node in the CATMAID viewer.

So here are the results

head(synapse_urls)
## [1] "https://neuropil.janelia.org/tracing/fafb/v14/?pid=1&zp=147840&yp=157927&xp=357331&tool=tracingtool&sid0=5&s0=1.000000&active_skeleton_id=493185&active_node_id=2590191"
## [2] "https://neuropil.janelia.org/tracing/fafb/v14/?pid=1&zp=154480&yp=159372&xp=362663&tool=tracingtool&sid0=5&s0=1.000000&active_skeleton_id=480523&active_node_id=2556964"
## [3] "https://neuropil.janelia.org/tracing/fafb/v14/?pid=1&zp=152400&yp=163496&xp=360706&tool=tracingtool&sid0=5&s0=1.000000&active_skeleton_id=480775&active_node_id=2557244"
## [4] "https://neuropil.janelia.org/tracing/fafb/v14/?pid=1&zp=152280&yp=163631&xp=359321&tool=tracingtool&sid0=5&s0=1.000000&active_skeleton_id=480801&active_node_id=2557262"
## [5] "https://neuropil.janelia.org/tracing/fafb/v14/?pid=1&zp=147720&yp=147507&xp=352228&tool=tracingtool&sid0=5&s0=1.000000&active_skeleton_id=487240&active_node_id=2573115"
## [6] "https://neuropil.janelia.org/tracing/fafb/v14/?pid=1&zp=156880&yp=151615&xp=370232&tool=tracingtool&sid0=5&s0=1.000000&active_skeleton_id=479606&active_node_id=2556166"

Using the results

Let’s make a plot to get an idea of where those synapses are: First fetch the full structure of the seed PN

seedpn=read.neuron.catmaid(61221)

Now plot the selected and unselected synapses

# find bounding box containing selected synapses
synapse_bounds=apply(random_syn[,c("post_node_x", "post_node_y", "post_node_z")],2,range)
plot(seedpn, WithNodes=F, boundingbox=synapse_bounds)
# plot the synapses we didn't select
cda1.lh %>%
  filter(!post_node_id %in% random_syn$post_node_id & pre_skid %in% random_syn$pre_skid) %>%
  select(post_node_x:post_node_y) %>%
  points(col='grey')
# and then the ones we did
points(random_syn$post_node_x, random_syn$post_node_y, col='red', pch=19)

Then you can open those urls directly in CATMAID:

for (u in synapse_urls) {
  readline("Press a key to move to open the next synapse (or Esc to cancel).")
  browseURL(u)
}

or write them to a text file

writeLines(synapse_urls, con = 'PN61221_rand_synapse_urls.txt')

or upload them to a google doc

library(googlesheets)
random_syn$url=synapse_urls
random_syn$tracer=""
random_syn$date_started=""
tf=tempfile(fileext = '.tsv')
write.table(random_syn, file=tf, sep="\t")
gs_upload(tf, sheet_title = 'PN61221_rand_synapse_urls')
unlink(tf)