11# ContrastsFacades -----
22
3+ .assert_aggregated_facade_input <- function (lfqdata , facade_name ) {
4+ subject_id <- lfqdata $ subject_Id()
5+ hierarchy_keys <- lfqdata $ config $ hierarchy_keys()
6+ if (! identical(subject_id , hierarchy_keys )) {
7+ stop(
8+ facade_name ,
9+ " requires aggregated LFQData. " ,
10+ " `lfqdata$subject_Id()` must equal `lfqdata$config$hierarchy_keys()`. " ,
11+ " Aggregate first." ,
12+ call. = FALSE
13+ )
14+ }
15+ }
16+
17+ .assert_nested_facade_input <- function (lfqdata , facade_name ) {
18+ subject_id <- lfqdata $ subject_Id()
19+ hierarchy_keys <- lfqdata $ config $ hierarchy_keys()
20+ if (! (all(subject_id %in% hierarchy_keys ) && length(subject_id ) < length(hierarchy_keys ))) {
21+ stop(
22+ facade_name ,
23+ " requires LFQData with additional hierarchy below `subject_Id()`. " ,
24+ " `lfqdata$subject_Id()` must be a strict subset of " ,
25+ " `lfqdata$config$hierarchy_keys()`. Do not aggregate first." ,
26+ call. = FALSE
27+ )
28+ }
29+ }
30+
31+ .add_facade_column <- function (res , facade_name ) {
32+ if (! (" facade" %in% colnames(res ))) {
33+ res <- dplyr :: mutate(res , facade = facade_name , .before = 1 )
34+ }
35+ res
36+ }
37+
338# ' Limma contrast analysis facade
439# '
540# ' Encapsulates the pipeline: \code{\link{strategy_limma}} ->
843# ' @export
944# ' @family modelling
1045# ' @examples
11- # ' istar <- sim_lfq_data_peptide_config ()
46+ # ' istar <- sim_lfq_data_protein_config ()
1247# ' lfqdata <- LFQData$new(istar$data, istar$config)
13- # ' lfqdata <- lfqdata$get_Transformer()$log2()$lfq
48+ # ' lfqdata$rename_response("transformedIntensity")
1449# ' contrasts <- c("A_vs_Ctrl" = "group_A - group_Ctrl")
1550# ' fa <- ContrastsLimmaFacade$new(lfqdata, "~ group_", contrasts)
1651# ' head(fa$get_contrasts())
@@ -29,6 +64,7 @@ ContrastsLimmaFacade <- R6::R6Class(
2964 # ' @param contrasts named character vector of contrasts
3065 # ' @param ... passed to \code{\link{strategy_limma}} (e.g. trend, robust)
3166 initialize = function (lfqdata , modelstr , contrasts , ... ) {
67+ .assert_aggregated_facade_input(lfqdata , " ContrastsLimmaFacade" )
3268 response <- lfqdata $ config $ get_response()
3369 full_formula <- paste(response , modelstr )
3470 strat <- strategy_limma(full_formula , ... )
@@ -37,7 +73,9 @@ ContrastsLimmaFacade <- R6::R6Class(
3773 },
3874 # ' @description get contrast results
3975 # ' @param ... passed to ContrastsLimma$get_contrasts
40- get_contrasts = function (... ) self $ contrast $ get_contrasts(... ),
76+ get_contrasts = function (... ) {
77+ .add_facade_column(self $ contrast $ get_contrasts(... ), " limma" )
78+ },
4179 # ' @description get ContrastsPlotter
4280 # ' @param ... passed to ContrastsLimma$get_Plotter
4381 get_Plotter = function (... ) self $ contrast $ get_Plotter(... ),
@@ -57,9 +95,9 @@ ContrastsLimmaFacade <- R6::R6Class(
5795# ' @export
5896# ' @family modelling
5997# ' @examples
60- # ' istar <- sim_lfq_data_peptide_config ()
98+ # ' istar <- sim_lfq_data_protein_config ()
6199# ' lfqdata <- LFQData$new(istar$data, istar$config)
62- # ' lfqdata <- lfqdata$get_Transformer()$log2()$lfq
100+ # ' lfqdata$rename_response("transformedIntensity")
63101# ' contrasts <- c("A_vs_Ctrl" = "group_A - group_Ctrl")
64102# ' fa <- ContrastsLMFacade$new(lfqdata, "~ group_", contrasts)
65103# ' head(fa$get_contrasts())
@@ -78,6 +116,7 @@ ContrastsLMFacade <- R6::R6Class(
78116 # ' @param contrasts named character vector of contrasts
79117 # ' @param ... passed to \code{\link{strategy_lm}}
80118 initialize = function (lfqdata , modelstr , contrasts , ... ) {
119+ .assert_aggregated_facade_input(lfqdata , " ContrastsLMFacade" )
81120 response <- lfqdata $ config $ get_response()
82121 full_formula <- paste(response , modelstr )
83122 strat <- strategy_lm(full_formula , ... )
@@ -86,7 +125,9 @@ ContrastsLMFacade <- R6::R6Class(
86125 },
87126 # ' @description get contrast results
88127 # ' @param ... passed to ContrastsModerated$get_contrasts
89- get_contrasts = function (... ) self $ contrast $ get_contrasts(... ),
128+ get_contrasts = function (... ) {
129+ .add_facade_column(self $ contrast $ get_contrasts(... ), " lm" )
130+ },
90131 # ' @description get ContrastsPlotter
91132 # ' @param ... passed to ContrastsModerated$get_Plotter
92133 get_Plotter = function (... ) self $ contrast $ get_Plotter(... ),
@@ -97,6 +138,66 @@ ContrastsLMFacade <- R6::R6Class(
97138)
98139
99140
141+ # ' Lmer contrast analysis facade
142+ # '
143+ # ' Encapsulates the pipeline: \code{\link{strategy_lmer}} ->
144+ # ' \code{\link{build_model}} -> \code{\link{Contrasts}} ->
145+ # ' \code{\link{ContrastsModerated}}.
146+ # '
147+ # ' This facade requires data with hierarchy below the analysis subject, for
148+ # ' example peptide-level measurements nested within proteins.
149+ # '
150+ # ' @export
151+ # ' @family modelling
152+ # ' @examples
153+ # ' istar <- sim_lfq_data_peptide_config()
154+ # ' istar$config <- old2new(istar$config)
155+ # ' lfqdata <- LFQData$new(istar$data, istar$config)
156+ # ' lfqdata <- lfqdata$get_Transformer()$log2()$lfq
157+ # ' contrasts <- c("A_vs_Ctrl" = "group_A - group_Ctrl")
158+ # ' fa <- ContrastsLmerFacade$new(
159+ # ' lfqdata,
160+ # ' "~ group_ + (1 | peptide_Id) + (1 | sampleName)",
161+ # ' contrasts
162+ # ' )
163+ # ' head(fa$get_contrasts())
164+ # ' fa$to_wide()
165+ ContrastsLmerFacade <- R6 :: R6Class(
166+ " ContrastsLmerFacade" ,
167+ public = list (
168+ # ' @field model Model object
169+ model = NULL ,
170+ # ' @field contrast ContrastsModerated object
171+ contrast = NULL ,
172+ # ' @description
173+ # ' initialize
174+ # ' @param lfqdata LFQData object
175+ # ' @param modelstr model formula string (e.g. "~ group_ + (1 | peptide_Id)")
176+ # ' @param contrasts named character vector of contrasts
177+ # ' @param ... passed to \code{\link{strategy_lmer}}
178+ initialize = function (lfqdata , modelstr , contrasts , ... ) {
179+ .assert_nested_facade_input(lfqdata , " ContrastsLmerFacade" )
180+ response <- lfqdata $ config $ get_response()
181+ full_formula <- paste(response , modelstr )
182+ strat <- strategy_lmer(full_formula , ... )
183+ self $ model <- build_model(lfqdata , strat )
184+ self $ contrast <- ContrastsModerated $ new(Contrasts $ new(self $ model , contrasts ))
185+ },
186+ # ' @description get contrast results
187+ # ' @param ... passed to ContrastsModerated$get_contrasts
188+ get_contrasts = function (... ) {
189+ .add_facade_column(self $ contrast $ get_contrasts(... ), " lmer" )
190+ },
191+ # ' @description get ContrastsPlotter
192+ # ' @param ... passed to ContrastsModerated$get_Plotter
193+ get_Plotter = function (... ) self $ contrast $ get_Plotter(... ),
194+ # ' @description convert results to wide format
195+ # ' @param ... passed to ContrastsModerated$to_wide
196+ to_wide = function (... ) self $ contrast $ to_wide(... )
197+ )
198+ )
199+
200+ # '
100201# ' LM + missing-value imputation contrast analysis facade
101202# '
102203# ' Encapsulates the pipeline: \code{\link{strategy_lm}} ->
@@ -136,6 +237,7 @@ ContrastsLMMissingFacade <- R6::R6Class(
136237 # ' @param contrasts named character vector of contrasts
137238 # ' @param ... passed to \code{\link{strategy_lm}}
138239 initialize = function (lfqdata , modelstr , contrasts , ... ) {
240+ .assert_aggregated_facade_input(lfqdata , " ContrastsLMMissingFacade" )
139241 response <- lfqdata $ config $ get_response()
140242 full_formula <- paste(response , modelstr )
141243 strat <- strategy_lm(full_formula , ... )
@@ -147,7 +249,9 @@ ContrastsLMMissingFacade <- R6::R6Class(
147249 },
148250 # ' @description get contrast results
149251 # ' @param ... passed to ContrastsTable$get_contrasts
150- get_contrasts = function (... ) self $ contrast $ get_contrasts(... ),
252+ get_contrasts = function (... ) {
253+ .add_facade_column(self $ contrast $ get_contrasts(... ), " lm_missing" )
254+ },
151255 # ' @description get ContrastsPlotter
152256 # ' @param ... passed to ContrastsTable$get_Plotter
153257 get_Plotter = function (... ) self $ contrast $ get_Plotter(... ),
@@ -189,6 +293,7 @@ ContrastsDEqMSFacade <- R6::R6Class(
189293 # ' @param contrasts named character vector of contrasts
190294 # ' @param ... passed to \code{\link{strategy_lm}}
191295 initialize = function (lfqdata , modelstr , contrasts , ... ) {
296+ .assert_aggregated_facade_input(lfqdata , " ContrastsDEqMSFacade" )
192297 response <- lfqdata $ config $ get_response()
193298 full_formula <- paste(response , modelstr )
194299 strat <- strategy_lm(full_formula , ... )
@@ -203,7 +308,9 @@ ContrastsDEqMSFacade <- R6::R6Class(
203308 },
204309 # ' @description get contrast results
205310 # ' @param ... passed to ContrastsModeratedDEqMS$get_contrasts
206- get_contrasts = function (... ) self $ contrast $ get_contrasts(... ),
311+ get_contrasts = function (... ) {
312+ .add_facade_column(self $ contrast $ get_contrasts(... ), " deqms" )
313+ },
207314 # ' @description get ContrastsPlotter
208315 # ' @param ... passed to ContrastsModeratedDEqMS$get_Plotter
209316 get_Plotter = function (... ) self $ contrast $ get_Plotter(... ),
@@ -249,6 +356,7 @@ ContrastsROPECAFacade <- R6::R6Class(
249356 # ' @param contrasts named character vector of contrasts
250357 # ' @param ... passed to \code{\link{strategy_lm}}
251358 initialize = function (lfqdata , modelstr , contrasts , ... ) {
359+ .assert_nested_facade_input(lfqdata , " ContrastsROPECAFacade" )
252360 response <- lfqdata $ config $ get_response()
253361 full_formula <- paste(response , modelstr )
254362 strat <- strategy_lm(full_formula , ... )
@@ -293,7 +401,8 @@ ContrastsROPECAFacade <- R6::R6Class(
293401 standard_cols <- c(protein_Id , " modelName" , " contrast" , " avgAbd" ,
294402 " diff" , " FDR" , " statistic" , " std.error" , " df" ,
295403 " p.value" , " conf.low" , " conf.high" , " sigma" )
296- res [, standard_cols , drop = FALSE ]
404+ res <- res [, standard_cols , drop = FALSE ]
405+ .add_facade_column(res , " ropeca" )
297406 },
298407 # ' @description get ContrastsPlotter (uses standardized column names)
299408 # ' @param FCthreshold fold change threshold
0 commit comments