forked from software-github/SCRABBLE_PAPER
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanalysis_main.R
More file actions
525 lines (383 loc) · 16.3 KB
/
analysis_main.R
File metadata and controls
525 lines (383 loc) · 16.3 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# This is the main R file to generate the results related our manuscript
# figure 4AB and the supplementary figures related to figure 4AB.
# Please contact Tao Peng: pengt@email.chop.edu if you have any questions
# about the scripts or data
# load the libraries
library(dplyr)
library(vsn)
library(Seurat)
library(scater)
library(edgeR)
library(gridExtra)
library(R.matlab)
library(cowplot)
library(biomaRt)
library(data.table)
library(lattice)
library(scImpute)
library(SCRABBLE)
library(VennDiagram)
library(Rtsne)
library(DT)
library(ggpubr)
library(ggsignif)
library(scatterplot3d)
library(ggplot2)
library(reshape2)
library(ggfortify)
library(refGenome)
library(pheatmap)
library(RColorBrewer)
library(dendsort)
library(entropy)
library(DrImpute)
library(splatter)
library(RColorBrewer)
library(mcriPalettes)
library(plotly)
library(factoextra)
library(cluster)
library(NbClust)
library(fpc)
library(class)
source("analysis_library.R")
# Load the data
# ---------------------------------------------------------------------------------
# load the single cell RNAseq data, bulk RNAseq data, and the gene name information
data_sc_info <- read.table(file = "data/data_sc.txt",
fill = TRUE,
header = TRUE,
sep = "\t",
stringsAsFactors = FALSE)
# load the annotationn file
anno_temp <- read.table(file = "data/mouse_gene_id.txt",
header = FALSE,
sep = ",",
stringsAsFactors = FALSE)
anno <- data.frame(apply(anno_temp,2,function(x)gsub('\\s+', '',x)),stringsAsFactors = FALSE)
# load the bulk data
data_bulk_info <- read.table(file = "data/data_bulk.txt",
fill = TRUE,
header = TRUE,
sep = "\t",
stringsAsFactors = FALSE)
# ---------------------------------------------------------------------------------
# Preprocess the data
# ---------------------------------------------------------------------------------
# prepaare the data
data_sc_info_tmp <- cbind(id = rownames(data_sc_info),data_sc_info)
# make the annotation of genes
index <- match(rownames(data_sc_info),anno$V1)
gene_name <- anno[index,]
index <- match(gene_name$V1,rownames(data_sc_info))
# make the gene names
data_sc_tmp <- data_sc_info_tmp[index,]
data_sc_tmp <- cbind(gene_name,data_sc_tmp)
data_sc <- data_sc_tmp[rowSums(is.na(data_sc_tmp)) == 0,]
# extract the common genes
index <- match(data_bulk_infos$tracking_id, data_sc$V2)
gene_common <- data_sc$V2[index]
gene_common <- gene_common[!is.na(gene_common)]
# select the scRNAseq data
index <- match(gene_common,data_sc$V2)
data_sc_final <- data_sc[index,]
# select the bulk RNAseq data
index <- match(gene_common,data_info1$tracking_id)
data_bulk_final <- data_info1[index,]
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# select the bulk RNAseq, SCRB RNAseq data, Dropseq data, and gene names
# select the bulk RNAseq data
data_bulk_rep <- data.frame(rep1 = data_bulk_final$WT_ES, stringsAsFactors = FALSE)
# select the DropSeq scRNAseq data
col_name <- colnames(data_sc_final)
is.Dropseq <- grepl("^Drop", col_name)
data_sc_dropseq <- data_sc_final[,is.Dropseq]
# select the SCRB scRNAseq data
is.SCRB <- grepl("^SCRB", col_name)
data_sc_SCRBseq <- data_sc_final[,is.SCRB]
# select the gene names
gene_bulk_sc <- data_sc_final$V2
# ---------------------------------------------------------------------------------
# normalize by the library size
# ---------------------------------------------------------------------------------
# by the bulk RNAseq data
tmp <- rep.row(colSums(data_bulk_rep),dim(data_bulk_rep)[1])
data_bulk_rep_normalized <- 1e6*data_bulk_rep/tmp
# by the dropseq RNAseq data
tmp <- rep.row(colSums(data_sc_dropseq),dim(data_sc_dropseq)[1])
data_sc_dropseq_normalized <- 1e6*data_sc_dropseq/tmp
# by the scrb RNAseq data
tmp <- rep.row(colSums(data_sc_SCRBseq),dim(data_sc_SCRBseq)[1])
data_sc_SCRBseq_normalized <- 1e6*data_sc_SCRBseq/tmp
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# calculate the number cells of Dropseq RNAseq data and of the SCRB RNAseq data
n_drop <- dim(data_sc_dropseq_normalized)[2]
n_SCRB <- dim(data_sc_SCRBseq_normalized)[2]
# get the index of genes of Dropseq RNAseq data and of the SCRB RNAseq data without dropouts
tmp_drop <- rowSums((data_sc_dropseq_normalized) > 0) == n_drop
tmp_SCRB <- rowSums((data_sc_SCRBseq_normalized) > 0) == n_SCRB
# calculate the mean values of bulk RNAseq, Dropseq RNAseq, and SCRB RNAseq
means_drop <- rowMeans(data_sc_dropseq_normalized)
means_SCRB <- rowMeans(data_sc_SCRBseq_normalized)
means_bulk <- rowMeans(data_bulk_rep_normalized)
# get the common gene between the dropseq and scrb
tmp_index <- tmp_drop & tmp_SCRB
common_gene_single <- gene_bulk_sc[tmp_index]
# extract the common genes without dropouts only at SCRB RNAseq data
gene_only_SCRB <- gene_bulk_sc[tmp_SCRB]
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# calculate the normalized ratio between Dropseq and SCRB
x <- means_drop[tmp_index]
y <- means_SCRB[tmp_index]
c1 <- lm(x ~ y + 0)
# calculate the normalized ratio between Dropseq and bulk
x <- means_drop[tmp_index]
y <- means_bulk[tmp_index]
c2 <- lm(x ~ y + 0)
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# normalize the data
data_sc_SCRBseq_normalized <- c1$coefficients*data_sc_SCRBseq_normalized
data_bulk_rep_normalized <- c2$coefficients*data_bulk_rep_normalized
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# remove the genes with low expression values
rowMeans1 <- rowMeans(data_bulk_rep_normalized)
index <- rowMeans1 > 0.5
data_sc_dropseq_tmp <- data_sc_dropseq_normalized[index,]
data_sc_SCRB_tmp <- data_sc_SCRBseq_normalized[index,]
gene_list <- gene_bulk_sc[index]
data_bulk_tmp <- rowMeans1[index]
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# remove the mt genes
is.mito <- grepl("^mt", gene_list)
gene_list <- gene_list[which(!is.mito)]
data_sc_dropseq_tmp <- data_sc_dropseq_tmp[which(!is.mito),]
data_sc_SCRB_tmp <- data_sc_SCRB_tmp[which(!is.mito),]
data_bulk_tmp <- data_bulk_tmp[which(!is.mito)]
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# calculate the variance data of dropseq data
tmp <- rowVars(as.matrix(data_sc_dropseq_tmp), suma = NULL, std = FALSE)
tmp[is.na(tmp)] <- 0
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# remove the genes with low variances
index <- which(tmp > 1e-2)
gene_list <- gene_list[index]
data_sc_dropseq <- data_sc_dropseq_tmp[index,]
data_sc_scrb <- data_sc_scrb_tmp[index,]
data_bulk <- data_bulk_tmp[index]
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# save the data
saveRDS(gene_list, file = "/data_all/impute_gene.rds")
saveRDS(gene_only_SCRB, file = "/data_all/gene_only_SCRB.rds")
write.table(data_sc_dropseq,
file = paste0("/data_all/sc_data_ES.csv"),
sep=',',row.names = FALSE, col.names = FALSE)
write.table(data_sc_scrb,
file = paste0("/data_all/sc_data_ES_SCRB.csv"),
sep=',',row.names = FALSE, col.names = FALSE)
write.table(data_bulk,
file = paste0("/data_all/bulk_data_ES.csv"),
sep=',',row.names = FALSE, col.names = FALSE)
# ---------------------------------------------------------------------------------
# Run DrImpute
# ---------------------------------------------------------------------------------
dir.create(file.path("imputation_data/"),
showWarnings = FALSE)
data_sc <- as.matrix(fread("/data_all/sc_data_ES.csv"))
extdata <- DrImpute(data_sc)
saveRDS(extdata, file = "imputation_data/data_drimpute_imputation.rds")
# ---------------------------------------------------------------------------------
# Run scImpute
# ---------------------------------------------------------------------------------
# create the folder of the imputation result
dir.create(file.path("imputation_data/"),
showWarnings = FALSE)
# load the data
data_sc <- as.matrix(fread("/data_all/sc_data_ES.csv"))
write.table(data_sc, file = "/data_all/sc_data_ES_scimpute.csv", sep = ",")
scimpute(
"/data_all/sc_data_ES_scimpute.csv",
infile = "csv",
outfile = "csv",
out_dir = paste0("imputation_data/"),
drop_thre = 0.5,
Kcluster = 2,
ncores = 2)
# move all data to the data folder
tmp1 <- read.table( file = "scimpute_count.csv" , header = TRUE, sep=",")
tmp1$X <- NULL
tmp1[is.na(tmp1)] <- 0
# save the data
write.table(tmp1, file = "imputation_data/data_scimpute_imputation.csv",
sep=',',row.names = FALSE, col.names = FALSE)
# ---------------------------------------------------------------------------------
# Run MAGIC
# ---------------------------------------------------------------------------------
# cwd = os.getwd()
#
# if not os.path.exists(cwd+"/imputation_data"):
# os.makedirs(cwd+"/imputation_data")
#
#
# X = pd.read_csv(cwd + "/data_all/sc_data_ES.csv",sep = ',',header=None)
#
# magic_operator = magic.MAGIC(n_pca = 76)
#
# X_magic = magic_operator.fit_transform(X.T)
#
# out_magic = X_magic.T
#
# out_magic.to_csv(cwd+"/magic_data/data_imputation.csv", sep = ',', header= None)
# ---------------------------------------------------------------------------------
# Run VIPER
# ---------------------------------------------------------------------------------
dir.create(file.path("imputation_data/"),
showWarnings = FALSE)
data_sc <- as.matrix(fread("/data_all/sc_data_ES.csv"))
extdata <- VIPER(as.matrix(data_sc), num = 5000, percentage.cutoff = 0.1, minbool = FALSE, alpha = 1,
report = FALSE, outdir = NULL, prefix = NULL)
saveRDS(extdata$imputed, file = "imputation_data/data_viper_imputation.rds")
# ---------------------------------------------------------------------------------
# Run SCRABBLE
# ---------------------------------------------------------------------------------
# create the folder
dir.create(file.path("/imputation_data/"),
showWarnings = FALSE)
# load the scRNAseq data
data_sc <- as.matrix(fread(paste0(cwd,"/data_all/sc_data_ES.csv")))
# load the bulk RNAseq data
data_bulk <- as.matrix(fread(paste0(cwd,"/data_all/bulk_data_ES.csv")))
# prepare the data
data <- list()
data[[1]] <- as.matrix(data_sc)
data[[2]] <- as.matrix(data_bulk)
# set up the parameter
parameter <- c(1,1e-7,1e-5)
# run the SCRABBLE
result <- scrabble(data,
parameter = parameter,
nIter = 30,
error_out_threshold = 1e-14,
nIter_inner = 30,
error_inner_threshold = 1e-14)
# save the result
saveRDS(result, file = "/imputation_data/data_scrabble_imputation.rds")
# ---------------------------------------------------------------------------------
# Assemble all data: raw data, gold standard data, imputed data
# ---------------------------------------------------------------------------------
# load DropSeq data
data_sc_Dropseq <- read.table(file = "data_all/sc_data_ES.csv",
fill = TRUE, header = FALSE, sep = ",",
stringsAsFactors = FALSE)
data_sc_Dropseq <- log10(as.matrix(data_sc_Dropseq) + 1)
# load SCRB RNAseq data
data_sc_SCRBseq <- read.table(file = "data_all/sc_data_ES_SCRB.csv",
fill = TRUE, header = FALSE, sep = ",",
stringsAsFactors = FALSE)
data_sc_SCRBseq <- log10(as.matrix(data_sc_SCRBseq) + 1)
# load bulk RNAseq data
data_sc_bulk <- read.table(file = "data_all/bulk_data_ES.csv",
fill = TRUE, header = FALSE, sep = ",",
stringsAsFactors = FALSE)
data_sc_bulk <- log10(as.matrix(data_sc_bulk) + 1)
# load the imputed data of DrImpute
data_sc_drimpute <- readRDS(file = "imputation_data/data_drimpute_imputation.rds")
data_sc_drimpute <- log10(data_sc_drimpute + 1)
# load the imputed data of scImpute
data_sc_scimpute <- read.table(file = "imputation_data/data_scimpute_imputation.csv",
fill = TRUE, header = FALSE, sep = ",",
stringsAsFactors = FALSE)
data_sc_scimpute <- log10(data_sc_scimpute + 1)
# load the imputed data of MAGIC
data_sc_magic <- read.table(file = "imputation_data/data_magic_imputation.csv",
header = FALSE, sep = ",",
stringsAsFactors = FALSE)
data_sc_magic <- data_sc_magic[,-1]
data_sc_magic <- log10(data_sc_magic + 1)
# load the imputed data of VIPER
data_sc_viper <- readRDS(file = "imputation_data/data_viper_imputation.rds")
data_sc_viper <- log10(data_sc_viper + 1)
# load the imputed data of SCRABBLE
data_sc_scrabble <- readRDS(file = "imputation_data/data_scrabble_imputation.rds")
data_sc_scrabble <- log10(data_sc_scrabble + 1)
# load the gene lists
gene_filter <- readRDS(file = "/data_all/impute_gene.rds")
gene_only_SCRB <- readRDS(file = "/data_all/gene_only_SCRB.rds")
# combine all data
data_de <- cbind(data_sc_SCRBseq,
data_sc_Dropseq,
data_sc_drimpute,
data_sc_scimpute,
data_sc_magic,
data_sc_viper,
data_sc_scrabble)
# define cell types
dataType <- c(rep(1,dim(data_sc_Dropseq)[2]),
rep(2,dim(data_sc_SCRBseq)[2]),
rep(3,dim(data_sc_Dropseq)[2]),
rep(4,dim(data_sc_Dropseq)[2]),
rep(5,dim(data_sc_Dropseq)[2]),
rep(6,dim(data_sc_Dropseq)[2]),
rep(7,dim(data_sc_Dropseq)[2]))
# ---------------------------------------------------------------------------------
# Plot the boxplot for the comparison
# ---------------------------------------------------------------------------------
p <- performance_comparison(gene_only_SCRB, gene_filter, 22, data_de)
ggsave(
filename = paste0("Figure_performance_59_dropout_29.pdf"),
plot = p,
width = 4,
height = 4
)
p <- performance_comparison(gene_only_SCRB, gene_filter, 30, data_de)
ggsave(
filename = paste0("Figure_performance_59_dropout_29.pdf"),
plot = p,
width = 4,
height = 4
)
# ---------------------------------------------------------------------------------
# Plot the distribution comparison
# ---------------------------------------------------------------------------------
# plot the distribution
p <- performance_distribution_comparison(gene_only_SCRB, gene_filter, 22, data_de)
# Save the PDF of the plot of the distribution of the first 20 genes
main <- grid.arrange(grobs = p[c(1:20)],ncol = 4, top = "main")
ggsave(
filename = paste0("Figure_gene_performance_compare_1.pdf"),
plot = main,
width = 14,
height = 20
)
# Save the PDF of the plot of the distribution of the next 20 genes
main <- grid.arrange(grobs = p[c(21:40)],ncol = 4, top = "main")
ggsave(
filename = paste0("Figure_gene_performance_compare_2.pdf"),
plot = main,
width = 14,
height = 20
)
# Save the PDF of the plot of the distribution of the final 16 genes
main <- grid.arrange(grobs = p[c(41:56)],ncol = 4, top = "main")
ggsave(
filename = paste0("Figure_gene_performance_compare_3.pdf"),
plot = main,
width = 14,
height = 20
)
# performance
p <- performance_mean(gene_only_SCRB,gene_filter,22, data_sc_Dropseq, data_de)
ggsave(
filename = paste0("performance_59_dropout_29_mean.pdf"),
plot = p,
width = 6,
height = 4
)