drisso / SingleCellExperiment

Clone of the Bioconductor repository for the SingleCellExperiment package, see https://bioconductor.org/packages/devel/bioc/html/SingleCellExperiment.html for the official development version.
66 stars 18 forks source link

Error when making a class inheriting from SingleClassExperiment #69

Open HarkiratSohi opened 1 year ago

HarkiratSohi commented 1 year ago

I get the following error when I use setClass to define a class , with contains = SingleCellExperiment and when I specify a new attribute in "slot ="

Error in checkSlotAssignment(object, name, value) : assignment of an object of class "S4" is not valid for slot 'int_elementMetadata' in an object of class "SingleCellExperiment"; is(value, "DataFrame") is not TRUE

I don't get this error if I make a new class using setClass, inheriting from SummarizedExperiment. I've searched a lot on the internet but have not been able to resolve this issue. Is this an issue specific to SingleCellExperiment? I can't quite tell. Any help would be appreciated.

LTLA commented 1 year ago

hard to say. can you give a MRE?

HarkiratSohi commented 1 year ago

Thanks for looking into this. Here is a MRE:

The following gives no errors:

setClass("testClass1", contains = "SingleCellExperiment" ) tc1 = new("testClass1")

Simple test: adding a slot

The following gives the noted error

setClass("testClass4", slots = c(coordinates="numeric"), contains = "SingleCellExperiment" ) tc4 = new("testClass4",coordinates = 3)

Error in checkSlotAssignment(object, name, value) : assignment of an object of class "S4" is not valid for slot 'int_elementMetadata' in an object of class "SingleCellExperiment"; is(value, "DataFrame") is not TRUE

I was able to trace back and find that this error is reported when possibleExtends("S4", "DataFrame", ClassDef2 = getClassDef("DataFrame", where = .classEnv(DataFrame))) is false.

Thank you for your help!

HarkiratSohi commented 1 year ago

To follow up, switching to contains = "SummarizedExperiment" doesn't give the error.

No error here:

setClass("testClass5", slots = c(coordinates="numeric"), contains = "SummarizedExperiment" ) tc5 = new("testClass5",coordinates = 3)

HarkiratSohi commented 1 year ago

@LTLA Could someone from the SingleCellExperiment team provide some guidance on this issue? Thank you!

LTLA commented 1 year ago

Probably because there's no prototype set up for the SCE class. In theory, we should add a prototype, but in practice, it doesn't matter, because very few people should be constructing an empty SCE with new anyway. In your case, I suspect that you already have an SCE that you want to convert to your custom class, in which case you can just do:

tc4 = new("testClass4", sce, coordinates=3)

Or, if you really do want an empty instance of your new class:

tc4 = new("testClass4", SingleCellExperiment(), coordinates=3)
HarkiratSohi commented 1 year ago

Thanks @LTLA , that's helpful to know. I basically want to extend the functionality of an existing class.

LTLA commented 1 year ago

Sure, and that's fine, but it seems highly unlikely that anyone will be calling new to construct an instance of your class. If an instance is to be constructed, it should be done using the calls containing sce as shown above.

Check out the SpatialExperiment package for an example of an extended SCE.

HarkiratSohi commented 1 year ago

Thanks!