-
Notifications
You must be signed in to change notification settings - Fork 1
refactor(createTurnoverRatios): Speed up code to use data.table #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| ^.*\.Rproj$ | ||
| ^\.Rproj\.user$ | ||
| ^\.positai$ | ||
| ^\.claude$ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ | |
| .RData | ||
| .Ruserdata | ||
| *.Rproj | ||
| .positai | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,6 @@ Imports: | |
| stats, | ||
| parallel, | ||
| data.table, | ||
| tidyr, | ||
| stringr, | ||
| plotly, | ||
| utils | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| # Tests for calculateTurnoverRatios and its helpers. | ||
|
|
||
| make_feature_data <- function() { | ||
| grid <- expand.grid( | ||
| PROTEIN = c("P1", "P2"), | ||
| PEPTIDE = c("PEP[+80]TIDE", "SEQVENCE", "THIRDPEP"), | ||
| GROUP = c("0hr", "1hr", "12hrs", "168hrs"), | ||
| RUN = c("R1", "R2"), | ||
| LABEL = c("H", "L"), | ||
| CHARGE = c(2, 3), | ||
| stringsAsFactors = FALSE | ||
| ) | ||
| grid <- grid[!(grid$PROTEIN == "P2" & grid$PEPTIDE != "THIRDPEP"), ] | ||
| grid <- grid[!(grid$PROTEIN == "P1" & grid$PEPTIDE == "THIRDPEP"), ] | ||
| grid$INTENSITY <- seq_len(nrow(grid)) * 100 | ||
| rownames(grid) <- NULL | ||
| grid | ||
| } | ||
|
|
||
| test_that("parse_timepoint converts hours, days and weeks to hours", { | ||
| expect_equal(parse_timepoint(c("0hr", "1hr", "12hrs", "168hrs")), | ||
| c(0, 1, 12, 168)) | ||
| expect_equal(parse_timepoint(c("2d", "1day")), c(48, 24)) | ||
| expect_equal(parse_timepoint(c("1w", "2weeks")), c(168, 336)) | ||
| expect_true(is.na(parse_timepoint("control"))) | ||
| }) | ||
|
|
||
| test_that("calculateTurnoverRatios errors on missing required columns", { | ||
| feat <- make_feature_data() | ||
| expect_error( | ||
| calculateTurnoverRatios(feat[, setdiff(names(feat), "RUN")]), | ||
| "Missing required columns: RUN" | ||
| ) | ||
| }) | ||
|
|
||
| test_that("calculateTurnoverRatios returns the documented columns", { | ||
| res <- calculateTurnoverRatios(make_feature_data()) | ||
| expect_true(data.table::is.data.table(res)) | ||
| expect_equal(names(res), | ||
| c("Protein", "BaseSequence", "TimeVal", "Run", | ||
| "Heavy", "Light", "Total", "H_frac", "L_frac")) | ||
| expect_gt(nrow(res), 0) | ||
| }) | ||
|
|
||
| test_that("modifications are stripped from the peptide sequence", { | ||
| res <- calculateTurnoverRatios(make_feature_data()) | ||
| expect_setequal(unique(res$BaseSequence), | ||
| c("PEPTIDE", "SEQVENCE", "THIRDPEP")) | ||
| expect_false(any(grepl("[", res$BaseSequence, fixed = TRUE))) | ||
| }) | ||
|
|
||
| test_that("fractions sum to one and Total is the H + L sum", { | ||
| res <- calculateTurnoverRatios(make_feature_data()) | ||
| expect_equal(res$Total, res$Heavy + res$Light) | ||
| expect_equal(res$H_frac + res$L_frac, rep(1, nrow(res))) | ||
| expect_equal(res$H_frac, res$Heavy / res$Total) | ||
| }) | ||
|
|
||
| test_that("duplicate features are collapsed by agg_function", { | ||
| feat <- make_feature_data() | ||
| n_expected <- nrow(unique(feat[, c("PROTEIN", "PEPTIDE", "GROUP", "RUN")])) | ||
| res_max <- calculateTurnoverRatios(feat) | ||
| expect_equal(nrow(res_max), n_expected) | ||
|
|
||
| res_min <- calculateTurnoverRatios(feat, agg_function = min) | ||
| expect_true(all(res_max$Heavy >= res_min$Heavy)) | ||
|
|
||
| cell <- feat[feat$PROTEIN == "P1" & feat$PEPTIDE == "SEQVENCE" & | ||
| feat$GROUP == "1hr" & feat$RUN == "R1" & feat$LABEL == "H", ] | ||
| target <- res_max[res_max$Protein == "P1" & res_max$BaseSequence == "SEQVENCE" & | ||
| res_max$TimeVal == 1 & res_max$Run == "R1", ] | ||
| expect_equal(target$Heavy, max(cell$INTENSITY)) | ||
| }) | ||
|
|
||
| test_that("unparseable timepoints and non-H/L labels are dropped", { | ||
| feat <- make_feature_data() | ||
| noise <- rbind( | ||
| transform(feat[1:4, ], GROUP = "control"), | ||
| transform(feat[1:4, ], LABEL = "M") | ||
| ) | ||
| res_clean <- calculateTurnoverRatios(feat) | ||
| res_noisy <- calculateTurnoverRatios(rbind(feat, noise)) | ||
| expect_equal(as.data.frame(res_noisy), as.data.frame(res_clean)) | ||
| }) | ||
|
|
||
| test_that("rows without both channels are dropped", { | ||
| feat <- make_feature_data() | ||
| drop <- feat$PROTEIN == "P1" & feat$PEPTIDE == "SEQVENCE" & | ||
| feat$GROUP == "1hr" & feat$RUN == "R2" & feat$LABEL == "L" | ||
| res <- calculateTurnoverRatios(feat[!drop, ]) | ||
| orphan <- res[res$Protein == "P1" & res$BaseSequence == "SEQVENCE" & | ||
| res$TimeVal == 1 & res$Run == "R2", ] | ||
| expect_equal(nrow(orphan), 0) | ||
| expect_false(anyNA(res$Heavy)) | ||
| expect_false(anyNA(res$Light)) | ||
| }) | ||
|
|
||
| test_that("no matching Heavy/Light labels warns and returns no rows", { | ||
| feat <- transform(make_feature_data(), LABEL = "X") | ||
| expect_warning(res <- calculateTurnoverRatios(feat), | ||
| "No data found matching Heavy/Light labels") | ||
| expect_equal(nrow(res), 0) | ||
| }) | ||
|
|
||
| test_that("peptide_selector is applied within each protein", { | ||
| feat <- make_feature_data() | ||
| keep_first <- function(df) { | ||
| df[df$BaseSequence == sort(unique(df$BaseSequence))[1], ] | ||
| } | ||
| res <- calculateTurnoverRatios(feat, peptide_selector = keep_first) | ||
| by_protein <- tapply(res$BaseSequence, res$Protein, function(x) unique(x)) | ||
| expect_equal(unname(by_protein[["P1"]]), "PEPTIDE") | ||
| expect_equal(unname(by_protein[["P2"]]), "THIRDPEP") | ||
| }) | ||
|
|
||
| test_that("tracer normalization rescales H_frac and floors L_frac at zero", { | ||
| feat <- make_feature_data() | ||
| tc <- c("0hr" = 1.0, "1hr" = 0.95, "12hrs" = 0.85, "168hrs" = 0.75) | ||
| plain <- calculateTurnoverRatios(feat) | ||
| norm <- calculateTurnoverRatios(feat, normalize_tracer = TRUE, | ||
| tracer_constants = tc) | ||
|
|
||
| expect_true("tracer_factor" %in% names(norm)) | ||
| expect_equal(norm$tracer_factor, unname(tc[match(norm$TimeVal, c(0, 1, 12, 168))])) | ||
| expect_equal(norm$H_frac, plain$H_frac / norm$tracer_factor) | ||
| expect_equal(norm$L_frac, pmax(1 - norm$H_frac, 0)) | ||
| expect_true(all(norm$L_frac >= 0)) | ||
| }) | ||
|
|
||
| test_that("tracer normalization requires constants", { | ||
| expect_error( | ||
| calculateTurnoverRatios(make_feature_data(), normalize_tracer = TRUE), | ||
| "tracer_constants was not provided" | ||
| ) | ||
| }) | ||
|
|
||
| test_that("custom column names and heavy/light labels are honored", { | ||
| feat <- make_feature_data() | ||
| names(feat)[names(feat) == "PROTEIN"] <- "Protein" | ||
| names(feat)[names(feat) == "GROUP"] <- "Condition" | ||
| feat$LABEL <- ifelse(feat$LABEL == "H", "Heavy", "Light") | ||
| res <- calculateTurnoverRatios( | ||
| feat, time_col = "Condition", peptide_col = "Protein", | ||
| protein_col = "Protein", heavy_label = "Heavy", light_label = "Light" | ||
| ) | ||
| expect_true(all(c("Heavy", "Light") %in% names(res))) | ||
| expect_setequal(unique(res$BaseSequence), c("P1", "P2")) | ||
| }) | ||
|
|
||
| test_that("a data.table input is not modified in place", { | ||
| feat <- data.table::as.data.table(make_feature_data()) | ||
| before <- data.table::copy(feat) | ||
| calculateTurnoverRatios(feat) | ||
| expect_equal(feat, before) | ||
| }) | ||
|
|
||
| test_that("data.frame and data.table inputs give the same result", { | ||
| feat <- make_feature_data() | ||
| expect_equal(calculateTurnoverRatios(feat), | ||
| calculateTurnoverRatios(data.table::as.data.table(feat))) | ||
| }) | ||
|
|
||
| test_that("kendall_monotonicity floors at zero and tolerates sparse input", { | ||
| expect_equal(kendall_monotonicity(c(1, 2, 3), c(1, 2, 3)), 1) | ||
| expect_equal(kendall_monotonicity(c(1, 2, 3), c(3, 2, 1)), 0) | ||
| expect_equal(kendall_monotonicity(1, 1), 0) | ||
| expect_equal(kendall_monotonicity(c(1, 2), c(NA, NA)), 0) | ||
| expect_equal(kendall_monotonicity(c(1, 2, 3), c(5, 5, 5)), 0) | ||
| }) | ||
|
|
||
| test_that("calculatePeptideWeights consumes calculateTurnoverRatios output", { | ||
| feat <- make_feature_data() | ||
| res <- calculateTurnoverRatios(feat[feat$RUN == "R1", ]) | ||
| weighted <- calculatePeptideWeights(res) | ||
| expect_true(all(c("coverage_score", "monotonicity_score", "validity_flag", | ||
| "weight") %in% names(weighted))) | ||
| expect_equal(nrow(weighted), nrow(res)) | ||
| expect_true(all(weighted$weight >= 0 & weighted$weight <= 1)) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: Vitek-Lab/MSstatsResponse
Length of output: 208
🏁 Script executed:
Repository: Vitek-Lab/MSstatsResponse
Length of output: 3176
🏁 Script executed:
Repository: Vitek-Lab/MSstatsResponse
Length of output: 4618
🌐 Web query:
data.table dcast documentation drop missing measure variable levels absent column💡 Result:
In the data.table package, the dcast function uses the drop argument to control how missing combinations of variables are handled in the output [1][2]. The default behavior is drop = TRUE, which excludes combinations of values not present in the input data [1][3]. By setting drop = FALSE, you force dcast to include all possible combinations of the variables defined in the formula, regardless of whether they exist in the input [2][4]. Specific control over which side of the formula (LHS or RHS) should have missing combinations included is possible by passing a logical vector of length two to the drop argument [1][5]: - drop = c(FALSE, TRUE): Includes all missing combinations of the formula LHS, but only existing combinations of the RHS [1][4]. - drop = c(TRUE, FALSE): Includes only existing combinations of the LHS, but all missing combinations of the RHS [1][5]. When drop is configured to include missing combinations, any cells created for these missing combinations are filled with the value specified by the fill argument [2][3]. If fill is not provided (is NULL), the function will apply the aggregation function (fun.aggregate) to a 0-length vector to determine the fill value [2][5]. Note that the fill value is automatically converted to the class of the value.var column [1][4].
Citations:
Handle input with only one configured channel.
data.table::dcast()omits label columns that are absent from the filtered data. The subsequentdata.table::setnames()calls can then fail before incomplete pairs are removed. Return the empty result when either configured label is absent after filtering.🤖 Prompt for AI Agents