Skip to content

feat: check charge balance in getElementalBalance - #679

Merged
edkerk merged 1 commit into
develop3from
feat/charge-balance
Jul 17, 2026
Merged

feat: check charge balance in getElementalBalance#679
edkerk merged 1 commit into
develop3from
feat/charge-balance

Conversation

@edkerk

@edkerk edkerk commented Jul 17, 2026

Copy link
Copy Markdown
Member

Main improvements in this PR:

  • features:
    • getElementalBalance now also reports chargeStatus (1 balanced, 0 unbalanced, -1 unknown) and chargeResidual. RAVEN previously checked charge nowhere. They are sibling fields; balanceStatus is unchanged and stays elemental.
  • fix:
    • scaleBiomassPseudoreaction and applyCondition summed charges with 'omitnan', so a metabolite with an unset charge counted as neutral and the balancing metabolite silently got the wrong coefficient. Both now refuse and name the metabolites whose charge is unset.
  • documentation:
    • Charge-balance tests in tQueries.m and tBiomass.m.

Instructions on merging this PR:

  • This PR has develop3 as target branch, and will be resolved with a squash-merge.

RAVEN checked charge nowhere: getElementalBalance is elemental only and
parseFormulas strips +/- from formulas. getElementalBalance now also reports
chargeStatus (1 balanced, 0 unbalanced, -1 unknown) and chargeResidual, as
sibling fields rather than folded into balanceStatus, which removeBadRxns and
printModelStats read as an elemental verdict.

Both are masked to the metabolites that participate in each reaction. S is
sparse and 0*NaN is NaN, so an unmasked sum would let a single unset charge
anywhere in the model poison every reaction's residual (measured in R2024b).

Meanwhile RAVEN had two functions that rebalance charge, and both summed with
'omitnan':

    model.S(Hc, rxnPos) = -sum(model.S(:, rxnPos) .* model.metCharges, 'omitnan');

addMets defaults metCharges to NaN, so 'omitnan' treated an unknown charge as
neutral and silently wrote the wrong coefficient onto the balancing
metabolite. Both now refuse, naming the metabolites whose charge is unset: an
unknown charge balance is not a zero one.
@github-actions

Copy link
Copy Markdown

Function test results

244 tests   222 ✅  1m 4s ⏱️
 23 suites   22 💤
  1 files      0 ❌

Results for commit d6ab2f4.

@edkerk
edkerk merged commit 9cf45e0 into develop3 Jul 17, 2026
4 checks passed
@edkerk
edkerk deleted the feat/charge-balance branch July 17, 2026 17:07
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