@@ -13,8 +13,9 @@ clusterCells <- function(object) {
1313 n_workers <- getOption(" mc.cores" )
1414 n_workers <- if (is.null(n_workers )) 2 else n_workers
1515
16+ K <- min(object @ num_neighbors , 30 )
1617 kn <- ball_tree_knn(res ,
17- min(round(sqrt(nrow( res ))), 30 ) ,
18+ K ,
1819 n_workers )
1920
2021 cl <- louvainCluster(kn , res )
@@ -67,7 +68,8 @@ poolCells <- function(object,
6768 cellsPerPartition = object @ cellsPerPartition ,
6869 filterInput = object @ projection_genes ,
6970 filterThreshold = object @ threshold ,
70- latentSpace = object @ latentSpace )
71+ latentSpace = object @ latentSpace ,
72+ K = object @ num_neighbors )
7173
7274 object @ pools <- pools
7375 }
@@ -232,6 +234,82 @@ calcSignatureScores <- function(object,
232234 return (object )
233235}
234236
237+
238+ # ' calculate gene-signature importance
239+ # '
240+ # ' For each signature, the contribution of each gene to the signature score
241+ # ' is evaluated by calculating the covariance between signature scores and expression
242+ # ' The correlation of genes with a negative sign in the signature are inverted.
243+ # '
244+ # ' @importFrom pbmcapply pbmclapply
245+ # ' @importFrom matrixStats colSds
246+ # ' @importFrom matrixStats rowSds
247+ # '
248+ # ' @param object the VISION object
249+ # ' @return the VISION object, with SigGeneImportance slot populated
250+ evalSigGeneImportance <- function (object ){
251+
252+ message(" Evaluating signature-gene importance...\n " )
253+
254+ sigScores <- object @ sigScores
255+
256+ if (length(object @ sigData ) == 0 ) {
257+ object @ SigGeneImportance <- list ()
258+ return (object )
259+ }
260+
261+ if (length(sigScores ) < = 1 ){
262+ stop(
263+ sprintf(" Signature scores have not yet been computed. `calcSignatureScores` must be run before running `evalSigGeneImportance`" )
264+ )
265+ }
266+
267+ normExpr <- getNormalizedCopy(object @ exprData , object @ sig_norm_method )
268+
269+ # Center each column of sigScores first
270+
271+ mu <- colMeans(sigScores )
272+
273+ sigScores <- t(sigScores )
274+ sigScores <- (sigScores - mu )
275+ sigScores <- t(sigScores )
276+
277+ # Center each row of normExpr
278+ mu <- rowMeans(normExpr )
279+
280+ normExpr <- (normExpr - mu )
281+
282+ # Compute Covariances
283+ sigData <- object @ sigData
284+
285+ sigGene <- function (signame ) {
286+ sigdata <- sigData [[signame ]]
287+
288+ genes <- sigdata @ sigDict
289+
290+ sigvals <- sigScores [, signame ]
291+
292+ geneIndices <- match(names(genes ), rownames(normExpr ))
293+
294+ corr <- sigGeneInner(sigvals , normExpr , geneIndices )
295+
296+ names(corr ) <- names(genes )
297+
298+ corr <- corr * genes
299+
300+ return (corr )
301+ }
302+
303+ sigs <- colnames(sigScores )
304+ res <- pbmclapply(setNames(sigs , sigs ), sigGene )
305+
306+ object @ SigGeneImportance <- res
307+
308+
309+ return (object )
310+ }
311+
312+
235313# ' Computes the latent space of the expression matrix using PCA
236314# '
237315# ' @param object the VISION object for which compute the latent space
@@ -302,13 +380,24 @@ generateProjections <- function(object) {
302380 projections <- generateProjectionsInner(object @ exprData ,
303381 object @ latentSpace ,
304382 projection_genes = object @ projection_genes ,
305- projection_methods = object @ projection_methods )
383+ projection_methods = object @ projection_methods ,
384+ K = object @ num_neighbors )
306385
307386 # Add inputProjections
308387 for (proj in names(object @ inputProjections )){
309388 projections [[proj ]] <- object @ inputProjections [[proj ]]
310389 }
311390
391+ # Make sure all projections have column names
392+ n <- names(projections )
393+ projections <- lapply(setNames(n , n ), function (pname ){
394+ proj <- projections [[pname ]]
395+ if (is.null(colnames(proj ))){
396+ colnames(proj ) <- paste0(pname , " -" , seq_len(ncol(proj )))
397+ }
398+ return (proj )
399+ })
400+
312401 object @ Projections <- projections
313402
314403 message(" " )
@@ -337,7 +426,8 @@ analyzeLocalCorrelations <- function(object, signatureBackground = NULL) {
337426 object @ latentSpace ,
338427 object @ sigScores ,
339428 object @ metaData ,
340- signatureBackground )
429+ signatureBackground ,
430+ object @ num_neighbors )
341431
342432 message(" Clustering signatures...\n " )
343433 sigClusters <- clusterSignatures(object @ sigScores ,
0 commit comments