aertslab / SCopeLoomR

R package (compatible with SCope) to create generic .loom files and extend them with other data e.g.: SCENIC regulons, Seurat clusters and markers, ...
MIT License
40 stars 15 forks source link

Bug on add_seurat_clustering() for Seurat v3 objects #12

Closed cbravo93 closed 4 years ago

cbravo93 commented 4 years ago

Hi guys!

In line 663 on the loom.R file, this results on a vector with cell names and the resolution (instead of clusters as value), so the function crashes:

Seurat::Idents(object = seurat)<-paste0(seurat.clustering.prefix, res)
#Output:
10X01_1_AAGTTCCTAACCTG-1 10X19_2_CTTGTATGCCGTTC-1 10X02_1_CTGGAAACTGAAGA-1 10X01_1_GGCCGAACGCAAGG-1 10X01_1_GTGGTAACCTGATG-1 
                     0.6                      0.6                      0.6                      0.6                      0.6 
10X02_1_GCAGCCGATGACAC-1 
                     0.6 

I made a quick fix to it by replacing that line with the code below, now everything seems to work:

cluster.ids<-seurat@meta.data[, paste0('RNA_snn_res.', res)]
names(cluster.ids) <- rownames(seurat@meta.data)

Cheers! C

cbravo93 commented 4 years ago

Also in lines 786 and 788 it is needed to add as.vector(unlist()) to annotate correctly the clusters:

create_cluster_annotation<-function(clusters
                                    , cluster.meta.data.df = NULL
                                    , cluster.id.cn =  NULL
                                    , cluster.description.cn = NULL) {
  if(is.factor(clusters)) {
    unique.clusters<-sort(as.integer(levels(clusters)), decreasing = F)
  } else {
    unique.clusters<-sort(unique(clusters), decreasing = F)
  }
  annotation<-setNames(object = rep(NA, length(clusters)), nm = names(clusters))
  for(cluster in unique.clusters) {
    description<-paste0("NDA - Cluster ", cluster)
    if(!is.null(cluster.meta.data.df)) {
      if(!(cluster.description.cn %in% colnames(cluster.meta.data.df))) {
        stop(paste0("The given column ",cluster.description.cn, " does not exists in the annotation provided."))
      }
      description<-as.vector(unlist(cluster.meta.data.df[cluster.meta.data.df[[cluster.id.cn]] == cluster, cluster.description.cn]))
    }
    annotation[as.vector(unlist(clusters)) == cluster]<-description
  }
  annotation<-factor(x = annotation)
  names(x = annotation)<-names(clusters)
  return (annotation)
}
dweemx commented 4 years ago

Thanks for the bug report and the solution to solve it! @cbravo93 This has been fixed in version 0.5.1