forked from satijalab/seurat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_determination.R
275 lines (251 loc) · 10.8 KB
/
cluster_determination.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
#' @include seurat.R
NULL
#' Cluster Determination
#'
#' Identify clusters of cells by a shared nearest neighbor (SNN) modularity optimization
#' based clustering algorithm. First calculate k-nearest neighbors and construct
#' the SNN graph. Then optimize the modularity function to determine clusters.
#' For a full description of the algorithms, see Waltman and van Eck (2013)
#' \emph{The European Physical Journal B}.
#'
#'
#' @param object Seurat object
#' @param genes.use Gene expression data
#' @param pc.use Which PCs to use for construction of the SNN graph
#' @param k.param Defines k for the k-nearest neighbor algorithm
#' @param k.scale granularity option for k.param
#' @param plot.SNN Plot the SNN graph
#' @param prune.SNN Stringency of pruning for the SNN graph (0 - no pruning,
#' 1 - prune everything)
#' @param save.SNN Whether to save the SNN in an object slot
#' @param reuse.SNN Force utilization of stored SNN. If none store, this will
#' throw an error.
#' @param do.sparse Option to store and use SNN matrix as a sparse matrix.
#' May be necessary datasets containing a large number of cells.
#' @param modularity.fxn Modularity function (1 = standard; 2 = alternative).
#' @param resolution Value of the resolution parameter, use a value above
#' (below) 1.0 if you want to obtain a larger (smaller) number of
#' communities.
#' @param algorithm Algorithm for modularity optimization (1 = original Louvain
#' algorithm; 2 = Louvain algorithm with multilevel refinement;
#' 3 = SLM algorithm).
#' @param n.start Number of random starts.
#' @param n.iter Maximal number of iterations per random start.
#' @param random.seed Seed of the random number generator.
#' @param print.output Whether or not to print output to the console
#' @param temp.file.location Directory where intermediate files will be written. Specify the
#' ABSOLUTE path.
#' @importFrom FNN get.knn
#' @importFrom igraph plot.igraph graph.adjlist
#' @importFrom Matrix sparseMatrix
#' @return Returns a Seurat object and optionally the SNN matrix,
#' object@@ident has been updated with new cluster info
#' @export
setGeneric("FindClusters", function(object, genes.use = NULL, pc.use = NULL,
k.param = 30, k.scale = 25,
plot.SNN = FALSE, prune.SNN = 1/15,
save.SNN = FALSE, reuse.SNN = FALSE,
do.sparse = FALSE, modularity.fxn = 1,
resolution = 0.8, algorithm = 1,
n.start = 100, n.iter = 10, random.seed = 0,
print.output = TRUE, temp.file.location = NULL)
standardGeneric("FindClusters"))
#' @export
setMethod("FindClusters", signature = "seurat",
function(object, genes.use = NULL, pc.use = NULL, k.param = 30,
k.scale = 25, plot.SNN = FALSE, prune.SNN = 1/15,
save.SNN = FALSE, reuse.SNN = FALSE, do.sparse = FALSE,
modularity.fxn = 1, resolution = 0.8, algorithm = 1,
n.start = 100, n.iter = 10, random.seed = 0, print.output = TRUE,
temp.file.location = NULL){
# for older objects without the snn.k slot
if(typeof(validObject(object, test = T)) == "character"){
[email protected] <- numeric()
}
snn.built <- FALSE
if (.hasSlot(object, "snn.dense")) {
if (length([email protected]) > 1) {
snn.built <- TRUE
}
}
if (.hasSlot(object, "snn.sparse")) {
if (length([email protected]) > 1) {
snn.built <- TRUE
}
}
if((missing(genes.use) && missing(pc.use) && missing(k.param) && missing(k.scale) &&
missing(prune.SNN) && snn.built) || reuse.SNN){
save.SNN <- TRUE
if (reuse.SNN && !snn.built){
stop("No SNN stored to reuse.")
}
if (reuse.SNN && (!missing(genes.use) || !missing(pc.use) || !missing(k.param) ||
!missing(k.scale) || !missing(prune.SNN))){
warning("SNN was not be rebuilt with new parameters. Continued with stored SNN. To suppress this
warning, remove all SNN building parameters.")
}
}
# if any SNN building parameters are provided or it hasn't been built, build a new SNN
else{
object <- BuildSNN(object, genes.use, pc.use, k.param, k.scale,
plot.SNN, prune.SNN, do.sparse, print.output)
}
# deal with sparse SNNs
if (length([email protected]) > 1) {
SNN.use <- [email protected]
} else {
SNN.use <- [email protected]
}
for (r in resolution) {
object <- RunModularityClustering(object, SNN.use, modularity.fxn, r,
algorithm, n.start, n.iter, random.seed,
print.output, temp.file.location)
object <- GroupSingletons(object, SNN.use)
name <- paste("res.", r, sep = "")
object <- StashIdent(object, name)
}
if (!save.SNN) {
[email protected] <- sparseMatrix(1, 1, x = 1)
[email protected] <- matrix()
[email protected] <- integer()
}
return(object)
})
#' @export
setGeneric("GetClusters", function(object) standardGeneric("GetClusters"))
setMethod("GetClusters", signature="seurat",
function(object){
return(data.frame(object@ident))
}
)
#' @export
setGeneric("SetClusters", function(object, clusters=NULL)
standardGeneric("SetClusters"))
setMethod("SetClusters", signature="seurat",
function(object, clusters = NULL){
cells.use <- rownames(clusters)
ident.use <- as.numeric(clusters[, 1])
object <- SetIdent(object, cells.use, ident.use)
return(object)
}
)
#' @export
setGeneric("SaveClusters", function(object, file)
standardGeneric("SaveClusters"))
setMethod("SaveClusters", signature="seurat",
function(object, file){
my.clusters <- GetClusters(object)
write.table(my.clusters, file = file, sep="\t", quote = FALSE)
}
)
#' @export
setGeneric("NumberClusters", function(object)
standardGeneric("NumberClusters"))
setMethod("NumberClusters", signature="seurat",
function(object){
clusters <- unique(object@ident)
if (typeof(clusters) == "integer") {
n <- as.numeric(max(clusters)) + 1
for (i in clusters) {
object <- SetIdent(object, cells.use = WhichCells(object, i),
ident.use = n)
n <- n + 1
}
clusters <- unique(object@ident)
}
n <- 1
for (i in clusters) {
object <- SetIdent(object, cells.use = WhichCells(object, i),
ident.use = n)
n <- n + 1
}
return(object)
}
)
RunModularityClustering <- function(object, SNN = matrix(), modularity = 1,
resolution = 0.8, algorithm = 1,
n.start = 100, n.iter = 10, random.seed = 0,
print.output = TRUE, temp.file.location = NULL){
seurat.dir <- system.file(package="Seurat")
ModularityJarFile <- paste(seurat.dir,
"/java/ModularityOptimizer.jar", sep = "")
seurat.dir <- paste0(strsplit(seurat.dir, "/")[[1]][0:(length(strsplit(seurat.dir, "/")[[1]])-1)], collapse = "/")
seurat.dir <- paste0(seurat.dir, "/", sep = "")
diag(SNN) <- 0
if (is.object(SNN)) {
SNN <- as(SNN, "dgTMatrix")
edge <- cbind(i = SNN@j, j = SNN@i, x = SNN@x)
} else {
swap <- which(SNN != 0, arr.ind = TRUE) - 1
temp <- swap[, 1]
swap[, 1] <- swap[, 2]
swap[, 2] <- temp
edge <- cbind(swap, SNN[which(SNN != 0, arr.ind = TRUE)])
}
rownames(edge) <- NULL
colnames(edge) <- NULL
edge <- edge[!duplicated(edge[, 1:2]), ]
temp.file.location <- set.ifnull(temp.file.location, seurat.dir)
unique_ID <- sample(10000 : 99999, 1)
edge_file <- paste(temp.file.location, "edge_", unique_ID, ".txt", sep = "")
output_file <- paste(temp.file.location, "output_", unique_ID, ".txt", sep = "")
while (file.exists(edge_file)) {
unique_ID <- sample(10000 : 99999, 1)
edge_file <- paste(temp.file.location, "edge_", unique_ID, ".txt", sep = "")
output_file <- paste(temp.file.location, "output", unique_ID, ".txt", sep = "")
}
if (print.output) {
print.output <- 1
}
else {
print.output <- 0
}
write.table(x = edge, file = edge_file, sep = "\t", row.names = FALSE,
col.names = FALSE)
if (modularity == 2 && resolution > 1){
stop("error: resolution<1 for alternative modularity")
}
command <- paste("java -jar", shQuote(ModularityJarFile), shQuote(edge_file), shQuote(output_file),
modularity, resolution, algorithm, n.start, n.iter,
random.seed, print.output, sep = " ")
system(command, wait = TRUE)
ident.use <- read.table(file = output_file, header = FALSE, sep = "\t")[, 1]
object <- SetIdent(object, [email protected], ident.use)
file.remove(edge_file)
file.remove(output_file)
return (object)
}
GroupSingletons <- function(object, SNN){
# identify singletons
singletons <- c()
for (cluster in unique(object@ident)) {
if (length(WhichCells(object, cluster)) == 1) {
singletons <- append(singletons, cluster)
}
}
# calculate connectivity of singletons to other clusters, add singleton
# to cluster it is most connected to
cluster_names <- unique(object@ident)
cluster_names <- setdiff(cluster_names, singletons)
connectivity <- vector(mode="numeric", length = length(cluster_names))
names(connectivity) <- cluster_names
for (i in singletons) {
for (j in cluster_names) {
subSNN = SNN[WhichCells(object, i), match(WhichCells(object, j), colnames(SNN))]
if (is.object(subSNN)) {
connectivity[j] <- sum(subSNN) / (nrow(subSNN) * ncol(subSNN))
} else {
connectivity[j] <- mean(subSNN)
}
}
m <- max(connectivity, na.rm = T)
mi <- which(connectivity == m, arr.ind = TRUE)
closest_cluster <- sample(names(connectivity[mi]), 1)
object <- SetIdent(object, cells.use = WhichCells(object,i),
ident.use = closest_cluster)
}
if (length(singletons) > 0){
print(paste(length(singletons), "singletons identified.", length(unique(object@ident)), "final clusters."))
}
return(object)
}