-
Notifications
You must be signed in to change notification settings - Fork 0
/
lda_mouseData.R
169 lines (147 loc) · 6.13 KB
/
lda_mouseData.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
library(Matrix)
library(text2vec)
library(data.table)
library(ggplot2)
library(tidyverse)
library(fastcluster)
library(devtools)
library(ComplexHeatmap)
library(scales)
library(Rtsne)
library(umap)
setwd("~/Desktop/STATS254/mouseData")
# Load data & build model
combMAT <- Matrix::readMM(paste0('./scATACRNA.binarized','.mtx.gz'))*1
head(combMAT)
gene.names = read.delim(paste0('./scATACRNA.binarized.genes','.tsv'),
header = FALSE,
stringsAsFactors = FALSE)
annots = read.delim(paste0('./scATACRNA.binarized.annot','.tsv'),
header = TRUE,
stringsAsFactors = FALSE)
colnames(combMAT) <- gene.names$V1
rownames(combMAT) <- annots$cellLabel
topics <- 15
lda_model <- LDA$new(n_topics = topics, doc_topic_prior = 50/topics, topic_word_prior = 0.1)
doc_topic_distr <- lda_model$fit_transform(x = combMAT,
n_iter = 150,
convergence_tol = 0.0001,
n_check_convergence = 4,
progressbar = TRUE, normalize='none')
model <- list()
model$topics <- lda_model$components
model$topic_sums <- as.matrix(rowSums(lda_model$components))
#model$document_sums <- t(doc_topic_distr)
model$topic_doc_distr <- t(doc_topic_distr)
model$topic_word_distr <- lda_model$topic_word_distribution
model$log.likelihoods <- t(attributes(doc_topic_distr)$likelihood)
model$perplexity <- perplexity(combMAT, lda_model$topic_word_distribution, doc_topic_distr)
# Plot Results
print(model$perplexity)
plot(model$log.likelihoods[1,], model$log.likelihoods[2,])
# TxC heatmap
dat <- model$topic_doc_distr
rownames(dat) <- paste0("Topic",seq(1,topics))
colnames(dat) <- annots$cellID
dat %>%
as.data.frame() %>%
rownames_to_column("Topic") %>%
pivot_longer(-c(Topic), names_to = "Sample", values_to = "porb") %>%
ggplot(aes(x=Topic, y=Sample, fill=porb)) +
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank()) +
geom_raster() +
scale_fill_viridis_c()
# TxG heatmap
dat <- model$topic_word_distr
rownames(dat) <- paste0("Topic",seq(1,topics))
colnames(dat) <- gene.names
dat %>%
as.data.frame() %>%
rownames_to_column("Topic") %>%
pivot_longer(-c(Topic), names_to = "Gene", values_to = "porb") %>%
ggplot(aes(x=Topic, y=Gene, fill=porb)) +
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank()) +
geom_raster() +
scale_fill_viridis_c()
# Complex TxC heatmap
col.low = "floralwhite"
col.mid = "pink"
col.high = "red"
distinctColorPalette <-function(k) {
ColorSpace <- t(unique(col2rgb(scales::hue_pal(l=85)(2e3))))
km <- kmeans(ColorSpace, k, iter.max=20)
colors <- rgb(round(km$centers), maxColorValue=255)
return(colors)
}
dat <- as.matrix(model$topic_doc_distr)
rownames(dat) <- paste0("Topic",seq(1,topics))
annots$cellLabel[annots$cellLabel == ""] = "UKN"
colnames(dat) <- annots$cellLabel
cl.cells <- fastcluster::hclust.vector(t(dat), method="ward", metric="euclidean")
dd.cells <- as.dendrogram(cl.cells)
colorPal <- colorRampPalette(c(col.low, col.mid, col.high))
colorBy <- c( 'tissue', 'source')
colVars <- list()
for (variable in colorBy){
colVars[[variable]] <- setNames(distinctColorPalette(length(unique(annots[variable])[,1])), as.vector(sort(unique(annots[variable])[,1])))
#cellColor <- setNames(colVars[[variable]][annots[variable]], rownames(dat))
}
annotation <- ComplexHeatmap::HeatmapAnnotation(df = annots[,colorBy,drop=FALSE] , col = colVars, which='column')
heatmap <- ComplexHeatmap::Heatmap(dat, col=colorPal(20), cluster_columns=dd.cells, name='Probability', use_raster = TRUE,
show_column_names=FALSE, show_row_names = TRUE, top_annotation = annotation,
column_order = sort(colnames(dat)), title_position='topcenter',
heatmap_legend_param = list(legend_direction = "horizontal", legend_width = unit(5, "cm"),
column_title = "Topic contribution per cell", column_title_gp = gpar(fontface = 'bold')))
pdf("./heatmap.pdf", width = 16, height = 8)
ComplexHeatmap::draw(heatmap, heatmap_legend_side = "bottom", annotation_legend_side = "right")
dev.off()
# Run tSNE
tSNE <- Rtsne::Rtsne(t(dat))
rownames(tSNE$Y) <- annots$cellID
colnames(tSNE$Y) <- paste0('tSNE', 1:ncol(tSNE$Y))
ggplot(data=data.frame(x = tSNE$Y[,1], y = tSNE$Y[,2], col = annots$cellLabel),
aes(x=x, y=y, color=col)) +
geom_point() +
guides(fill=guide_legend(ncol=10)) +
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.position = "bottom",
legend.key.size = unit(0.1, "cm"))
ggplot(data=data.frame(x = tSNE$Y[,1], y = tSNE$Y[,2], col = annots$source),
aes(x=x, y=y, color=col)) +
geom_point() +
guides(fill=guide_legend(ncol=10)) +
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.position = "bottom",
legend.key.size = unit(0.1, "cm"))
# Run UMAP
Umap <- umap::umap(t(dat))
# rownames(Umap$layout) <- annots$cellID
# colnames(Umap$layout) <- paste0('UMAP', 1:ncol(Umap$layout))
ggplot(data=data.frame(UMAP1 = Umap$layout[,1], UMAP2 = Umap$layout[,2], cell = annots$cellLabel),
aes(x=UMAP1, y=UMAP2, color=cell)) +
geom_point() +
guides(fill=guide_legend(ncol=10)) +
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.position = "bottom",
legend.key.size = unit(0.1, "cm"))
ggplot(data=data.frame(UMAP1 = Umap$layout[,1], UMAP2 = Umap$layout[,2], SRC = annots$source),
aes(x=UMAP1, y=UMAP2, color=SRC)) +
geom_point() +
guides(fill=guide_legend(ncol=10)) +
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.position = "bottom",
legend.key.size = unit(0.1, "cm"))