forked from satijalab/seurat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdimensional_reduction_internal.R
573 lines (553 loc) · 17.8 KB
/
dimensional_reduction_internal.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#' @include seurat.R
#' @importFrom methods setMethod setGeneric
NULL
# Set up dim.reduction class
dim.reduction <- setClass(
Class = "dim.reduction",
slots = list(
cell.embeddings = "matrix",
gene.loadings = "matrix",
gene.loadings.full = "matrix",
sdev = "numeric",
key = "character",
jackstraw = "ANY",
misc = "ANY"
)
)
################################################################################
################################### Generics ###################################
################################################################################
# Prep data for dimensional reduction
#
# Common checks and preparatory steps before running certain dimensional
# reduction techniques
#
# @param object Seurat object
# @param genes.use Genes to use as input for the dimensional reduction technique.
# Default is object@@var.genes
#
# PrepDR <- function(object, ...) {
# UseMethod(generic = 'PrepDR', object = object)
# }
setGeneric(
name = 'PrepDR',
def = function(object, ...) {
return(standardGeneric(f = 'PrepDR'))
}
)
################################################################################
############################ Functions and Methods #############################
################################################################################
# @param use.imputed Whether to run the dimensional reduction on imputed values
# @param assay.type Assay to scale data for. Default is RNA. Can be changed for multimodal analysis
#
#
setMethod(
f = 'PrepDR',
signature = c('object' = 'seurat'),
definition = function(
object,
genes.use = NULL,
use.imputed = FALSE,
assay.type="RNA",
...
) {
{
if (length([email protected]) == 0 && is.null(x = genes.use)) {
stop("Variable genes haven't been set. Run FindVariableGenes or provide a vector
of genes names in genes.use and retry.")
}
if (use.imputed) {
data.use <- t(x = scale(x = t(x = object@imputed)))
} else {
data.use <- GetAssayData(object, assay.type = assay.type,slot = "scale.data")
}
genes.use <- SetIfNull(x = genes.use, default = [email protected])
genes.use <- unique(x = genes.use[genes.use %in% rownames(x = data.use)])
genes.var <- apply(X = data.use[genes.use, ], MARGIN = 1, FUN = var)
genes.use <- genes.use[genes.var > 0]
genes.use <- genes.use[!is.na(x = genes.use)]
data.use <- data.use[genes.use, ]
return(data.use)
}
}
)
# @param var.genes Path to variable genes dataset
#
setMethod(
f = 'PrepDR',
signature = c('object' = 'loom'),
definition = function(
object,
genes.use = NULL,
var.genes = '/row_attrs/var_genes',
...
) {
if (is.null(x = genes.use)) {
genes.use <- which(x = object[[var.genes]][])
}
data.use <- GetAssayData(object = object, slot = 'scale.data', genes.use = genes.use)
genes.var <- apply(X = data.use, MARGIN = 1, FUN = var)
data.use <- data.use[genes.var > 0]
return(data.use)
}
)
DoTSNE <- function(
object,
reduction.use = "pca",
cells.use = NULL,
dims.use = 1:5,
genes.use = NULL,
seed.use = 1,
tsne.method = "Rtsne",
add.iter = 0,
dim.embed = 2,
distance.matrix = NULL,
reduction.name = "tsne",
reduction.key = "tSNE_",
overwrite = FALSE,
...
) {
if (!is.null(x = distance.matrix)) {
# genes.use <- rownames(x = object@data)
genes.use <- GetGenes(object = object, use.scaled = FALSE)
}
if (is.null(x = genes.use)) {
data.use <- GetDimReduction(
object = object,
reduction.type = reduction.use,
slot = "cell.embeddings"
)[, dims.use]
} else {
data.use <- t(x = PrepDR(
object = object,
genes.use = genes.use))
}
set.seed(seed = seed.use)
if (tsne.method == "Rtsne") {
if (is.null(x = distance.matrix)) {
data.tsne <- Rtsne(
X = as.matrix(x = data.use),
dims = dim.embed,
pca = FALSE, ...
)
} else {
data.tsne <- Rtsne(
X = as.matrix(x = distance.matrix),
dims = dim.embed,
is_distance = TRUE,
...
)
}
data.tsne <- data.tsne$Y
} else if (tsne.method == "FIt-SNE" & is.null(x = distance.matrix)) {
data.tsne <- fftRtsne(X = as.matrix(x = data.use), dims = dim.embed, rand_seed = seed.use, ...)
} else if (tsne.method == "tsne") {
data.tsne <- tsne(X = data.use, k = dim.embed, ...)
} else {
stop("Invalid tsne.method: Please select from Rtsne, tsne, or FIt-SNE")
}
if (add.iter > 0) {
data.tsne <- tsne(
X = data.use,
initial_config = as.matrix(x = data.tsne),
max_iter = add.iter,
...
)
}
colnames(x = data.tsne) <- paste0(reduction.key, 1:ncol(x = data.tsne))
rownames(x = data.tsne) <- rownames(x = data.use)
object <- SetDimReduction(
object = object,
reduction.type = reduction.name,
slot = "cell.embeddings",
new.data = data.tsne,
overwrite = overwrite
)
suppressWarnings(expr = object <- SetDimReduction(
object = object,
reduction.type = reduction.name,
slot = "key",
new.data = reduction.key,
overwrite = overwrite
))
invisible(x = object)
}
# Get the top genes associated with given dimensional reduction scores
#
# @param i Dimension for which to pull genes
# @param dim.scores Matrix containing the dimensional reduction scores
# @param do.balanced Whether to pull genes associated with both large and small
# scores (+/-)
# @param num.genes Number of genes to return
GetTopGenes <- function(
i,
dim.scores,
do.balanced = FALSE,
num.genes = 30
) {
if (do.balanced) {
num.genes <- round(x = num.genes / 2)
sx <- dim.scores[order(dim.scores[, i]), , drop = FALSE]
genes.1 <- (rownames(x = sx[1:num.genes, , drop = FALSE]))
genes.2 <- (rownames(x = sx[(nrow(x = sx) - num.genes + 1):nrow(x = sx), , drop = FALSE]))
return(c(genes.1, genes.2))
} else {
sx <- dim.scores[rev(x = order(abs(x = dim.scores[, i]))), ,drop = FALSE]
genes.1 <- (rownames(x = sx[1:num.genes, , drop = FALSE]))
genes.1 <- genes.1[order(dim.scores[genes.1, i])]
return(genes.1)
}
}
# Check group exists either as an ident or that all cells passed as vector are
# present
#
# @param object Seurat object
# @param group Identity or vector of cell names
# @param group.id Corresponds to the the either group1 or group2 parameter from
# RunCCA
CheckGroup <- function(object, group, group.id) {
if (all(group %in% unique(x = object@ident))) {
cells.use <- WhichCells(object = object, ident = group)
} else {
if (all(group %in% [email protected])) {
cells.use <- group
} else {
stop(paste(
group.id,
"must be either a vector of valid cell names or idents"
))
}
}
if (length(cells.use) == 0) {
stop(paste0("No cells present in group: ", group.id))
}
return(cells.use)
}
# Check that genes have non-zero variance
#
# @param data.use Gene expression matrix (genes are rows)
# @param genes.use Genes in expression matrix to check
#
# @return Returns a vector of genes that is the subset of genes.use
# that have non-zero variance
#
CheckGenes <- function(data.use, genes.use) {
genes.var <- apply(X = data.use[genes.use, ], MARGIN = 1, FUN = var)
genes.use <- genes.use[genes.var > 0]
genes.use <- genes.use[! is.na(x = genes.use)]
return(genes.use)
}
# Run the diagonal canonical correlation procedure
#
# @param mat1 First matrix
# @param mat2 Second matrix
# @param standardize Standardize matrices - scales columns to have unit
# variance and mean 0
# @param k Number of canonical correlation vectors (CCs) to calculate
#
# @return Returns the canonical correlation vectors - corresponding
# to the left and right singular vectors after SVD - as well
# as the singular values.
#
CanonCor <- function(mat1, mat2, standardize = TRUE, k = 20) {
set.seed(seed = 42)
if (standardize) {
mat1 <- Standardize(mat = mat1, display_progress = FALSE)
mat2 <- Standardize(mat = mat2, display_progress = FALSE)
}
mat3 <- FastMatMult(m1 = t(x = mat1), m2 = mat2)
cca.svd <- irlba(A = mat3, nv = k)
return(list(u = cca.svd$u, v = cca.svd$v, d = cca.svd$d))
}
# Calculate percent variance explained
#
# Projects dataset onto the orthonormal space defined by some dimensional
# reduction technique (e.g. PCA, CCA) and calculates the percent of the
# variance in gene expression explained by each cell in that lower dimensional
# space.
#
# @param object Seurat object
# @param reduction.type Name of the reduction to use for the projection
# @param dims.use Vector of dimensions to project onto (default is the
# 1:number stored for given technique)
# @param genes.use vector of genes to use in calculation
#
# @return Returns a Seurat object wih the variance in gene
# expression explained by each cell in a low dimensional
# space stored as metadata.
#
CalcProjectedVar <- function(
object,
low.dim.data,
reduction.type = "pca",
dims.use,
genes.use
) {
if (missing(x = low.dim.data)) {
low.dim.data <- CalcLDProj(
object = object,
reduction.type = reduction.type,
dims.use = dims.use,
genes.use = genes.use
)
}
projected.var <- apply(X = low.dim.data, MARGIN = 2, FUN = var)
calc.name <- paste0(reduction.type, ".var")
object <- AddMetaData(
object = object,
metadata = projected.var,
col.name = calc.name
)
return(object)
}
# Calculate a low dimensional projection of the data. First forms an orthonormal
# basis of the gene loadings via QR decomposition, projects the data onto that
# basis, and reconstructs the data using on the dimensions specified.
#
# @param object Seurat object
# @param reduction.type Type of dimensional reduction to use
# @param dims.use Dimensions to use in calculation
# @param genes.use Genes to consider when calculating
#
# @return Returns a matrix with the low dimensional reconstruction
#
CalcLDProj <- function(object, reduction.type, dims.use, genes.use) {
if (missing(x = dims.use)){
dims.use <- 1:ncol(x = GetCellEmbeddings(
object = object,
reduction.type = reduction.type
))
}
x.vec <- GetGeneLoadings(
object = object,
reduction.type = reduction.type,
dims.use = dims.use,
genes.use = genes.use
)
# form orthonormal basis via QR
x.norm <- qr.Q(qr = qr(x = x.vec))
data.use <- [email protected][rownames(x.vec), ]
# project data onto othronormal basis
projected.data <- t(x = data.use) %*% x.norm
# reconstruct data using only dims specified
low.dim.data <- x.norm %*% t(x = projected.data)
return(low.dim.data)
}
# MultiCCA helper function - calculates critical value (when to stop iterating
# in the while loop)
#
# Modified from PMA package
# @references Witten, Tibshirani, and Hastie, Biostatistics 2009
# @references \url{https://github.com/cran/PMA/blob/master/R/MultiCCA.R}
#
# @param mat.list list of matrices
# @param ws vector of projection vectors
# @param num.sets number of datasets
#
# @return returns updated critical value
#
GetCrit <- function(mat.list, ws, num.sets){
crit <- 0
for(i in 2:num.sets){
for(j in 1:(i-1)){
crit <- crit + t(ws[[i]])%*%t(mat.list[[i]])%*%mat.list[[j]]%*%ws[[j]]
}
}
return(crit)
}
# MultiCCA helper function - updates W
#
# Modified from PMA package
# @references Witten, Tibshirani, and Hastie, Biostatistics 2009
# @references \url{https://github.com/cran/PMA/blob/master/R/MultiCCA.R}
#
# @param mat.list list of matrices
# @param i index of current matrix
# @param num.sets number of datasets
# @param ws initial vector of projection vectors
# @param ws.final final vector of projection vectors
#
# @return returns updated w value
#
UpdateW <- function(mat.list, i, num.sets, ws, ws.final){
tots <- 0
for(j in (1:num.sets)[-i]){
diagmat <- (t(ws.final[[i]])%*%t(mat.list[[i]]))%*%(mat.list[[j]]%*%ws.final[[j]])
diagmat[row(diagmat)!=col(diagmat)] <- 0
tots <- tots + t(mat.list[[i]])%*%(mat.list[[j]]%*%ws[[j]]) - ws.final[[i]]%*%(diagmat%*%(t(ws.final[[j]])%*%ws[[j]]))
}
w <- tots/l2n(tots)
return(w)
}
# Calculates the l2-norm of a vector
#
# Modified from PMA package
# @references Witten, Tibshirani, and Hastie, Biostatistics 2009
# @references \url{https://github.com/cran/PMA/blob/master/R/PMD.R}
#
# @param vec numeric vector
#
# @return returns the l2-norm.
#
l2n <- function(vec){
a <- sqrt(sum(vec^2))
if(a==0){
a <- .05
}
return(a)
}
# MultiCCA helper function - calculates correlation
#
# Modified from PMA package
# @references Witten, Tibshirani, and Hastie, Biostatistics 2009
# @references \url{https://github.com/cran/PMA/blob/master/R/MultiCCA.R}
#
# @param mat.list list of matrices to calculate correlation
# @param ws vector of projection vectors
# @param num.sets number of datasets
#
# @return total correlation
#
GetCors <- function(mat.list, ws, num.sets){
cors <- 0
for(i in 2:num.sets){
for(j in 1:(i-1)){
thiscor <- cor(mat.list[[i]]%*%ws[[i]], mat.list[[j]]%*%ws[[j]])
if(is.na(thiscor)) thiscor <- 0
cors <- cors + thiscor
}
}
return(cors)
}
# FIt-SNE helper function for calling fast_tsne from R
#
# Based on Kluger Lab code on https://github.com/ChristophH/FIt-SNE
# commit ec25f1b36598a2d21869d10a258ac366a12f0b05
#
#' @importFrom utils file_test
#
fftRtsne <- function(
X,
dims = 2,
perplexity = 30,
theta = 0.5,
check_duplicates = TRUE,
max_iter = 1000,
fft_not_bh = TRUE,
ann_not_vptree = TRUE,
stop_lying_iter = 250,
exaggeration_factor = 12.0,
no_momentum_during_exag = FALSE,
start_late_exag_iter = -1.0,
late_exag_coeff = 1.0,
n_trees = 50,
search_k = -1,
rand_seed = -1,
nterms = 3,
intervals_per_integer = 1,
min_num_intervals = 50,
data_path = NULL,
result_path = NULL,
fast_tsne_path = NULL,
nthreads = getOption('mc.cores', default = 1),
...
) {
if (is.null(x = data_path)) {
data_path <- tempfile(pattern = 'fftRtsne_data_', fileext = '.dat')
}
if (is.null(x = result_path)) {
result_path <- tempfile(pattern = 'fftRtsne_result_', fileext = '.dat')
}
if (is.null(x = fast_tsne_path)) {
fast_tsne_path <- system2(command = 'which', args = 'fast_tsne', stdout = TRUE)
if (length(x = fast_tsne_path) == 0) {
stop("no fast_tsne_path specified and fast_tsne binary is not in the search path")
}
}
fast_tsne_path <- normalizePath(path = fast_tsne_path)
if (!file_test(op = '-x', x = fast_tsne_path)) {
stop("fast_tsne_path '", fast_tsne_path, "' does not exist or is not executable")
}
is.wholenumber <- function(x, tol = .Machine$double.eps ^ 0.5) {
return(abs(x = x - round(x = x)) < tol)
}
if (!is.numeric(x = theta) || (theta < 0.0) || (theta > 1.0) ) {
stop("Incorrect theta.")
}
if (nrow(x = X) - 1 < 3 * perplexity) {
stop("Perplexity is too large.")
}
if (!is.matrix(x = X)) {
stop("Input X is not a matrix")
}
if (!(max_iter > 0)) {
stop("Incorrect number of iterations.")
}
if (!is.wholenumber(x = stop_lying_iter) || stop_lying_iter < 0) {
stop("stop_lying_iter should be a positive integer")
}
if (!is.numeric(x = exaggeration_factor)) {
stop("exaggeration_factor should be numeric")
}
if (!is.wholenumber(x = dims) || dims <= 0) {
stop("Incorrect dimensionality.")
}
if (search_k == -1) {
search_k = n_trees * perplexity * 3
}
# if (fft_not_bh) {
# nbody_algo <- 2
# } else {
# nbody_algo <- 1
# }
nbody_algo <- ifelse(test = fft_not_bh, yes = 2, no = 1)
# if (ann_not_vptree) {
# knn_algo <- 1
# }else{
# knn_algo <- 2
# }
knn_algo <- ifelse(test = ann_not_vptree, yes = 1, no = 2)
tX = c(t(x = X))
f <- file(data_path, "wb")
n = nrow(x = X)
D = ncol(x = X)
writeBin(object = as.integer(x = n), con = f, size = 4)
writeBin(object = as.integer(x = D), con = f, size = 4)
writeBin(object = as.numeric(x = 0.5), con = f, size = 8) #theta
writeBin(object = as.numeric(x = perplexity), con = f, size = 8) #theta
writeBin(object = as.integer(x = dims), con = f, size = 4) #theta
writeBin(object = as.integer(x = max_iter), con = f, size = 4)
writeBin(object = as.integer(x = stop_lying_iter), con = f, size = 4)
writeBin(object = as.integer(x = -1), con = f, size = 4) #K
writeBin(object = as.numeric(x = -30.0), con = f, size = 8) #sigma
writeBin(object = as.integer(x = nbody_algo), con = f, size = 4) #not barnes hut
writeBin(object = as.integer(x = knn_algo), con = f, size = 4)
writeBin(object = as.numeric(x = exaggeration_factor), con = f, size = 8) #compexag
writeBin(object = as.integer(x = no_momentum_during_exag), con = f, size = 4)
writeBin(object = as.integer(x = n_trees), con = f, size = 4)
writeBin(object = as.integer(x = search_k), con = f, size = 4)
writeBin(object = as.integer(x = start_late_exag_iter), con = f, size = 4)
writeBin(object = as.numeric(x = late_exag_coeff), con = f, size = 8)
writeBin(object = as.integer(x = nterms), con = f, size = 4)
writeBin(object = as.numeric(x = intervals_per_integer), con = f, size = 8)
writeBin(object = as.integer(x = min_num_intervals), con = f, size = 4)
tX = c(t(x = X))
writeBin(object = tX, con = f)
writeBin(object = as.integer(x = rand_seed), con = f, size = 4)
close(f)
flag <- system2(command = fast_tsne_path, args = c(data_path, result_path, nthreads))
if (flag != 0) {
stop('tsne call failed');
}
f <- file(description = result_path, open = "rb")
initialError <- readBin(f, integer(), n = 1, size = 8)
n <- readBin(con = f, what = integer(), n = 1, size = 4)
d <- readBin(con = f, what = integer(), n = 1, size = 4)
Y <- readBin(con = f, what = numeric(), n = n * d)
Yout <- t(x = matrix(data = Y, nrow = d))
close(f)
file.remove(data_path)
file.remove(result_path)
return(Yout)
}