Skip to content

Study View: Add chart's X vs Y tab, a density chart for any two numeric attributes (STV-AC-7, STS-SC-1) - #462

Merged
alisman merged 3 commits into
masterfrom
studyview-xvsy
Nov 19, 2026
Merged

alisman merged 3 commits into
masterfrom
studyview-xvsy

Conversation

@alisman

@alisman alisman commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add chart gains the portal's X vs Y tab (STV-AC-7), and Explore draws a density chart for any two numeric clinical attributes (STS-SC-1), not only Mutation Count against FGA.

  • The tab: two selects, "Select first clinical attribute" and "Select second clinical attribute". They list every clinical attribute except a survival one other than OS_STATUS, as the portal's (xVsYAttributes).

  • The button, as the portal's, case by case (xVsYButton):

    Pair Button
    either missing "Add Chart" (disabled)
    the same attribute "Please choose two different attributes."
    a chart that exists, either way round (the default included) "A chart with these attributes already exists"
    two categorical "Can't add a chart with two categorical attributes (yet)."
    numeric + categorical "Add violin/box plot table"
    two numeric "Add density plot"

    The violin table (STS-VI) is not drawn here yet, so the numeric + categorical button stays disabled and its tooltip says why. After an add, " vs added." shows, and the selects reset.

  • The chart: " vs ", drawn beside the default chart, with the default's menu:

    • Swap Axes;
    • Log Scale on an axis that offers it (Mutation Count);
    • Download: Data, SVG, PDF;
    • Remove chart, which takes the chart's filters on both attributes and their temporary bar edges with it, as the portal's (xyChartRemoval);
    • a drag box that filters on both attributes. Its ends become temporary bar edges on the two bar charts (Study View: a density drag adds its ranges to the bar charts as custom bins (STS-SC-6) #449), kept per attribute: a drag on one density chart replaces only its own attributes' edges, and another chart's stay while its filter does (densityBinsAfterDrag, as the portal's _customBinsFromScatterPlotSelectionSet).

    Axis domains follow the existing rules (FGA 0–1, Mutation Count from 0, any other attribute its data's range).

  • Code: the density card is now a component per pair (DensityCard), each with its own request and settings. The density functions take the pair (densityAxes, densityView, densitySelection), defaulting to FGA / Mutation Count. The survival card now asks for survival alone.

Spec IDs

  • Covered: STV-AC-7 (the tab and its button), STS-SC-1 (X vs Y density charts, titled " vs ", one per pair).
  • Left out: the violin / box table for a numeric × categorical pair (STS-VI-1…8).

Verification

  • Browser, dev server, coadread:

  • e2e: a new step in test/e2e/explore.cjs on luad. It checks the numeric + categorical and numeric pairs' buttons, the added chart's title, and that the default chart stays; then a drag on the added chart filters on both its attributes, and Remove chart takes the chart and both filters. Run on its own against my dev server: pass.

  • Browser, two density charts (coadread): a drag on the default chart gives Mutation Count its temporary edges (… (240, 271] (271, 6,361] > 6,361); a drag on an added Diagnosis Age × FGA chart then keeps them.

  • Unit tests:

    • test/study-view-add-chart-spec.test.ts: the button's seven cases (an existing pair either way round, the violin refusal's reason) and the attribute list.
    • test/study-view-density-spec.test.ts: a pair's axes, swap, Log Scale only where offered, the pair's key either way round, the view's attributes and range; a drag on one chart keeping another's temporary edges; Remove chart's pair, filters and edges.

    npm test 6,903 pass, 0 fail (with master merged in); tsc --noEmit clean.

  • Coverage: passes.

  • Planted bugs: 5 in scripts/mutants/study-view/xvsy.ts (guard unit).

Not done / follow-ups

  • The violin / box table for a numeric × categorical pair (STS-VI).
  • The charts added here are the page's, not kept in the URL or a saved layout (saved layouts are out of this lane).

Tracker

Under "Study View → filters, actions and tabs", this line (docs/PORTAL-PARITY.md:479):

  • The X vs Y tab (STV-AC-7)

becomes:

Under "Study View → summary charts", this line (docs/PORTAL-PARITY.md:404):

  • Chart kinds: treatment tables, the GA frequency table (arm-level CNA by default), variant annotations, X-vs-Y density and violin, gene-specific violin, continuous gene bar (STS-OT-4…6, STS-VI-1…8, STS-GS-4, STS-SC-1, STS-STM-16, STS-STM-17)

gains:

Scorecards:

31-study-view-shell.md, row STV-AC-7 (was | STV-AC-7 | ❌ | B | No X vs Y |):

| STV-AC-7 | 🟡 | B | The X vs Y tab: two attribute selects (survival but OS_STATUS left out) and the portal's button for each pair (xVsYButton); two numeric add a density chart; numeric × categorical refused, the violin table not drawn yet |

30-study-summary.md, row STS-SC-1 (was | STS-SC-1 | 🟡 | BC | Fixed "Mutation count vs fraction genome altered" card when both attributes exist; no other X vs Y |):

| STS-SC-1 | ✅ | BC | The default "Mutation Count vs Fraction Genome Altered" card when both attributes exist (hidden when unchecked in the Genomic tab); any other numeric pair from Add chart › X vs Y, " vs ", one per pair (DensityCard, densityPairKey) |

Preview

/lab?tab=explore&study=coadread_tcga_pan_can_atlas_2018: Add chart → X vs Y → Diagnosis Age and Mutation Count.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Q3ezTaCo6wHFVsML6Qd4ht

…ic attributes (STV-AC-7, STS-SC-1)

As the portal's: two selects of clinical attributes (survival ones but
OS_STATUS left out) and its button for the pair, case by case
(xVsYButton): Add Chart, two different attributes, a chart that exists
(either way round), two categorical, a numeric with a categorical (the
portal's violin table, not drawn here, so refused saying so), and Add
density plot. A numeric pair is drawn as its own density chart, "<Y> vs
<X>", with Swap Axes, Log Scale where the axis offers it, the download
and a drag box, beside the default Mutation Count vs FGA chart.

The density card is now a component per pair (DensityCard) with its own
request; the survival card asks for survival alone.

Verified on coadread: every button state; AGE vs Mutation Count drawn,
532 of 594 samples have both; the default chart and survival unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q3ezTaCo6wHFVsML6Qd4ht
@vercel

vercel Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
cbiolab-genie Ready Ready Preview Nov 19, 2026 4:54pm UTC
cbiolab-new Ready Ready Preview Nov 19, 2026 4:54pm UTC
cohort-lab Ready Ready Preview Nov 19, 2026 4:54pm UTC

Request Review

const title = densityTitle(nameOf(axes.x.attribute), nameOf(axes.y.attribute));
const names = { x: nameOf(axes.x.attribute), y: nameOf(axes.y.attribute) };
return (
<div className="card xc-card" data-chart={added ? "xvsy" : "mutcount-fga"} data-pair={densityPairKey(pair)}>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

An added X vs Y card has data-chart="xvsy", and cardOrders (lib/study-view-config.ts CARD_PRIORITY) has no entry for it. Ordered cards get negative order values (i - cards.length), so this card keeps the default order: 0 and goes to the end of the facet grid, after every clinical card. It is not "beside the default chart" as the PR says. The e2e step only counts the cards, so it doesn't catch this. Fix: give the X vs Y cards an order next to [data-chart="mutcount-fga"] (for example, a [data-chart="xvsy"] entry at priority 200).

<div className="xc-body">
{plot && plot.shape === "scatter" && !plot.grid && <p className="note">No data to plot.</p>}
{plot && plot.shape === "scatter" && plot.grid &&
<Density data={plot} axes={axes} names={names} figure={densityFile} onSelect={(box, span) => addClauses(densitySelection(box, dset, span, pair))} />}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Every density card now calls addDensitySelection, which replaces densityBins.current wholesale (use-explore-state.ts:602). With two charts on screen: drag on the default chart, then drag on Age vs Mutation Count. FGA's temporary bar edges from the first drag disappear, but its FGA range filter stays in the draft. The portal keeps scatter-selection bins per attribute (_customBinsFromScatterPlotSelectionSet.set(attr, …)). Fix: merge the new edges by attribute instead of overwriting the whole array.

const [added, setAdded] = useState<string | null>(null);
const ax = attrs.find((a) => a.attribute === x) ?? null, ay = attrs.find((a) => a.attribute === y) ?? null;
// the default chart counts as existing, as on the portal
const existing = [...xyCharts.map(densityPairKey), densityPairKey(DEFAULT_DENSITY_PAIR)];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The default pair always counts as existing, even when its chart is not drawn. That happens when the reader unchecked the density in the Genomic tab (density=false, so hasScatter is false). Picking FGA + Mutation Count then says "A chart with these attributes already exists", but no such chart is on the page. The pair also can't be added as an X vs Y chart, because shown filters out the default key. Fix: count the default only when chartChecked("genomic|density") is true.

*/
function DensityCard({ pair, added, studies, draft, selection, addClauses, nameOf, onRemove }: { pair: DensityPair; added: boolean; studies: string[];
draft: Draft; selection: Props["selection"]; addClauses: Props["addClauses"]; nameOf: (a: string) => string; onRemove?: () => void }) {
const storeKey = added ? DENSITY_KEY + "." + densityPairKey(pair) : DENSITY_KEY;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Each added chart's settings are stored under the sorted pair key, but swapped is relative to the pair's x/y order as picked. Example: add Age (x) vs Mutation Count (y), Swap Axes, then Remove chart. Re-adding it as Mutation Count (x) vs Age (y) reads swapped: true from storage, so the chart draws Age along the bottom, the opposite of what was picked. Remove chart also leaves the stored settings behind. Fix: key the storage by the ordered pair, or clear it in onRemovePair.

export function ExploreCharts({ studies, selection, draft, attrs, endpoints, addClauses, km, density = true, pairs = [], onRemovePair }: Props) {
const has = (a: string) => attrs.some((x) => x.attribute === a);
const hasScatter = density && has(MUTATION_COUNT) && has(FGA);
const nameOf = (a: string) => attrs.find((x) => x.attribute === a)?.displayName || DENSITY_NAMES[a] || a;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

nameOf now uses the study's displayName before DENSITY_NAMES, so the default chart's title, axis labels and download file names follow each study's metadata. Before, they were always the portal's "Mutation Count vs Fraction Genome Altered". A study whose display name differs (for example "Fraction of genome altered" or a different case) gets a different title and file name than the portal. Fix: use DENSITY_NAMES first for those two attributes.

setLoading(true);
const cohort: CohortDef = { ...(studies.length > 1 ? { studies } : {}), all: (draft ? draft.all : []) as CohortDef["all"] };
const ac = new AbortController();
api.analysis({ study: studies[0], cohorts: { Selected: cohort }, views: [densityView(dset, pair)] }, { signal: ac.signal })

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Survival and each density chart now send their own api.analysis request, where the default density used to share one request with survival. With k added pairs, every filter change sends k+2 analysis requests, and each one resolves the same cohort on the server. Each card also sends a throwaway first request with {} settings before its localStorage effect runs, and that request is aborted. Fix: batch the views into one request in ExploreCharts and hand each card its view's result.

{ label: string; adds: boolean; tip?: string } {
if (!x || !y) return { label: "Add Chart", adds: false };
if (x.attribute === y.attribute) return { label: "Please choose two different attributes.", adds: false };
if (existing.includes([x.attribute, y.attribute].sort().join("|"))) return { label: "A chart with these attributes already exists", adds: false };

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

[x.attribute, y.attribute].sort().join("|") re-implements densityPairKey from lib/study-view-density.ts, and the caller in explore-sections.tsx builds existing with densityPairKey. If the key format changes in one place, this check silently stops matching. Fix: use densityPairKey({ x: x.attribute, y: y.attribute }) and update the mutant's find string to match.


/**
* One X vs Y density chart (STS-SC-1…6): its own settings (Swap Axes, Log Scale; the default
* chart's kept under its old key), its own request, its menu, and a drag box that filters on both.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This comment describes history ("the default chart's kept under its old key"). ~/projects/CLAUDE.md says: "No "why I changed this" comments in code. Comments describe the current code's intent, not its history". Suggest: "the default chart's under cbiolab.explore.density, an added one's under that plus its pair key".

@alisman

alisman commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Review verdict: changes requested (head 29e69061)

The tab, the button cases, the attribute list and the per-sample pairing are right. A patient attribute against a sample one is plotted per sample with the patient's value spread to each sample, the bins are computed in SQL, and the denominator is samples. The drag's two range clauses select the samples shown in the box. Three things block:

  1. Remove chart leaves the chart's filters in place. removeXyChart only drops the pair from the list. The portal's removal of an X vs Y chart (resetFilterAndChangeChartVisibility → updateScatterPlotFilterByValues(key) with no bounds) deletes the clinical filters on both attributes and their scatter-selection bins. This repo already has the pattern (clearAttributeFilters, used when a chart is unchecked). Removing the chart should call it for pair.x and pair.y.
  2. A drag on one density chart wipes another chart's temporary bar edges (inline comment on explore-charts.tsx:188). addDensitySelection replaces densityBins.current wholesale. The portal keeps _customBinsFromScatterPlotSelectionSet per attribute (.set(attrId, …)), so a drag on chart B leaves chart A's edges while A's filters still hold. Fix: merge by attribute, replacing only the two dragged attributes.
  3. The drag and the removal on an added chart are untested. No unit test calls densitySelection(box, s, span, pair) with a non-default pair (an AGE axis's rounding, which attribute goes on which clause after Swap Axes). Neither of the two planted bugs touches the drag or the removal. Please add a unit test for the pair's drag clauses, and a planted bug that densitySelection ignores pair, or that removal keeps the filters. Each should have a test that catches it.

Not blocking (see the inline comments): an added card goes to the end of the grid, which matches the portal's priority 0 for X vs Y charts. The other inline comments are nits: the default pair counts as existing while unchecked, the swapped setting is stored under the sorted key, the default title takes the study's displayName, there is one analysis request per card, densityPairKey is duplicated, and one comment describes history.

CI was still pending at review time.

alisman and others added 2 commits November 19, 2026 16:47
…her chart's temporary edges

- Remove chart on an added density chart takes its filters on both attributes and their temporary
  bar edges with it, as the portal's (xyChartRemoval).
- A drag on one density chart replaces only its own two attributes' temporary edges; another
  chart's stay while its filter does, as the portal keeps them per attribute
  (_customBinsFromScatterPlotSelectionSet; densityBinsAfterDrag, densityBinsWithout).
- The X vs Y e2e step drags on the added chart and removes it, checking its filters go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q3ezTaCo6wHFVsML6Qd4ht
# Conflicts:
#	app/lab/explore-sections.tsx
#	app/lab/use-explore-state.ts
#	lib/study-view-add-chart.ts
#	test/e2e/explore.cjs
#	test/study-view-add-chart-spec.test.ts
@alisman

alisman commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 76fcd76 (master merged in at 4394754). Each fix has its test written first and a planted bug.

  1. Remove chart: it now takes the chart's filters on both attributes and their temporary bar edges with it (xyChartRemoval in lib/study-view-density.ts, applied through clearAttributeFilters).
  2. Temporary edges per attribute: a drag replaces only its own two attributes' edges (densityBinsAfterDrag). Another density chart's edges stay while its filter does, as the portal's _customBinsFromScatterPlotSelectionSet. Browser, coadread: a drag on the default chart gives Mutation Count (… (240, 271] (271, 6,361] > 6,361); a drag on an added Diagnosis Age × FGA chart keeps them.
  3. Coverage:
    • Unit tests for both rules: a drag keeping another chart's edges; removal's pair, filters and edges.
    • Three planted bugs: wholesale replace, removal keeping the filters, removal keeping the edges.
    • The e2e step now drags on the added chart, checks both its filters, then removes it and checks they go. Run on its own: pass.

With master merged in (#459, #460): the tab row reads Clinical · Genomic · Gene status · Arm-level CNA · Genetic Ancestry · Methylation · Derived timeline filters · Custom data · X vs Y. npm test 6,903 pass, 0 fail; tsc --noEmit and eslint clean.

@alisman

alisman commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Re-review: pass. 76fcd76 fixes all three blocking findings:

  1. Remove chart: xyChartRemoval drops the pair and returns both attributes to clearAttributeFilters, with their temporary edges removed, as the portal's removal does.
  2. Drag: densityBinsAfterDrag replaces only the dragged chart's two attributes' edges, so another density chart's edges stay while its filter does, matching the portal's per-attribute _customBinsFromScatterPlotSelectionSet.
  3. Tests: unit tests cover both on an added pair. Three planted bugs (density-drag-wipes-other-chart, xy-remove-keeps-filters, xy-remove-keeps-edges) are each caught. The e2e now drags on the added Mutation Count vs Diagnosis Age chart, sees both filters, removes the chart and checks they're gone.

This branch was successfully deployed

1 active deployment
Preview – cohort-lab — 4394754f Deployed Nov 19, 2026 by vercel[bot]
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.

1 participant