Skip to content
Prev 18559 / 21312 Next

[Bioc-devel] Best Practices for Renaming S4 Slots

Hi Chris,

There was some formatting issues with my previous answer so I'm sending 
it again. Hopefully this time the formatting is better. See below.
On 14/10/2021 13:08, Chris Eeles wrote:
Yes updateObject() is the proper function to use but the function has no 
way to guess how to fix your objects. The way you tell it what to do is 
by implementing methods for your objects.

For example if you renamed the 'cell' slot -> 'sample', your 
updateObject() method will be something like this:


setMethod("updateObject", "CoreSet",
     function(object, ..., verbose=FALSE)
     {
         ## The "cell" slot was renamed -> "sample" in CoreGx_1.7.1.
         if (.hasSlot(object, "cell")) {
             object <- new("CoreSet",
                           sensitivity=object at sensitivity,
                           annotation=object at annotation,
                           molecularProfiles=object at molecularProfiles,
                           sample=object at cell,
                           datasetType=object at datasetType,
                           perturbation=object at perturbation,
                           curation=object at curation)
             return(object)
         }
         object
     }
)

The best time to do this internal renaming is at the beginning of the 
BioC 3.15 development cycle (i.e. right after the 3.14 release).

If in the future, other slots get renamed or added, you'll need to 
modify the updateObject() method above like this:

setMethod("updateObject", "CoreSet",
     function(object, ..., verbose=FALSE)
     {
         ## The "cell" slot was renamed -> "sample" in CoreGx_1.7.1.
         if (.hasSlot(object, "cell")) {
             object <- new("CoreSet",
                           sensitivity=object at sensitivity,
                           annotation=object at annotation,
                           molecularProfiles=object at molecularProfiles,
                           sample=object at cell,
                           datasetType=object at datasetType,
                           perturbation=object at perturbation,
                           titi=object at curation)  # use "titi" here too!
             return(object)
         }
         ## The "curation" slot was renamed -> "titi" in CoreGx_1.9.1.
         if (.hasSlot(object, "curation")) {
             object <- new("CoreSet",
                           sensitivity=object at sensitivity,
                           annotation=object at annotation,
                           molecularProfiles=object at molecularProfiles,
                           sample=object at sample,  # use "sample" here!
                           datasetType=object at datasetType,
                           perturbation=object at perturbation,
                           titi=object at curation)
             return(object)
         }
         object
     }
)

And so on...

Hope this helps,
H.