GreenleafLab / 10x-scATAC-2019

Publication Page for Satpathy*, Granja* et al 2019
92 stars 54 forks source link

02 #5

Open hjack123 opened 5 years ago

hjack123 commented 5 years ago

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 commented 5 years ago

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

jgranja24 commented 5 years ago

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)))

xuzhougeng commented 5 years ago

@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(obj@meta.data)[grepl(currentResolution, colnames(obj@meta.data))]
  minSize <- min(table(obj@meta.data[[res_name]]))
  nClust <- length(unique(paste0(obj@meta.data[[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(obj@meta.data)[grepl(currentResolution, colnames(obj@meta.data))]

    obj@meta.data <- obj@meta.data[,-which(colnames(obj@meta.data)==res_name)]
    currentResolution <- currentResolution*initialResolution
    obj <- FindClusters(obj, resolution = currentResolution)

    res_name <-  colnames(obj@meta.data)[grepl(currentResolution, colnames(obj@meta.data))]
    minSize <- min(table(obj@meta.data[[res_name]]))
    nClust <- length(unique(paste0(obj@meta.data[[res_name]])))

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

@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(obj@meta.data)[grepl(currentResolution, colnames(obj@meta.data))]
  minSize <- min(table(obj@meta.data[[res_name]]))
  nClust <- length(unique(paste0(obj@meta.data[[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(obj@meta.data)[grepl(currentResolution, colnames(obj@meta.data))]

    obj@meta.data <- obj@meta.data[,-which(colnames(obj@meta.data)==res_name)]
    currentResolution <- currentResolution*initialResolution
    obj <- FindClusters(obj, resolution = currentResolution)

    res_name <-  colnames(obj@meta.data)[grepl(currentResolution, colnames(obj@meta.data))]
    minSize <- min(table(obj@meta.data[[res_name]]))
    nClust <- length(unique(paste0(obj@meta.data[[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!

xuzhougeng commented 4 years ago

@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.

jfass commented 4 years ago

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. 
craiga02 commented 4 years ago

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)

}
ccruizm commented 4 years ago

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)