Skip to content
Merged
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
2 changes: 2 additions & 0 deletions s2f/common/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@
"closest_genomes": {"type": "array"},
"tree_newick": {"type": ["string", "null"]},
"cga_job_id": {"type": ["string", "null"]},
"contigs": {"type": ["integer", "null"], "description": "number of contigs (#71)"},
"genome_length": {"type": ["integer", "null"], "description": "total length in bp (#71)"},
},
"additionalProperties": True,
},
Expand Down
15 changes: 10 additions & 5 deletions s2f/full_report/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ def tab_organism(report: dict[str, Any]) -> str:
("Genus", genome.get("genus")),
("Taxon", genome.get("taxon_id")),
("Genetic code", taxonomy.get("genetic_code")),
("Length", f"{quality.get('genome_length')} bp"
if quality.get("genome_length") else None),
("Contigs", genome.get("contigs")),
("Length", f"{_genome_length(genome)} bp"
if _genome_length(genome) else None),
("Contigs", genome.get("contigs") or quality.get("contigs")),
("Quality", quality.get("genome_quality")),
("Proteins", len(_rows(report.get("proteins"))))]:
if value not in (None, "", "None bp"):
Expand Down Expand Up @@ -734,8 +734,8 @@ def build_page(report: dict[str, Any], m2_run: dict[str, Any] | None = None) ->
for key, value in [("Genome", genome.get("genome_id")),
("Taxon", genome.get("taxon_id")),
("Route", (genome.get("annotation_route") or "").upper()),
("Length", f"{quality.get('genome_length')} bp"
if quality.get("genome_length") else None),
("Length", f"{_genome_length(genome)} bp"
if _genome_length(genome) else None),
("Quality", quality.get("genome_quality")),
("Proteins", tallies["genes"] or None),
("Selected", tallies["targets"] or None),
Expand Down Expand Up @@ -832,3 +832,8 @@ def render_run(run_dir: str | Path) -> str:
except (json.JSONDecodeError, OSError):
m2_run = {}
return build_page(report, m2_run)


def _genome_length(genome: dict) -> object:
"""Top-level since #71; runs written before that only carry it under quality."""
return genome.get("genome_length") or _mapping(genome.get("quality")).get("genome_length")
11 changes: 11 additions & 0 deletions s2f/m1_genome/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,9 @@ def genome_section(genome: dict[str, Any], bundle: dict[str, Any],
"cga_job_id": None,
# --- API-route provenance and metadata (no CGA equivalent) ---
"annotation_route": "api",
# Same keys as the CGA route (#71). Here they describe the reference genome.
"contigs": _int_or_none(genome.get("contigs")),
"genome_length": _int_or_none(genome.get("genome_length")),
"annotation_source": bundle.get("annotation", "PATRIC"),
"resolution": resolution,
"bvbrc_url": GENOME_URL.format(gid=genome.get("genome_id")),
Expand Down Expand Up @@ -895,3 +898,11 @@ def taxonomy_tree_text(lineage: list[dict[str, Any]], genome: dict[str, Any],
lines.append(f" - {neighbor.get('genome_name')} [{flag}] "
f"({neighbor.get('species')})")
return "\n".join(lines)


def _int_or_none(value: Any) -> int | None:
"""A count from a JSON record, or None; BV-BRC sometimes sends numbers as strings."""
try:
return int(value) if value not in (None, "") else None
except (TypeError, ValueError):
return None
12 changes: 12 additions & 0 deletions s2f/m1_genome/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ def genome_section(run: CgaRun, taxon_call: dict[str, Any] | None = None) -> dic
# fallback stays for runs written before this, but it is no longer the only way
# to identify a CGA run.
"annotation_route": "cga",
# Top-level so a report can quote the genome's size without digging into the
# quality block (#71). Same keys and types on the API route.
"contigs": _int_or_none(run.genome.get("contigs")),
"genome_length": _int_or_none(run.genome.get("genome_length")),
"quality": {
"genome_quality": run.quality,
"genome_quality_flags": run.quality_flags,
Expand Down Expand Up @@ -412,3 +416,11 @@ def write_m1_dir(run: CgaRun, m1_dir: str | Path) -> dict[str, int]:
spec_rows += 1

return {"proteins_faa": written, "feature_rows": rows, "specialty_rows": spec_rows}


def _int_or_none(value: Any) -> int | None:
"""A count from a JSON record, or None; BV-BRC sometimes sends numbers as strings."""
try:
return int(value) if value not in (None, "") else None
except (TypeError, ValueError):
return None
22 changes: 22 additions & 0 deletions tests/test_m1_bvbrc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,25 @@ def test_both_m1_routes_declare_their_own_annotation_route(api_bundle, kp_payloa
"both routes report the same annotation_route, which defeats the point of the "
"field: a consumer cannot tell a submitted assembly from a reference genome."
)


def test_both_routes_state_the_genome_size_at_the_top_level(api_bundle, kp_payload) -> None:
"""#71: `genome.contigs` and `genome.genome_length`, same keys and types on both routes."""
from s2f.common.schema import validate

api = collect.genome_section(kp_payload["genome"], api_bundle, "explicit genome_id")
cga = parse.genome_section(
parse.load_cga(Path(__file__).resolve().parents[1] / "fixtures/m1/cga_sample"))

assert (api["contigs"], api["genome_length"]) == (7, 5682322)
assert (cga["contigs"], cga["genome_length"]) == (1, 580076)
for section in (api, cga):
validate("genome", section)


def test_the_full_report_shows_contigs_and_length() -> None:
from s2f.full_report.render import _genome_length

assert _genome_length({"genome_length": 580076}) == 580076
# Runs written before #71 only carry it under quality.
assert _genome_length({"quality": {"genome_length": 580076}}) == 580076
Loading