Blood Gas Analysis Calculator
Relevant source files
The following files were used as context for generating this wiki page:
The Blood Gas Analysis Calculator is a comprehensive diagnostic tool within the Medical Calculators application. It is designed to interpret arterial or venous blood gas results, perform a series of complex calculations to identify acid-base disorders, and provide detailed interpretations, decision logic, and treatment advice. The calculator is implemented as a React component and is integrated into the main application page, allowing users to switch between different medical calculators.
This calculator goes beyond simple value checks by implementing multi-step diagnostic algorithms, including checks for internal consistency, analysis of compensation, identification of mixed disorders, and calculation of advanced parameters like osmolar and urine anion gaps.
Sources: src/calculators/BloodGasAnalysisCalculator.tsx, src/app/page.tsx:10, 80-87, 305-318, PROJECT_SUMMARY.md
Component Architecture
The calculator is a self-contained React functional component, BloodGasAnalysisCalculator, built using TypeScript. It manages its state internally and uses shared components like Card, NumberInput, Select, and Button for its user interface, consistent with the project's contribution guidelines.
Sources: src/calculators/BloodGasAnalysisCalculator.tsx:120, CONTRIBUTING.md:21-49, 100-107
State Management
The component utilizes the useState hook to manage three key pieces of state:
data: An object of typeBloodGasDatathat stores all user inputs.result: An object of typeBloodGasResultthat holds all calculated values and interpretations. It isnulluntil a calculation is performed.showAdvanced&showDecisionLogic: Booleans to toggle the visibility of advanced input fields and the detailed reasoning section in the results.
Sources: src/calculators/BloodGasAnalysisCalculator.tsx:156-158
Data Structures
The component relies on two primary interfaces to structure its data: BloodGasData for inputs and BloodGasResult for outputs.
This diagram shows the relationship between the main component and its state data structures.
BloodGasData Interface
This interface defines the shape of the user input object. It includes basic blood gas values, electrolytes, and several advanced parameters for more detailed analysis.
| Field | Type | Description |
|---|---|---|
ph | number | null | Blood pH |
pco2 | number | null | Partial pressure of CO2 |
hco3 | number | null | Bicarbonate level |
pao2 | number | null | Partial pressure of O2 |
na | number | null | Sodium level |
cl | number | null | Chloride level |
k | number | null | Potassium level |
sampleType | 'arterial' | 'venous' | Type of blood sample |
albumin | number | null | Albumin level (for corrected anion gap) |
includeK | boolean | Flag to include potassium in Anion Gap calculation |
fio2 | number | null | Fraction of inspired oxygen |
age | number | null | Patient's age |
glucose | number | null | Glucose level (for osmolar gap) |
bun | number | null | Blood Urea Nitrogen (for osmolar gap) |
ethanol | number | null | Ethanol level (for osmolar gap) |
urineNa | number | null | Urine sodium (for urine anion gap) |
urineK | number | null | Urine potassium (for urine anion gap) |
urineCl | number | null | Urine chloride (for urine anion gap) |
hemoglobin | number | null | Hemoglobin concentration (for Base Excess) |
oxygenSaturation | number | null | Oxygen saturation (for Base Excess) |
| Sources: src/calculators/BloodGasAnalysisCalculator.tsx:8-29 |
BloodGasResult Interface
This interface structures the output of the calculations, including raw results, interpretation objects, and treatment advice.
// src/calculators/BloodGasAnalysisCalculator.tsx:31-76
interface BloodGasResult {
anionGap: number;
anionGapCorrected: number | null;
expectedPaCO2Winter: number;
expectedPaCO2Metabolic: number;
deltaDelta: number | null;
pao2Status: string;
oxygenationIndex?: number;
totalCO2: number;
baseExcess: number;
hydrogenIon: number;
aaGradient: number | null;
osmolarGap: number | null;
urineAnionGap: number | null;
internalConsistency: {
isValid: boolean;
// ...
};
interpretation: {
primaryDisorder: string;
compensation: string;
mixedDisorder: boolean;
// ...
};
treatmentAdvice: {
detectedDisorders: string[];
specificAdvice: Array<{
disorder: string;
advice: string[];
// ...
}>;
generalPrinciples: string[];
};
}
Sources: src/calculators/BloodGasAnalysisCalculator.tsx:31-76
Calculation and Interpretation Logic
The core of the calculator is the handleCalculate function, which orchestrates a sequence of calculations and interpretations.
Main Calculation Flow
The following flowchart illustrates the sequence of operations when the "Calculate" button is clicked.
Key Calculation Functions
The calculator implements several standard and advanced medical formulas.
| Function | Formula / Purpose | Source Lines |
|---|---|---|
calculateAnionGap | Na - (Cl + HCO3) or Na - (Cl + HCO3 + K) | 161-170 |
calculateCorrectedAnionGap | anionGap + 2.5 * (4.0 - albuminGdL) | 188-194 |
calculateExpectedPaCO2Winter | 1.5 * HCO3 + 8 (Winter's Formula) | 173-176 |
calculateBaseExcess | Zander-van Slyke formula, using pH, PCO2, Hb, and SaO2. | 240-272 |
checkInternalConsistency | Compares calculated [H+] from (24 * PCO2) / HCO3 with [H+] from pH. | 301-324 |
calculateOsmolarGap | Calculated Osmolarity - Normal Osmolarity, where Calculated Osmolarity = 2*Na + glucose/18 + BUN/2.8 + ethanol/4.6 | 327-340 |
calculateUrineAnionGap | Urine Na + Urine K - Urine Cl | 343-350 |
Sources: src/calculators/BloodGasAnalysisCalculator.tsx:161-350
Acid-Base Interpretation Logic (interpretAcidBaseStatus)
This function is a multi-step algorithm to diagnose the patient's acid-base status.
- Internal Consistency Check: First, it validates the input data using the Henderson-Hasselbalch equation via
checkInternalConsistency. If the deviation is >10%, a warning is issued. - Primary Disorder Identification: It analyzes pH to determine acidemia (
<7.35) or alkalemia (>7.45). Then, it checks PCO2 and HCO3 to find the primary cause (metabolic or respiratory). - Compensation Analysis: For the identified primary disorder, it calculates the expected compensatory response.
- Metabolic Acidosis: Uses Winter's formula to check if respiratory compensation is adequate.
- Respiratory Disorders: Calculates expected HCO3 changes for acute vs. chronic conditions to determine chronicity.
- Mixed Disorder Detection: If the measured compensation deviates significantly from the expected value, a mixed disorder is diagnosed.
- Anion Gap Analysis: It categorizes the anion gap as normal (NAGMA), high (HAGMA), or low.
- Delta-Delta Ratio: If applicable, calculates the Δ/Δ ratio to check for a co-existing NAGMA or metabolic alkalosis.
- Advanced Gaps: Interprets the calculated Osmolar Gap and Urine Anion Gap to further refine the diagnosis (e.g., presence of toxins, renal vs. extra-renal cause of NAGMA).
This flowchart visualizes the high-level decision process.
Treatment Advice Generation (generateTreatmentAdvice)
Based on the final interpretation, this function compiles a set of treatment recommendations.
- It identifies all detected disorders (e.g., 'HAGMA', 'Respiratory Acidosis').
- It provides specific advice for each disorder, including potential interventions and medications.
- It flags critical conditions (e.g., pH < 7.0, invalid ABG data) with a
critical: trueproperty. - It lists general treatment principles, such as treating the underlying cause and monitoring the patient.
- It includes mnemonics for causes of HAGMA (OMUDPILES) and NAGMA (HARDUP).
Sources: src/calculators/BloodGasAnalysisCalculator.tsx:599-808, 1498-1558
UI and Integration
The calculator is rendered on the main page when activeCalculator state is set to 'bloodGas'. A dedicated card on the homepage allows users to navigate to it.
Sources: src/app/page.tsx:10, 222-239, 305-318
Input Form
- The UI is structured within a
<Card>component. - Basic parameters are always visible.
- Advanced parameters for Osmolar Gap, Urine Anion Gap, and Base Excess are in a collapsible section controlled by the
showAdvancedstate. - Input fields use the custom
NumberInput,Select, andCheckboxcomponents. - The component uses hardcoded Russian text for labels and descriptions, which is an inconsistency with the project's i18n setup seen in
src/i18n/translations.ts.
Sources: src/calculators/BloodGasAnalysisCalculator.tsx:1107-1300, src/i18n/translations.ts
Results Display
- Results are displayed in a separate
<Card>that appears after a successful calculation. - Interpretation Summary: A high-level summary of the findings is displayed prominently at the top.
- Primary Results: Key values like Anion Gap, Δ/Δ, and Expected PaCO2 are shown in large-font cards.
- Detailed Sections: The results are broken down into sections for Interpretation, Main Results, Normal Values, Oxygenation Analysis, Treatment Advice, and Decision Logic.
- Warnings: Critical warnings, especially for invalid internal consistency, are highlighted in red.
- Actions: Buttons are provided to copy a detailed text report to the clipboard (
handleCopyReport) or print the page (handlePrint).
Sources: src/calculators/BloodGasAnalysisCalculator.tsx:1101-1798, 1304-1306
Conclusion
The Blood Gas Analysis Calculator is a powerful and complex feature within the application. It leverages a multi-step algorithmic approach based on established medical formulas to provide a deep, actionable interpretation of blood gas data. The architecture is well-structured within a single React component, managing complex state and logic to guide clinical decision-making. Its detailed output, including step-by-step reasoning and treatment advice, makes it a valuable tool for healthcare professionals.
Sources: src/calculators/BloodGasAnalysisCalculator.tsx, PROJECT_SUMMARY.md, README.md