-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsupplementary_notes_scripts.Rmd
More file actions
417 lines (315 loc) · 13.1 KB
/
supplementary_notes_scripts.Rmd
File metadata and controls
417 lines (315 loc) · 13.1 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# Scripts needed to reproduce supplementary notes pictures
```{r}
source('../R/setup.R')
library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(svglite)
library(ggpubr)
```
## Negative proportions change plot
```{r}
## util method to make plot
plot_matrix_hist_with_negative <- function(data_, title="Distribution of values") {
toPlot <- as.data.frame(c(data_))
colnames(toPlot) <- "value"
toPlot$negative <- (toPlot$value < 0)
plt <- ggplot(toPlot) + geom_histogram(
aes(x=value,fill=negative), stat = "bin", binwidth=0.01, boundary = -0.3) + theme_minimal() + ggtitle(title)
return(plt)
}
```
### Simulated data
```{r}
set.seed(3)
K <- 3
sim <- create_simulation(n_genes = 10000,
n_samples = 100,
n_cell_types = K,
with_marker_genes = FALSE)
sim <- sim %>% add_noise(noise_deviation = 2.5)
data_raw <- sim$data
true_basis <- sim$basis
true_proportions <- sim$proportions
```
### Create object
```{r}
dso <- DualSimplexSolver$new()
dso$set_data(data_raw)
dso$project(K)
dso$plot_svd_history()
dso$init_solution("random")
dso$plot_projected("zero_distance", "zero_distance",with_solution=TRUE, use_dims = list(2:3))
```
### Optimize and make plots
```{r}
set.seed(23)
dso$init_solution("random")
iterations <- 5000
dso$optim_solution(iterations, optim_config(coef_hinge_H = 1,
coef_hinge_W = 10,
coef_der_X = 0.001,
coef_der_Omega = 0.001
))
dso$finalize_solution()
dso$plot_projected("zero_distance", "zero_distance",with_solution=TRUE, use_dims = list(2:3))
plot_errors(dso$st$solution_proj, variables = c("deconv_error",
"lamdba_error",
"beta_error",
"total_error")) +geom_line(size=1)
```
### Prepare picture
```{r fig.width=10, fig.height=5}
plt_neg_prop <- dso$plot_negative_proportions_change()
plt_neg_basis <- dso$plot_negative_basis_change()
plt_hinge_prop_change <- plot_errors(dso$st$solution_proj, variables = c("lamdba_error")) +
geom_line(size=1) +
ggtitle("Hinge values for proportions")
plt_hinge_basis_change <- plot_errors(dso$st$solution_proj, variables = c("beta_error")) +
geom_line(size=1)
prop_hist <- plot_matrix_hist_with_negative(dso$st$solution_no_corr$H, "Distribution of proportion values")
row1 <- ggarrange(plt_hinge_prop_change, plt_neg_prop, nrow=1)
total <- ggarrange(row1, prop_hist, nrow=2)
total
```
## Choice of coefficients for GSE19830
### Data
```{r}
set.seed(5)
library('GEOquery')
K <- 3
## Get the data
dataset <- "GSE19830"
gse <- getGEO(dataset, AnnotGPL = T)
gse <- gse[[1]]
data_gse19830 <- Biobase::exprs(gse)
#linearize the data!
data_gse19830 <- linearize_dataset(data_gse19830)
## Map probes
library(biomaRt)
library(matrixStats)
# probes which are present in data
probe_mapping <- biomaRt::select(rat2302.db::rat2302.db, rownames(data_gse19830), c("SYMBOL"))
probe_mapping <- probe_mapping[probe_mapping$PROBEID %in% rownames(data_gse19830), ]
# not empty result gene id (now we many-to-many in in genes to probes)
probe_mapping <- probe_mapping[!is.na(probe_mapping$SYMBOL), ]
# This dataset is strange, 1 probe could be many genes, which is not correct (but you can try without this)
probe_mapping <- probe_mapping[!duplicated(probe_mapping$PROBEID), ]
## want to collapse gene for each gene and take mean value of data
data_gse19830 <- data_gse19830[probe_mapping$PROBEID,]
rownames(data_gse19830) <- probe_mapping$SYMBOL
# should be unique gene name as row name after this
data_gse19830 <- tapply(data_gse19830, list(row.names(data_gse19830)[row(data_gse19830)], colnames(data_gse19830)[col(data_gse19830)]), FUN = median)
coding_info <- biomaRt::select(rat2302.db::rat2302.db, rownames(data_gse19830),keytype="SYMBOL", c("SYMBOL", "GENETYPE"))
# possible values for genetype "protein-coding" "ncRNA""pseudo""other" "unknown"
MOUSE_PROTEIN_CODING_GENES <- coding_info[coding_info$GENETYPE == 'protein-coding', 'SYMBOL']
length(PROTEIN_CODING_GENES)
print(paste("Dim for the whole data:", toString(dim(data_gse19830))))
print(paste("Dim for hidden proportions (H):", toString(dim(true_proportions))))
print(paste("Dim for hidden basis (W):", toString(dim(true_basis))))
```
### Create the object
```{r fig.height = 5, fig.width = 11}
dso <- DualSimplexSolver$new()
dso$set_data(data_gse19830, gene_anno_lists = list(CODING = MOUSE_PROTEIN_CODING_GENES))
dso$basic_filter( keep_true_cols = c("CODING"), remove_true_cols_default = c())
dso$project(K)
dso$basic_filter(log_mad_gt = 1)
dso$project(K)
dso$distance_filter(plane_d_lt = 0.015, zero_d_lt = NULL, genes = T)
dso$project(K)
dso$plot_projected("zero_distance", "zero_distance", use_dims = list(2:3))
```
### Solve deconvolution
```{r fig.height = 5, fig.width = 11}
iterations = 1500
lambda_term = 0
beta_term = 0
lr_x = 0.01
lr_omega = 0.01
dso$init_solution("random")
#dso$default_optimization() you always can try this method instead of tuning parameters yourself
dso$optim_solution(iterations,
optim_config(coef_hinge_H = lambda_term,
coef_hinge_W =beta_term,
coef_der_X = lr_x,
coef_der_Omega = lr_omega
))
dso$plot_projected("zero_distance", "zero_distance", use_dims = list(2:3))
```
### Prepare plot
```{r}
# Define where deconvolution term and hinge term intersected
st <- dso$st$solution_proj$optim_history$errors_statistics
step <- c(1:dim(st)[1])
st <- cbind( step, st)
error_plot <- plot_errors(dso$st$solution_proj, variables = c("deconv_error", "lamdba_error", "beta_error")) +
geom_line(size=1) + ggtitle("Errors")
if (sum(st[,'deconv_error'] < st[, 'lamdba_error'])) {
intersect_row <- st[st[,'deconv_error'] < st[, 'lamdba_error'],][1,]
intersect_x <- intersect_row['step']
intersect_y <- log10(intersect_row['lamdba_error'])
pos_arrow_start_x <- intersect_x + 200
pos_arrow_start_y <- intersect_y + 1
pos_text_x <- pos_arrow_start_x + 100
pos_text_y <- pos_arrow_start_y + 0.2
error_plot <- error_plot +
annotate("text", x=pos_text_x, y=pos_text_y, label="Meeting point",colour="black", hjust = 'left') +
geom_segment(aes(x = pos_arrow_start_x,
y = pos_arrow_start_y,
xend = intersect_x,
yend = intersect_y),
linetype=1, size=1, color = 'dodgerblue4', arrow=arrow(length=unit(0.30,"cm"),type='closed'))
}
```
```{r fig.height = 7, fig.width=12}
neg_basis_plot <- dso$plot_negative_basis_change()
neg_proportions_plot <- dso$plot_negative_proportions_change()
points_plot <- dso$plot_projected(use_dims=2:3, show_plots = F)
ggarrange(points_plot, error_plot, neg_basis_plot, neg_proportions_plot)
```
## Choice of coefficients for GSE11058
### Data
```{r}
set.seed(5)
library('GEOquery')
K <- 4
## Get the data
dataset <- "GSE11058"
gse <- getGEO(dataset, AnnotGPL = T)
gse <- gse[[1]]
data_raw <- Biobase::exprs(gse)
#linearize the data!
data_raw <- linearize_dataset(data_raw)
## Map probes
library(biomaRt)
library(matrixStats)
# probes which are present in data
probe_mapping <- biomaRt::select(hgu133plus2.db::hgu133plus2.db, rownames(data_raw), c("SYMBOL"), keytype = "PROBEID")
probe_mapping <- probe_mapping[probe_mapping$PROBEID %in% rownames(data_raw), ]
# not empty result gene id (now we many-to-many in in genes to probes)
probe_mapping <- probe_mapping[!is.na(probe_mapping$SYMBOL), ]
## want to collapse gene for each gene and take mean value of data
data_raw <- data_raw[probe_mapping$PROBEID,]
rownames(data_raw) <- probe_mapping$SYMBOL
# should be unique gene name as row name after this
data_raw <- tapply(data_raw, list(row.names(data_raw)[row(data_raw)], colnames(data_raw)[col(data_raw)]), FUN = median)
coding_info <- biomaRt::select(hgu133plus2.db::hgu133plus2.db, rownames(data_raw),keytype="SYMBOL", c("SYMBOL", "GENETYPE"))
# possible values for genetype "protein-coding" "ncRNA""pseudo""other" "unknown"
PROTEIN_CODING_GENES <- coding_info[coding_info$GENETYPE == 'protein-coding', 'SYMBOL']
length(PROTEIN_CODING_GENES)
print(paste("Dim for the whole data:", toString(dim(data_raw))))
print(paste("Dim for hidden proportions (H):", toString(dim(true_proportions))))
print(paste("Dim for hidden basis (W):", toString(dim(true_basis))))
```
### Create the object
```{r fig.height = 5, fig.width = 11}
dso <- DualSimplexSolver$new()
dso$set_data(data_raw, gene_anno_lists = list(CODING = PROTEIN_CODING_GENES))
dso$basic_filter( keep_true_cols = c("CODING"), remove_true_cols_default = c())
dso$project(K)
dso$basic_filter(log_mad_gt = 2.5)
dso$project(K)
dso$distance_filter(plane_d_lt = 0.19, zero_d_lt = 0.3, genes = T)
dso$project(K)
dso$run_umap(neighbors_X = 25, neighbors_Omega = 5)
dso$plot_projected("zero_distance", "zero_distance", use_dims = list(2:3))
dso$plot_projected("zero_distance", "zero_distance", use_dims = NULL)
```
### Solve deconvolution
```{r fig.height = 5, fig.width = 11}
iterations = 15000
lambda_term = 1
beta_term = 1
lr_x = 0.001
lr_omega = 0.001
dso$init_solution("random")
dso$optim_solution(iterations,
optim_config(coef_hinge_H = lambda_term,
coef_hinge_W =beta_term,
coef_der_X = lr_x,
coef_der_Omega = lr_omega
))
dso$plot_projected("zero_distance", "zero_distance", use_dims = NULL)
```
### Prepare plot
```{r}
# Define where deconvolution term and hinge term intersected
st <- dso$st$solution_proj$optim_history$errors_statistics
step <- c(1:dim(st)[1])
st <- cbind( step, st)
error_plot <- plot_errors(dso$st$solution_proj, variables = c("deconv_error", "lamdba_error", "beta_error")) +
geom_line(size=1) + ggtitle("Errors")
if (sum(st[,'deconv_error'] < st[, 'lamdba_error'])) {
intersect_row <- st[st[,'deconv_error'] < st[, 'lamdba_error'],][1,]
intersect_x <- intersect_row['step']
intersect_y <- log10(intersect_row['lamdba_error'])
pos_arrow_start_x <- intersect_x + 200
pos_arrow_start_y <- intersect_y + 1
pos_text_x <- pos_arrow_start_x + 100
pos_text_y <- pos_arrow_start_y + 0.2
error_plot <- error_plot +
annotate("text", x=pos_text_x, y=pos_text_y, label="Meeting point",colour="black", hjust = 'left') +
geom_segment(aes(x = pos_arrow_start_x,
y = pos_arrow_start_y,
xend = intersect_x,
yend = intersect_y),
linetype=1, size=1, color = 'dodgerblue4', arrow=arrow(length=unit(0.30,"cm"),type='closed'))
}
```
```{r fig.height = 7, fig.width=12}
neg_basis_plot <- dso$plot_negative_basis_change()
neg_proportions_plot <- dso$plot_negative_proportions_change()
points_plot <- dso$plot_projected(use_dims=NULL, show_plots = F)
ggarrange(points_plot, error_plot, neg_basis_plot, neg_proportions_plot)
```
## Initialization strategies
### Select X on 19830
```{r fig.height = 10, fig.width = 11}
dso <- DualSimplexSolver$new()
K <- 3
dso$set_data(data_gse19830, gene_anno_lists = list(CODING = MOUSE_PROTEIN_CODING_GENES))
dso$basic_filter( keep_true_cols = c("CODING"), remove_true_cols_default = c())
dso$basic_filter(remove_true_cols_default = c())
dso$project(K)
dso$basic_filter(log_mad_gt = 1)
dso$project(K)
dso$distance_filter(plane_d_lt = 0.015, zero_d_lt = NULL, genes = T)
dso$project(K)
## Use Omega points to initialize solution
dso$init_solution("select_omega")
plt_omega_init <- dso$plot_projected("black", "black", use_dims = list(2:3), show_plots = F)
## Use X points to initialize solution
dso$init_solution("select_x")
plt_x_init <- dso$plot_projected("black", "black", use_dims = list(2:3), show_plots = F)
ggarrange(plotlist =(c(plt_x_init, plt_omega_init)), nrow = 2, labels=c("Single side select X init", "Single side select Omega init"), label.x=0.6)
```
### Solve deconvolution
```{r fig.height = 5, fig.width = 11}
iterations = 1500
lambda_term = 1
beta_term = 1
lr_x = 0.01
lr_omega = 0.01
dso$optim_solution(iterations,
optim_config(coef_hinge_H = lambda_term,
coef_hinge_W =beta_term,
coef_der_X = lr_x,
coef_der_Omega = lr_omega
))
dso$plot_projected("zero_distance", "zero_distance", use_dims = list(2:3))
```
### Prepare plot
```{r}
# Define where deconvolution term and hinge term intersected
st <- dso$st$solution_proj$optim_history$errors_statistics
step <- c(1:dim(st)[1])
st <- cbind( step, st)
error_plot <- plot_errors(dso$st$solution_proj, variables = c("deconv_error", "lamdba_error", "beta_error")) +geom_line(size=1) + ggtitle("Errors")
```
```{r fig.height = 7, fig.width=12}
neg_basis_plot <- dso$plot_negative_basis_change()
neg_proportions_plot <- dso$plot_negative_proportions_change()
points_plot <- dso$plot_projected(use_dims=2:3, show_plots = F)
ggarrange(points_plot, error_plot, neg_basis_plot, neg_proportions_plot)
```