Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/job_scripts/reconstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,13 @@ def now():
ws = get_storage_service(args.token)

import cobra.io
mdlutl.model.objective = "bio1"
reaction_ids = {r.id for r in mdlutl.model.reactions}
if "bio1" in reaction_ids:
mdlutl.model.objective = "bio1"
else:
bio_rxns = [r for r in mdlutl.model.reactions if r.id.startswith("bio")]
if bio_rxns:
mdlutl.model.objective = bio_rxns[0].id
cobra_json = json.dumps(cobra.io.model_to_dict(mdlutl.model))

if not hasattr(mdlutl.model, 'get_data'):
Expand Down
18 changes: 12 additions & 6 deletions src/modelseed_api/jobs/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,13 +744,19 @@ def reconstruct(
if "bio1" in {r.id for r in mdlutl.model.reactions}:
mdlutl.model.objective = "bio1"
else:
raise RuntimeError(
"Model has no 'bio1' biomass reaction; reconstruction "
"produced an incomplete model. Common causes: input "
"genome too small for the classifier, or the selected "
"template lacks a biomass definition. Try template_type "
"explicitly (gn/gp/ar) or provide a larger input genome."
logger.error(
"Reconstruction produced no bio1 for genome %s", genome_id,
)
return {
"status": "failed",
"error": (
"Model has no bio1 biomass reaction; reconstruction"
" produced an incomplete model. Common causes: input genome"
" too small for the classifier, or the selected template"
" lacks a biomass definition. Try template_type explicitly"
" (gn/gp/ar) or provide a larger input genome."
),
}
cobra_json = json.dumps(cobra.io.model_to_dict(mdlutl.model))

if not hasattr(mdlutl.model, 'get_data'):
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_reconstruct_bio1_guard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Unit tests for bio1 objective guard logic."""
import pytest

pytestmark = pytest.mark.unit


def _make_reactions(ids):
"""Create mock reactions from a list of IDs."""
return [type("R", (), {"id": i})() for i in ids]


class TestBio1Check:
def test_bio1_present_passes_guard(self):
reactions = _make_reactions(["rxn00001", "bio1"])
assert "bio1" in {r.id for r in reactions}

def test_bio1_absent_triggers_failure_path(self):
reactions = _make_reactions(["rxn00001", "rxn00002"])
assert "bio1" not in {r.id for r in reactions}

def test_bio_fallback_finds_bio2(self):
reactions = _make_reactions(["rxn00001", "bio2"])
fallback = next(
(r.id for r in reactions if r.id.startswith("bio")), None
)
assert fallback == "bio2"

def test_no_biomass_reaction_returns_none(self):
reactions = _make_reactions(["rxn00001", "rxn00002"])
fallback = next(
(r.id for r in reactions if r.id.startswith("bio")), None
)
assert fallback is None
Loading