Skip to content

Biochemistry: fix 33 imbalanced reaction stoichiometries and the methyl-branched acyl-ACP charge convention (63 compounds); 48 reactions return to OK with no regressions - #324

Open
freiburgermsu wants to merge 1 commit into
ModelSEED:devfrom
freiburgermsu:charge-imbalance-corrections
Open

freiburgermsu wants to merge 1 commit into
ModelSEED:devfrom
freiburgermsu:charge-imbalance-corrections

Conversation

@freiburgermsu

Copy link
Copy Markdown
Member

What this is

33 reaction stoichiometry corrections and 63 compound charge corrections. Together they take 48 reactions from an imbalanced status to OK, with zero reactions regressing from OK and zero reactions worsening, as judged by this repository's own balanceReaction.

Scope and how these were found

We were building community metabolic models of an anaerobic digester on the v7 Archaea / GramNegative / GramPositive templates. 247 reactions reachable in those templates carry a mass or charge imbalance. They surfaced not as a database-hygiene exercise but because they carried flux at their bounds in the community models: a charge-imbalanced reaction is a free proton or electron pump, so the solver finds it and parks flux there. In our case the community could produce 50 mmol/gDW/h of methane against a measured 6.73, and these reactions were part of how that was possible. That is the practical cost of these entries, and it is why we worked through them rather than filtering them out locally.

This PR submits only the subset we could verify to publishable standard. It is 48 of the 247, not all of them. The rest are accounted for below.

Method

  1. We used your checker, not our own. Every balance claim comes from BiochemPy.Reactions.balanceReaction. Run over the whole database at our base commit it reproduces 55,996 of 56,006 stored status strings exactly, with 0 disagreements (10 skipped as EMPTY or Duplicate reagents), and 247/247 of the in-scope statuses. Nothing here rests on a reimplementation that might disagree with yours about R-groups or CK flags.
  2. Independent recomputation. Each corrected stoichiometry was balanced twice — once with balanceReaction, once with a separately written element counter — and both return residual elements {} and charge 0 for all 33.
  3. Adversarial verification. Every proposed change was reviewed against the question "what would a reviewer reject this for?" Several proposals died there, including one whose own stated balance left a residual MI:H:-1/R:1, and one that was superseded once we found the correct fix already present in the database as an OK reaction.
  4. Database-wide collateral gate for compound edits. Any compound charge change was re-balanced against every reaction in the database containing it, not only the 247. The bar was zero OK → non-OK and zero worsening. This gate is what shaped the PR: all 77 compound edits our audit originally proposed break at least one currently-OK reaction in isolation, and the full acyl-ACP block nets 17 fixed against 19 broken. Only a verified subset survived.

What is included

33 reaction stoichiometry corrections. (The diff touches 124 reaction entries: these 33, plus 91 whose stoichiometry array changes only because Rebuild_Stoichiometry.py propagated the 63 new compound charges into their embedded per-reagent charge fields. Species and coefficients are identical in all 91 — verifiable by comparing (coefficient, compound, compartment) tuples.) The recurring defects are: an omitted redox partner or electron acceptor (a two-electron reaction written with no carrier, where the charge gap is the missing electrons); a quinone used where the enzyme makes the quinol; a free cofactor paired with an enzyme-bound product, or the reverse; a polymer or oligomer one unit too large; and double-counted two-electron ferredoxin.

18 of the 33 corrections reproduce an equation that already exists in this database with status OK. We consider this the strongest evidence available, and we list the pairs so each can be checked directly:

corrected already OK as
rxn00082 rxn14091
rxn00431 rxn45002
rxn00689, rxn30129 rxn27860, rxn27861
rxn03396 rxn17394
rxn04046 rxn14270
rxn04158 rxn04705
rxn04656 rxn24059
rxn04673 rxn24311, rxn24310 (`OK
rxn05266 rxn06831
rxn06031 rxn39309
rxn06586 rxn39442
rxn06621 rxn39453
rxn09507 rxn06834
rxn10600 rxn37794
rxn13838 rxn08392, rxn25752
rxn13974 rxn05938, rxn34265
rxn14048 rxn05939, rxn31312

Duplicate policy. After correction those 18 duplicate an existing entry, and Refresh_Reactions.sh runs Merge_Reactions.py, which will mark them obsolete with a linked_reaction. We are fine with that outcome and flag it rather than pre-empting it. We submit the stoichiometry fix rather than obsoleting the ids ourselves because the v7 templates reference these ids directly, so an unannounced merge would change template behaviour; we would rather the merge be your deliberate step.

63 compound charge corrections: the methyl-branched acyl-ACP convention. This is the more valuable finding, since one wrong compound charge imbalances many reactions. The acyl-ACP block is split between two internally self-consistent protonation conventions on the identical phosphopantetheine-thioester skeleton C(n)H(m)N2O(8-9)PRS: 157 compounds at charge −1 and 126 at −2. The methyl-branched / anteiso series cpd11496–cpd11568 sits entirely in the −1 convention while its own straight-chain homologs sit at −2. The evidence is entirely internal:

  • All 63 have a charge −2 homolog in the same N/O/P/S/R skeleton differing only by (CH2)n — identical functional-group content, contradictory charge.
  • 16 of the 63 have an exact-formula twin already stored at −2. For instance cpd11549 "4-methyl-pentanoyl-ACP" is C17H31N2O8PRS at −1 while cpd11472 "Hexanoyl-ACP" is C17H31N2O8PRS at −2. The database currently assigns two different charges to the same composition.
  • Changing these 63 to −2 touches 124 reactions database-wide: 15 become OK, 0 regress, 0 worsen, and all 70 OK → OK reactions contain block compounds on both sides — the 63 form a closed sub-network whose internal reactions are charge-invariant, with the 15 fixes occurring exactly where the block meets the −2 series.

The 15 unblocked reactions are the branched-chain fatty-acid synthesis series rxn05359, rxn05378, rxn05384, rxn05403, rxn05404, rxn05407, rxn05409, rxn05428, rxn05429, rxn05432, rxn05437, rxn05443, rxn05444, rxn05449, rxn05450 — MetaNetX-aliased with no EC numbers, consistent with the −1 convention having been imported from a model rather than curated.

Worth noting: Check_Charges.py structurally cannot find this family. It compares the stored charge against an InChI-derived charge, and R-group compounds have no InChI, so none of the 63 appear in Compound_Charge_Differences.txt.

What is deliberately excluded, and why

  • CPDFORMERROR (7,077 database-wide). Not fixable at the reaction level by definition. None fall inside our 247 in any case.
  • The acyl-ACP convention boundary. The straight-chain series cpd11478, cpd15416, cpd15373, cpd15366, cpd15239 and neighbours sits on the −1/−2 frontier, where correcting any one compound only relocates the imbalance one reaction along the pathway. We verified this directly: cpd11478 → −2 breaks rxn07997; the full 14-compound closure still breaks rxn08002 and makes rxn13840 worse. Our zero-collateral search converged by dropping exactly the boundary compounds. This needs a maintainer ruling on which protonation state is canonical for acyl-ACPs, followed by a coordinated family-wide rewrite. 109 of the 200 reactions we are not fixing live here. We think that ruling is the highest-value action available in this part of the database, and we are not going to guess it inside a PR.
  • R-group and generic/polymeric compounds where the convention is undecided. cpd28083/cpd27758 (flavodoxins) carry formula R alone; a charge change would fix 28 reactions with no collateral, but a bare R offers no internal evidence for any particular charge. Flagged, not guessed.
  • rxn06565 (EC 2.4.1.52, KEGG R04041). Its product cpd12563 is tri-glucosylated where the enzyme transfers one glucose. The correct repair needs a mono-glucosyl poly(glycerophosphate) C15H28O21P3R at −4, which does not exist in the database. A proposal that tripled the UDP-glucose coefficient closes the arithmetic but contradicts the enzyme, KEGG and the reaction's own name, so we dropped it.
  • Compound formula edits. All 12 we considered break currently-OK reactions. Excluded entirely.
  • Mass-column and nomenclature defects found in passing (eleven compounds carrying the sentinel mass 10000000.0; cpd11451, whose formula C21H24O2, mass 650 and name "menaquinol-7" mutually contradict; cpd15250, ten carbons smaller than its own precursor; cpd08368/cpd25539, duplicate species differing by 2 H). None affect balance, so they are out of scope here — but we have them written up and are happy to open separate issues.

One thing we want to be straight about

There is no case where n protons are simply missing among the 247. We checked specifically: zero reactions have an H residual equal to their charge residual. Adjust_Reaction_Protons.py has already absorbed that entire class. Every remaining defect is a genuine stoichiometry or compound error, which is why none of these corrections is a proton patch — and conversely why a proton patch on any of them would be wrong, since adding n protons to a mass-balanced reaction breaks hydrogen.

We are also not claiming to have solved the 247. We are claiming 48, verified, with no regressions, plus a documented account of what blocks the other ~200.

How to verify

Biochemistry/ at our base commit is byte-identical to this PR's target: git diff --name-only upstream/dev HEAD~1 -- Biochemistry returns nothing, and the same holds against upstream/master. So every original value applies cleanly. (Note for anyone re-running this on the freiburgermsu fork: that fork's own master is ~600 commits stale and 470 Biochemistry/ files differ against it, so compare against upstream/dev, not master.)

cd Scripts/Biochemistry/Refresh_DB_after_Changes
./Rebuild_Stoichiometry.py     # propagates the 63 new charges into the embedded per-reagent charges
./Rebalance_Reactions.py print # dry run: prints every status transition

Rebuild_Stoichiometry.py must run first. balanceReaction reads the charge embedded in each reaction's stoichiometry array, not the compound table, so without that step the compound edits are invisible to the balancer — the exact hazard that script's own in-code comment warns about.

Expected output: 48 Changing Status for ... to OK lines, two CI:2 -> CI:1 improvements (rxn51518, rxn51519), and no transition from OK to anything else. That last point is the one we would most like a reviewer to confirm independently.

Because the scope is template reactions, Scripts/Biochemistry/Maintain/Check_Template_Reactions.py is also a relevant check.

Validation plan

THE REPO HAS ITS OWN BALANCE CHECKER and we used it throughout rather than writing one: Libs/Python/BiochemPy/Reactions.py::balanceReaction, driven by Scripts/Biochemistry/Refresh_DB_after_Changes/Rebalance_Reactions.py. A second relevant script is Scripts/Biochemistry/Maintain/Check_Charges.py (compound charge vs InChI-derived charge, output Compound_Charge_Differences.txt), and Scripts/Biochemistry/Maintain/Check_Template_Reactions.py for the template scope.

BASELINE PARITY (already run; the gate everything else depends on). Running balanceReaction over all 56,006 reactions at the base commit reproduces 55,996 stored status strings exactly with 0 disagreements (10 skipped: EMPTY or 'Duplicate reagents'), and 247/247 of the in-scope statuses. A reviewer reproduces this with cd Scripts/Biochemistry/Refresh_DB_after_Changes && ./Rebalance_Reactions.py on a clean checkout - it should print no status changes at all.

WHICH COLUMNS THE APPLYING SCRIPT MUST TOUCH. stoichiometry is canonical. Reactions.rebuildReaction() regenerates code, equation, definition, compound_ids and the sorted stoichiometry from it, so those four must never be hand-edited and must change in lockstep. Three concrete traps: (1) the TSV stoichiometry string carries no charge, but the matching reaction_NN.json embeds formula and charge per reagent - BOTH files must be updated; (2) the quoted reagent display names in the stoichiometry string are cosmetic and must be regenerated from the compound table rather than hand-carried (our rxn08480 edit shows this: the stored name is "fe3" where the compound table says "Fe+3"); (3) Rebuild_Reactions.py will NOT regenerate the derived columns by itself - it only acts when redundancy removal changes something. So the applying script should, per reaction: set the new stoichiometry array, call reactions_helper.rebuildReaction(reactions_dict[rxn], new_array), then reactions_helper.saveReactions(reactions_dict).

APPLY ORDER, which matters:

  1. Compound edits: set charge in Biochemistry/compound_11.tsv AND compound_11.json for the 63 ids (all -1 -> -2).
  2. cd Scripts/Biochemistry/Refresh_DB_after_Changes && ./Rebuild_Stoichiometry.py - MANDATORY and easy to miss. balanceReaction reads the charge EMBEDDED in each reaction's stoichiometry array, not the compound table, so until this propagation runs the 63 compound edits are completely invisible to the balancer and the reviewer will see 33 fixes instead of 48. The script's own in-code comment documents exactly this failure mode (a past refresh that updated formula but not charge manufactured charge imbalances on 5,935 reactions and rewrote proton coefficients on 17,457).
  3. Reaction edits: apply the 33 new stoichiometry values via rebuildReaction + saveReactions as above.

THE EXACT COMMAND A REVIEWER RUNS. From the repository root (the scripts resolve Libs/Python by relative sys.path, so the cd is required):

cd Scripts/Biochemistry/Refresh_DB_after_Changes && ./Rebuild_Stoichiometry.py && ./Rebalance_Reactions.py print

Rebalance_Reactions.py is a DRY RUN unless passed save; print adds per-reaction charge detail. It emits one Changing Status for <rxn> from <old> to <new> line per transition.

PASS CRITERIA, in priority order:
(a) ZERO lines where the old status contains OK and the new one does not. This is the regression test and the single most important check.
(b) Exactly 48 lines ending in OK: the 33 edited reactions, plus rxn05359, rxn05378, rxn05384, rxn05403, rxn05404, rxn05407, rxn05409, rxn05428, rxn05429, rxn05432, rxn05437, rxn05443, rxn05444, rxn05449, rxn05450 from the compound block.
(c) Exactly two further lines, rxn51518 and rxn51519, going CI:2 -> CI:1 - improvements that do not reach OK, disclosed deliberately.
(d) No Updating previously checked (CK) reaction lines, since none of the 33 is CK-flagged.
Add save to write the statuses back once (a)-(d) are confirmed.

SCOPE-SPECIFIC CHECK. Because every targeted reaction is reachable in the Archaea/GramNeg/GramPos v7 templates, Scripts/Biochemistry/Maintain/Check_Template_Reactions.py is the relevant second check and should report no new problems. Scripts/Biochemistry/Maintain/Check_Charges.py will NOT change: it is InChI-driven and the 63 edited compounds are R-group species with no InChI, so none of them appear in Compound_Charge_Differences.txt before or after - worth stating so a reviewer does not read its silence as disagreement.

FULL-PIPELINE NOTE AND ONE CAVEAT. Refresh_Reactions.sh runs the whole sequence (Rebuild_Stoichiometry -> Rebuild_Reactions -> Rebalance_Reactions save -> Adjust_Reaction_Protons -> Adjust_Reaction_Water -> Merge_Reactions). It is the right thing to run before merging, but a reviewer validating THIS PR's claims should stop after Rebalance_Reactions.py, because the later steps legitimately make further changes that are not ours: Adjust_Reaction_Protons.py and Adjust_Reaction_Water.py may rewrite other reactions, and Merge_Reactions.py will detect that 18 of our corrected equations now duplicate existing OK reactions and mark them obsolete with a linked_reaction. That merge is expected and acceptable to us, but running it in the same pass would obscure the before/after status accounting.

INDEPENDENT CROSS-CHECK. Separately from balanceReaction, each of the 33 corrected stoichiometries was summed element-by-element and charge-by-charge with a second, independently written counter; all 33 give residual elements {} and residual charge 0. The full machine-readable edit set, the 247-reaction classification, the per-compound homolog and exact-twin evidence, the collateral matrices and the harness scripts are all at /home/freiburger/Documents/codiffusion_bioreactor/modeling_files/msdb_consolidation/ - specifically APPLY_READY_FINAL.json, classified_247.json, homolog_evidence.json, exact_twins.json, cpd_collateral_isolated.json, final_strict.json, rootcause_scan.json, harness.py, verify.py, cpdverify.py and final2.py - so a reviewer can re-run the gates rather than take the numbers on trust.


Generated by Claude Code

Fixes 48 charge- and mass-imbalanced reactions, 47 of them in a scope of 247
that are reachable from the Archaea/GramNeg/GramPos v7 model templates. Found
while building anaerobic-digester community models, where imbalanced reactions
carried flux at their bounds: if charge does not balance, neither do electrons,
so a community model can create or destroy reducing equivalents mid-network and
no amount of constraining its exchanges will reproduce measured methane.

Method. Every claim is made with this repository's own checker: candidates were
classified by BiochemPy.Reactions.balanceReaction (which reproduces 55,996 of
56,006 stored status strings exactly, and 247/247 in scope), each corrected
stoichiometry was re-verified twice -- once with balanceReaction and once with an
independent element counter -- and every proposed compound edit was re-balanced
against EVERY reaction in the database containing it, not only those in scope.

Reaction stoichiometries (33). None of these were missing protons:
Adjust_Reaction_Protons.py has already absorbed that class, so zero of the 247
were case "n protons short". What remains are real errors -- a wrong product, a
missing electron acceptor, a generic substrate paired with a concrete product.
18 of the 33 corrected equations are byte-identical to a reaction that ALREADY
exists here with status OK, which is the strongest internal evidence available.
Examples:
  rxn00082  NOS second half-step used 1 O2 and no water, dumping 4 free H+ to
            absorb the difference; 2 O2 + 2 H2O solves H+ to exactly 0
            (matches rxn14091, OK).
  rxn00431  methylamine dehydrogenase written with no electron acceptor -- the
            CI:2 is literally the two missing electrons (matches rxn45002, OK).
  rxn00689  folylpolyglutamate synthase adds ONE glutamate, but the product was
            tri-glutamylated; solving from the other six reagents gives
            cpd19097 digit for digit (matches rxn27860/rxn27861, OK).

Compound charges (63). Methyl-branched acyl-ACP compounds carried charge -1
where their structures give -2. These 63 form a closed sub-network: all 70
OK->OK reactions in the collateral set contain block compounds on both sides.
The set deliberately excludes cpd11478 and cpd15416, the two boundary compounds
whose correction would break a currently-OK reaction.

Verified DB-wide: 124 reactions touched, 48 newly OK, 0 OK->non-OK, 0 worsened
(2 improved CI:2 -> CI:1). Reprint_Biochemistry.py runs clean and
Check_Database_Consistency.py passes all invariants (56,006 reactions, 45,708
compounds).

Deliberately excluded: CPDFORMERROR reactions (not fixable at reaction level),
R-group and generic participants needing a convention decision, and reactions
whose intended chemistry is ambiguous. rxn06565 is left alone although its
balance can be closed by tripling a UDP-glucose coefficient: that contradicts
EC 2.4.1.52 and KEGG R04041, and the real repair needs a mono-glucosyl
poly(glycerophosphate) compound that does not exist here yet. 200 of the 247 in
scope remain imbalanced and are reported rather than guessed at.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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