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: nat.flybrains
## Loading required package: nat.templatebrains
## Loading required package: rgl
## Loading required package: nat
## Registered S3 method overwritten by 'nat':
##   method             from
##   as.mesh3d.ashape3d rgl
## 
## Attaching package: 'nat'
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, union
## Loading required package: nat.nblast
## Loading required package: catmaid
## Loading required package: httr
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.

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:

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

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:

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=156320&yp=166961&xp=361014&tool=tracingtool&sid0=5&s0=1.000000&active_skeleton_id=497506&active_node_id=2596208"  
## [2] "https://neuropil.janelia.org/tracing/fafb/v14/?pid=1&zp=150720&yp=157526&xp=356405&tool=tracingtool&sid0=5&s0=1.000000&active_skeleton_id=1897652&active_node_id=6732898" 
## [3] "https://neuropil.janelia.org/tracing/fafb/v14/?pid=1&zp=148240&yp=157342&xp=354327&tool=tracingtool&sid0=5&s0=1.000000&active_skeleton_id=5842191&active_node_id=19606946"
## [4] "https://neuropil.janelia.org/tracing/fafb/v14/?pid=1&zp=148120&yp=160585&xp=358070&tool=tracingtool&sid0=5&s0=1.000000&active_skeleton_id=487663&active_node_id=2574164"  
## [5] "https://neuropil.janelia.org/tracing/fafb/v14/?pid=1&zp=149280&yp=147960&xp=356525&tool=tracingtool&sid0=5&s0=1.000000&active_skeleton_id=486066&active_node_id=2571242"  
## [6] "https://neuropil.janelia.org/tracing/fafb/v14/?pid=1&zp=152480&yp=163274&xp=360735&tool=tracingtool&sid0=5&s0=1.000000&active_skeleton_id=496601&active_node_id=2595320"

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

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)