-
Notifications
You must be signed in to change notification settings - Fork 5
/
seurat-dim-plot.R
executable file
·195 lines (174 loc) · 5.14 KB
/
seurat-dim-plot.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
#!/usr/bin/env Rscript
# Load optparse we need to check inputs
suppressPackageStartupMessages(require(optparse))
# Load common functions
suppressPackageStartupMessages(require(workflowscriptscommon))
# parse options
option_list = list(
make_option(
c("-i", "--input-object-file"),
action = "store",
default = NA,
type = 'character',
help = "File name in which a serialized R matrix object may be found."
),
make_option(
c("--input-format"),
action = "store",
default = "seurat",
type = 'character',
help = "Either loom, seurat, anndata or singlecellexperiment for the input format to read."
),
make_option(
c("-r", "--reduction-use"),
action = "store",
default = NA,
type = 'character',
help = 'Which dimensionality reduction to use. Default is "umap", can also be "tsne", or "pca", assuming these are precomputed.'
),
make_option(
c("-a", "--dim-1"),
action = "store",
default = 1,
type = 'integer',
help = "Dimension for x-axis (default 1)"
),
make_option(
c("-b", "--dim-2"),
action = "store",
default = 2,
type = 'integer',
help = "Dimension for y-axis (default 2)"
),
make_option(
c("-c", "--cells-use"),
action = "store",
default = NULL,
type = 'character',
help = "File to be used to derive a vector of cells to plot (default is all cells)."
),
make_option(
c("-p", "--pt-size"),
action = "store",
default = 1,
type = 'integer',
help = "Adjust point size for plotting"
),
make_option(
c("-l", "--label-size"),
action = "store",
default = 4,
type = 'integer',
help = "Sets size of labels."
),
make_option(
c("-d", "--do-label"),
action = "store",
default = FALSE,
type = 'logical',
help = "Whether to label the clusters."
),
make_option(
c("--repel"),
action = "store",
default = FALSE,
type = 'logical',
help = "Repel labels."
),
make_option(
c("-f", "--group-by"),
action = "store",
default = 'ident',
type = 'character',
help = "Group (color) cells in different ways (for example, orig.ident)."
),
make_option(
c("-u", "--cols-use"),
action = "store",
default = NULL,
type = 'character',
help = "Comma-separated list of colors, each color corresponds to an identity class. By default, ggplot assigns colors."
),
make_option(
c("-e", "--pt-shape"),
action = "store",
default = NULL,
type = 'character',
help = "If NULL, all points are circles (default). You can specify any cell attribute (that can be pulled with FetchData) allowing for both different colors and different shapes on cells."
),
make_option(
c("-q", "--plot-order"),
action = "store",
default = NULL,
type = 'character',
help = "Comma-separated string specifying the order of plotting for the idents (clusters). This can be useful for crowded plots if points of interest are being buried. Provide either a full list of valid idents or a subset to be plotted last (on top).."
),
make_option(
c("-w", "--png-width"),
action = "store",
default = 1000,
type = 'integer',
help = "Width of png (px)."
),
make_option(
c("-j", "--png-height"),
action = "store",
default = 1000,
type = 'integer',
help = "Height of png file (px)."
),
make_option(
c("-o", "--output-image-file"),
action = "store",
default = NA,
type = 'character',
help = "File name in which to save the PCA image"
)
)
opt <- wsc_parse_args(option_list, mandatory = c('input_object_file', 'output_image_file'))
# Check parameter values
if ( ! file.exists(opt$input_object_file)){
stop((paste('File', opt$input_object_file, 'does not exist')))
}
# Now we're hapy with the arguments, load Seurat and do the work
suppressPackageStartupMessages(require(Seurat))
if(opt$input_format == "loom" ) {
suppressPackageStartupMessages(require(SeuratDisk))
} else if(opt$input_format == "singlecellexperiment" ) {
suppressPackageStartupMessages(require(scater))
}
# Input from serialized R object
seurat_object <- read_seurat4_object(input_path = opt$input_object_file, format = opt$input_format)
# Read cells file (if present)
if (! is.null(opt$cells_use)){
if (! file.exists(opt$cells_use)){
stop((paste('Supplied genes file', opt$cells_use, 'does not exist')))
}else{
cells_use <- readLines(opt$cells_use)
}
}else{
cells_use <- NULL
}
# Parse color list (if present)
cols_use <- opt$cols_use
if (! is.null(cols_use)){
cols_use <- wsc_split_string(cols_use)
}
# Parse plot order ident list (if present)
plot_order <- opt$plot_order
if (! is.null(plot_order)){
plot_order <- wsc_split_string(plot_order)
}
# Open the image
png(filename = opt$output_image_file, width = opt$png_width, height = opt$png_height)
DimPlot(seurat_object, reduction = opt$reduction_use,
dims = c(opt$dim_1, opt$dim_2),
cells = cells_use,
pt.size = opt$pt_size,
label.size = opt$label_size,
label = opt$do_label,
repel = opt$repel,
group.by = opt$group_by,
cols=cols_use, shape.by=opt$pt_shape,
order=plot_order)
dev.off()