Skip to content

fix(turnover): parse timepoint out of tracer constants#9

Merged
tonywu1999 merged 1 commit intodevelfrom
fix-tracer
Apr 22, 2026
Merged

fix(turnover): parse timepoint out of tracer constants#9
tonywu1999 merged 1 commit intodevelfrom
fix-tracer

Conversation

@tonywu1999
Copy link
Copy Markdown
Contributor

@tonywu1999 tonywu1999 commented Apr 22, 2026

Motivation and Context

The calculateTurnoverRatios() function supports tracer normalization via the normalize_tracer parameter, which uses a named numeric vector (tracer_constants) to normalize heavy isotope fractions. When normalizing tracer incorporation, the code needs to look up tracer constant values for each timepoint. However, a mismatch existed between how timepoint names were formatted in the tracer_constants parameter versus how they were parsed in the data.

Specifically:

  • Users provide tracer_constants with names like "0hr", "1hr", "12hrs", "168hrs"
  • Timepoints in the data are parsed using parse_timepoint() to convert them to numeric hours (0, 1, 12, 168)
  • The lookup operation tracer_constants[as.character(TimeVal)] converts TimeVal to character strings like "0", "1", "12", "168"
  • These numeric strings did not match the original timepoint labels, causing failed lookups

Solution: Parse the names of tracer_constants through parse_timepoint() and convert the result back to character strings, ensuring the constant names use the same numeric-only format as the parsed TimeVal values.

Changes

  • File: R/protein_turnover_ratio_helper.R
    • Added line 151: names(tracer_constants) = as.character(parse_timepoint(names(tracer_constants)))
    • This line is placed immediately after the null-check for tracer_constants in the tracer normalization block
    • Coerces the names of the tracer_constants vector to match the parsed timepoint format (numeric hours as character strings)
    • Ensures subsequent indexing with tracer_constants[as.character(TimeVal)] finds matching keys

Unit Tests

No unit tests were added or modified in this PR. The function currently has no existing test coverage for the tracer normalization feature.

Coding Guidelines

No coding guideline violations identified. The change follows the existing code style and conventions used throughout the file.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 22, 2026

📝 Walkthrough

Walkthrough

The calculateTurnoverRatios() function now properly normalizes tracer_constants names by parsing them as timepoint labels via parse_timepoint() before indexing. This ensures the constants' name format matches the parsed TimeVal representation, fixing potential key mismatch issues.

Changes

Cohort / File(s) Summary
Tracer Constants Name Normalization
R/protein_turnover_ratio_helper.R
Updated calculateTurnoverRatios() to parse tracer_constants names using parse_timepoint() when normalizing tracers, ensuring consistent name format matching with TimeVal indexing.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 The turnover traces now align with care,
Names parsed through timepoint's parsing stare,
Constants matched with precision true,
A small fix that sees things through!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and accurately describes the main change: parsing timepoint labels from tracer constants in the turnover ratio calculation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-tracer

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
R/protein_turnover_ratio_helper.R (1)

150-157: ⚠️ Potential issue | 🟡 Minor

Guard against silent NA propagation when tracer names fail to normalize.

The fix correctly aligns tracer_constants names with the parsed TimeVal representation. However, failure modes are silent:

  • If any original name doesn't match parse_timepoint's ^[0-9]+ pattern (e.g., "30min", "2h", typos), the normalized name becomes "NA", and tracer_constants[as.character(TimeVal)] returns NA, making H_frac silently NA.
  • If df_wide contains a TimeVal not present in tracer_constants, the same silent-NA outcome occurs.
  • Duplicate timepoints after normalization (e.g., both "1hr" and "1h""1") are silently deduplicated by first match.

Consider validating up front:

🛡️ Proposed validation
-    
-    names(tracer_constants) = as.character(parse_timepoint(names(tracer_constants)))
+
+    parsed_names <- parse_timepoint(names(tracer_constants))
+    if (any(is.na(parsed_names))) {
+      stop("Could not parse tracer_constants names: ",
+           paste(names(tracer_constants)[is.na(parsed_names)], collapse = ", "))
+    }
+    if (anyDuplicated(parsed_names)) {
+      stop("tracer_constants contains duplicate timepoints after normalization: ",
+           paste(parsed_names[duplicated(parsed_names)], collapse = ", "))
+    }
+    names(tracer_constants) <- as.character(parsed_names)
+
+    missing_tp <- setdiff(as.character(unique(df_wide$TimeVal)), names(tracer_constants))
+    if (length(missing_tp) > 0) {
+      stop("tracer_constants is missing entries for timepoints: ",
+           paste(missing_tp, collapse = ", "))
+    }
 
     df_wide <- df_wide %>%
       mutate(
         tracer_factor = tracer_constants[as.character(TimeVal)],
         H_frac = H_frac / tracer_factor
       )
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@R/protein_turnover_ratio_helper.R` around lines 150 - 157, Validate parsed
tracer names and lookup coverage before applying tracer factors: after setting
names(tracer_constants) =
as.character(parse_timepoint(names(tracer_constants))), check for any NA names
returned by parse_timepoint and fail with a clear error listing offending
original names; detect duplicated normalized names in tracer_constants and
require the author to disambiguate (or aggregate) instead of silently keeping
the first; before mutating df_wide, ensure all unique df_wide$TimeVal
(as.character(parse_timepoint(...)) if needed) are present in
names(tracer_constants) and throw an error or warning listing missing TimeVal
entries so tracer_factor lookup (tracer_constants[as.character(TimeVal)]) cannot
silently produce NA for H_frac.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@R/protein_turnover_ratio_helper.R`:
- Around line 150-157: Validate parsed tracer names and lookup coverage before
applying tracer factors: after setting names(tracer_constants) =
as.character(parse_timepoint(names(tracer_constants))), check for any NA names
returned by parse_timepoint and fail with a clear error listing offending
original names; detect duplicated normalized names in tracer_constants and
require the author to disambiguate (or aggregate) instead of silently keeping
the first; before mutating df_wide, ensure all unique df_wide$TimeVal
(as.character(parse_timepoint(...)) if needed) are present in
names(tracer_constants) and throw an error or warning listing missing TimeVal
entries so tracer_factor lookup (tracer_constants[as.character(TimeVal)]) cannot
silently produce NA for H_frac.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c5a852b9-ae15-42dd-91d5-4286b3bbbf94

📥 Commits

Reviewing files that changed from the base of the PR and between 3cef24e and ff55ac4.

📒 Files selected for processing (1)
  • R/protein_turnover_ratio_helper.R

@tonywu1999 tonywu1999 merged commit 9066ef9 into devel Apr 22, 2026
1 of 2 checks passed
@tonywu1999 tonywu1999 deleted the fix-tracer branch April 22, 2026 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant