[Bioc-devel] Adding additional validity checks when calling setter methods of inherited S4Vectors::DataFrame class
Lluís Revilla
||u|@@rev|||@ @end|ng |rom gm@||@com
Sat Jan 4 11:51:57 CET 2025
Hi Pariksheet,
You can check if the object is valid inside your methods.
I use something like:
method <- function(...) {
# Input processing
validObject(object)
object
}
So your setter method `[<-` could have this call inside it, and this
would prevent the creation of an invalid object.
In this case, even keeping the class and without adding the validity
check on the [<- method, the validation of the input data could be
done inside the function making the requests to the website.
I am part of a working group trying to make it easier for people to
use classes on Bioconductor.
I would like to write a guide about when to use classes and how to
choose between the different object oriented paradigms existing on R
(including the new S7) and when to use it. Why do you use a new S4
class for parameter input? Your comments on what made you try this
route would be helpful, thanks!
Best,
Lluís
On Fri, 3 Jan 2025 at 01:17, Nanda, Pariksheet via Bioc-devel
<bioc-devel using r-project.org> wrote:
>
> Hello S4-class boffins,
>
> How bad of an idea it is to inherit from a S4Vectors::DataFrame / DFrame S4 class to impose additional constraints on it? I'm writing a light-weight wrapper around the ToppGene web API (github com/ImmuSystems-Lab/toppgene/blob/main/R/categories.R) [1] and while it's functional, it currently only runs JSON web queries using default values. To pass non-default values, each category queried needs to have associated parameters within some boundaries. While it's intuitive for a Biconductor user to see and manipulate DataFrames containing the parameters, the trouble I'm seeing is that validObject() is of course not automagically run on the dispatched S4Vectors setters and I don't know how to inject validObject() into the process without rewriting / repeating a lot of the S4Vectors method implementation internals; callNextMethod() does not seem like it would work? Currently, the only time validObject() is called is when invoking the constructor, CategoriesDataFrame() and because code is worth a thousand words, see below the "---" line for what I mean.
>
> Pariksheet
>
> [1] Yes, I'm trying to avoid the GitHub URL from being mangled into illegible horrors by removing the protocol prefix and the dot before the domain, so you'll have to add at least the latter back in to visit the GitHub page.
>
> ---
>
> > devtools::load_all()
> [...]
>
> > cats <- CategoriesDataFrame()
>
> > cats
> ToppGene CategoriesDataFrame with 19 enabled categories
> PValue MinGenes MaxGenes MaxResults Correction Enabled
> Coexpression 0.05 2 1500 50 FDR TRUE
> CoexpressionAtlas 0.05 2 1500 50 FDR TRUE
> Computational 0.05 2 1500 50 FDR TRUE
> Cytoband 0.05 2 1500 50 FDR TRUE
> Disease 0.05 2 1500 50 FDR TRUE
> Domain 0.05 2 1500 50 FDR TRUE
> Drug 0.05 2 1500 50 FDR TRUE
> GeneFamily 0.05 2 1500 50 FDR TRUE
> GeneOntologyBiologicalProcess 0.05 2 1500 50 FDR TRUE
> GeneOntologyCellularComponent 0.05 2 1500 50 FDR TRUE
> GeneOntologyMolecularFunction 0.05 2 1500 50 FDR TRUE
> HumanPheno 0.05 2 1500 50 FDR TRUE
> Interaction 0.05 2 1500 50 FDR TRUE
> MicroRNA 0.05 2 1500 50 FDR TRUE
> MousePheno 0.05 2 1500 50 FDR TRUE
> Pathway 0.05 2 1500 50 FDR TRUE
> Pubmed 0.05 2 1500 50 FDR TRUE
> TFBS 0.05 2 1500 50 FDR TRUE
> ToppCell 0.05 2 1500 50 FDR TRUE
> ------------------------------
> Values allowed by ToppGene are:
> PValue: [0, 1] <numeric>
> MinGenes: [1, 5000] <integer>
> MaxGenes: [2, 5000] <integer>
> MaxResults: [1, 5000] <integer>
> Correction: {None, FDR, Bonferroni} <character>
>
> ## This next line should not complete without an error! But it does.
> > cats[, "PValue"] <- 2
>
> ## Explicitly calling validObject() will point out the problem post-hoc,
> ## but not prevent the above assignment.
> > validObject(cats)
> Error in validObject(cats) :
> invalid class “CategoriesDataFrame” object: column PValue must contain values <= 1
>
> _______________________________________________
> Bioc-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/bioc-devel
More information about the Bioc-devel
mailing list