Question: SetMethod to dispatch on class in unattached package
1
gravatar for brendan.innes
8 months ago by
brendan.innes10 wrote:

Hi friendly Bioc gang! I'm struggling with what seems like a silly problem. I'm trying to write a simple wrapper S4 generic that accesses the data slot of various S4 objects (seurat and SingleCellExperiment objects). So the generic is:

setGeneric("getGE",function(x) standardGeneric("getGE"))

And the methods are:

setMethod("getGE","seurat",
          function(x) Seurat::GetAssayData(x))
setMethod("getGE","SingleCellExperiment",
          function(x) SingleCellExperiment::logcounts(x))

Problem is that when I install the package, I get the warning

> in method for ‘getGE’ with signature ‘"seurat"’: no definition for class “seurat”

This isn't surprising, since Seurat isn't imported, but I don't want the user to have to import it if their data is in a SingleCellExperiment object. The function still seems to work fine if I attach Seurat and load a seurat object, so I'm tempted to just suppress the error. Before I do something so rash, does anyone have a suggestion for a less dumb way to do this?

Thanks so much!
Brendan

ADD COMMENTlink modified 8 months ago by Aaron Lun25k • written 8 months ago by brendan.innes10

And if the solution is to suppress the warning, how exactly would I do that? Because neither options(warn=-1) and suppressWarnings() seems to work. Which is probably because there's a less dumb way to do this, I hope!

ADD REPLYlink written 8 months ago by brendan.innes10
Answer: SetMethod to dispatch on class in unattached package
0
gravatar for Aaron Lun
8 months ago by
Aaron Lun25k
Cambridge, United Kingdom
Aaron Lun25k wrote:

If you're doing package development, this question would be better suited to the BioC-devel mailing list - see https://stat.ethz.ch/mailman/listinfo/bioc-devel

But for what it's worth: suppressing the warning (if that is even possible) is probably a bad idea. I don't know enough about the internal mechanics of S4 dispatch to say why, but the warning was probably put there for good reason - the mailing list members would be more knowledgeable about that. If you really want to avoid importing Seurat, the next-best approach is to not use S4, and to have an ordinary function that just uses the class names in the function body:

getGE <- function(input) {
    if (is(input, "SingleCellExperiment")) {
        # do something
    } else if (is(input, "seurat")) {
        # do something else  
    }
}

... which is not too hard if you've only got two classes to worry about.

ADD COMMENTlink modified 8 months ago • written 8 months ago by Aaron Lun25k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 16.09
Traffic: 217 users visited in the last hour