Skip to content

LouvainKRange run error #20

@chapmanchao

Description

@chapmanchao

Running this line of code:
pisces.obj <- LouvainKRange(pisces.obj, kmax = 100)

Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent

The code of LouvainKRange and LouvainClust are following:

LouvainClust <- function(dat.mat, k = 100) {
  require(MUDAN)
  set.seed(1)
  res <- MUDAN::getComMembership(t(dat.mat), k = k, method = igraph::cluster_infomap, verbose = FALSE)
  #as(adj.mat, "sparseMatrix")
  #res <- MUDAN::getComMembership(as(t(dat.mat),"sparseMatrix"), k = k, method = igraph::cluster_infomap, verbose = FALSE)
  return(res)
}

LouvainKRange <- function(data.obj, kmin = 5, kmax = 50, kstep = 5) {
  require(igraph, quietly = TRUE)
  # check if seurat object
  if (class(data.obj)[1] == "Seurat") {
   dist.mat <- data.obj@assays[[[email protected]]]@misc$dist.mat
  } else {
    dist.mat <- data.obj
  }
  # create lists
  cluster.list <- list()
  sil.list <- list()
  # setup iteration
  k <- kmin
  while (k <= kmax) {
    print(paste("Clustering with k = ", k, "...", sep = ''))
    # generate clustering and silhouette score
    clust.vec <- LouvainClust(dist.mat, k)
    sil.score <- cluster::silhouette(clust.vec, dist.mat)
    # add to list
    k.ind <- paste('k', k, sep = '.')
    cluster.list[[k.ind]] <- clust.vec
    sil.list[[k.ind]] <- mean(sil.score[,3])
    # iterate
    k <- k + kstep
  }
  # identify optimal cluster
  opt.clust <- cluster.list[[which.max(sil.list)]]
  # add to data.object
  if (class(data.obj)[1] == "Seurat") {
    data.obj@assays[[[email protected]]]@misc[['pisces.cluster']] <- opt.clust
    data.obj@assays[[[email protected]]]@misc[['clustering.obj']] <- list('clusterings' = cluster.list, 'sils' = sil.list)
    return(data.obj)
  } else {
    return(list('pisces.cluster' = opt.clust, 'clustering.obj' = list('clusterings' = cluster.list, 'sils' = sil.list)))
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions