Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/dataProcess.R
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ MSstatsSummarizeSingleLinear = function(single_protein,

cols = intersect(
colnames(single_protein),
c("newABUNDANCE", "cen", "RUN", "FEATURE", "ref")
c("newABUNDANCE", "cen", "RUN", "FEATURE", "ref_covariate")
)

single_protein = single_protein[
Expand Down Expand Up @@ -513,7 +513,7 @@ MSstatsSummarizeSingleTMP = function(single_protein, impute, censored_symbol,
newABUNDANCE = n_obs = n_obs_run = RUN = FEATURE = LABEL = NULL
predicted = censored = NULL
cols = intersect(colnames(single_protein), c("newABUNDANCE", "cen", "RUN",
"FEATURE", "ref"))
"FEATURE", "ref_covariate"))
single_protein = single_protein[(n_obs > 1 & !is.na(n_obs)) &
(n_obs_run > 0 & !is.na(n_obs_run))]
if (nrow(single_protein) == 0) {
Expand Down
6 changes: 3 additions & 3 deletions R/utils_imputation.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
if (length(unique(input$FEATURE)) == 1) {
# with single feature, not converge, wrong intercept
# need to check
fit = survreg(Surv(newABUNDANCE, cen, type='left') ~ RUN + ref,
fit = survreg(Surv(newABUNDANCE, cen, type='left') ~ RUN + ref_covariate,
data = input, dist = "gaussian",
control = list(maxiter=aft_iterations))
} else {
if (countdf) {
fit = survreg(Surv(newABUNDANCE, cen, type='left') ~ RUN + ref,
fit = survreg(Surv(newABUNDANCE, cen, type='left') ~ RUN + ref_covariate,
data = input, dist = "gaussian",
control = list(maxiter=aft_iterations))
} else {
fit = survreg(Surv(newABUNDANCE, cen, type='left') ~ FEATURE + RUN + ref,
fit = survreg(Surv(newABUNDANCE, cen, type='left') ~ FEATURE + RUN + ref_covariate,
data = input, dist = "gaussian",
control = list(maxiter=aft_iterations))
}
Expand Down
4 changes: 2 additions & 2 deletions R/utils_summarization.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@
}
} else {
if (is_single_feature) {
linear_model = lm(ABUNDANCE ~ RUN + ref , data = input)
linear_model = lm(ABUNDANCE ~ RUN + ref_covariate, data = input)
} else {
linear_model = lm(ABUNDANCE ~ FEATURE + RUN + ref, data = input)
linear_model = lm(ABUNDANCE ~ FEATURE + RUN + ref_covariate, data = input)
}
}
if (!equal_variances) {
Expand Down
2 changes: 1 addition & 1 deletion R/utils_summarization_prepare.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ MSstatsPrepareForSummarization = function(input, method, impute, censored_symbol

label = data.table::uniqueN(input$LABEL) == 2
if (label) {
input[, ref := factor(ifelse(LABEL == "L", RUN, 0))]
input[, ref_covariate := factor(ifelse(LABEL == "L", RUN, 0))]
}

if (is.element("remove", colnames(input))) {
Expand Down
8 changes: 8 additions & 0 deletions inst/tinytest/test_dataProcess.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ expect_true(
nrow(QuantDataDefault$ProteinLevelData) > 0,
info = "SRMRawData must produce non-empty ProteinLevelData"
)
expect_true(
"LogIntensities" %in% colnames(QuantDataDefault$ProteinLevelData),
info = "regression: ProteinLevelData must have LogIntensities column"
)
expect_true(
"GROUP" %in% colnames(QuantDataDefault$ProteinLevelData),
info = "regression: ProteinLevelData must have GROUP column"
)


# Test dataProcess with technical replicates & fractions ------------------
Expand Down
133 changes: 133 additions & 0 deletions inst/tinytest/test_utils_imputation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
make_surv_labeled_single <- function() {
runs <- paste0("R", 1:3)
dt <- data.table::rbindlist(list(
data.table::data.table(
FEATURE = factor(rep("F1", 9)),
RUN = factor(rep(runs, each = 3)),
LABEL = "H",
newABUNDANCE = seq(10.1, by = 0.1, length.out = 9),
cen = 1L
),
data.table::data.table(
FEATURE = factor(rep("F1", 9)),
RUN = factor(rep(runs, each = 3)),
LABEL = "L",
newABUNDANCE = seq(14.1, by = 0.1, length.out = 9),
cen = 1L
)
))
ref_vals <- ifelse(dt$LABEL == "L", as.character(dt$RUN), "0")
dt[["ref_covariate"]] <- factor(ref_vals, levels = c("0", runs))
dt
}

make_surv_labeled_multi_welldetermined <- function() {
features <- paste0("F", 1:3)
runs <- paste0("R", 1:4)
dt <- data.table::CJ(FEATURE = features, RUN = runs, LABEL = c("H", "L"))
dt[, FEATURE := factor(FEATURE)]
dt[, RUN := factor(RUN)]
dt[, newABUNDANCE := 10 + as.integer(FEATURE) + as.integer(RUN) * 0.5 +
ifelse(LABEL == "L", 4, 0)]
dt[, cen := 1L]
ref_vals <- ifelse(dt$LABEL == "L", as.character(dt$RUN), "0")
dt[["ref_covariate"]] <- factor(ref_vals, levels = c("0", runs))
dt
}

make_surv_labeled_underdetermined <- function() {
runs <- c("R1", "R2", "R3")
dt_h <- data.table::data.table(
FEATURE = factor(paste0("F", 1:8)),
RUN = factor(rep_len(runs, 8)),
LABEL = "H",
newABUNDANCE = seq(10, by = 0.5, length.out = 8),
cen = 1L
)
dt_l <- data.table::data.table(
FEATURE = factor("F1"),
RUN = factor("R1"),
LABEL = "L",
newABUNDANCE = 14,
cen = 1L
)
dt <- data.table::rbindlist(list(dt_h, dt_l))
ref_vals <- ifelse(dt$LABEL == "L", as.character(dt$RUN), "0")
dt[["ref_covariate"]] <- factor(ref_vals, levels = c("0", runs))
dt
}

make_surv_unlabeled_single <- function() {
data.table::data.table(
FEATURE = factor(rep("F1", 15)),
RUN = factor(rep(paste0("R", 1:5), each = 3)),
LABEL = "L",
newABUNDANCE = seq(10, by = 0.5, length.out = 15),
cen = 1L
)
}

make_surv_unlabeled_multi_welldetermined <- function() {
dt <- data.table::CJ(
FEATURE = paste0("F", 1:3),
RUN = paste0("R", 1:5)
)
dt[, FEATURE := factor(FEATURE)]
dt[, RUN := factor(RUN)]
dt[, LABEL := "L"]
dt[, newABUNDANCE := seq(10, by = 0.5, length.out = .N)]
dt[, cen := 1L]
dt
}

coef_names <- function(fit) names(coef(fit))
surv_labeled_single <- MSstats:::.fitSurvival(make_surv_labeled_single(), 90)

expect_true(
any(grepl("ref_covariate", coef_names(surv_labeled_single))),
info = ".fitSurvival labeled single-feature: ref_covariate must appear in coefficients"
)
expect_false(
any(grepl("^FEATURE", coef_names(surv_labeled_single))),
info = ".fitSurvival labeled single-feature: FEATURE must not appear (only one feature)"
)

surv_labeled_multi_wd <- MSstats:::.fitSurvival(make_surv_labeled_multi_welldetermined(), 90)
expect_true(
any(grepl("ref_covariate", coef_names(surv_labeled_multi_wd))),
info = ".fitSurvival labeled multi well-determined: ref_covariate must appear in coefficients"
)
expect_true(
any(grepl("^FEATURE", coef_names(surv_labeled_multi_wd))),
info = ".fitSurvival labeled multi well-determined: FEATURE must appear in coefficients"
)

surv_labeled_under <- MSstats:::.fitSurvival(make_surv_labeled_underdetermined(), 90)
expect_true(
any(grepl("ref_covariate", coef_names(surv_labeled_under))),
info = ".fitSurvival labeled underdetermined: ref_covariate must appear in fallback coefficients"
)
expect_false(
any(grepl("^FEATURE", coef_names(surv_labeled_under))),
info = ".fitSurvival labeled underdetermined: FEATURE must not appear in fallback formula"
)

surv_unlabeled_single <- MSstats:::.fitSurvival(make_surv_unlabeled_single(), 90)
expect_false(
any(grepl("ref_covariate", coef_names(surv_unlabeled_single))),
info = ".fitSurvival unlabeled single-feature: ref_covariate must not appear"
)
expect_false(
any(grepl("^FEATURE", coef_names(surv_unlabeled_single))),
info = ".fitSurvival unlabeled single-feature: FEATURE must not appear (only one feature)"
)

surv_unlabeled_multi_wd <- MSstats:::.fitSurvival(make_surv_unlabeled_multi_welldetermined(), 90)
expect_false(
any(grepl("ref_covariate", coef_names(surv_unlabeled_multi_wd))),
info = ".fitSurvival unlabeled multi well-determined: ref_covariate must not appear"
)
expect_true(
any(grepl("^FEATURE", coef_names(surv_unlabeled_multi_wd))),
info = ".fitSurvival unlabeled multi well-determined: FEATURE must appear in coefficients"
)
32 changes: 32 additions & 0 deletions inst/tinytest/test_utils_summarization.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,35 @@ feature_coefficient_no_weights =
summarized_no_weights$coefficients[['FEATUREASGLLLER_2_y3_1']]
expect_false(feature_coefficient_with_weights == feature_coefficient_no_weights,
info = "Feature coefficients should differ when weights are used")


make_srm_input <- function() {
dt <- data.table::data.table(
PROTEIN = rep("P1", 12),
FEATURE = factor(rep(c("F1", "F2", "F3"), each = 4)),
RUN = factor(rep(c("R1","R2","R3","R4"), 3)),
LABEL = rep(c("H","H","L","L"), 3),
ABUNDANCE = c(10, 11, 14, 15,
10.2, 11.2, 14.2, 15.2,
9.8, 10.8, 13.8, 14.8),
newABUNDANCE = c(10, 11, 14, 15,
10.2, 11.2, 14.2, 15.2,
9.8, 10.8, 13.8, 14.8),
weights = rep(NA_real_, 12)
)
dt[["ref_covariate"]] <- factor(
rep(c("0","0","R3","R4"), 3),
levels = c("0","R3","R4")
)
dt
}
lm_fit <- MSstats:::.fitLinearModel(
make_srm_input(),
is_single_feature = FALSE,
is_labeled = TRUE,
equal_variances = TRUE
)
expect_true(
any(grepl("ref_covariate", names(coef(lm_fit)))),
info = "ref_covariate: model coefficients must include 'ref_covariate' terms"
)
72 changes: 72 additions & 0 deletions inst/tinytest/test_utils_summarization_prepare.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
make_srm_prep_input <- function() {
data.table::data.table(
PROTEIN = rep("P1", 8),
FEATURE = rep(c("F1", "F2"), each = 4),
RUN = c("R1","R1","R2","R2","R1","R1","R2","R2"),
LABEL = c("H","L","H","L","H","L","H","L"),
ABUNDANCE = c(10, 14, 11, 15, 10.2, 14.2, 11.2, 15.2)
)
}

make_labelfree_prep_input <- function() {
data.table::data.table(
PROTEIN = rep("P1", 8),
FEATURE = rep(c("F1", "F2"), each = 4),
RUN = rep(c("R1","R2","R3","R4"), 2),
LABEL = rep("L", 8),
ABUNDANCE = c(10, 11, 12, 13, 10.2, 11.2, 12.2, 13.2)
)
}

MSstatsConvert::MSstatsLogsSettings(FALSE)

prep_labeled <- MSstatsPrepareForSummarization(
make_srm_prep_input(),
method = "TMP", impute = FALSE,
censored_symbol = NULL,
remove_uninformative_feature_outlier = FALSE
)

expect_true(
"ref_covariate" %in% colnames(prep_labeled),
info = "ref_covariate: MSstatsPrepareForSummarization must create column for labeled data"
)
expect_true(
is.factor(prep_labeled$ref_covariate),
info = "ref_covariate: must be stored as a factor"
)
h_vals <- unique(as.character(prep_labeled$ref_covariate[prep_labeled$LABEL == "H"]))
expect_identical(
h_vals, "0",
info = "ref_covariate: heavy (H) rows must map to '0'"
)
l_rows <- prep_labeled[prep_labeled$LABEL == "L", ]
expect_true(
all(as.character(l_rows$ref_covariate) == l_rows$RUN),
info = "ref_covariate: light (L) rows must map to their RUN value"
)
l_runs <- unique(prep_labeled$RUN[prep_labeled$LABEL == "L"])
rc_levels <- levels(prep_labeled$ref_covariate)
expect_true(
"0" %in% rc_levels,
info = "ref_covariate: '0' must be a factor level (placeholder for H rows)"
)
expect_true(
all(l_runs %in% rc_levels),
info = "ref_covariate: all light-run IDs must appear as factor levels"
)
expect_equal(
sort(rc_levels),
sort(c("0", l_runs)),
info = "ref_covariate: factor levels must be exactly '0' plus the L-run IDs"
)
prep_labelfree <- MSstatsPrepareForSummarization(
make_labelfree_prep_input(),
method = "TMP", impute = FALSE,
censored_symbol = NULL,
remove_uninformative_feature_outlier = FALSE
)
expect_false(
"ref_covariate" %in% colnames(prep_labelfree),
info = "ref_covariate: must NOT be created for label-free (single LABEL) data"
)
Loading