MPAL-Single-Cell-2019 icon indicating copy to clipboard operation
MPAL-Single-Cell-2019 copied to clipboard

Runtime of getting UMAP reductions

Open mohit1997 opened this issue 4 years ago • 4 comments

I am trying to run scRNA_01_Clustering_UMAP_v1.R, and apparently the function optimizeLSI is iterative. The code shows that each iteration take 30 seconds and the maximum number of iterations is 3000. Am I doing something wrong or is it supposed to be this slow?

mohit1997 avatar May 04 '20 21:05 mohit1997

This was a bug my mistake

I updated it to this

nTop <- c(3000, 3000, 3000) #Choose a higher number of variable peaks

Best

Jeff

jgranja24 avatar May 05 '20 03:05 jgranja24

Works like charm! Thanks a lot. A few more suggestions, I encountered an error while calling metadata(se) Instead calling se@metadata works fine. (Note: I am new to R and might be missing something obvious here, but this worked for me.) So after optimizeLSI, we can replace the rest of the lines with the following code. Also edited the third line which was using lsi instead of lsiObj.

se@metadata$optimizeLSI <- lsiObj
se@metadata$matSVD <- lsiObj[[length(lsiObj)]][[1]][[1]] #Last one
se@metadata$variableGenes <- rownames(se)[lsiObj[[length(lsiObj)]]$varFeatures] #Variable genes

####################################################
#For Creating UMAP Start Here
####################################################
matSVD <- se@metadata$matSVD
clusters <- colData(se)$Clusters

#Set Seed and perform UMAP on LSI-SVD Matrix
set.seed(1)
uwotUmap <- uwot::umap(
  matSVD, 
  n_neighbors = 35, 
  min_dist = 0.45, 
  metric = "euclidean", 
  n_threads = 1, 
  verbose = TRUE, 
  ret_nn = TRUE,
  ret_model = TRUE
)

pdf("Plot_UMAP-NN-35-MD-45.pdf", width = 12, height = 12, useDingbats = FALSE)
df <- data.frame(
  x = uwotUmap[[1]][,1],
  y = -uwotUmap[[1]][,2], 
  color = clusters
)
ggplot(df,aes(x,y,color=color)) + 
  geom_point() + 
  theme_bw() + 
  scale_color_manual(values=se@metadata$colorMap$Clusters) +
  xlab("UMAP Dimension 1") + 
  ylab("UMAP Dimension 2")
dev.off()

mohit1997 avatar May 05 '20 04:05 mohit1997

Hi @mohit1997 , Thanks for opening this issue! ;) Do you have any suggestions on this topic, maybe?:

I am trying to replicate 'scRNA_02_Cluster_Disease_w_Reference_v1.R' . However, in "Project Into LSI UMAP", line366, for umapProjection <- uwot::umap_transform(as.matrix(lsiProjection$matSVD)[,1:25], umapManifold, verbose = TRUE) I get an error: Error in lsiProjection$matSVD : $ operator not defined for this S4 class

(for umapProjection <- uwot::umap_transform(as.matrix(lsiProjection@matSVD)[,1:25], umapManifold, verbose = TRUE) :

Error in as.matrix(lsiProjection@matSVD) : 
  no slot of name "matSVD" for this object of class "dgeMatrix"

)

Thanks!

pbrazda avatar Sep 14 '20 07:09 pbrazda

Hi,

Works like charm! Thanks a lot. A few more suggestions, I encountered an error while calling metadata(se) Instead calling se@metadata works fine. (Note: I am new to R and might be missing something obvious here, but this worked for me.) So after optimizeLSI, we can replace the rest of the lines with the following code. Also edited the third line which was using lsi instead of lsiObj.

se@metadata$optimizeLSI <- lsiObj
se@metadata$matSVD <- lsiObj[[length(lsiObj)]][[1]][[1]] #Last one
se@metadata$variableGenes <- rownames(se)[lsiObj[[length(lsiObj)]]$varFeatures] #Variable genes

####################################################
#For Creating UMAP Start Here
####################################################
matSVD <- se@metadata$matSVD
clusters <- colData(se)$Clusters

#Set Seed and perform UMAP on LSI-SVD Matrix
set.seed(1)
uwotUmap <- uwot::umap(
  matSVD, 
  n_neighbors = 35, 
  min_dist = 0.45, 
  metric = "euclidean", 
  n_threads = 1, 
  verbose = TRUE, 
  ret_nn = TRUE,
  ret_model = TRUE
)

pdf("Plot_UMAP-NN-35-MD-45.pdf", width = 12, height = 12, useDingbats = FALSE)
df <- data.frame(
  x = uwotUmap[[1]][,1],
  y = -uwotUmap[[1]][,2], 
  color = clusters
)
ggplot(df,aes(x,y,color=color)) + 
  geom_point() + 
  theme_bw() + 
  scale_color_manual(values=se@metadata$colorMap$Clusters) +
  xlab("UMAP Dimension 1") + 
  ylab("UMAP Dimension 2")
dev.off()

Hi @mohit1997 , I had the same problem with you that I got error when I run metadata, then I tried se@metadata, but I got another error when I run the following codes: [email protected]$optimizeLSI <- lsiObj [email protected]$matSVD <- lsiObj[[length(lsiObj)]][[1]][[1]] #Last one [email protected]$variableGenes <- rownames(se)[lsiObj[[length(lsiObj)]]$varFeatures] #Variable genes and I got these errores respectively: Error in $<-.data.frame(tmp, optimizeLSI, value = list(iter1 = list( : replacement has 3 rows, data has 3434 Error in $<-.data.frame(tmp, variableGenes, value = c("IGLV2-14", : replacement has 3000 rows, data has 3434

How do you think about these errors? Thanks!

MugenQin avatar May 20 '23 10:05 MugenQin