-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvarcomp.R
151 lines (109 loc) · 4.77 KB
/
varcomp.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
library(Seurat)
library(Signac)
library(gaston)
library(ggplot2)
library(reshape2)
library(data.table)
library(Matrix)
library(GenomicRanges)
######## Function for variance component model ########
varcomp <- function(object, shuffle = FALSE, seed = 2021, ncores = 10){
## CollapseToLongestTranscript(), DistanceToTSS() functions can be found in utils.R
gene.coords <- CollapseToLongestTranscript(ranges = Annotation(object = object[['ATAC']]))
rna = object[['SCT']]@data
if(shuffle){
set.seed(seed)
rna = rna[,sample(1:ncol(rna), ncol(rna), replace = F)]
}
genes = rownames(rna)[which(rowMeans(rna > 0) >= 0.1)]
gene.coords.use <- gene.coords[gene.coords$gene_name %in% genes,]
peaks <- granges(object[['ATAC']])
atac <- object[['ATAC']]@data
peaks.keep <- which(rowMeans(atac > 0) >= 0.1)
peaks <- peaks[peaks.keep]
atac <- atac[peaks.keep,]
## Identify peaks at genes' promoter regions, i.e. peaks_TSS
gene.promoters <- promoters(gene.coords.use, upstream = 1000, downstream = 100)
hits <- findOverlaps(query = peaks, subject = gene.promoters)
peaks_TSS <- rownames(atac)[unique(queryHits(hits))]
## Identify peaks at genes' enhancer regions, i.e. peaks_DE
peak_distance_DE <- DistanceToTSS(
peaks = peaks,
genes = gene.coords.use,
distance = 5e+05
)
peaks_DE <- rownames(atac)[which(rowSums(peak_distance_DE) >= 1)]
peaks_DE <- setdiff(peaks_DE, peaks_TSS)
## Generate correlation matrices for different components
P = cor(as.matrix(atac[peaks_TSS,]))
E = cor(as.matrix(atac[peaks_DE,]))
I = matrix(0, nrow = nrow(P), ncol = ncol(P), dimnames = list(rownames(P), colnames(P)))
samples <- object$sample.id
for (i in samples) {
cells <- which(object$sample.id == i)
I[cells, cells] <- 1
}
A = matrix(0, nrow = nrow(P), ncol = ncol(P), dimnames = list(rownames(P), colnames(P)))
ageGroups <- object$age.group
for (i in ageGroups) {
cells <- which(object$age.group == i)
A[cells, cells] <- 1
}
## Apply AIREML to infer variance parameters
vals <- parallel::mclapply(genes, function(i){
Y <- scale(rna[i,])
mod <- lmm.aireml(Y = Y, K = list(P, E, I, A), verbose = FALSE)
round(c(mod$sigma2, mod$tau), 3)
}, mc.cores = ncores)
vals <- matrix(unlist(vals), ncol = 5, byrow = TRUE)
rownames(vals) = genes
return(vals)
}
######## Perform variance component model to the original and shuffled data ########
load('../data_processed/pseudobulk_500_50_0119.rda')
pbmc <- pseudobulk
rm(pseudobulk); gc()
val_all <- varcomp(pbmc)
val_shuffle <- varcomp(pbmc, shuffle = T)
save(val_all, val_shuffle, file = '../data_processed/varcomp_vals_0624.rda')
######## Plotting ########
vals = val_all
# vals = val_shuffle
vdf <- data.frame(vals/rowSums(vals)*100)
names(vdf) <- c('Unexplained', 'Promoter', 'Enhancer', 'Individual', 'Age')
vdf$gene <- rownames(vals)
vdf <- vdf[order((vdf$Promoter + vdf$Enhancer), decreasing = T),]
vdf2 <- rbind(vdf[(vdf$Promoter+vdf$Enhancer) > 1,],
(vdf[(vdf$Promoter+vdf$Enhancer) < 1,])[(order(vdf[(vdf$Promoter + vdf$Enhancer) < 1, 'Age'], decreasing = T)),]
)
vdf2$rank <- 1:dim(vdf2)[1]
extLabels = sapply(colnames(vdf2)[1:5], function(x) paste0(x, " (", round(mean(vdf2[,x]),2), "%)"))
ldf <- melt(vdf2, id.var=c("gene", "rank"))
ldf$variable = factor(ldf$variable, levels = c('Unexplained', 'Individual', 'Age', 'Promoter', 'Enhancer'), ordered = T)
ldf_all = ldf #original
# ldf_random = ldf #shuffled
p1 <- ggplot(ldf_all, aes(x = rank, y = value, fill = variable)) +
geom_bar(stat = "identity") +
labs(fill = "Component", x = "Genes", y = "% Variance Explained") +
scale_fill_manual(values = c("grey", "blue", "#E4288A", "#E4AB00", "#67A61A"),
labels=extLabels) +
theme(panel.grid = element_blank(), panel.border = element_blank(),
legend.position = c(0.2, 0.3), legend.title = element_blank(),
legend.background = element_rect(fill = "white", color = "white")) +
theme(plot.margin=grid::unit(c(0,0,0,0), "mm")) +
scale_x_continuous( expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0)) +
ggtitle('Original')
p1
p2 <- ggplot(ldf_random, aes(x = rank, y = value, fill = variable)) +
geom_bar(stat = "identity") +
labs(fill = "Component", x = "Genes") +
scale_fill_manual(values = c("grey", "blue", "#E4288A", "#E4AB00", "#67A61A"),
labels=extLabels_random) +
theme(panel.grid = element_blank(), panel.border = element_blank()) +
theme(legend.title = element_blank(), legend.position = c(0.8, 0.7),
legend.background = element_rect(fill = "white", color = "white")) +
theme(plot.margin=grid::unit(c(0,0,0,0), "mm")) +
scale_x_continuous( expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0)) +
ggtitle('Permuted')
p2
p1+p2