Repository navigation
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
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this is
33 reaction
stoichiometrycorrections and 63 compoundchargecorrections. Together they take 48 reactions from an imbalanced status toOK, with zero reactions regressing fromOKand zero reactions worsening, as judged by this repository's ownbalanceReaction.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
BiochemPy.Reactions.balanceReaction. Run over the whole database at our base commit it reproduces 55,996 of 56,006 storedstatusstrings exactly, with 0 disagreements (10 skipped asEMPTYorDuplicate reagents), and 247/247 of the in-scope statuses. Nothing here rests on a reimplementation that might disagree with yours about R-groups orCKflags.balanceReaction, once with a separately written element counter — and both return residual elements{}and charge0for all 33.MI:H:-1/R:1, and one that was superseded once we found the correct fix already present in the database as anOKreaction.OK→ non-OKand zero worsening. This gate is what shaped the PR: all 77 compound edits our audit originally proposed break at least one currently-OKreaction in isolation, and the full acyl-ACP block nets 17 fixed against 19 broken. Only a verified subset survived.What is included
33 reaction
stoichiometrycorrections. (The diff touches 124 reaction entries: these 33, plus 91 whosestoichiometryarray changes only becauseRebuild_Stoichiometry.pypropagated the 63 new compound charges into their embedded per-reagentchargefields. 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:OKasDuplicate policy. After correction those 18 duplicate an existing entry, and
Refresh_Reactions.shrunsMerge_Reactions.py, which will mark them obsolete with alinked_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
chargecorrections: 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 skeletonC(n)H(m)N2O(8-9)PRS: 157 compounds at charge −1 and 126 at −2. The methyl-branched / anteiso seriescpd11496–cpd11568sits entirely in the −1 convention while its own straight-chain homologs sit at −2. The evidence is entirely internal:N/O/P/S/Rskeleton differing only by(CH2)n— identical functional-group content, contradictory charge.cpd11549"4-methyl-pentanoyl-ACP" isC17H31N2O8PRSat −1 whilecpd11472"Hexanoyl-ACP" isC17H31N2O8PRSat −2. The database currently assigns two different charges to the same composition.OK, 0 regress, 0 worsen, and all 70OK→OKreactions 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.pystructurally 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 inCompound_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.cpd11478,cpd15416,cpd15373,cpd15366,cpd15239and 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 breaksrxn07997; the full 14-compound closure still breaksrxn08002and makesrxn13840worse. 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.cpd28083/cpd27758(flavodoxins) carry formulaRalone; a charge change would fix 28 reactions with no collateral, but a bareRoffers no internal evidence for any particular charge. Flagged, not guessed.rxn06565(EC 2.4.1.52, KEGG R04041). Its productcpd12563is tri-glucosylated where the enzyme transfers one glucose. The correct repair needs a mono-glucosyl poly(glycerophosphate)C15H28O21P3Rat −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.OKreactions. Excluded entirely.10000000.0;cpd11451, whose formulaC21H24O2, mass650and 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.pyhas 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 -- Biochemistryreturns nothing, and the same holds againstupstream/master. So every original value applies cleanly. (Note for anyone re-running this on thefreiburgermsufork: that fork's ownmasteris ~600 commits stale and 470Biochemistry/files differ against it, so compare againstupstream/dev, notmaster.)Rebuild_Stoichiometry.pymust run first.balanceReactionreads the charge embedded in each reaction'sstoichiometryarray, 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 OKlines, twoCI:2 -> CI:1improvements (rxn51518,rxn51519), and no transition fromOKto 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.pyis 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.pyon a clean checkout - it should print no status changes at all.WHICH COLUMNS THE APPLYING SCRIPT MUST TOUCH.
stoichiometryis canonical. Reactions.rebuildReaction() regeneratescode,equation,definition,compound_idsand the sortedstoichiometryfrom it, so those four must never be hand-edited and must change in lockstep. Three concrete traps: (1) the TSVstoichiometrystring carries no charge, but the matching reaction_NN.json embedsformulaandchargeper 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:
chargein Biochemistry/compound_11.tsv AND compound_11.json for the 63 ids (all -1 -> -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).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;printadds per-reaction charge detail. It emits oneChanging 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) reactionlines, since none of the 33 is CK-flagged.Add
saveto 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.pyis the relevant second check and should report no new problems.Scripts/Biochemistry/Maintain/Check_Charges.pywill 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