satijalab / seurat

R toolkit for single cell genomics
http://www.satijalab.org/seurat
Other
2.27k stars 910 forks source link

Non-numeric argument to binary operator #3983

Closed knightsUCF closed 3 years ago

knightsUCF commented 3 years ago

Hi,

I am getting this error:

Error in anno * expect : non-numeric argument to binary operator In addition: Warning messages: 1: In getSigGroups(object, test.stat) : No enrichment can pe performed - there are no feasible GO terms! 2: In .genesInNode(graph(object), whichGO) :

Error in anno * expect : non-numeric argument to binary operator

library(dplyr)
library(Seurat)
library(patchwork)
library(Matrix)
library(topGO)
library(xtable)

pbmc <- readRDS("pbmc.RDS")

topGOterms = function( fg.genes = NULL,
                       bg.genes = NULL,
                       organism = "Mouse", 
                       ontology.use = "BP",
                       stats.use = "fisher",
                       algorithm.use = "weight01",
                       topnodes.print=20,
                       num.char=100){

  if (is.null(fg.genes) | is.null(bg.genes)){
    stop("Error : Both gene lists are empty")
  }

  require(topGO)
  if (organism == "Mouse"){
    mapping.use = "org.Mm.eg.db"
    library(org.Mm.eg.db)
  } else if (organism == "Human"){
    mapping.use = "org.Hs.eg.db"
    library(org.Hs.eg.db)
  } else {
    stop("Error : Organisms other than mouse not supported currently")
  }

  n = length(bg.genes)
  geneList = integer(n)
  names(geneList) = bg.genes
  geneList[intersect(names(geneList), fg.genes)]=1
  print(paste0("Total ", length(geneList), " genes. ", sum(geneList), " genes in the foreground"))
  geneList = factor(geneList)

  if (ontology.use %in% c("BP", "CC", "MF")){
    print(paste0("Using Ontology : ", ontology.use))
  } else {
    stop("Error: Ontology not available. Should be one of BP, CC or MF")
  }
  # Make GO object
  GOdata <- new("topGOdata",
                description = "GOanalysis",
                ontology = ontology.use,
                allGenes = geneList,
                annot = annFUN.org,
                mapping = mapping.use,
                ID = "SYMBOL",
                nodeSize = 10)
  print(paste0("Using the ", stats.use, " statistic with the ", algorithm.use, " algorithm"))
  res.result <- runTest(GOdata, statistic = stats.use, algorithm = algorithm.use)
  to.return = list()
  to.return$GOdata = GOdata
  to.return$res.table <- GenTable(GOdata, pval = res.result, topNodes = topnodes.print, numChar = num.char)
  return(to.return)
}

row_names = GetAssayData(pbmc, slot = "data")

pbmc.cell.type.genes <- unique(pbmc.markers$gene)
GOterms.pbmc = topGOterms(fg.genes = pbmc.cell.type.genes, bg.genes = rownames(row_names), organism = "Mouse")

print(GOterms.pbmc$res.table)

# https://github.com/satijalab/seurat/issues/3276
# "You have a metadata column named "CD69," and our plotting functions usually search metadata before assays. If you rename this column, everything should work fine"

I am wondering if this could be a similar metadata column issue.

Thank you

timoast commented 3 years ago

It looks like this has more to do with the GO term enrichment package topGO than Seurat, I'd suggest seeking support from the topGO authors

knightsUCF commented 3 years ago

Thank you for taking a look at this. I have been spending a lot of time on this issue and just wanted to confirm. There was a similar issue someone had going from Seurat version 2.0 to 3.0, and one of the replies was that something "strange" happened in the upgrade: https://github.com/satijalab/seurat/issues/1165

I am wondering if this is related. The working example I have works for Seurat version 2.0 and not version 3.0, so I am unsure whether this is a topGO issue, but I am looking into this in the meantime:

GOterms.pbmc = topGOterms(fg.genes = pbmc.cell.type.genes, bg.genes = rownames(pbmc@data), organism = "Mouse")

This worked on Seurat 2.0.

However when trying to run this on Seurat 3.0, I am getting:

Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'rownames': no slot of name "data" for this object of class "Seurat"

I am new to R, Seurat, and topGO, and am trying to figure out the shape of the data to transform.

Here they reference that rownames should be accessible by data: (pbmc@data) https://github.com/satijalab/seurat/issues/978

Should in the current Seurat version pbmc@data return row names of genes? At least this seemed to have work on Seurat 2.0.

Thank you.

ktessema commented 3 years ago

Thank you for taking a look at this. I have been spending a lot of time on this issue and just wanted to confirm. There was a similar issue someone had going from Seurat version 2.0 to 3.0, and one of the replies was that something "strange" happened in the upgrade: #1165

I am wondering if this is related. The working example I have works for Seurat version 2.0 and not version 3.0, so I am unsure whether this is a topGO issue, but I am looking into this in the meantime:

GOterms.pbmc = topGOterms(fg.genes = pbmc.cell.type.genes, bg.genes = rownames(pbmc@data), organism = "Mouse")

This worked on Seurat 2.0.

However when trying to run this on Seurat 3.0, I am getting:

Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'rownames': no slot of name "data" for this object of class "Seurat"

I am new to R, Seurat, and topGO, and am trying to figure out the shape of the data to transform.

Here they reference that rownames should be accessible by data: (pbmc@data) #978

Should in the current Seurat version pbmc@data return row names of genes? At least this seemed to have work on Seurat 2.0.

Thank you.

Hi! In case you're still wondering, rownames(object) works for me in Seurat 4. There isn't a 'data' slot, so I'm not sure what @data would be retrieving. Or if you want the genes from a specific assay, you can do rownames(object@assays$RNA) (for the RNA assay). Or for a specific slot: rownames(object@assays$RNA@counts)

Note that rownames(object) will retrieve from the default assay, so probably best to decide which assay you want the genes from and get them specifically from there.

akinbosk commented 3 years ago

I am a similar problem with the function ssanova() from library(gss). R keeps saying " Error in mx - mn : non-numeric argument to binary operator" even though the same function works in an older version of R and R studio.