SetMethod to dispatch on class in unattached package
1
2
Entering edit mode
@brendaninnes-16895
Last seen 5.5 years ago

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

s4 methods SummarizedExperiment • 1.3k views
ADD COMMENT
0
Entering edit mode

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 REPLY
0
Entering edit mode
Aaron Lun ★ 28k
@alun
Last seen 9 hours ago
The city by the bay

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 COMMENT

Login before adding your answer.

Traffic: 916 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6