Question: SetMethod to dispatch on class in unattached package
1
5 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

modified 5 months ago by Aaron Lun24k • written 5 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!

Answer: SetMethod to dispatch on class in unattached package
0
5 months ago by
Aaron Lun24k
Cambridge, United Kingdom
Aaron Lun24k 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.