-
Notifications
You must be signed in to change notification settings - Fork 28
/
NeuroGeometry.Rmd
216 lines (176 loc) · 5.89 KB
/
NeuroGeometry.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
---
title: "NeuroGeometry: Analysing 3D morphology of neurons"
author: "Gregory Jefferis"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{NeuroGeometry}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
## Introduction
One of the key distinguishing features of nat is the ability to analyse
neuronal morphology and connectivity from a geometric perspective.
In other words nat provides functions to analyse the placement of neurons within
the context of the brain and the organisation of local circuits.
This vignette will give a few examples, but this is only skimming the surface
of what is possible.
If you see an analysis in a paper associated with the Jefferis lab but cannot
figure out how to use nat to achieve this, then feel free to contact the
[nat–user Google group](https://groups.google.com/d/forum/nat-user) to ask for help.
## Vertex information
```{r setup}
library(nat)
```
One of the key tools for geometric work in nat is the `xyzmatrix` function,
which extracts 3-D coordinate information from any object containing vertices.
```{r, message=FALSE, fig.width=6, fig.height=5}
n=Cell07PNs[[1]]
xyz=xyzmatrix(n)
summary(xyz)
plot(xyz[,c("X","Y")], type='p')
```
This also works for neuronlists, so we can extract all of the points in a
collection of neurons:
```{r}
xyz=xyzmatrix(Cell07PNs)
```
We could ask for all the points close to a specific X coordinate
```{r}
close_to_x250=abs(xyz[,'X']-250)<10
table(close_to_x250)
```
We can also select points interactively by using the `rgl::select3d` function.
## Worked example - plane cutting a tract
As a worked example let's try to find the plane cutting a tract defined by the
axons of some example neurons:
```{r, fig.width=6, fig.height=5}
plot(Cell07PNs, WithNodes=F)
dist=10
abline(v=250, col='red')
abline(v=c(250-dist, 250+dist), col='red', lty='dotted')
```
We could ask for all the points close to a specific X coordinate
```{r}
close_to_x250=abs(xyz[,'X']-250)<10
table(close_to_x250)
```
Let's find the centroid of those selected points:
```{r, fig.width=6, fig.height=5}
centroid=colMeans(xyz[close_to_x250,])
centroid
plot(Cell07PNs, WithNodes=F)
points(matrix(centroid[1:2],ncol=2), pch=20)
```
### PCA to find tract vector
Now let's do a principal components analysis on those points
```{r, fig.width=6, fig.height=5}
pc=prcomp(xyz[close_to_x250,])
pc1=pc$rotation[,'PC1']
plot(Cell07PNs, WithNodes=F)
points(matrix(centroid[1:2],ncol=2), pch=20)
res=rbind(centroid-pc1*10, centroid, centroid+pc1*10)
lines(res[,1:2])
```
OK, perhaps it would be better if we only used the spine of the neurons
```{r}
# There is a problem with one neuron with a loop, hence OmitFailures
spines=nlapply(Cell07PNs, spine, UseStartPoint=T, OmitFailures = T)
xyz=xyzmatrix(spines)
close_to_x250=abs(xyz[,'X']-250)<10
table(close_to_x250)
```
```{r, fig.width=6, fig.height=5}
pc=prcomp(xyz[close_to_x250,])
pc1=pc$rotation[,'PC1']
plot(spines, WithNodes=F)
points(matrix(centroid[1:2],ncol=2), pch=20)
res=rbind(centroid-pc1*10, centroid, centroid+pc1*10)
# nb 1:2 => just the xy coords
lines(res[,1:2])
```
### Tract vector for each neuron
Alternatively, we could find the vector of the first PC for each neuron.
Let's write a function to do that and then apply it to the spine of each neuron.
```{r, fig.width=6, fig.height=5}
# returns 6 vector
# first 3 values are centroid
# second three values first principal component
tract_vector <- function(n, xval=250, thresh=10) {
p=xyzmatrix(n)
near=abs(p[,'X']-xval)<thresh
pc=prcomp(p[near,])
c(colMeans(p[near,]), pc$rotation[,'PC1'])
}
tvs=sapply(spines, tract_vector)
mean_cent=rowMeans(tvs[1:3,])
mean_vec=rowMeans(tvs[4:6,])
# nb *10 to make the vector longer (10 µm) and more visible
res2=rbind(mean_cent-mean_vec*10, mean_cent, mean_cent+mean_vec*10)
plot(spines, col='grey', WithNodes=F)
lines(res2[,1:2], lwd=3)
```
### Lines crossing a plane
```{r, echo=F}
rgl::setupKnitr()
```
We can look at the plane we have defined in a 3D context as follows:
```{r, rgl=T}
plc=plane_coefficients(mean_cent, mean_vec)
plot3d(Cell07PNs[c(1,30)], col='grey', WithNodes = F)
planes3d(plc[,1:3], d=plc[,'d'])
```
We can also make a small square plane centred on the chosen point
```{r, rgl=T}
library(rgl)
clear3d()
make_centred_plane <- function(point, normal, scale = 1) {
# find the two orthogonal vectors to this one
uv = Morpho::tangentPlane(normal)
uv = sapply(uv, "*", scale, simplify = F)
qmesh3d(
cbind(
point + uv$z,
point + uv$y,
point - uv$z,
point - uv$y
),
homogeneous = F,
indices = 1:4
)
}
plane=make_centred_plane(mean_cent, mean_vec, scale=15)
plot3d(spines, col='grey')
shade3d(plane, col='red')
```
Finally, we can find the positions at which each neuron intersects the plane.
Note that when there are multiple intersections, we only choose the closest
point to the centroid that we calculated when defining the plane.
```{r}
intersections=t(sapply(Cell07PNs, intersect_plane, plc, closestpoint=mean_cent))
# center those intersection points
intersections.cent=scale(intersections, center = T, scale = F)
d=sqrt(rowSums(intersections.cent^2))
mean(d)
```
Then we can plot those positions on the plane
```{r, webgl=T}
spheres3d(intersections, col='red', rad=.5)
shade3d(plane, col='black')
plot3d(spines, col='grey')
```
Finally we can construct a 2D coordinate system on the plane and project
the intersection positions onto that.
```{r, fig.width=5, fig.height=4}
# find pair of orthogonal tangent vectors of plane
uv = Morpho::tangentPlane(mean_vec)
# centre our points on mean
sp=scale(intersections, scale = F, center=T)
DotProduct=function(a,b) sum(a*b)
# find coordinates w.r.t. to our two basis vectors
xy=data.frame(u=apply(sp, 1, DotProduct, uv[[1]]),
v=apply(sp, 1, DotProduct, uv[[2]]))
# maintain consist x-y aspect ratio
plot(xy, pch=19, asp=1, main = "Position of Axon intersection in plane",
xlab="u /µm", ylab="v /µm")
```