-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizeLSI.R
184 lines (157 loc) · 5.58 KB
/
optimizeLSI.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
library(Matrix)
library(SummarizedExperiment)
library(tidyverse)
library(uwot)
library(edgeR)
library(FNN)
library(matrixStats)
library(Rcpp)
set.seed(1)
#Optimized LSI for scRNA-seq analysis
optimizeLSI <- function(inSCE, mat, scaleTo = 10000, priorCount = 3, pcsUse = 1:25,
resolution = c(0.2, 0.4, 0.8), varFeatures = c(2500, 2500, 2500), seed = 1){
set.seed(seed)
stopifnot(length(resolution) > 1)
stopifnot(length(resolution) == length(varFeatures))
#Initialize List
lsiOut <- list()
#Initial LSI uses variances that are across all single cells and will have larger batch relationships
i <- 1
message("Initial LSI...")
matNorm <- t(t(mat)/Matrix::colSums(mat)) * scaleTo
matNorm@x <- log2(matNorm@x + 1)
idVarFeatures <- head(order(sparseRowVariances(matNorm),decreasing=TRUE), varFeatures[i])
lsiObj <- calcLSI(mat[idVarFeatures,], binarize = FALSE, nComponents = max(pcsUse))
# clusters <- seuratSNN(lsiObj$matSVD, dims.use = pcsUse, resolution = resolution[i], n.start = 10, print.output = FALSE)
pca <- lsiObj$matSVD
rownames(pca) <- gsub("_", "-", rownames(pca))
new_pca <- Seurat::CreateDimReducObject(embeddings = pca, key = "PC_")
inSCE <- runSeuratFindClusters(inSCE, externalReduction = new_pca, dims = pcsUse, resolution = resolution[i])
clusters <- colData(inSCE)[[paste0("Seurat_louvain_Resolution", resolution[i])]]
#Store
lsiOut[[paste0("iter", i)]] <- list(
lsiMat = lsiObj$matSVD,
varFeatures = idVarFeatures,
clusters = clusters
)
for(i in seq(2, length(varFeatures))){
message(sprintf("Additional LSI %s...", i))
#Run LSI
clusterMat <- edgeR::cpm(groupSums(mat, clusters, sparse = TRUE), log=TRUE, prior.count = priorCount)
idVarFeatures <- head(order(rowVars(clusterMat), decreasing=TRUE), varFeatures[i])
lsiObj <- calcLSI(mat[idVarFeatures,], binarize = FALSE, nComponents = max(pcsUse))
# clusters <- seuratSNN(lsiObj$matSVD, dims.use = pcsUse, resolution = resolution[i], n.start = 10, print.output = FALSE)
pca <- lsiObj$matSVD
rownames(pca) <- gsub("_", "-", rownames(pca))
new_pca <- Seurat::CreateDimReducObject(embeddings = pca, key = "PC_")
inSCE <- runSeuratFindClusters(inSCE, externalReduction = new_pca, dims = pcsUse, resolution = resolution[i])
clusters <- colData(inSCE)[[paste0("Seurat_louvain_Resolution", resolution[i])]]
if(i == length(varFeatures)){
#Save All Information from LSI Attempt
lsiOut[[paste0("iter", i)]] <- list(
lsiObj = lsiObj,
varFeatures = idVarFeatures,
clusters = clusters,
matNorm = matNorm
)
}else{
lsiOut[[paste0("iter", i)]] <- list(
lsiMat = lsiObj$matSVD,
varFeatures = idVarFeatures,
clusters = clusters
)
}
}
return(lsiOut)
}
#Compute Fast Sparse Row Variances
sparseRowVariances <- function (m){
rM <- Matrix::rowMeans(m)
rV <- computeSparseRowVariances(m@i + 1, m@x, rM, ncol(m))
return(rV)
}
#Sparse Variances Rcpp
sourceCpp(code='
#include <Rcpp.h>
using namespace Rcpp;
using namespace std;
// [[Rcpp::export]]
Rcpp::NumericVector computeSparseRowVariances(IntegerVector j, NumericVector val, NumericVector rm, int n) {
const int nv = j.size();
const int nm = rm.size();
Rcpp::NumericVector rv(nm);
Rcpp::NumericVector rit(nm);
int current;
// Calculate RowVars Initial
for (int i = 0; i < nv; ++i) {
current = j(i) - 1;
rv(current) = rv(current) + (val(i) - rm(current)) * (val(i) - rm(current));
rit(current) = rit(current) + 1;
}
// Calculate Remainder Variance
for (int i = 0; i < nm; ++i) {
rv(i) = rv(i) + (n - rit(i))*rm(i)*rm(i);
}
rv = rv / (n - 1);
return(rv);
}'
)
calcLSI <- function(mat, nComponents = 50, binarize = TRUE, nFeatures = NULL){
set.seed(1)
#TF IDF LSI adapted from flyATAC
if(binarize){
message(paste0("Binarizing matrix..."))
mat@x[mat@x > 0] <- 1
}
if(!is.null(nFeatures)){
message(paste0("Getting top ", nFeatures, " features..."))
idx <- head(order(Matrix::rowSums(mat), decreasing = TRUE), nFeatures)
mat <- mat[idx,]
}else{
idx <- which(Matrix::rowSums(mat) > 0)
mat <- mat[idx,]
}
#Calc RowSums and ColSums
colSm <- Matrix::colSums(mat)
rowSm <- Matrix::rowSums(mat)
#Calc TF IDF
message("Computing Term Frequency IDF...")
freqs <- t(t(mat)/colSm)
idf <- as(log(1 + ncol(mat) / rowSm), "sparseVector")
tfidf <- as(Matrix::Diagonal(x=as.vector(idf)), "sparseMatrix") %*% freqs
#Calc SVD then LSI
message("Computing SVD using irlba...")
svd <- irlba::irlba(tfidf, nComponents, nComponents)
svdDiag <- matrix(0, nrow=nComponents, ncol=nComponents)
diag(svdDiag) <- svd$d
matSVD <- t(svdDiag %*% t(svd$v))
rownames(matSVD) <- colnames(mat)
colnames(matSVD) <- paste0("PC",seq_len(ncol(matSVD)))
#Return Object
out <- list(
matSVD = matSVD,
rowSm = rowSm,
colSm = colSm,
idx = idx,
svd = svd,
binarize = binarize,
nComponents = nComponents,
date = Sys.Date(),
seed = 1)
out
}
#Helper function for summing sparse matrix groups
groupSums <- function (mat, groups = NULL, na.rm = TRUE, sparse = FALSE){
stopifnot(!is.null(groups))
stopifnot(length(groups) == ncol(mat))
gm <- lapply(unique(groups), function(x) {
if (sparse) {
Matrix::rowSums(mat[, which(groups == x), drop = F], na.rm = na.rm)
}
else {
rowSums(mat[, which(groups == x), drop = F], na.rm = na.rm)
}
}) %>% Reduce("cbind", .)
colnames(gm) <- unique(groups)
return(gm)
}