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
7 changes: 5 additions & 2 deletions app/lab/pills.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<span className="fgroup">
{portal && <span className="fconn">(</span>}
{c.any.map((l: any, j: any) => (
<Fragment key={j}>
{j > 0 && <span className="fconn">OR</span>}
{j > 0 && <span className="fconn">{portal ? "or" : "OR"}</span>}
<Pill leaf={l} onCustomData={onCustomData} />
</Fragment>
))}
{portal && <span className="fconn">)</span>}
{onRemove && <button className="fx" aria-label="remove group" onClick={onRemove}>×</button>}
</span>
);
Expand Down
10 changes: 7 additions & 3 deletions app/lab/study-view-pills.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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 (
<span className="svpills">
{filterGroups(draft, labelOf).map((g) => g.kind === "other"
? <ClausePill key={g.clause} clause={draft.all![g.clause]} onRemove={() => onRemoveClause(g.clause)} onCustomData={onCustomData} />
{groups.map((g, i) => g.kind === "other"
? <Fragment key={g.clause}>
{and[i] && <span className="svop">and</span>}
<ClausePill clause={draft.all![g.clause]} onRemove={() => onRemoveClause(g.clause)} onCustomData={onCustomData} portal />
</Fragment>
: (
<span key={g.clause} className="svgroup" data-sv-chart={g.attribute}>
<span className="svname" data-tip={g.attribute}>{g.label}</span>
Expand Down
25 changes: 25 additions & 0 deletions lib/study-view-pills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
}
10 changes: 10 additions & 0 deletions scripts/mutants/study-view/pill-words.ts
Original file line number Diff line number Diff line change
@@ -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: ";" },
];
18 changes: 17 additions & 1 deletion test/study-view-pills.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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, []), []);
});
Loading