Skip to content

feat(manhattan): support string chromosome columns ('X', 'Y', 'MT') - #782

Open
thanvish21 wants to merge 1 commit into
plotly:masterfrom
thanvish21:feat/manhattan-string-chromosome
Open

thanvish21 wants to merge 1 commit into
plotly:masterfrom
thanvish21:feat/manhattan-string-chromosome

Conversation

@thanvish21

Copy link
Copy Markdown

Summary

Closes #772.

Currently ManhattanPlot raises TypeError when the chrm column is non-numeric, forcing users to manually recode 'X', 'Y', and 'MT' chromosomes to integers. This matters for:

  • Species with named chromosomes (e.g. fruit fly: 2L, 2R, 3L, 3R)
  • Standard human GWAS datasets where the chromosome column is stored as strings

Changes

  • Removes the TypeError that rejected string chrm columns — string columns are now first-class citizens.
  • Canonical genomic sort order (1–22, X, Y, MT) is applied to string chromosome labels via _CHR_SORT_ORDER and a stable sort key so x-axis ticks always appear in biological order.
  • INDEX column kept as int regardless of chrm dtype, so positional arithmetic works correctly for string columns (previously it was cast back to the original dtype, which broke positional sums for strings).
  • Crowded-label suppression (>10 chromosomes) now uses an index-based modulo check instead of int(t), which crashed for string labels.
  • Trace name formatting fixed in both the single- and multi-chromosome figure paths (%i → scalar element extracted with [0]).
  • Docstrings updated to reflect string column support.
  • 7 unit tests added in tests/unit/test_manhattan_plot.py covering:
    • String chrm columns raise no error
    • Correct trace count per chromosome
    • Genomic sort order of tick labels
    • Crowded-label suppression with 24 chromosomes
    • Single string chromosome
    • Numeric chromosome regression (existing behaviour preserved)
    • Tick count matches unique chromosome count

Before / After

# Before — raises TypeError
df["CHR"] = df["CHR"].astype(str)
ManhattanPlot(df, chrm="CHR", ...)
# TypeError: CHR column should be numeric. Do you have 'X', 'Y', 'MT', etc?

# After — works
ManhattanPlot(df, chrm="CHR", ...)  # ✓

🤖 Generated with Claude Code

Closes plotly#772. Previously ManhattanPlot raised TypeError when the chrm
column had a non-numeric dtype, forcing users to manually recode 'X',
'Y', and 'MT' chromosomes to integers. This change:

- Removes the TypeError that rejected string chrm columns.
- Adds canonical genomic sort order (1–22, X, Y, MT) for string
  chromosome labels via _CHR_SORT_ORDER and a stable sort key, so
  x-axis ticks always appear in biological order regardless of the
  order rows appear in the dataframe.
- Keeps INDEX as int regardless of chrm dtype so positional arithmetic
  works correctly for string chrm columns.
- Fixes crowded-label suppression (>10 chromosomes) which previously
  called int(t) on each label—crashing for string chromosomes—replaced
  with an index-based modulo check.
- Fixes trace name formatting (%i → scalar element) in both the
  single-chromosome and multi-chromosome figure paths.
- Updates docstrings to reflect that string chrm columns are supported.
- Adds tests/unit/test_manhattan_plot.py with 7 unit tests covering
  the new behaviour and numeric-chromosome regression.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@camdecoster

Copy link
Copy Markdown

Thanks for the PR! This repo needs some work before we can even review this. It's in progress, but this will have to remain open and unreviewed until we get the infrastructure for CI working again.

This branch has not been deployed

No deployments
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.

Feature request: Support for string type chromosome column in ManhattanPlot

2 participants