-
Notifications
You must be signed in to change notification settings - Fork 2
/
spectral-clustering.Rmd
270 lines (189 loc) · 7.83 KB
/
spectral-clustering.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
---
title: "Spectral Clustering"
description: |
This R script performs unsupervised spectral clustering to investigate the existence of diverse phenotypes of astrocytes and microglia in control and AD brains.
author:
- first_name: "Ayush"
last_name: "Noori"
url: https://www.github.com/ayushnoori
affiliation: Massachusetts General Hospital
affiliation_url: https://www.serranopozolab.org
orcid_id: 0000-0003-1420-1236
output:
distill::distill_article:
toc: true
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(eval = FALSE)
```
# Dependencies
Load requisite packages and define directories. Note that this script uses my personal utilities package `brainstorm`, which can be downloaded via `devtools::install_github("ayushnoori/brainstorm")`.
```{r load-packages, message=FALSE, warning=FALSE}
# data manipulation
library(data.table)
library(purrr)
library(magrittr)
# spectral clustering
library(stringr)
library(SNFtool)
# heatmap
library(pheatmap)
library(ggplot2)
library(RColorBrewer)
# utility functions
library(brainstorm)
```
Note that directories are relative to the R project path.
```{r define-directores}
# set directories
dir3 = file.path("Results", "3 - ROI Measurements")
dir4 = file.path("Results", "4 - Spectral Clustering")
```
# Load Data
Load ROI measurement data from the `3 - ROI Measurements` directory and split by `Group`.
```{r load-data}
all = fread(file.path(dir3, "ROI Measurements.csv")) %>% split(.$Group)
```
# Define Clustering Functions
Define functions to compute spectral clustering using the `SNFtool` package. Here, `affinityCustom` is a modified version of `affinityMatrix` from `SNFtool` without sparsifying the affinity matrix by a K-nearest neighbors approach (i.e., removing the assumption that local pairwise similarities with high values are more reliable than remote ones).
```{r affinity-matrix, include = FALSE}
affinityCustom = function (diff, sigma = 0.5)
{
N <- nrow(diff)
diff <- (diff + t(diff))/2
diag(diff) <- 0
sortedColumns <- as.matrix(t(apply(diff, 2, sort)))
finiteMean <- function(x) {
return(mean(x[is.finite(x)]))
}
# this line has been modified to remove [, 1:K + 1]
means <- apply(sortedColumns, 1, finiteMean) +
.Machine$double.eps
avg <- function(x, y) {
return((x + y)/2)
}
Sig <- outer(means, means, avg)/3 * 2 + diff/3 + .Machine$double.eps
Sig[Sig <= .Machine$double.eps] <- .Machine$double.eps
densities <- dnorm(diff, 0, sigma * Sig, log = FALSE)
W <- (densities + t(densities))/2
return(W)
}
```
Define spectral clustering function.
```{r spectral-clustering}
spectral_clustering = function(dat, lab, mx, k = 3) {
# print log
cat(paste("\n", toupper(lab), "ANALYSIS\n"))
# calculate distance matrix
distM = as.matrix(dat[, ..mx]) %>% dist2(., .) %>% .^(1/2)
# calculate similarity matrix
simM = affinityCustom(distM)
# perform spectral clustering
cat(paste0("- Performing Spectral Clustering, ", word(Sys.time(), 2), "\n"))
clust = spectralClustering(simM, K = k)
# add spectral clustering labels to data
dat[, State := clust]
return(dat)
}
```
Define function to bin distance, where `distlab` is a character vector specifying the name of the distance column of interest.
```{r bin-distance}
bin_distance = function(dat, distlab,
distbins = c(0, 25, 50),
distlevels = c("< 25 um", "25-50 um", "> 50 um"),
distna = "None") {
# bin distance labels
dat %>%
.[, TemporaryBin := .SD, .SDcols = distlab] %>%
.[, TemporaryBin := cut(TemporaryBin,
breaks = c(distbins, max(TemporaryBin, na.rm = T)),
include.lowest = T)] %>%
.[, TemporaryBin := addNA(TemporaryBin)] %>%
.[, TemporaryBin := plyr::mapvalues(TemporaryBin, levels(TemporaryBin),
c(distlevels, "None"))]
# group None with > 50 um
dat[TemporaryBin == "None", TemporaryBin := distna]
# rename column
setnames(dat, "TemporaryBin", paste0(distlab, "Bin"))
return(invisible(dat))
}
```
Define function to (a) prepare data for heatmap by refactoring label, reordering, and binning distance, then (b) plot the heatmap using the `pheatmap` package.
```{r plot-heatmap}
scale_data = function(b) { return(100*(b - min(b))/(max(b) - min(b))) }
plot_heatmap = function(dat, lab, mx, hmcols, hmsel) {
# subset marker/metadata columns
dat = dat[, .SD, .SDcols = c(mx, hmsel)]
# calculate proportion of control ROIs by state
prop = dat[, sum(Condition == "Control")/.N, by = .(State)] %>%
.[order(-V1), State]
# convert to factor then reorder ROIs
dat = dat %>%
.[, State := factor(State, levels = prop, labels = c("Homeostatic", "Intermediate", "Reactive"))] %>%
# .[, State := factor(State)] %>%
.[, Sample := factor(Sample)] %>%
.[, Condition := factor(Condition, levels = c("Control", "Alzheimer"))] %>%
.[order(State, Condition, Layer, runif(nrow(.))), ]
# calculate distances
dat = dat %>% bin_distance("Distance")
# write file
setcolorder(dat, c("ID", "State"))
fwrite(dat, file.path(dir4, paste(lab, "Spectral Clustering.csv")))
# calculate column gaps
gaps = cumsum(summary(dat[, State]))
# prepare for heatmap
hmdat = dat[, ..mx] %>% map_dfc(~scale_data(.x)) %>% t()
# select row names and column names
hmannos = dat[, .SD, .SDcols = c("Layer", "Condition", "State")]
# group None with > 50
group_none = function(x) hmannos[get(x) == "None", c(x) := "> 50 um"]
walk(hmdists, group_none)
# remove "Bin" label
colnames(hmannos) = gsub("Bin", "", colnames(hmannos), fixed = T)
# set rownames and colnames
colnames(hmdat) = dat$ID; rownames(hmannos) = dat$ID
# plot heatmap
hm = pheatmap(hmdat,
cluster_cols = FALSE, cluster_rows = FALSE,
annotation_colors = hmcols, annotation_col = hmannos,
border_color = NA, # main = paste(lab, "Heatmap"),
show_colnames = FALSE, gaps_col = gaps, silent = TRUE)
ggsave(file.path(dir4, paste(lab, "Heatmap.pdf")), hm, width = 8, height = 6)
ggsave(file.path(dir4, paste(lab, "Heatmap.png")), hm, width = 8, height = 6, dpi = 1200)
return(dat)
}
```
# Perform Spectral Clustering
Apply `spectral_clustering` function over ROI measurement data by `Type`.
```{r perform-clustering}
# define markers of interest
markers = list(Astrocyte = c("GFAP", "YKL40", "VIM", "TSPO",
"EAAT1", "EAAT2", "GS"),
Microglia = c("MHC2", "CD68", "TMEM119", "TSPO", "FTL"),
Vessel = c("GFAP", "YKL40", "VIM", "TSPO", "EAAT1", "EAAT2", "GS"))
# comment out the below lines if you do NOT want to re-run spectral clustering
# perform spectral clustering with k = 3 clusters
all = imap(all, ~spectral_clustering(.x, .y, markers[[.y]], 4))
# save spectral clustering result
saveRDS(all, file.path(dir4, "Spectral Clustering.rds"))
# read spectral clustering result
# all = readRDS(file.path(dir4, "Spectral Clustering.rds"))
```
# Plot Heatmap
Plot data as heatmaps.
```{r create-heatmap}
# define metadata of interest for output .csv file
sel = colnames(all$Astrocyte) %>% .[!(. %in% c(unlist(markers), c("ALDH1L1", "Abeta", "DAPI", "HuC.D", "IBA1", "PHF1.tau")))]
# define distance colors
distcols = c('< 25 um' = "#FFAF85", '25-50 um' = "#FFED85", '> 50 um' = "#96BDD9")
# define heatmap color palette
cols = list(
Distance = distcols,
Layer = c(II = "#DDF2B2", III = "#8DD2B9", IV = "#39AEC3", V = "#2072B1", VI = "#0C2C84"),
Condition = c(Control = "#377EB8", Alzheimer = "#CE6D8B"),
State = c('Homeostatic' = "#39B200", 'Intermediate' = "#F0C808", 'Reactive' = "#960200"))
# plot heatmap data
hm = imap(all, ~plot_heatmap(.x, .y, markers[[.y]], cols, sel))
# save processed data
saveRDS(hm, file.path(dir4, "Z-Score Data.rds"))
```