-
Notifications
You must be signed in to change notification settings - Fork 9
/
10x_DLPFC-n3_step04_exploringSpatial_LAH.R
executable file
·385 lines (319 loc) · 11.1 KB
/
10x_DLPFC-n3_step04_exploringSpatial_LAH.R
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
### MNT 10x snRNA-seq workflow: step 02b
### **Region-specific analyses**
### - (3x) DLPFC samples from: Br5161, Br5212, Br207
### **Comparing to Spatial Transcriptomics data
### Taken/adapted from /dcl02/lieber/ajaffe/SpatialTranscriptomics/HumanPilot/Analysis/Layer_Guesses
### Initiated MNT 17Feb2020
### Updated LAH 08Jun2021
#####################################################################
library(SingleCellExperiment)
library(EnsDb.Hsapiens.v86)
library(scater)
library(scran)
library(batchelor)
library(DropletUtils)
library(jaffelab)
library(here)
library(limma)
library(lattice)
library(RColorBrewer)
library(pheatmap)
# library(Seurat)
load(here("rdas/revision/regionSpecific_DLPFC-n3_cleaned-combined_SCE_LAH2021.rda"), verbose=T)
# sce.dlpfc
# chosen.hvgs.dlpfc
# pc.choice.dlpfc
# clusterRefTab.dlpfc
# ref.sampleInfo
# annotationTab.dlpfc
# cell_colors
#### get pseudobulk ####
sce.dlpfc$PseudoSample = paste0(sce.dlpfc$donor, ":", sce.dlpfc$cellType)
table(sce.dlpfc$PseudoSample)
length(unique(sce.dlpfc$PseudoSample))
# [1] 53
cIndexes = splitit(sce.dlpfc$PseudoSample)
umiComb <- sapply(cIndexes, function(ii)
rowSums(assays(sce.dlpfc)$counts[, ii, drop = FALSE]))
table(rowSums(umiComb) == 0)
# FALSE TRUE
# 29310 4228
phenoComb = colData(sce.dlpfc)[!duplicated(sce.dlpfc$PseudoSample), c(12:21)]
phenoComb$PseudoSample <- sce.dlpfc$PseudoSample[!duplicated(sce.dlpfc$PseudoSample)]
rownames(phenoComb) = phenoComb$PseudoSample
phenoComb = phenoComb[colnames(umiComb), ]
phenoComb = DataFrame(phenoComb)
sce_pseudobulk <-
logNormCounts(SingleCellExperiment(
list(counts = umiComb),
colData = phenoComb,
rowData = rowData(sce.dlpfc)
))
#### extract expression ####
mat <- assays(sce_pseudobulk)$logcounts
## Build a group model
#previoulsy prelimClusters
mod <- with(colData(sce_pseudobulk),
model.matrix(~ 0 + cellType))
# colnames(mod) <- gsub('prelimCluster', '', colnames(mod))
## get duplicate correlation
## was 'sample' swap to 'donor'
corfit <- duplicateCorrelation(mat, mod,
block = sce_pseudobulk$donor)
corfit$consensus.correlation
# [1] 0.08889248
#save(corfit, file = "rda/dlpfc_snRNAseq_pseudobulked_dupCor.Rdata")
## Next for each layer test that layer vs the rest
# cell_idx <- splitit(sce_pseudobulk$prelimCluster)
cell_idx <- splitit(sce_pseudobulk$cellType)
eb0_list_cell <- lapply(cell_idx, function(x) {
res <- rep(0, ncol(sce_pseudobulk))
res[x] <- 1
m <- with(colData(sce_pseudobulk),
model.matrix(~ res))
eBayes(
lmFit(
mat,design = m,
block = sce_pseudobulk$donor,
correlation = corfit$consensus.correlation
)
)
})
## MNT addition: [\n] Warning messages:
#1: Zero sample variances detected, have been offset away from zero
#2: Zero sample variances detected, have been offset away from zero
## LAH Coefficents not estimabale: res
## for each cell type: "1: Zero sample variances detected, have been offset away from zero"
#save(eb0_list_cell, file = "rda/dlpfc_snRNAseq_pseudobulked_specific_Ts.Rdata")
## Extract the p-values
pvals0_contrasts_cell <- sapply(eb0_list_cell, function(x) {
x$p.value[, 2, drop = FALSE]
})
rownames(pvals0_contrasts_cell) = rowData(sce_pseudobulk)$gene_id
t0_contrasts_cell <- sapply(eb0_list_cell, function(x) {
x$t[, 2, drop = FALSE]
})
rownames(t0_contrasts_cell) = rowData(sce_pseudobulk)$gene_id
fdrs0_contrasts_cell = apply(pvals0_contrasts_cell, 2, p.adjust, 'fdr')
data.frame(
'FDRsig' = colSums(fdrs0_contrasts_cell < 0.05 &
t0_contrasts_cell > 0),
'Pval10-6sig' = colSums(pvals0_contrasts_cell < 1e-6 &
t0_contrasts_cell > 0),
'Pval10-8sig' = colSums(pvals0_contrasts_cell < 1e-8 &
t0_contrasts_cell > 0)
)
# FDRsig Pval10.6sig Pval10.8sig
# Astro 1388 337 211
# Excit_A 279 61 40
# Excit_B 321 51 31
# Excit_C 207 30 15
# Excit_D 49 12 7
# Excit_E 50 4 2
# Excit_F 837 98 33
# Inhib_A 230 52 27
# Inhib_B 154 40 19
# Inhib_C 110 19 11
# Inhib_D 152 32 20
# Inhib_E 0 0 0 *
# Inhib_F 3 2 2
# Macrophage 20 2 1
# Micro 1017 423 290
# Mural 23 8 7
# Oligo 20487 3031 1549
# OPC 586 126 73
# Tcell 63 13 9
#### correlate to layer?? ####
## load modeling outputs
load("/dcl02/lieber/ajaffe/SpatialTranscriptomics/HumanPilot/Analysis/Layer_Guesses/rda/eb_contrasts.Rdata", verbose=T)
# eb_contrasts
load("/dcl02/lieber/ajaffe/SpatialTranscriptomics/HumanPilot/Analysis/Layer_Guesses/rda/eb0_list.Rdata", verbose=T)
# eb0_list
## Extract the p-values
pvals0_contrasts <- sapply(eb0_list, function(x) {
x$p.value[, 2, drop = FALSE]
})
rownames(pvals0_contrasts) = rownames(eb_contrasts)
fdrs0_contrasts = apply(pvals0_contrasts, 2, p.adjust, "fdr")
## Extract the t-stats
t0_contrasts <- sapply(eb0_list, function(x) {
x$t[, 2, drop = FALSE]
})
rownames(t0_contrasts) = rownames(eb_contrasts)
## line up
all(rownames(pvals0_contrasts) %in% rownames(pvals0_contrasts_cell))
all(rownames(pvals0_contrasts_cell) %in% rownames(pvals0_contrasts))
common_genes <- intersect(rownames(pvals0_contrasts), rownames(pvals0_contrasts_cell))
length(common_genes)
# [1] 22331
# Layer
pvals0_contrasts = pvals0_contrasts[common_genes, ]
t0_contrasts = t0_contrasts[common_genes, ]
fdrs0_contrasts = fdrs0_contrasts[common_genes, ]
# SN-PB'd
pvals0_contrasts_cell = pvals0_contrasts_cell[common_genes, ]
t0_contrasts_cell = t0_contrasts_cell[common_genes, ]
fdrs0_contrasts_cell = fdrs0_contrasts_cell[common_genes, ]
cor_t = cor(t0_contrasts_cell, t0_contrasts)
signif(cor_t, 2)
### just layer specific genes from ones left
layer_specific_indices = mapply(function(t) {
oo = order(t, decreasing = TRUE)[1:100]},
as.data.frame(t0_contrasts))
layer_ind = unique(as.numeric(layer_specific_indices))
length(layer_ind)
cor_t_layer = cor(t0_contrasts_cell[layer_ind, ],
t0_contrasts[layer_ind, ])
signif(cor_t_layer, 2)
### heatmap
theSeq = seq(-.81, .81, by = 0.01)
my.col <- colorRampPalette(brewer.pal(7, "PRGn"))(length(theSeq))
# ct = colData(sce_pseudobulk)
# ct = ct[!duplicated(sce_pseudobulk$collapsedCluster),]
# ct = ct[order(ct$cellType, ct$collapsedCluster),]
# ct$lab = paste0(ct$collapsedCluster, " (", ct$cellType,")")
cor_t_toPlot = cor_t[, c(1, 7:2)]
cor_t_layer_toPlot = cor_t_layer[, c(1, 7:2)]
pdf(here("pdfs/revision/exploration/overlap-ST-dlpfc_with_10x-snRNA-DLPFC-n3_LAH2021.pdf"),
height=5)
print(
levelplot(
cor_t_toPlot,
aspect = "fill",
at = theSeq,
col.regions = my.col,
ylab = "",
xlab = "",
scales = list(x = list(rot = 90, cex = 0.9), y = list(cex = 1.5)),
main = list("All Genes",side=1,line=0.5)
)
)
print(
levelplot(
cor_t_layer_toPlot,
aspect = "fill",
at = theSeq,
col.regions = my.col,
ylab = "",
xlab = "",
scales = list(x = list(rot = 90, cex = 0.9), y = list(cex = 1.5)),
main = list("Top 100 Layer Specific Genes",side=1,line=0.5)
)
)
dev.off()
pdf("pdfs/revision/exploration/dlpfc_snRNAseq_overlap_pheatmap_LAH2021.pdf", width = 10)
print(
pheatmap(
cor_t_toPlot,
main = "All Genes"
)
)
print(
pheatmap(
cor_t_layer_toPlot,
main = "Top 100 Layer Specific Genes"
)
)
dev.off()
#### gene expression
g = c("SNAP25", "CAMK2A", "GAD2", "SOX11",
"FOXP2", "PDGFRA", "MBP", "PLP1",
"AQP4", "GFAP", "CD74")
g2 = rowData(sce.dlpfc)[g,]$gene_id
t0_contrasts_cell_markers = t0_contrasts_cell[g2,]
cc_cell_layer = cor(t(t0_contrasts_cell_markers), cor_t_layer)
rownames(cc_cell_layer) <- g
signif(cc_cell_layer,3)
### heatmap
theSeq2 = seq(-5, 5, by = 0.01)
my.col2 <- colorRampPalette(brewer.pal(7, "RdBu"))(length(theSeq2))
t0_contrasts_cell_markers_plot = t(t0_contrasts_cell_markers)
t0_contrasts_cell_markers_plot[t0_contrasts_cell_markers_plot > 5] = 5
t0_contrasts_cell_markers_plot = t0_contrasts_cell_markers_plot[,ncol(t0_contrasts_cell_markers_plot):1]
#pdf("pdf/dlpfc_snRNAseq_marker_heatmap.pdf", width = 10)
print(
levelplot(
t0_contrasts_cell_markers_plot,
aspect = "fill",
at = theSeq2,
col.regions = my.col2,
ylab = "",
xlab = "",
scales = list(x = list(rot = 90, cex = 1.5), y = list(cex = 1.5))
)
)
#dev.off()
#### Compare with findMarkers ####
load(here("rdas/revision/markers-stats_DLPFC-n3_findMarkers-SN-LEVEL_LAH2021.rda"), verbose = TRUE)
## fix
logFC_fm <- sapply(markers.t.1vAll, function(x) {
x <- x[, "std.logFC", drop = FALSE]
x <- x[rownames(markers.t.1vAll[["Astro"]]),]
return(x)
})
t0_fm_cell <- logFC_fm * ncol(sce.dlpfc)
rownames(t0_fm_cell) <- rowData(sce_pseudobulk[rownames(t0_fm_cell)])$gene_id
common_genes2 <- intersect(common_genes, rownames(t0_fm_cell))
t0_fm_cell <- t0_fm_cell[common_genes2, ]
t0_contrasts_cell2 <- t0_contrasts_cell[common_genes2,]
## Marker Finding Corelation
cor_t_cells = cor(t0_contrasts_cell2, t0_fm_cell)
signif(cor_t_cells, 2)
t0_contrasts2 <- t0_contrasts[common_genes2,]
cor_t_fm = cor(t0_fm_cell, t0_contrasts2)
signif(cor_t_fm, 2)
s
layer_specific_indices2 <- mapply(function(t) {
oo = order(t, decreasing = TRUE)[1:100]},
as.data.frame(t0_contrasts2))
layer_ind2 = unique(as.numeric(layer_specific_indices2))
length(layer_ind2)
# [1] 691
cor_t_layer_fm = cor(t0_fm_cell[layer_ind2, ],
t0_contrasts[layer_ind2, ])
signif(cor_t_layer_fm, 2)
## Top100 cell Type genes
cellType_specific_indices <- mapply(function(t) {
oo = order(t, decreasing = TRUE)[1:100]},
as.data.frame(t0_fm_cell))
cellType_ind = unique(as.numeric(cellType_specific_indices))
length(cellType_ind)
# [1] 1402
cor_t_layer_ct_fm = cor(t0_fm_cell[cellType_ind, ],
t0_contrasts[cellType_ind, ])
signif(cor_t_layer_ct_fm, 2)
cor_fm <- list(cor_t_cells, cor_t_fm[, c(1, 7:2)], cor_t_layer_fm[, c(1, 7:2)], cor_t_layer_ct_fm[, c(1, 7:2)])
titles <- list("manual vs. findMarkers", "All Genes" , "Top 100 Layer Specific Genes", "Top 100 cell type specific genes")
theSeq3 = seq(-.12, .12, by = 0.005)
my.col3 <- colorRampPalette(brewer.pal(7, "PRGn"))(length(theSeq3))
pdf(here("pdfs/revision/exploration/overlap-ST-dlpfc_with_10x-snRNA-DLPFC-n3_findMarkers_LAH2021.pdf"),
height=5)
for(i in 1:length(cor_fm)){
print(
levelplot(
cor_fm[[i]],
aspect = "fill",
at = theSeq,
col.regions = my.col,
ylab = "",
xlab = "",
scales = list(x = list(rot = 90, cex = 0.9), y = list(cex = 1.5)),
main = list(titles[[i]],side=1,line=0.5)
)
)
}
for(i in 3:length(cor_fm)){
print(
levelplot(
cor_fm[[i]],
aspect = "fill",
at = theSeq3,
col.regions = my.col3,
ylab = "",
xlab = "",
scales = list(x = list(rot = 90, cex = 0.9), y = list(cex = 1.5)),
main = list(paste0(titles[[i]],"\nAdjusted Color Range"),side=1,line=0.5),
)
)
}
dev.off()