From e06f1a718bf0b1a3bfff73111a3bd856052999fa Mon Sep 17 00:00:00 2001 From: Aaron Lisman Date: Thu, 19 Nov 2026 12:06:09 +0000 Subject: [PATCH] Study View filter bar: the portal's words between groups (STV-FB-2) Generic pills read as the portal's GroupLogic writes them: an OR group "( a or b )", lower-case and in parentheses, and two groups in a row from the same chart joined by "and" (joinedByAnd); a clinical chart's pill, or another chart's group, gets no word. The Cohorts tab's pills keep their own AND / OR. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Q3ezTaCo6wHFVsML6Qd4ht --- app/lab/pills.tsx | 7 +++++-- app/lab/study-view-pills.tsx | 10 +++++++--- lib/study-view-pills.ts | 25 ++++++++++++++++++++++++ scripts/mutants/study-view/pill-words.ts | 10 ++++++++++ test/study-view-pills.test.ts | 18 ++++++++++++++++- 5 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 scripts/mutants/study-view/pill-words.ts diff --git a/app/lab/pills.tsx b/app/lab/pills.tsx index 8b3664ffd..ada0fb06f 100644 --- a/app/lab/pills.tsx +++ b/app/lab/pills.tsx @@ -152,15 +152,18 @@ export function normalizeDef(def: any) { } /** one top-level clause: a pill, a NOT pill, or an OR group, with an × when it can be removed */ -export function ClausePill({ clause: c, onRemove, onCustomData }: { clause: any; onRemove?: () => void; onCustomData?: OnCustomData }) { +/** `portal`: an OR group written as the Study View's filter bar writes it, "( a or b )" (STV-FB-2) */ +export function ClausePill({ clause: c, onRemove, onCustomData, portal }: { clause: any; onRemove?: () => void; onCustomData?: OnCustomData; portal?: boolean }) { if (c.any) return ( + {portal && (} {c.any.map((l: any, j: any) => ( - {j > 0 && OR} + {j > 0 && {portal ? "or" : "OR"}} ))} + {portal && )} {onRemove && } ); diff --git a/app/lab/study-view-pills.tsx b/app/lab/study-view-pills.tsx index cf35ff975..5a63e0fa5 100644 --- a/app/lab/study-view-pills.tsx +++ b/app/lab/study-view-pills.tsx @@ -3,7 +3,7 @@ // numeric intervals with editable ends; clauses of no clinical chart drawn as the generic pills. import { Fragment, useEffect, useState } from "react"; import type { CohortDef } from "../../lib/types.ts"; -import { editInterval, filterGroups, intervalEnds, intervalText, removePill, type Interval, type SvPill } from "../../lib/study-view-pills.ts"; +import { editInterval, filterGroups, intervalEnds, joinedByAnd, intervalText, removePill, type Interval, type SvPill } from "../../lib/study-view-pills.ts"; import { ClausePill, CLINICAL_COLOR, type OnCustomData } from "./pills"; type Result = { draft: CohortDef } | { why: string }; @@ -45,10 +45,14 @@ export default function StudyViewPills({ draft, labelOf, apply, onIntervalEdited onRemoveClause: (i: number) => void; onCustomData?: OnCustomData; }) { + const groups = filterGroups(draft, labelOf), and = joinedByAnd(draft, groups); return ( - {filterGroups(draft, labelOf).map((g) => g.kind === "other" - ? onRemoveClause(g.clause)} onCustomData={onCustomData} /> + {groups.map((g, i) => g.kind === "other" + ? + {and[i] && and} + onRemoveClause(g.clause)} onCustomData={onCustomData} portal /> + : ( {g.label} diff --git a/lib/study-view-pills.ts b/lib/study-view-pills.ts index 7f5f9dad8..1cb9d30a4 100644 --- a/lib/study-view-pills.ts +++ b/lib/study-view-pills.ts @@ -134,3 +134,28 @@ export function intervalEnds(draft: CohortDef, clause: number, leaf: number): nu if (!iv) return []; return iv.kind === "point" ? [iv.value] : [iv.lo, iv.hi].filter((x): x is number => x != null); } + +/** which chart a generic clause comes from, as far as its leaves say: their kind, and the gene or entity of a genomic one */ +function chartOfClause(c: unknown): string { + const x = c as { any?: unknown[]; not?: unknown }; + const l = (Array.isArray(x?.any) ? x.any[0] : x?.not ?? c) as { kind?: string; gene?: string; profileType?: string; entity?: string; time?: string } | undefined; + if (!l) return ""; + if (l.kind === "genomicData") return "genomicData|" + l.gene + "|" + l.profileType; + if (l.kind === "genericAssay") return "genericAssay|" + l.profileType + "|" + l.entity; + if (l.kind === "treatment") return "treatment|" + (l.time ? "sample" : "patient"); + return String(l.kind ?? "event"); +} + +/** + * The words between the filter bar's groups (STV-FB-2): two generic groups in a row from the same + * chart (two gene-table selections, two case-list selections) are joined by "and", as the portal + * writes "( APC or TP53 ) and ( KRAS )"; a clinical chart's pill, or a group from another chart, + * gets no word. One entry per group: whether "and" goes before it. + */ +export function joinedByAnd(draft: CohortDef | null | undefined, groups: SvGroup[]): boolean[] { + const all = draft?.all || []; + return groups.map((g, i) => { + const prev = groups[i - 1]; + return i > 0 && g.kind === "other" && prev.kind === "other" && chartOfClause(all[g.clause]) === chartOfClause(all[prev.clause]); + }); +} diff --git a/scripts/mutants/study-view/pill-words.ts b/scripts/mutants/study-view/pill-words.ts new file mode 100644 index 000000000..ca3d3d09c --- /dev/null +++ b/scripts/mutants/study-view/pill-words.ts @@ -0,0 +1,10 @@ +// Planted bugs: the words between the Study View filter bar's groups (STV-FB-2). How they are planted and run: scripts/check-mutants.ts. +import type { GuardCommands, Mutant } from "../types.ts"; + +export const guards: GuardCommands = {}; + +export const mutants: Mutant[] = [ + { id: "pill-and-across-charts", layer: "counts", guard: "unit", file: "lib/study-view-pills.ts", + effect: "An \"and\" is written between generic groups of different charts (a case list after a gene), where the portal writes none.", + find: "&& chartOfClause(all[g.clause]) === chartOfClause(all[prev.clause]);", replace: ";" }, +]; diff --git a/test/study-view-pills.test.ts b/test/study-view-pills.test.ts index 2697d0798..a4198afae 100644 --- a/test/study-view-pills.test.ts +++ b/test/study-view-pills.test.ts @@ -2,7 +2,7 @@ // chart (STV-FB-3), the interval text (STV-FB-4), its editable ends, and each pill's ×. import { test, describe } from "node:test"; import assert from "node:assert/strict"; -import { editInterval, filterGroups, intervalEnds, intervalText, leafInterval, removePill } from "../lib/study-view-pills.ts"; +import { editInterval, filterGroups, intervalEnds, intervalText, leafInterval, removePill, joinedByAnd } from "../lib/study-view-pills.ts"; import { naClause } from "../lib/explore-selection.ts"; import type { ClinicalLeaf, CohortDef } from "../lib/types.ts"; @@ -109,3 +109,19 @@ describe("an interval's numbers after an edit", () => { assert.deepEqual(intervalEnds(d, 9, 0), []); }); }); + +// STV-FB-2: the words between the bar's groups, as the portal's GroupLogic writes them +test("two generic groups in a row from the same chart are joined by \"and\"; a clinical chart's pill or another chart's group by nothing", () => { + const draft = { all: [ + { any: [{ kind: "oql", q: "APC: MUT" }, { kind: "oql", q: "TP53: MUT" }] }, // a gene-table selection + { kind: "oql", q: "KRAS: MUT" }, // another one: "and" + { kind: "clinical", attribute: "SEX", op: "=", value: "Female" }, // a clinical pill: no word + { kind: "caseList", id: "coadread_all" }, // another chart: no word + { kind: "caseList", id: "coadread_cna" }, // the same chart: "and" + { kind: "treatment", agents: ["A"] }, // treatments per patient + { kind: "treatment", agents: ["B"], time: "Pre" }, // per sample: another table + ] } as unknown as CohortDef; + const groups = filterGroups(draft, () => undefined); + assert.deepEqual(joinedByAnd(draft, groups), [false, true, false, false, true, false, false]); + assert.deepEqual(joinedByAnd(null, []), []); +});