-
Notifications
You must be signed in to change notification settings - Fork 1
/
Analysis5_ParameterTuning.Rmd
158 lines (113 loc) · 4.45 KB
/
Analysis5_ParameterTuning.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
---
title: 'Result Part 5: Experiment for choosing the optimal t'
output: html_notebook
---
#Oversmoothing Experiments
- In downsampling, compare MSE to the reference dataset
- In other experiments, compare MSE to the observed dataset
### Read in data
```{r}
imp <- readRDS("Oversmooth_t_1to10.rds")
ds.files = list.files(path = '../Downsampling/Data',pattern = "ds.rds", full.names = TRUE)
ref.files <- list.files(path = '../Downsampling/Data',pattern = "ref", full.names = TRUE)
# add ds and ref file to dat
dat <- vector("list", 3)
names(dat) <- c("Reyfman", "PBMC", "Zeisel")
for(i in 1:3){
ref <- readRDS(ref.files[i])
ds <- readRDS(ds.files[i])
dat[[i]] <- c(list(ref, ds), imp[[i]])
names(dat[[i]]) = c("ref", "ds", names(imp[[i]]))
}
```
### MSE plot
```{r}
res <- readRDS( file = "oversmooth_res.rds")
library(ggplot2)
library(reshape2)
res = melt(res)
f1 <- ggplot(res, aes(Var2, value, group=factor(Var1))) + geom_line(color= 'gainsboro', size = 1) + geom_point(aes(color=factor(Var1)), size = 5)+theme_minimal()+xlab("Diffusion Step") + ylab("MSE") + labs(colour = "Datasets") + geom_point(shape = 1,size = 5,colour = "black")+
theme(text = element_text(size=20)) + theme(axis.text=element_text(size=20),
axis.title=element_text(size=20,face="bold"))+
theme(plot.margin = unit(c(1,1,1,1), "cm")) + theme(axis.title.x = element_text(margin = margin(t = 20))) + theme(axis.text.x = element_text(color = 'black'),axis.text.y = element_text(color = 'black'))+ theme(legend.text = element_text(size=20,
), legend.title = element_text(size=20,
))
```
### Obtain gene-wise and cell-wise correlation
```{r}
source("../Downsampling/my_functions.R")
source("../Downsampling/violin_plot.R")
#prepare input
cor.dat = vector("list", 2)
for (i in 1:2) {
cor.dat[[i]] <- vector("list", 3)
names(cor.dat[[i]]) <- names(dat)
for (j in 1:3) {
cor.dat[[i]][[j]] <- vector("list", 11)
names(cor.dat[[i]][[j]]) <- c("obs", names(dat[[i]])[3:12])
}
}
for (i in 1:3) {
for (j in 2:12){
# compare with normalized reference datset
ref <- normalizeData(dat[[i]][[1]])
tmp <- normalizeData(dat[[i]][[j]])
cor.dat[[1]][[i]][[j-1]] <- get.cor.gene(ref, tmp, "pearson")
cor.dat[[2]][[i]][[j-1]] <- get.cor.cell(ref, tmp, 'pearson')
}
}
saveRDS(cor.dat, 'oversmooth_cor_dat_2.rds')
```
### Generate plot
```{r}
cor.dat <- readRDS('oversmooth_cor_dat_2.rds')
library(RColorBrewer)
a = colorRampPalette(c("#DEEBF7", "#08306B"))
cor.dat <- lapply(cor.dat, function(x) {lapply(x, function(y){names(y) <- gsub('_', ': ',names(y)); return(y)})})
ds.plot = function(dim = "gene", ylim = c(0.0, 1)){
x <- c(1, 2, 3)
plot(x, c(-5, -5, -5), type = "p", ylab = " ", xlab = " ", cex = 1.5,
ylim = ylim, xlim = c(1, 9), lwd = 2, pch = 5, axes = FALSE, main = " ")
axis(1, at = c(2.1, 4.6, 7.1), labels = FALSE)
label_adj <- ifelse(dim=="gene", 0, 0.04)
text(c(2.1, 4.6, 7.1), par()$usr[3]-0.05+label_adj,
labels = c("Reyfman", "PBMC", "Zeisel"), srt = 0, adj = 0.5,
xpd = TRUE, cex = 2)
axis(2, pos = 1.1)
par(las = 0)
mtext("Correlation with Reference", side = 2, line = 1, cex = 2)
fill <- c("white", "#DEEBF7", "#C6D6E7", "#AEC1D7","#96ACC8","#7E97B8","#6783A9","#4F6E99","#37598A","#1F447A","#08306B")
xloc <- seq(1.5, by = 0.15, length.out = 11)
idx = ifelse(dim=="gene", 1, 2)
for (i in 1:3) {
for (j in 1:11) {
boxplot.ej(cor.dat[[idx]][[i]][[j]], xloc = xloc[j]+2.2*(i-1),
cex.boxpoint = ps, fill = fill[j],width.box = 0.1, width.hor = 0.1)
}
}
yadj = 0.10+0.2
xadj = 2.50
l = seq(from = 0.4, by = -0.06, length.out = 11)
r = l + 0.02
t = l + 0.01
imp_names = gsub( ": ", "=", names(cor.dat[[1]][[1]]))
for (i in 1:11){
rect(5.25+xadj, l[i]+yadj, 5.35+xadj, r[i]+yadj, col = fill[i], lwd = 2)
text(5.4+xadj, t[i]+yadj , imp_names[i], cex = 1.6, font = 1, adj = 0)
}
}
library(cowplot)
library(gridBase)
library(grid)
gen_fi <- function(){
par(cex.main = 2, mar = c(1, 2, 2, 1) + 0.1, oma = c(1, 3, 2, 0),
mgp = c(3.5, 1, 0),
cex.lab = 1.6, font.lab = 2, cex.axis = 1.6, mfrow = c(2, 1))
ds.plot(dim = "gene")
title("Gene", line = -2, outer = TRUE)
ds.plot(dim = "cell", ylim = c(0.8, 1))
title("Cell", line = -37, outer = TRUE)}
pdf("Plots/Figure_oversmooth_rev2.pdf",24, 14)
plot_grid(f1, gen_fi, labels = c('A', 'B'), label_size = 28)
dev.off()
```