diff --git a/R/BSseq-mod-class.R b/R/BSseq-mod-class.R index dbb330f..7c50a78 100644 --- a/R/BSseq-mod-class.R +++ b/R/BSseq-mod-class.R @@ -9,6 +9,46 @@ contains = "RangedSummarizedExperiment" ) +# Validity methods ------------------------------------------------------------- + +setValidity("BSseq_mod", function(object) { + msg <- NULL + + if (identical(object, .BSseq_mod())) { + # No validity checks for object returned by internal constructor + return(msg) + } + + msg <- validMsg(msg, bsseq:::.checkAssayNames(object, c("U", "M", "Other_mod"))) + + if(class(rowRanges(object)) != "GRanges") { + msg <- validMsg(msg, sprintf("object of class '%s' needs to have a + 'GRanges' in slot 'rowRanges'", class(object))) + } + + ## benchmarking shows that min(assay()) < 0 is faster than any(assay() < 0) if it is false + if(min(assay(object, "M")) < 0 || any(is.null(assay(object, "M"))) || + any(is.infinite(assay(object, "M")))){ + msg <- validMsg(msg, "the 'M' assay has negative, NA or infinite entries") + } + + if(min(assay(object, "U")) < 0 || any(is.null(assay(object, "U"))) || + any(is.infinite(assay(object, "U")))) { + msg <- validMsg(msg, "the 'U' assay has negative, NA or infinite entries") + } + + if(min(assay(object, "Other_mod")) < 0 || any(is.null(assay(object, "Other_mod"))) || + any(is.infinite(assay(object, "Other_mod")))){ + msg <- validMsg(msg, "the 'Other_mod' assay has negative, NA or inifinite entries") + } + + if (is.null(msg)) { + TRUE + } else { + msg + } +}) + # Exported functions ----------------------------------------------------------- # BSseq class object for handling other modification calls generated by modkit