10x-scATAC-2019 icon indicating copy to clipboard operation
10x-scATAC-2019 copied to clipboard

02

Open hjack123 opened this issue 5 years ago • 8 comments

Making Seurat LSI Object... Binarizing matrix... Getting top 20000 features... Computing Term Frequency IDF... Computing SVD using irlba... Making Seurat Object... Error in CreateSeuratObject(mat, project = "scATAC", min.cells = 0, min.genes = 0) : unused argument (min.genes = 0)

hjack123 avatar Aug 11 '19 08:08 hjack123

Making Seurat LSI Object... Binarizing matrix... Getting top 20000 features... Computing Term Frequency IDF... Computing SVD using irlba... Making Seurat Object... Error in CreateAssayObject(counts = counts, min.cells = min.cells, min.features = min.features) : No feature names (rownames) names present in the input matrix

hjack123 avatar Aug 11 '19 09:08 hjack123

Sorry I am not familiar with Seurat V3, the error message seems to be focused on the fact that there are no rownames in the input counts object. Just add somewhere rownames whether it be simply

rownames(counts) <- paste0("n",seq_len(nrow(counts)))

jgranja24 avatar Aug 12 '19 02:08 jgranja24

@hjack123 I modify the origin code that use Seurat V3 to do LSI dimension reduction and find cluster

seuratLSI <- function(mat, nComponents = 50,
                        binarize = TRUE,
                        nFeatures = NULL, ...){

  #TF IDF LSI adapted from flyATAC
  cs <- Matrix::colSums(mat)
  if(binarize){
    message(paste0("Binarizing matrix..."))
    mat@x[mat@x > 0] <- 1
  }
  if(!is.null(nFeatures)){
    message(paste0("Getting top ", nFeatures, " features..."))
    mat <- mat[head(order(Matrix::rowSums(mat),decreasing = TRUE),nFeatures),]
  }

  # create a seurat object
  row.names(mat) <- seq(1, nrow(mat))
  obj <- CreateSeuratObject(mat,
                            assay = "ATAC",
                            project='scATAC',
                            min.cells=0, min.features=0)
  #Calc TF IDF, Calc SVD then LSI
  obj <- RunLSI(object = obj, n = nComponents, scale.max = NULL)

  return(obj)

}
addClusters <- function(obj,
                        minGroupSize = 50,
                        dims.use = seq_len(50),
                        initialResolution = 0.8,
                        n.start = 10, ...){
  # get the SNN
  obj<- FindNeighbors(obj, reduction = "lsi", dims = dims.use)

  #First Iteration of Find Clusters

  currentResolution <- initialResolution
  obj <- FindClusters(obj, resolution = currentResolution)
  res_name <-  colnames([email protected])[grepl(currentResolution, colnames([email protected]))]
  minSize <- min(table([email protected][[res_name]]))
  nClust <- length(unique(paste0([email protected][[res_name]])))
  message(sprintf("Current Resolution = %s, No of Clusters = %s, Minimum Cluster Size = %s", currentResolution, nClust, minSize))

  if (is.null(minGroupSize)){
    return(obj)
  }

  #If clusters are smaller than minimum group size
  while(minSize <= minGroupSize){

    res_name <-  colnames([email protected])[grepl(currentResolution, colnames([email protected]))]

    [email protected] <- [email protected][,-which(colnames([email protected])==res_name)]
    currentResolution <- currentResolution*initialResolution
    obj <- FindClusters(obj, resolution = currentResolution)

    res_name <-  colnames([email protected])[grepl(currentResolution, colnames([email protected]))]
    minSize <- min(table([email protected][[res_name]]))
    nClust <- length(unique(paste0([email protected][[res_name]])))

    message(sprintf("Current Resolution = %s, No of Clusters = %s, Minimum Cluster Size = %s", currentResolution, nClust, minSize))
  }
  return(obj)
}

xuzhougeng avatar Aug 13 '19 10:08 xuzhougeng

@hjack123 I modify the origin code that use Seurat V3 to do LSI dimension reduction and find cluster

seuratLSI <- function(mat, nComponents = 50,
                        binarize = TRUE,
                        nFeatures = NULL, ...){

  #TF IDF LSI adapted from flyATAC
  cs <- Matrix::colSums(mat)
  if(binarize){
    message(paste0("Binarizing matrix..."))
    mat@x[mat@x > 0] <- 1
  }
  if(!is.null(nFeatures)){
    message(paste0("Getting top ", nFeatures, " features..."))
    mat <- mat[head(order(Matrix::rowSums(mat),decreasing = TRUE),nFeatures),]
  }

  # create a seurat object
  row.names(mat) <- seq(1, nrow(mat))
  obj <- CreateSeuratObject(mat,
                            assay = "ATAC",
                            project='scATAC',
                            min.cells=0, min.features=0)
  #Calc TF IDF, Calc SVD then LSI
  obj <- RunLSI(object = obj, n = nComponents, scale.max = NULL)

  return(obj)

}
addClusters <- function(obj,
                        minGroupSize = 50,
                        dims.use = seq_len(50),
                        initialResolution = 0.8,
                        n.start = 10, ...){
  # get the SNN
  obj<- FindNeighbors(obj, reduction = "lsi", dims = dims.use)

  #First Iteration of Find Clusters

  currentResolution <- initialResolution
  obj <- FindClusters(obj, resolution = currentResolution)
  res_name <-  colnames([email protected])[grepl(currentResolution, colnames([email protected]))]
  minSize <- min(table([email protected][[res_name]]))
  nClust <- length(unique(paste0([email protected][[res_name]])))
  message(sprintf("Current Resolution = %s, No of Clusters = %s, Minimum Cluster Size = %s", currentResolution, nClust, minSize))

  if (is.null(minGroupSize)){
    return(obj)
  }

  #If clusters are smaller than minimum group size
  while(minSize <= minGroupSize){

    res_name <-  colnames([email protected])[grepl(currentResolution, colnames([email protected]))]

    [email protected] <- [email protected][,-which(colnames([email protected])==res_name)]
    currentResolution <- currentResolution*initialResolution
    obj <- FindClusters(obj, resolution = currentResolution)

    res_name <-  colnames([email protected])[grepl(currentResolution, colnames([email protected]))]
    minSize <- min(table([email protected][[res_name]]))
    nClust <- length(unique(paste0([email protected][[res_name]])))

    message(sprintf("Current Resolution = %s, No of Clusters = %s, Minimum Cluster Size = %s", currentResolution, nClust, minSize))
  }
  return(obj)
}

Hi with your code I got the following msg, would you happen to have a solution? 1: RunLSI is being moved to Signac. Equivalent functionality can be achieved via the Signac::RunTFIDF and Signac::RunSVD functions; for more information on Signac, please see https://github.com/timoast/Signac 2: RunLSI is being moved to Signac. Equivalent functionality can be achieved via the Signac::RunTFIDF and Signac::RunSVD functions; for more information on Signac, please see https://github.com/timoast/Signac Error in LogNorm(data = tf.idf, display_progress = verbose, scale_factor = 10000) : No such slot: i.

thanks!

hjack123 avatar Jan 14 '20 11:01 hjack123

@hjack123 I got the msg too. It is beacuase the Seurat Group have develop a new package to process the scATAC-seq data, it is called Signac, you can ignore it.

xuzhougeng avatar Feb 03 '20 06:02 xuzhougeng

I get the same warnings as @hjack123 , but the real issue is the error:

Error in LogNorm(data = tf.idf, display_progress = verbose, scale_factor = 10000) : 
  No such slot: i. 

jfass avatar Mar 02 '20 20:03 jfass

I was able to solve that by setting the variable features as the row.names before running RunLSI in the seuratLSI function.

VariableFeatures(obj) <- row.names(obj)


#TF IDF LSI adapted from flyATAC
  cs <- Matrix::colSums(mat)
  if(binarize){
    message(paste0("Binarizing matrix..."))
    mat@x[mat@x > 0] <- 1
  }
  if(!is.null(nFeatures)){
    message(paste0("Getting top ", nFeatures, " features..."))
    mat <- mat[head(order(Matrix::rowSums(mat),decreasing = TRUE),nFeatures),]
  }
  #create a seurat object
  obj <- CreateSeuratObject(mat,
                            assay = "ATAC",
                            project='scATAC',
                            min.cells=0, min.features=0)
  #Calc TF IDF, Calc SVD then LSI
  VariableFeatures(obj) <- row.names(obj)
  obj <- RunLSI(object = obj, n = nComponents, scale.max = NULL)
  
  return(obj)
  
}

craiga02 avatar Mar 04 '20 15:03 craiga02

Thanks, @craiga02 for helping out with this issue! only a small correction. Add the row.names(mat) <- seq(1, nrow(mat)) before creating the Seurat object. It was before but is missing now somehow ;)

#create a seurat object
  row.names(mat) <- seq(1, nrow(mat))
  obj <- CreateSeuratObject(mat,
                            assay = "ATAC",
                            project='scATAC',
                            min.cells=0, min.features=0)
  #Calc TF IDF, Calc SVD then LSI
  VariableFeatures(obj) <- row.names(obj)
  obj <- RunLSI(object = obj, n = nComponents, scale.max = NULL)
  
  return(obj)

ccruizm avatar Aug 03 '20 08:08 ccruizm