simulate_O2
Simulate
For a true closed-circuit respirometry system, the chamber volume is static (eg, 1 Liter for a mouse), it is sealed with the animal inside, and runs are restricted to short intervals, typically 30 to 60 minutes to prevent oxygen depletion and carbon dioxide buildup from reaching dangerous thresholds.
Below is a simulation script redesigned for a true closed system.
It models a 25g female SWR mouse inside a 1-Liter sealed chamber over a safe 60-minute timeline, tracking the accumulation of metabolic gases at 1-minute intervals.
R code
# =========================================================================
# CLOSED-SYSTEM RESPIROMETRY DATA SIMULATION (60-MINUTE TIMELINE)
# =========================================================================
# Subject Profile: Female Swiss Webster (SWR) Mouse, 25.0 Grams
# Chamber Architecture: True Closed System, 1.0 Liter Static Volume (1000 mL)
# Safety Boundary: Run terminates within 1 hour before O2 drops below safe limits.
set.seed(7) # For realistic micro-fluctuations
# --- 1. CHAMBER AND TIME SPECIFICATIONS ---
chamber_volume_ml <- 1000
duration_minutes <- 60
interval_minutes <- 1
time_vector <- seq(from = interval_minutes, by = interval_minutes, length.out = duration_minutes)
# Initial baseline gas fractions at sealing (ambient air)
Fi_O2 <- 0.2095
Fi_CO2 <- 0.0004
# --- 2. BIOLOGICAL CONSUMPTION MODEL ---
# A 25g mouse consumes ~1.5 mL of O2 per minute at rest.
# SWR mice are active; we will assume an active metabolic rate of ~1.8 mL/min of O2.
# Assumed true internal RER = 0.82 (mixed fuel source).
true_vo2_rate <- 1.80 + rnorm(duration_minutes, mean = 0, sd = 0.1)
true_vco2_rate <- true_vo2_rate * (0.82 + rnorm(duration_minutes, mean = 0, sd = 0.01))
# --- 3. CLOSED SYSTEM ACCUMULATION MATH ---
# In a sealed box, O2 goes down linearly/gradually while CO2 goes up.
# Volume of O2 consumed up to time t:
cumulative_vo2_ml <- cumsum(true_vo2_rate)
cumulative_vco2_ml <- cumsum(true_vco2_rate)
# Calculate current fractional concentration remaining in the 1L tank at each minute
# Fe_O2 = (Initial O2 Volume - Consumed O2 Volume) / Total Chamber Volume
raw_fe_o2 <- ( (chamber_volume_ml * Fi_O2) - cumulative_vo2_ml ) / chamber_volume_ml
raw_fe_co2 <- ( (chamber_volume_ml * Fi_CO2) + cumulative_vco2_ml ) / chamber_volume_ml
# --- 4. HARDWARE DEFECT INJECTION (For testing validation scripts) ---
# Inject an impossible reading at minute 45 (e.g., electronic signal glitch / drop)
raw_fe_o2[45] <- 0.2250 # O2 cannot spike upward in a closed chamber with an animal
# --- 5. SYNTHESIZE AND EXPORT RAW COMPLIANT SHEET ---
closed_system_dataset <- data.frame(
Timestamp_Min = time_vector,
Chamber_Volume_mL = rep(chamber_volume_ml, duration_minutes),
Initial_O2_Frac = rep(Fi_O2, duration_minutes),
Initial_CO2_Frac = rep(Fi_CO2, duration_minutes),
Fe_O2 = round(raw_fe_o2, 5),
Fe_CO2 = round(raw_fe_co2, 5)
)
# Export standard CSV spreadsheet layout
write.csv(closed_system_dataset, "mouse_closed_metabolic_data.csv", row.names = FALSE)
# --- 6. DATA PREVIEW VERIFICATION ---
cat("=========================================================================\n")
cat(" CLOSED-SYSTEM METABOLIC SIMULATION SETUP COMPLETE \n")
cat("=========================================================================\n")
cat(sprintf(" Chamber Envelope: %.1f Liter Static Airtight Seal\n", chamber_volume_ml / 1000))
cat(sprintf(" Safe Testing Span: %d Minutes (%d Single-Minute Logs)\n", duration_minutes, nrow(closed_system_dataset)))
cat(" Destination Filename: 'mouse_closed_metabolic_data.csv'\n")
cat("-------------------------------------------------------------------------\n")
cat("First 5 Minutes (Sealing Baseline Profile):\n")
print(head(closed_system_dataset, 5))
cat("\nLast 3 Minutes (Maximum Closed Gas Saturation Limits):\n")
print(tail(closed_system_dataset, 3))
cat("=========================================================================\n")
End R code
Open-circuit respirometry