Skip to content

Add --chinese (CJK) figure-label rendering, plus related fixes#849

Merged
tanghaibao merged 3 commits into
mainfrom
add-chinese-figure-labels
Jun 21, 2026
Merged

Add --chinese (CJK) figure-label rendering, plus related fixes#849
tanghaibao merged 3 commits into
mainfrom
add-chinese-figure-labels

Conversation

@tanghaibao

Copy link
Copy Markdown
Owner

Summary

Adds opt-in Chinese (CJK) label rendering for figures, plus several
rendering fixes uncovered while producing Chinese versions of the textbook
figures.

Feature: --chinese

  • New global --chinese option in set_image_options. It implies --notex
    (LaTeX can't render CJK) and selects a CJK-capable font via the new
    set_chinese_font() / CJK_FONTS (cross-platform: macOS / Windows / Linux).
  • markup() now degrades LaTeX-only markup when usetex is off:
    \textit{...} → mathtext italic (ASCII) or plain (CJK), strips *..*,
    \noindent, and turns \\ into a newline — so labels render without LaTeX.
  • Chinese variants wired through the figure code: graphics.tree
    (geoscale era/axis names), graphics.dotplot (title), graphics.karyotype,
    graphics.align demo, and the project figures projects.misc waterlilyGOM,
    projects.jcvi synteny, projects.napus fig3.

Usage: add --chinese to any of these commands (no separate --notex needed).

Fixes (independent of i18n)

  • karyotype: restrict a provided sizes mapping to each track's seqids.
    Previously every track inherited the genome-wide total, so per-chromosome
    gauge/coverage axes collapsed (e.g. napus fig3 panel A).
  • tree: on geoscale trees, align leaf tips at max_dist so a rerooted
    outgroup is drawn at the present rather than in the past.
  • compara.ks: tolerate Biopython ≥ 1.78 (Bio.Align.Applications removed)
    and resolve PAML/PAL2NAL paths lazily so importing the module no longer
    prompts for binaries that only calc needs.
  • graphics.align: integer slice indices in OpticalMapTrack.duplicate.
  • projects.napus: modernize annotate() arrowprops (drop the removed
    frac kwarg).

Notes

  • The per-figure Chinese strings live in the project/figure functions that are
    already paper-specific; waterlilyGOM/synteny/fig3 read *.cn label
    files or use Chinese label dicts only under --chinese.
  • Backward compatible: default behavior (no --chinese) is unchanged; the
    fixes apply regardless of language.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QEaqko1qcY3wLLB93giokq

Add a global --chinese option (in set_image_options) that renders figure
labels in Chinese: it implies --notex and selects a CJK-capable font
(set_chinese_font / CJK_FONTS, cross-platform). markup() now degrades LaTeX
markup when usetex is off (\textit{} -> mathtext italic, strips *..*, \\,
\noindent) so labels stay readable without LaTeX.

Per-figure Chinese variants are wired through where the textbook figures are
generated: graphics.tree (geoscale era/axis names), graphics.dotplot (title),
graphics.karyotype, graphics.align demo, projects.misc waterlilyGOM,
projects.jcvi synteny, projects.napus fig3.

Also fixes several rendering issues found along the way:
- karyotype: restrict provided `sizes` to each track's seqids, otherwise every
  track inherits the genome-wide total and the per-chromosome gauge/coverage
  axes collapse (e.g. napus fig3 panel A).
- tree: align leaf tips at max_dist on geoscale trees so a rerooted outgroup
  is drawn at the present instead of in the past.
- compara.ks: tolerate Biopython>=1.78 (Bio.Align.Applications removed) and
  resolve PAML/PAL2NAL paths lazily so importing the module no longer prompts.
- graphics.align: integer slice indices in OpticalMapTrack.duplicate.
- projects.napus: modernize annotate() arrowprops (drop removed `frac` kwarg).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QEaqko1qcY3wLLB93giokq
Copilot AI review requested due to automatic review settings June 20, 2026 23:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an opt-in --chinese rendering mode for figure labels (disabling LaTeX and selecting CJK-capable fonts), and applies several rendering and correctness fixes discovered while producing Chinese versions of figures across the projects and graphics modules.

Changes:

  • Introduces --chinese as a global image option, selects CJK fonts, and improves markup() behavior when LaTeX is disabled.
  • Wires Chinese label variants through multiple figure generators (tree geoscale labels, dotplot titles, several project figures).
  • Fixes several independent issues (karyotype per-track sizing, geoscale tree tip alignment, Biopython compatibility scaffolding, align slice indices, arrowprops modernization).

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/jcvi/projects/napus.py Adds Chinese template/labels for fig3 and adjusts label rendering to work without LaTeX.
src/jcvi/projects/misc.py Adds Chinese label/file selection to waterlilyGOM() and passes Chinese flag into tree rendering.
src/jcvi/projects/jcvi.py Updates synteny() theme setup to respect usetex, re-applies CJK font, and outputs in requested format.
src/jcvi/graphics/tree.py Adds chinese flag support for geoscale labels and aligns geoscale leaf tips to present.
src/jcvi/graphics/karyotype.py Fixes per-track sizes handling so tracks don’t inherit genome-wide totals.
src/jcvi/graphics/dotplot.py Adds chinese flag and localizes default title text.
src/jcvi/graphics/coverage.py Routes labels through markup() to render without LaTeX.
src/jcvi/graphics/base.py Enhances markup() for non-LaTeX rendering and adds CJK font selection helpers.
src/jcvi/graphics/align.py Fixes OpticalMapTrack.duplicate() to use integer slice indices; localizes demo headers.
src/jcvi/compara/ks.py Attempts Biopython >= 1.78 import tolerance and makes PAML/PAL2NAL resolution lazy.
src/jcvi/apps/base.py Adds global --chinese CLI option, implies --notex, and applies CJK font selection.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 197 to 200
if plot_label:
label = labels_dict.get(label, label.capitalize())
label = r"\textit{{{0}}}".format(label)
label = markup(r"\textit{{{0}}}".format(label))
root.text(x - 0.015, yy + yinterval / 2, label, ha="right", va="center")
Comment thread src/jcvi/compara/ks.py Outdated
Comment on lines +19 to +22
try:
from Bio.Align.Applications import ClustalwCommandline, MuscleCommandline
except ImportError: # Biopython >= 1.78 removed the Applications command wrappers
ClustalwCommandline = MuscleCommandline = None
Comment on lines +386 to +395
if sizes:
# Restrict the provided sizes to this track's seqids; otherwise
# every track inherits the genome-wide total and the per-chr
# gauge/coverage axes collapse.
sz = dict((x, sizes[x]) for x in seqids if x in sizes)
else:
sz = dict(
(x, max(z.end for z in list(bed.sub_bed(x)))) for x in seqids
)
assert sz, "sizes not available and cannot be inferred"
…validation

- coverage: format track labels via the *..* convention so markup() produces
  valid output under both latex (-> \textit) and --notex (-> mathtext italic);
  previously a pre-formatted \textit{} string was latex-escaped and broke.
- markup: render *..* like \textit{} when usetex is off (mathtext italic for
  ASCII, plain for CJK).
- compara.ks: replace the None fallback with minimal Clustalw/Muscle command
  shims (build the command and run the binary) so call sites keep working on
  Biopython >= 1.78 instead of failing with a NoneType TypeError.
- karyotype: when sizes is provided, validate every seqid has a size and fail
  early with a clear message instead of a later KeyError in Track.draw.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QEaqko1qcY3wLLB93giokq
@tanghaibao

Copy link
Copy Markdown
Owner Author

Addressed all three review comments in c1e9c4b:

  • coverage: format track labels via the *..* convention so markup() stays valid under both latex (→ \textit{}) and --notex (→ mathtext italic). The previous markup(r"\textit{...}") was latex-escaped and broke the default path.
  • compara.ks: replaced the None fallback with minimal Clustalw/Muscle command shims (build + run the binary) so the calc/alignment call sites keep working on Biopython ≥ 1.78 instead of failing with a NoneType TypeError.
  • karyotype: when sizes is provided, validate every seqid has a size and fail early with a clear message instead of a later KeyError in Track.draw.

@tanghaibao tanghaibao merged commit 846be3c into main Jun 21, 2026
6 checks passed
@tanghaibao tanghaibao deleted the add-chinese-figure-labels branch June 21, 2026 00:26
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.

2 participants