-
Notifications
You must be signed in to change notification settings - Fork 1
/
scmap-index-cell.R
executable file
·115 lines (99 loc) · 3.23 KB
/
scmap-index-cell.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
#!/usr/bin/env Rscript
# Create an index for a dataset to enable fast approximate nearest neighbour search.
# 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 = "singleCellExperiment object containing expression values and experimental information. Must have been appropriately prepared."
),
make_option(
c("-m", "--number-chunks"),
action = "store",
default = NULL,
type = 'numeric',
help = 'Number of chunks into which the expr matrix is split.'
),
make_option(
c("-k", "--number-clusters"),
action = "store",
default = NULL,
type = 'numeric',
help = 'Number of clusters per group for k-means clustering.'
),
make_option(
c("-f", "--train-id"),
action = "store",
default = NA,
type = 'character',
help = 'ID of the training dataset (optional)'
),
make_option(
c("-e", "--remove-mat"),
action = "store_true",
default = FALSE,
type = 'logical',
help = 'Should expression data be removed from index object? Default: FALSE'
),
make_option(
c("-r", "--random-seed"),
action = "store",
default = NULL,
type = 'numeric',
help = 'Set random seed to make scmap-cell reproducible.'
),
make_option(
c("-o", "--output-object-file"),
action = "store",
default = NA,
type = 'character',
help = "File name in which to store serialized R object of type 'SingleCellExperiment'."
)
)
opt <- wsc_parse_args(option_list, mandatory = c('input_object_file', 'output_object_file'))
if (!is.null(opt$number_chunks) && opt$number_chunks == 0){
opt$number_chunks = NULL
}
if (!is.null(opt$number_clusters) && opt$number_clusters == 0){
opt$number_clusters = NULL
}
# Once arguments are satisfcatory, load scmap package
suppressPackageStartupMessages(require(scmap))
suppressPackageStartupMessages(require(SingleCellExperiment))
# Check parameter values defined
if ( ! file.exists(opt$input_object_file)){
stop((paste('File object or matrix', opt$input_object_file, 'does not exist')))
}
# Read R object
SingleCellExperiment <- readRDS(opt$input_object_file)
# Set random seed
if (is.null(opt$random_seed)){
set.seed(1)
}
# Run indexing function
SingleCellExperiment <- indexCell(SingleCellExperiment, M = opt$number_chunks,
k = opt$number_clusters)
# add dataset field to the SingleCellExperiment object
if(!is.na(opt$train_id)){
attributes(SingleCellExperiment)$dataset = opt$train_id
} else{
attributes(SingleCellExperiment)$dataset = NA
}
# Remove expression matrix, if specified
if(opt$remove_mat){
for(assay_type in c('counts', 'normcounts', 'logcounts')){
if(assay_type %in% names(assays(SingleCellExperiment))){
assays(SingleCellExperiment)[assay_type] = NULL
}
}
}
# Print introspective information
cat(capture.output(SingleCellExperiment), sep='\n')
# Output to a serialized R object
saveRDS(SingleCellExperiment, file = opt$output_object_file)