GreenleafLab / MPAL-Single-Cell-2019

Publication Page for MPAL Paper 2019
76 stars 38 forks source link

Runtime of getting UMAP reductions #18

Open mohit1997 opened 4 years ago

mohit1997 commented 4 years ago

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?

jgranja24 commented 4 years ago

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

mohit1997 commented 4 years ago

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()
pbrazda commented 4 years ago

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!

MugenQin commented 1 year ago

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: se@meta.data$optimizeLSI <- lsiObj se@meta.data$matSVD <- lsiObj[[length(lsiObj)]][[1]][[1]] #Last one se@meta.data$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!