-
Notifications
You must be signed in to change notification settings - Fork 0
/
sc_rna_cluster.R
541 lines (521 loc) · 21.5 KB
/
sc_rna_cluster.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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#!/usr/bin/env Rscript
options(warn=-1)
options("width"=200)
options(error=function(){traceback(3); quit(save="no", status=1, runLast=FALSE)})
suppressMessages(library(Seurat))
suppressMessages(library(Signac))
suppressMessages(library(modules))
suppressMessages(library(argparse))
HERE <- (function() {return (dirname(sub("--file=", "", commandArgs(trailingOnly=FALSE)[grep("--file=", commandArgs(trailingOnly=FALSE))])))})()
suppressMessages(analyses <- modules::use(file.path(HERE, "modules/analyses.R")))
suppressMessages(debug <- modules::use(file.path(HERE, "modules/debug.R")))
suppressMessages(graphics <- modules::use(file.path(HERE, "modules/graphics.R")))
suppressMessages(io <- modules::use(file.path(HERE, "modules/io.R")))
suppressMessages(prod <- modules::use(file.path(HERE, "modules/prod.R")))
suppressMessages(ucsc <- modules::use(file.path(HERE, "modules/ucsc.R")))
export_all_clustering_plots <- function(seurat_data, args){
Idents(seurat_data) <- "new.ident" # safety measure
downsampled_to <- analyses$get_min_ident_size(SplitObject(seurat_data, split.by="new.ident")) # need to split it for consistency
downsampled_data <- subset(seurat_data, downsample=downsampled_to)
for (i in 1:length(args$resolution)) {
current_resolution <- args$resolution[i]
graphics$dim_plot(
data=seurat_data,
reduction="rnaumap",
plot_title=paste("Clustered cells UMAP. Resolution", current_resolution),
legend_title="Cluster",
group_by=paste("rna_res", current_resolution, sep="."),
label=TRUE,
label_color="black",
palette_colors=graphics$D40_COLORS,
theme=args$theme,
rootname=paste(args$output, "umap_res", current_resolution, sep="_"),
pdf=args$pdf
)
graphics$silhouette_plot(
data=seurat_data,
reduction="pca",
dims=args$dimensions,
downsample=500,
plot_title=paste("Silhouette scores. Downsampled to max 500 cells per cluster. Resolution", current_resolution),
legend_title="Cluster",
group_by=paste("rna_res", current_resolution, sep="."),
palette_colors=graphics$D40_COLORS,
theme=args$theme,
rootname=paste(args$output, "slh_res", current_resolution, sep="_"),
pdf=args$pdf
)
if (length(unique(as.vector(as.character(Idents(seurat_data))))) > 1){
graphics$dim_plot(
data=seurat_data,
reduction="rnaumap",
plot_title=paste("Split by dataset clustered cells UMAP. Resolution", current_resolution),
legend_title="Cluster",
group_by=paste("rna_res", current_resolution, sep="."),
split_by="new.ident",
label=TRUE,
label_color="black",
palette_colors=graphics$D40_COLORS,
theme=args$theme,
rootname=paste(args$output, "umap_spl_idnt_res", current_resolution, sep="_"),
pdf=args$pdf
)
graphics$composition_plot(
data=downsampled_data,
plot_title=paste(
"Grouped by cluster split by dataset cells composition plot.",
"Downsampled to", downsampled_to, "cells per dataset.",
"Resolution", current_resolution),
legend_title="Cluster",
group_by=paste("rna_res", current_resolution, sep="."),
split_by="new.ident",
x_label="Dataset",
y_label="Cells percentage",
palette_colors=graphics$D40_COLORS,
theme=args$theme,
rootname=paste(args$output, "cmp_gr_clst_spl_idnt_res", current_resolution, sep="_"),
pdf=args$pdf
)
graphics$composition_plot(
data=downsampled_data,
plot_title=paste(
"Grouped by dataset split by cluster cells composition plot.",
"Downsampled to", downsampled_to, "cells per dataset.",
"Resolution", current_resolution),
legend_title="Dataset",
group_by="new.ident",
split_by=paste("rna_res", current_resolution, sep="."),
x_label="Cluster",
y_label="Cells percentage",
palette_colors=graphics$D40_COLORS,
theme=args$theme,
rootname=paste(args$output, "cmp_gr_idnt_spl_clst_res", current_resolution, sep="_"),
pdf=args$pdf
)
}
if (all(as.vector(as.character([email protected]$new.ident)) != as.vector(as.character([email protected]$condition)))){
graphics$dim_plot(
data=seurat_data,
reduction="rnaumap",
plot_title=paste("Split by grouping condition clustered cells UMAP. Resolution", current_resolution),
legend_title="Cluster",
group_by=paste("rna_res", current_resolution, sep="."),
split_by="condition",
label=TRUE,
label_color="black",
palette_colors=graphics$D40_COLORS,
theme=args$theme,
rootname=paste(args$output, "umap_spl_cnd_res", current_resolution, sep="_"),
pdf=args$pdf
)
graphics$composition_plot(
data=downsampled_data,
plot_title=paste(
"Grouped by cluster split by condition cells composition plot.",
"Downsampled to", downsampled_to, "cells per dataset.",
"Resolution", current_resolution
),
legend_title="Cluster",
group_by=paste("rna_res", current_resolution, sep="."),
split_by="condition",
x_label="Condition",
y_label="Cells percentage",
palette_colors=graphics$D40_COLORS,
theme=args$theme,
rootname=paste(args$output, "cmp_gr_clst_spl_cnd_res", current_resolution, sep="_"),
pdf=args$pdf
)
graphics$composition_plot(
data=downsampled_data,
plot_title=paste(
"Grouped by condition split by cluster cells composition plot.",
"Downsampled to", downsampled_to, "cells per dataset.",
"Resolution", current_resolution
),
legend_title="Condition",
group_by="condition",
split_by=paste("rna_res", current_resolution, sep="."),
x_label="Cluster",
y_label="Cells percentage",
palette_colors=graphics$D40_COLORS,
theme=args$theme,
rootname=paste(args$output, "cmp_gr_cnd_spl_clst_res", current_resolution, sep="_"),
pdf=args$pdf
)
}
if ("Phase" %in% colnames([email protected])){
graphics$dim_plot(
data=seurat_data,
reduction="rnaumap",
plot_title=paste("Split by cell cycle phase clustered cells UMAP. Resolution", current_resolution),
legend_title="Cluster",
group_by=paste("rna_res", current_resolution, sep="."),
split_by="Phase",
label=TRUE,
label_color="black",
alpha=0.5,
palette_colors=graphics$D40_COLORS,
theme=args$theme,
rootname=paste(args$output, "umap_spl_ph_res", current_resolution, sep="_"),
pdf=args$pdf
)
if (length(unique(as.vector(as.character(Idents(seurat_data))))) > 1){
graphics$composition_plot(
data=downsampled_data,
plot_title=paste(
"Grouped by cell cycle phase split by dataset cells composition plot.",
"Downsampled to", downsampled_to, "cells per dataset.",
"Resolution", current_resolution),
legend_title="Phase",
group_by="Phase",
split_by="new.ident",
x_label="Dataset",
y_label="Cells percentage",
palette_colors=graphics$D40_COLORS,
theme=args$theme,
rootname=paste(args$output, "cmp_gr_ph_spl_idnt_res", current_resolution, sep="_"),
pdf=args$pdf
)
}
graphics$composition_plot(
data=downsampled_data,
plot_title=paste(
"Grouped by cell cycle phase split by cluster cells composition plot.",
"Downsampled to", downsampled_to, "cells per dataset.",
"Resolution", current_resolution),
legend_title="Phase",
group_by="Phase",
split_by=paste("rna_res", current_resolution, sep="."),
x_label="Cluster",
y_label="Cells percentage",
palette_colors=graphics$D40_COLORS,
theme=args$theme,
rootname=paste(args$output, "cmp_gr_ph_spl_clst_res", current_resolution, sep="_"),
pdf=args$pdf
)
}
}
rm(downsampled_data)
gc(verbose=FALSE)
}
export_all_expression_plots <- function(seurat_data, args) {
SeuratObject::DefaultAssay(seurat_data) <- "RNA" # safety measure
SeuratObject::Idents(seurat_data) <- "new.ident" # safety measure
for (i in 1:length(args$resolution)) {
current_resolution <- args$resolution[i]
Idents(seurat_data) <- paste("rna_res", current_resolution, sep=".")
graphics$dot_plot(
data=seurat_data,
features=args$genes,
plot_title=paste("Log normalized scaled average gene expression per cluster. Resolution", current_resolution),
x_label="Genes",
y_label="Clusters",
cluster_idents=FALSE,
theme=args$theme,
rootname=paste(args$output, "xpr_avg_res", current_resolution, sep="_"),
pdf=args$pdf
)
if (length(args$genes) > 0){
for (i in 1:length(args$genes)) {
current_gene <- args$genes[i]
graphics$feature_plot(
data=seurat_data,
features=current_gene,
labels=current_gene,
reduction="rnaumap",
plot_title=paste("Log normalized gene expression on cells UMAP. Resolution", current_resolution),
label=TRUE,
order=TRUE,
max_cutoff="q99", # to prevent cells with overexpressed gene from distoring the color bar
combine_guides="keep",
width=800,
height=800,
theme=args$theme,
rootname=paste(args$output, "xpr_per_cell_res", current_resolution, current_gene, sep="_"),
pdf=args$pdf
)
graphics$expression_density_plot(
data=seurat_data,
features=current_gene,
reduction="rnaumap",
plot_title=paste("Log normalized gene expression density on cells UMAP. Resolution", current_resolution),
joint=FALSE,
width=800,
height=800,
theme=args$theme,
rootname=paste(args$output, "xpr_per_cell_sgnl_res", current_resolution, current_gene, sep="_"),
pdf=args$pdf
)
graphics$vln_plot(
data=seurat_data,
features=current_gene,
labels=current_gene,
plot_title=paste("Log normalized gene expression density per cluster. Resolution", current_resolution),
legend_title="Cluster",
log=TRUE,
pt_size=0,
combine_guides="collect",
width=800,
height=600,
palette_colors=graphics$D40_COLORS,
theme=args$theme,
rootname=paste(args$output, "xpr_dnst_res", current_resolution, current_gene, sep="_"),
pdf=args$pdf
)
}
}
}
SeuratObject::Idents(seurat_data) <- "new.ident" # safety measure
}
get_args <- function(){
parser <- ArgumentParser(description="Single-cell RNA-Seq Cluster Analysis")
parser$add_argument(
"--query",
help=paste(
"Path to the RDS file to load Seurat object from. This file should include genes",
"expression information stored in the RNA assay, as well as 'pca' and 'rnaumap'",
"dimensionality reductions applied to that assay."
),
type="character", required="True"
)
parser$add_argument(
"--dimensions",
help=paste(
"Dimensionality to use when constructing nearest-neighbor graph before clustering",
"(from 1 to 50). If single value N is provided, use from 1 to N dimensions. If",
"multiple values are provided, subset to only selected dimensions.",
"Default: from 1 to 10"
),
type="integer", default=10, nargs="*"
)
parser$add_argument(
"--ametric",
help=paste(
"Distance metric used when constructing nearest-neighbor graph before clustering.",
"Default: euclidean"
),
type="character", default="euclidean",
choices=c(
"euclidean", "cosine", "manhattan", "hamming"
)
)
parser$add_argument(
"--algorithm",
help=paste(
"Algorithm for modularity optimization when running clustering.",
"Default: louvain"
),
type="character", default="louvain",
choices=c(
"louvain", "mult-louvain", "slm", "leiden"
)
)
parser$add_argument(
"--resolution",
help=paste(
"Clustering resolution applied to the constructed nearest-neighbor graph.",
"Can be set as an array.",
"Default: 0.3, 0.5, 1.0"
),
type="double", default=c(0.3, 0.5, 1.0), nargs="*"
)
parser$add_argument(
"--genes",
help=paste(
"Genes of interest to build genes expression plots.",
"Default: None"
),
type="character", nargs="*"
)
parser$add_argument(
"--diffgenes",
help=paste(
"Identify differentially expressed genes (putative gene markers) between each",
"pair of clusters for all resolutions.",
"Default: false"
),
action="store_true"
)
parser$add_argument(
"--logfc",
help=paste(
"For putative gene markers identification include only those genes that",
"on average have log fold change difference in expression between every",
"tested pair of clusters not lower than this value. Ignored if '--diffgenes'",
"is not set.",
"Default: 0.25"
),
type="double", default=0.25
)
parser$add_argument(
"--minpct",
help=paste(
"For putative gene markers identification include only those genes that",
"are detected in not lower than this fraction of cells in either of the",
"two tested clusters. Ignored if '--diffgenes' is not set.",
"Default: 0.1"
),
type="double", default=0.1
)
parser$add_argument(
"--onlypos",
help=paste(
"For putative gene markers identification return only positive markers.",
"Ignored if '--diffgenes' is not set.",
"Default: false"
),
action="store_true"
)
parser$add_argument(
"--testuse",
help=paste(
"Statistical test to use for putative gene markers identification.",
"Ignored if '--diffgenes' is not set.",
"Default: wilcox"
),
type="character", default="wilcox",
choices=c("wilcox", "bimod", "roc", "t", "negbinom", "poisson", "LR", "MAST", "DESeq2")
)
parser$add_argument(
"--pdf",
help="Export plots in PDF. Default: false",
action="store_true"
)
parser$add_argument(
"--verbose",
help="Print debug information. Default: false",
action="store_true"
)
parser$add_argument(
"--h5seurat",
help="Save Seurat data to h5seurat file. Default: false",
action="store_true"
)
parser$add_argument(
"--h5ad",
help="Save Seurat data to h5ad file. Default: false",
action="store_true"
)
parser$add_argument(
"--cbbuild",
help="Export results to UCSC Cell Browser. Default: false",
action="store_true"
)
parser$add_argument(
"--output",
help="Output prefix. Default: ./sc",
type="character", default="./sc"
)
parser$add_argument(
"--theme",
help=paste(
"Color theme for all generated plots.",
"Default: classic"
),
type="character", default="classic",
choices=c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void")
)
parser$add_argument(
"--cpus",
help="Number of cores/cpus to use. Default: 1",
type="integer", default=1
)
parser$add_argument(
"--memory",
help=paste(
"Maximum memory in GB allowed to be shared between the workers",
"when using multiple --cpus.",
"Default: 32"
),
type="integer", default=32
)
args <- parser$parse_args(commandArgs(trailingOnly = TRUE))
return (args)
}
args <- get_args()
print("Input parameters")
print(args)
if (length(args$dimensions) == 1) {
print("Adjusting --dimensions parameter as only a single value was provided")
args$dimensions <- c(1:args$dimensions[1])
print(paste("--dimensions was adjusted to", paste(args$dimensions, collapse=", ")))
}
print(
paste(
"Setting parallelization to", args$cpus, "cores, and", args$memory,
"GB of memory allowed to be shared between the processes"
)
)
prod$parallel(args)
print(paste("Loading Seurat data from", args$query))
seurat_data <- readRDS(args$query)
print("Setting default assay to RNA")
DefaultAssay(seurat_data) <- "RNA"
debug$print_info(seurat_data, args)
if (!all(c("pca", "rnaumap") %in% names(seurat_data@reductions))){
print("Loaded Seurat object doesn't have 'pca' and/or 'rnaumap' reduction(s). Exiting.")
quit(save="no", status=1, runLast=FALSE)
}
print(paste("Clustering RNA data using", paste(args$dimensions, collapse=", "), "principal components"))
seurat_data <- analyses$add_clusters(
seurat_data=seurat_data,
assay="RNA",
graph_name="rna", # will be used in all the plot generating functions
reduction="pca",
args=args
)
debug$print_info(seurat_data, args)
export_all_clustering_plots(
seurat_data=seurat_data,
args=args
)
if (!is.null(args$genes)){
print("Adjusting genes of interest to include only those that are present in the loaded Seurat object")
args$genes <- unique(args$genes)
args$genes <- args$genes[args$genes %in% as.vector(as.character(rownames(seurat_data)))] # with RNA assay set as default the rownames should be genes
print(args$genes)
}
if(args$cbbuild){
print("Exporting RNA assay to UCSC Cellbrowser")
ucsc$export_cellbrowser(
seurat_data=seurat_data,
assay="RNA",
slot="counts",
short_label="RNA",
features=args$genes, # can be NULL
rootname=paste(args$output, "_cellbrowser", sep=""),
)
}
if (!is.null(args$genes) || args$diffgenes) {
print("Normalizing counts in RNA assay before evaluating genes expression or identifying putative gene markers")
DefaultAssay(seurat_data) <- "RNA"
seurat_data <- NormalizeData(seurat_data, verbose=FALSE)
if (!is.null(args$genes)){
print("Generating genes expression plots")
export_all_expression_plots(seurat_data=seurat_data, args=args)
}
if(args$diffgenes){
print("Identifying differentially expressed genes between each pair of clusters for all resolutions")
all_putative_markers <- analyses$get_putative_markers(
seurat_data=seurat_data,
assay="RNA",
resolution_prefix="rna_res",
args=args
)
io$export_data(
all_putative_markers,
paste(args$output, "_gene_markers.tsv", sep="")
)
}
}
DefaultAssay(seurat_data) <- "RNA" # better to stick to RNA assay by default https://www.biostars.org/p/395951/#395954
print("Exporting results to RDS file")
io$export_rds(seurat_data, paste(args$output, "_data.rds", sep=""))
if(args$h5seurat){
print("Exporting results to h5seurat file")
io$export_h5seurat(seurat_data, paste(args$output, "_data.h5seurat", sep=""))
}
if(args$h5ad){
print("Exporting results to h5ad file")
io$export_h5ad(seurat_data, paste(args$output, "_data.h5ad", sep=""))
}