@platforma-sdk/model 1.90.20 → 1.92.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV2.cjs +2 -2
  2. package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV2.cjs.map +1 -1
  3. package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV2.d.ts.map +1 -1
  4. package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV2.js +2 -2
  5. package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV2.js.map +1 -1
  6. package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV3.cjs +5 -4
  7. package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV3.cjs.map +1 -1
  8. package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV3.d.ts +12 -0
  9. package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV3.d.ts.map +1 -1
  10. package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV3.js +5 -4
  11. package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV3.js.map +1 -1
  12. package/dist/components/PlDataTable/createPlDataTableSheet.cjs +7 -3
  13. package/dist/components/PlDataTable/createPlDataTableSheet.cjs.map +1 -1
  14. package/dist/components/PlDataTable/createPlDataTableSheet.d.ts +7 -2
  15. package/dist/components/PlDataTable/createPlDataTableSheet.d.ts.map +1 -1
  16. package/dist/components/PlDataTable/createPlDataTableSheet.js +7 -3
  17. package/dist/components/PlDataTable/createPlDataTableSheet.js.map +1 -1
  18. package/dist/components/PlDataTable/index.cjs +2 -0
  19. package/dist/components/PlDataTable/index.d.ts +2 -1
  20. package/dist/components/PlDataTable/index.js +2 -1
  21. package/dist/components/PlDataTable/state-migration.cjs.map +1 -1
  22. package/dist/components/PlDataTable/state-migration.d.ts +2 -2
  23. package/dist/components/PlDataTable/state-migration.d.ts.map +1 -1
  24. package/dist/components/PlDataTable/state-migration.js.map +1 -1
  25. package/dist/components/PlDataTable/tableParams.cjs +61 -15
  26. package/dist/components/PlDataTable/tableParams.cjs.map +1 -1
  27. package/dist/components/PlDataTable/tableParams.d.ts +8 -0
  28. package/dist/components/PlDataTable/tableParams.d.ts.map +1 -0
  29. package/dist/components/PlDataTable/tableParams.js +60 -16
  30. package/dist/components/PlDataTable/tableParams.js.map +1 -1
  31. package/dist/components/PlDataTable/typesV8.d.ts +18 -0
  32. package/dist/components/PlDataTable/typesV8.d.ts.map +1 -1
  33. package/dist/components/PlDatasetSelector/build_dataset_options.cjs +43 -33
  34. package/dist/components/PlDatasetSelector/build_dataset_options.cjs.map +1 -1
  35. package/dist/components/PlDatasetSelector/build_dataset_options.d.ts +6 -2
  36. package/dist/components/PlDatasetSelector/build_dataset_options.d.ts.map +1 -1
  37. package/dist/components/PlDatasetSelector/build_dataset_options.js +46 -36
  38. package/dist/components/PlDatasetSelector/build_dataset_options.js.map +1 -1
  39. package/dist/components/PlDatasetSelector/dataset_selection.cjs.map +1 -1
  40. package/dist/components/PlDatasetSelector/dataset_selection.d.ts +13 -4
  41. package/dist/components/PlDatasetSelector/dataset_selection.d.ts.map +1 -1
  42. package/dist/components/PlDatasetSelector/dataset_selection.js.map +1 -1
  43. package/dist/components/PlDatasetSelector/enrichment_discovery.cjs +20 -26
  44. package/dist/components/PlDatasetSelector/enrichment_discovery.cjs.map +1 -1
  45. package/dist/components/PlDatasetSelector/enrichment_discovery.js +22 -28
  46. package/dist/components/PlDatasetSelector/enrichment_discovery.js.map +1 -1
  47. package/dist/components/PlDatasetSelector/filter_discovery.cjs +6 -14
  48. package/dist/components/PlDatasetSelector/filter_discovery.cjs.map +1 -1
  49. package/dist/components/PlDatasetSelector/filter_discovery.d.ts +5 -10
  50. package/dist/components/PlDatasetSelector/filter_discovery.d.ts.map +1 -1
  51. package/dist/components/PlDatasetSelector/filter_discovery.js +8 -13
  52. package/dist/components/PlDatasetSelector/filter_discovery.js.map +1 -1
  53. package/dist/components/PlDatasetSelector/index.cjs +0 -1
  54. package/dist/components/PlDatasetSelector/index.d.ts +3 -3
  55. package/dist/components/PlDatasetSelector/index.js +2 -2
  56. package/dist/components/index.cjs +2 -1
  57. package/dist/components/index.d.ts +4 -3
  58. package/dist/components/index.js +3 -2
  59. package/dist/index.cjs +2 -1
  60. package/dist/index.d.ts +4 -3
  61. package/dist/index.js +3 -2
  62. package/dist/package.cjs +1 -1
  63. package/dist/package.js +1 -1
  64. package/package.json +7 -7
  65. package/src/columns/__test_helpers__/collection_driver.ts +45 -5
  66. package/src/components/PlDataTable/createPlDataTable/createPlDataTableV2.ts +2 -1
  67. package/src/components/PlDataTable/createPlDataTable/createPlDataTableV3.ts +20 -2
  68. package/src/components/PlDataTable/createPlDataTableSheet.ts +6 -1
  69. package/src/components/PlDataTable/index.ts +1 -0
  70. package/src/components/PlDataTable/state-migration.ts +5 -5
  71. package/src/components/PlDataTable/tableParams.test.ts +71 -0
  72. package/src/components/PlDataTable/tableParams.ts +66 -9
  73. package/src/components/PlDataTable/typesV8.ts +27 -6
  74. package/src/components/PlDatasetSelector/build_dataset_options.test.ts +195 -0
  75. package/src/components/PlDatasetSelector/build_dataset_options.ts +86 -63
  76. package/src/components/PlDatasetSelector/dataset_selection.ts +14 -4
  77. package/src/components/PlDatasetSelector/enrichment_discovery.ts +26 -35
  78. package/src/components/PlDatasetSelector/filter_discovery.test.ts +16 -53
  79. package/src/components/PlDatasetSelector/filter_discovery.ts +10 -20
@@ -11,7 +11,9 @@ import {
11
11
  collectSearchColumns,
12
12
  createSearchFilter,
13
13
  createSheetFilters,
14
+ isSheetFixedToOneValue,
14
15
  joinFilters,
16
+ joinSorting,
15
17
  readPlDataTableUserParams,
16
18
  type PlDataTableFilterNode,
17
19
  type PlDataTableSearchCandidate,
@@ -125,6 +127,62 @@ describe("createSheetFilters", () => {
125
127
  { type: "equal", column: { type: "axis", id: roundAxis }, x: 3 },
126
128
  ]);
127
129
  });
130
+
131
+ test("selects the values picked on a sheet showing several", () => {
132
+ const column: PTableColumnId = { type: "axis", id: getAxisId(axis("sample")) };
133
+ expect(createSheetFilters([{ axisId: column.id, values: ["s1", "s2", 3] }])).toEqual([
134
+ {
135
+ type: "or",
136
+ filters: [
137
+ { type: "inSet", column, value: ["s1", "s2"] },
138
+ { type: "equal", column, x: 3 },
139
+ ],
140
+ },
141
+ ]);
142
+ expect(createSheetFilters([{ axisId: column.id, values: ["s1"] }])).toEqual([
143
+ { type: "inSet", column, value: ["s1"] },
144
+ ]);
145
+ });
146
+
147
+ test("filters nothing for a sheet taking all values or with none picked", () => {
148
+ const column: PTableColumnId = { type: "axis", id: getAxisId(axis("sample")) };
149
+ expect(createSheetFilters([{ axisId: column.id, allValues: true }])).toEqual([]);
150
+ expect(createSheetFilters([{ axisId: column.id, values: [] }])).toEqual([]);
151
+ });
152
+ });
153
+
154
+ describe("isSheetFixedToOneValue", () => {
155
+ const axisId = getAxisId(axis("sample"));
156
+
157
+ test("holds for a single value, also picked on a sheet showing several", () => {
158
+ expect(isSheetFixedToOneValue({ axisId, value: "s1" })).toBe(true);
159
+ expect(isSheetFixedToOneValue({ axisId, values: ["s1"] })).toBe(true);
160
+ });
161
+
162
+ test("does not hold for several values or all of them", () => {
163
+ expect(isSheetFixedToOneValue({ axisId, values: ["s1", "s2"] })).toBe(false);
164
+ expect(isSheetFixedToOneValue({ axisId, allValues: true })).toBe(false);
165
+ });
166
+ });
167
+
168
+ describe("joinSorting", () => {
169
+ const sample: PTableColumnId = { type: "axis", id: getAxisId(axis("sample")) };
170
+ const count: PTableColumnId = { type: "column", id: col("count") };
171
+
172
+ test("puts the fixed sorting first and drops the rest's entries on its columns", () => {
173
+ expect(
174
+ joinSorting(
175
+ [{ column: sample, ascending: true, naAndAbsentAreLeastValues: true }],
176
+ [
177
+ { column: count, ascending: false, naAndAbsentAreLeastValues: false },
178
+ { column: sample, ascending: false, naAndAbsentAreLeastValues: false },
179
+ ],
180
+ ),
181
+ ).toEqual([
182
+ { column: sample, ascending: true, naAndAbsentAreLeastValues: true },
183
+ { column: count, ascending: false, naAndAbsentAreLeastValues: false },
184
+ ]);
185
+ });
128
186
  });
129
187
 
130
188
  describe("collectSearchColumns", () => {
@@ -162,6 +220,19 @@ describe("collectSearchColumns", () => {
162
220
  expect(found).not.toContainEqual({ type: "axis", id: getAxisId(clone) });
163
221
  });
164
222
 
223
+ test("skips the label column of an axis a sheet fixes to one value", () => {
224
+ const found = collectSearchColumns({
225
+ columns,
226
+ isAxisHidden: () => false,
227
+ sheetAxes: [getAxisId(clone)],
228
+ hiddenColIds: null,
229
+ }).map((c) => c.id);
230
+ expect(found).toEqual([
231
+ { type: "axis", id: getAxisId(sample) },
232
+ { type: "column", id: col("count") },
233
+ ]);
234
+ });
235
+
165
236
  test("follows the user's hidden list instead of the optional defaults", () => {
166
237
  const found = collectSearchColumns({
167
238
  columns,
@@ -94,27 +94,56 @@ export function readPlDataTableUserParams(
94
94
  };
95
95
  }
96
96
 
97
- /** One filter per sheet, selecting the value picked on it. */
97
+ /**
98
+ * One filter per sheet, selecting the values picked on it. A sheet that takes
99
+ * all its values filters nothing, and so does one with no value picked: that
100
+ * choice is unfinished, and the UI never saves it but falls back to the
101
+ * sheet's default.
102
+ */
98
103
  export function createSheetFilters(sheetsState: PlDataTableSheetState[]): PlDataTableFilterNode[] {
99
- return sheetsState.map((sheet) => {
104
+ return sheetsState.flatMap((sheet): PlDataTableFilterNode[] => {
100
105
  const column: PTableColumnId = { type: "axis", id: sheet.axisId };
101
- return typeof sheet.value === "number"
102
- ? { type: "equal", column, x: sheet.value }
103
- : { type: "patternEquals", column, value: sheet.value };
106
+ if ("value" in sheet) return [singleValueFilter(column, sheet.value)];
107
+ if ("allValues" in sheet) return [];
108
+ return multiValueFilter(column, sheet.values);
104
109
  });
105
110
  }
106
111
 
112
+ /**
113
+ * Whether the sheet shows a single value. Every row then holds the same value
114
+ * of the sheet's axis, so the axis is neither shown nor searched.
115
+ */
116
+ export function isSheetFixedToOneValue(sheet: PlDataTableSheetState): boolean {
117
+ return "value" in sheet || ("values" in sheet && sheet.values.length === 1);
118
+ }
119
+
120
+ /**
121
+ * The sorting the table is built with: the block's fixed sorting first, then the
122
+ * rest, which is the user's sorting or the block's default while the user has
123
+ * not sorted. Entries of the rest on a column the fixed sorting already orders
124
+ * by are left out.
125
+ */
126
+ export function joinSorting(fixed: PTableSorting[], rest: PTableSorting[]): PTableSorting[] {
127
+ const fixedColumns = new Set(fixed.map((s) => canonicalizeJson<PTableColumnId>(s.column)));
128
+ return [
129
+ ...fixed,
130
+ ...rest.filter((s) => !fixedColumns.has(canonicalizeJson<PTableColumnId>(s.column))),
131
+ ];
132
+ }
133
+
107
134
  /**
108
135
  * The columns the fast search looks into: the visible ones, and the visible axes
109
136
  * except those a sheet fixes to a single value, where every row would match, and
110
137
  * those the table shows through their label column, whose raw values are never
111
- * on screen. Hidden are columns the block's rules hide, linker columns, and the
112
- * columns in the user's hidden list, or, while the user has not touched it, the
113
- * optional ones.
138
+ * on screen. The label column of an axis a sheet fixes to a single value is not
139
+ * shown either, for the same reason as the axis. Hidden are columns the block's
140
+ * rules hide, linker columns, and the columns in the user's hidden list, or,
141
+ * while the user has not touched it, the optional ones.
114
142
  */
115
143
  export function collectSearchColumns(params: {
116
144
  columns: PlDataTableSearchCandidate[];
117
145
  isAxisHidden: (axis: AxisSpec) => boolean;
146
+ /** Axes a sheet fixes to one value */
118
147
  sheetAxes: AxisId[];
119
148
  hiddenColIds: null | PTableColumnId[];
120
149
  }): PlDataTableSearchColumn[] {
@@ -131,7 +160,15 @@ export function collectSearchColumns(params: {
131
160
  !labeledAxes.some((labeled) => matchAxisId(labeled, id))
132
161
  );
133
162
  });
134
- const shownColumns = columns.filter((c) => c.visibility !== "hidden" && !isLinkerColumn(c.spec));
163
+ const shownColumns = columns.filter(
164
+ (c) =>
165
+ c.visibility !== "hidden" &&
166
+ !isLinkerColumn(c.spec) &&
167
+ !(
168
+ isLabelColumn(c.spec) &&
169
+ c.spec.axesSpec.some((axis) => sheetAxisKeys.has(axisKey(getAxisId(axis))))
170
+ ),
171
+ );
135
172
 
136
173
  const hidden = new Set(
137
174
  (
@@ -194,6 +231,26 @@ export type PlDataTableFilterNode = FilterSpecNode<PlDataTableFilterSpecLeaf>;
194
231
 
195
232
  // Internals
196
233
 
234
+ function singleValueFilter(column: PTableColumnId, value: string | number): PlDataTableFilterNode {
235
+ return typeof value === "number"
236
+ ? { type: "equal", column, x: value }
237
+ : { type: "patternEquals", column, value };
238
+ }
239
+
240
+ function multiValueFilter(
241
+ column: PTableColumnId,
242
+ values: (string | number)[],
243
+ ): PlDataTableFilterNode[] {
244
+ const texts = values.filter((v): v is string => typeof v === "string");
245
+ const parts: PlDataTableFilterNode[] = [
246
+ ...(texts.length > 0 ? [{ type: "inSet" as const, column, value: texts }] : []),
247
+ ...values.flatMap((v): PlDataTableFilterNode[] =>
248
+ typeof v === "number" ? [{ type: "equal", column, x: v }] : [],
249
+ ),
250
+ ];
251
+ return parts.length > 1 ? [{ type: "or", filters: parts }] : parts;
252
+ }
253
+
197
254
  function findEntry(
198
255
  state: PlDataTableStateV2Normalized,
199
256
  sourceId: null | string,
@@ -47,14 +47,35 @@ export type PlDataTableSheet = {
47
47
  options: ListOptionBase<string | number>[];
48
48
  /** default (selected) value */
49
49
  defaultValue?: string | number;
50
+ /** Whether the user picks one value of the axis at a time, or any number of them */
51
+ selection: "single" | "multiple";
50
52
  };
51
53
 
52
- export type PlDataTableSheetState = {
53
- /** id of the axis */
54
- axisId: AxisId;
55
- /** selected value */
56
- value: string | number;
57
- };
54
+ /**
55
+ * The values picked on a sheet. A sheet with `selection: "single"` stores one
56
+ * `value`; a sheet with `selection: "multiple"` stores the picked `values`, or
57
+ * `allValues` to take every value the sheet offers, including ones that appear
58
+ * later.
59
+ */
60
+ export type PlDataTableSheetState =
61
+ | {
62
+ /** id of the axis */
63
+ axisId: AxisId;
64
+ /** selected value */
65
+ value: string | number;
66
+ }
67
+ | {
68
+ /** id of the axis */
69
+ axisId: AxisId;
70
+ /** selected values */
71
+ values: (string | number)[];
72
+ }
73
+ | {
74
+ /** id of the axis */
75
+ axisId: AxisId;
76
+ /** every value of the sheet, including ones that appear later */
77
+ allValues: true;
78
+ };
58
79
 
59
80
  /** Tree-based filter state compatible with PlAdvancedFilter's RootFilter */
60
81
  export type PlDataTableFilterMeta = {
@@ -0,0 +1,195 @@
1
+ import {
2
+ Annotation,
3
+ createColumnDiscoveredId,
4
+ createGlobalPObjectId,
5
+ createPlRef,
6
+ isPColumnSpec,
7
+ } from "@milaboratories/pl-model-common";
8
+ import type { AxisSpec, PColumnSpec, PObjectSpec } from "@milaboratories/pl-model-common";
9
+ import { afterEach, beforeEach, describe, expect, test } from "vitest";
10
+ import { BlockRenderCtx } from "../../render";
11
+ import { buildDatasetOptions } from "./build_dataset_options";
12
+ import {
13
+ createTestCollectionDriver,
14
+ type TestCollectionDriverHandle,
15
+ } from "../../columns/__test_helpers__/collection_driver";
16
+
17
+ let driverHandle: TestCollectionDriverHandle;
18
+
19
+ beforeEach(() => {
20
+ driverHandle = createTestCollectionDriver();
21
+ driverHandle.installAmbientCtx();
22
+ });
23
+
24
+ afterEach(async () => {
25
+ driverHandle.uninstallAmbientCtx();
26
+ await driverHandle.dispose();
27
+ });
28
+
29
+ const sampleAxis: AxisSpec = { name: "sample", type: "String" };
30
+ const geneAxis: AxisSpec = { name: "gene", type: "String" };
31
+ const pathwayAxis: AxisSpec = { name: "pathway", type: "String" };
32
+
33
+ function spec(
34
+ name: string,
35
+ axesSpec: AxisSpec[],
36
+ annotations: Record<string, string> = {},
37
+ ): PColumnSpec {
38
+ return { kind: "PColumn", name, valueType: "Int", axesSpec, annotations };
39
+ }
40
+
41
+ /** Registers the columns as outputs of the block, named as their specs are. */
42
+ function registerOutputs(blockId: string, specs: PColumnSpec[]): void {
43
+ driverHandle.register(
44
+ specs.map((s) => ({ id: createGlobalPObjectId(blockId, s.name), spec: s })),
45
+ );
46
+ }
47
+
48
+ const isDataset = (s: PObjectSpec): boolean => s.name === "abundance";
49
+
50
+ const abundance = spec("abundance", [sampleAxis, geneAxis], { [Annotation.Label]: "Abundance" });
51
+ const other = spec("other", [sampleAxis, geneAxis], { [Annotation.Label]: "Other" });
52
+ const subset = spec("subset", [geneAxis], { [Annotation.IsSubset]: "true" });
53
+ const sampleSubset = spec("sampleSubset", [sampleAxis], { [Annotation.IsSubset]: "true" });
54
+ const foreignSubset = spec("foreignSubset", [pathwayAxis], { [Annotation.IsSubset]: "true" });
55
+
56
+ describe("buildDatasetOptions", () => {
57
+ test("offers the result pool columns the predicate accepts", () => {
58
+ registerOutputs("b1", [abundance, other]);
59
+
60
+ const options = buildDatasetOptions(new BlockRenderCtx(), { primary: isDataset });
61
+
62
+ expect(options).toEqual([
63
+ { primary: { ref: createPlRef("b1", "abundance", true), label: "Abundance" } },
64
+ ]);
65
+ });
66
+
67
+ test("offers every result pool column without a primary option", () => {
68
+ registerOutputs("b1", [abundance, other]);
69
+
70
+ const options = buildDatasetOptions(new BlockRenderCtx()) ?? [];
71
+
72
+ expect(options.map((o) => o.primary.label).sort()).toEqual(["Abundance", "Other"]);
73
+ });
74
+
75
+ test("a selector offers the same datasets as the predicate that says the same", () => {
76
+ registerOutputs("b1", [abundance, other, subset]);
77
+ const ctx = new BlockRenderCtx();
78
+
79
+ const bySelector = buildDatasetOptions(ctx, {
80
+ primary: { name: [{ type: "exact", value: "abundance" }] },
81
+ });
82
+ const byAxis = buildDatasetOptions(ctx, {
83
+ primary: { axes: [{ name: [{ type: "exact", value: "sample" }] }] },
84
+ filter: () => false,
85
+ });
86
+
87
+ expect(bySelector).toEqual(buildDatasetOptions(ctx, { primary: isDataset }));
88
+ expect(byAxis).toEqual(
89
+ buildDatasetOptions(ctx, {
90
+ primary: (s) => isPColumnSpec(s) && s.axesSpec.some((a) => a.name === "sample"),
91
+ filter: () => false,
92
+ }),
93
+ );
94
+ expect(byAxis).toHaveLength(2);
95
+ });
96
+
97
+ test("returns no options when nothing qualifies", () => {
98
+ registerOutputs("b1", [other]);
99
+
100
+ expect(buildDatasetOptions(new BlockRenderCtx(), { primary: isDataset })).toEqual([]);
101
+ });
102
+
103
+ test("leaves requireEnrichments off the dataset refs when asked", () => {
104
+ registerOutputs("b1", [abundance]);
105
+
106
+ const options = buildDatasetOptions(new BlockRenderCtx(), {
107
+ primary: isDataset,
108
+ requireEnrichments: false,
109
+ });
110
+
111
+ expect(options?.map((o) => o.primary.ref)).toEqual([createPlRef("b1", "abundance")]);
112
+ });
113
+
114
+ test("gives same-named datasets of different blocks distinct labels", () => {
115
+ const traced = (label: string): PColumnSpec => ({
116
+ ...abundance,
117
+ domain: { "pl7.app/blockId": label },
118
+ annotations: {
119
+ ...abundance.annotations,
120
+ [Annotation.Trace]: JSON.stringify([{ type: "import", label, importance: 10 }]),
121
+ },
122
+ });
123
+ registerOutputs("b1", [traced("First")]);
124
+ registerOutputs("b2", [traced("Second")]);
125
+
126
+ const options = buildDatasetOptions(new BlockRenderCtx(), { primary: isDataset }) ?? [];
127
+
128
+ expect(options.map((o) => o.primary.ref)).toEqual([
129
+ createPlRef("b1", "abundance", true),
130
+ createPlRef("b2", "abundance", true),
131
+ ]);
132
+ expect(new Set(options.map((o) => o.primary.label)).size).toBe(2);
133
+ });
134
+
135
+ test("attaches the subsets on the dataset's axes as filters", () => {
136
+ registerOutputs("b1", [abundance]);
137
+ registerOutputs("b2", [subset, sampleSubset, foreignSubset]);
138
+
139
+ const options = buildDatasetOptions(new BlockRenderCtx(), { primary: isDataset }) ?? [];
140
+
141
+ expect(options).toHaveLength(1);
142
+ expect(options[0].filters?.map((f) => f.ref)).toEqual(
143
+ expect.arrayContaining([createPlRef("b2", "subset"), createPlRef("b2", "sampleSubset")]),
144
+ );
145
+ expect(options[0].filters).toHaveLength(2);
146
+ });
147
+
148
+ test("the filter option restricts the attached subsets", () => {
149
+ registerOutputs("b1", [abundance]);
150
+ registerOutputs("b2", [subset, sampleSubset]);
151
+ const ctx = new BlockRenderCtx();
152
+
153
+ const byPredicate = buildDatasetOptions(ctx, {
154
+ primary: isDataset,
155
+ filter: (s) => s.name === "subset",
156
+ });
157
+ const bySelector = buildDatasetOptions(ctx, {
158
+ primary: isDataset,
159
+ filter: { name: [{ type: "exact", value: "subset" }] },
160
+ });
161
+ const none = buildDatasetOptions(ctx, { primary: isDataset, filter: () => false });
162
+
163
+ expect(byPredicate?.[0].filters?.map((f) => f.ref)).toEqual([createPlRef("b2", "subset")]);
164
+ expect(bySelector).toEqual(byPredicate);
165
+ expect(none).toEqual([
166
+ { primary: { ref: createPlRef("b1", "abundance", true), label: "Abundance" } },
167
+ ]);
168
+ });
169
+
170
+ test("attaches the columns reached through a linker as enrichments", () => {
171
+ const linker = spec("pathwayToGene", [pathwayAxis, geneAxis], {
172
+ [Annotation.IsLinkerColumn]: "true",
173
+ });
174
+ const pathwayScore = spec("pathwayScore", [pathwayAxis], { [Annotation.Label]: "Score" });
175
+ registerOutputs("b1", [abundance]);
176
+ registerOutputs("b2", [linker, pathwayScore]);
177
+ const ctx = new BlockRenderCtx();
178
+
179
+ const without = buildDatasetOptions(ctx, { primary: isDataset }) ?? [];
180
+ const withAll = buildDatasetOptions(ctx, { primary: isDataset, withEnrichments: () => true });
181
+ const withNone = buildDatasetOptions(ctx, { primary: isDataset, withEnrichments: () => false });
182
+
183
+ expect(without[0].enrichments).toBeUndefined();
184
+ expect(withNone).toEqual(without);
185
+ expect(withAll?.[0].enrichments).toEqual([
186
+ {
187
+ id: createColumnDiscoveredId({
188
+ column: createGlobalPObjectId("b2", "pathwayScore"),
189
+ path: [{ type: "linker", column: createGlobalPObjectId("b2", "pathwayToGene") }],
190
+ }),
191
+ label: expect.any(String),
192
+ },
193
+ ]);
194
+ });
195
+ });
@@ -1,24 +1,26 @@
1
1
  import type { MultiColumnSelector, Option, PObjectSpec } from "@milaboratories/pl-model-common";
2
- import { isPColumnSpec, multiColumnSelectorsToPredicate } from "@milaboratories/pl-model-common";
3
- import type { DeriveLabelsOptions } from "../../labels/derive_distinct_labels";
2
+ import {
3
+ columnProducer,
4
+ createPlRef,
5
+ multiColumnSelectorsToPredicate,
6
+ } from "@milaboratories/pl-model-common";
7
+ import type { ColumnRecipe } from "../../columns";
8
+ import { ColumnsCollection } from "../../columns";
9
+ import {
10
+ deriveDistinctLabels,
11
+ type DeriveLabelsOptions,
12
+ } from "../../labels/derive_distinct_labels";
4
13
  import type { RenderCtxBase } from "../../render";
5
14
  import type { DatasetOption } from "./dataset_selection";
6
- import { buildRefMap, filterMatchesToOptions, findFilterColumns } from "./filter_discovery";
7
- import { enrichmentVariantsToRefs, findEnrichmentColumns } from "./enrichment_discovery";
8
- import { ColumnsProvider, getCtxProviders } from "../../columns";
9
-
10
- type SpecPredicateOption =
11
- | MultiColumnSelector
12
- | MultiColumnSelector[]
13
- | ((spec: PObjectSpec) => boolean);
14
-
15
- function toPredicate(opt: SpecPredicateOption | undefined): (spec: PObjectSpec) => boolean {
16
- if (opt === undefined) return () => true;
17
- return typeof opt === "function" ? opt : multiColumnSelectorsToPredicate(opt);
18
- }
15
+ import { filterMatchesToOptions, findFilterColumns } from "./filter_discovery";
16
+ import { findEnrichmentColumns, labelEnrichments } from "./enrichment_discovery";
19
17
 
20
18
  export type BuildDatasetOptions = {
21
- /** Which result pool columns qualify as datasets. Defaults to all. */
19
+ /**
20
+ * Which result pool columns qualify as datasets. Defaults to all. A selector
21
+ * is matched by the host; a predicate reads the spec of every result pool
22
+ * column, so prefer a selector when it can say the same.
23
+ */
22
24
  primary?: SpecPredicateOption;
23
25
  /**
24
26
  * Restricts which result pool columns are considered as filters. Intersected
@@ -57,62 +59,83 @@ export function buildDatasetOptions(
57
59
  ctx: RenderCtxBase,
58
60
  opts?: BuildDatasetOptions,
59
61
  ): DatasetOption[] | undefined {
60
- const primaryPredicate = toPredicate(opts?.primary);
61
- const filterPredicate = toPredicate(opts?.filter);
62
-
63
- const options = ctx.resultPool.getOptions(
64
- (spec) => isPColumnSpec(spec) && primaryPredicate(spec),
65
- {
66
- refsWithEnrichments: opts?.requireEnrichments ?? true,
67
- },
68
- );
69
- if (options.length === 0) return [];
62
+ const pool = ColumnsCollection(["result_pool"], { ctx: ctx.ctx });
70
63
 
71
- const refMap = buildRefMap(ctx.resultPool.getSpecs().entries);
64
+ // A dataset is stored as a reference to a block output, so only a column
65
+ // that is one can be offered.
66
+ const datasets = findDatasets(pool, opts?.primary).flatMap((column) => {
67
+ const producer = columnProducer(column.id);
68
+ return producer === undefined ? [] : [{ column, producer }];
69
+ });
70
+ if (datasets.length === 0) return [];
72
71
 
73
- const withEnrichments = opts?.withEnrichments ?? false;
74
- const filterSource = ColumnsProvider(ctx.ctx.getUpstreamBlockCtx());
75
- // Hoist providers once: getCtxProviders walks the full output tree, so
76
- // calling it per dataset option would be O(N × tree).
77
- const enrichmentSources = withEnrichments ? getCtxProviders(ctx) : undefined;
72
+ const labels = deriveDistinctLabels(datasets.map(({ column }) => column.getSpec()));
73
+ const requireEnrichments = opts?.requireEnrichments ?? true;
74
+ const filterPredicate = toPredicate(opts?.filter);
78
75
 
79
- return options.map((primary: Option): DatasetOption => {
80
- const datasetSpec = ctx.resultPool.getPColumnSpecByRef(primary.ref);
81
- if (!datasetSpec) return { primary };
76
+ const withEnrichments = opts?.withEnrichments;
77
+ const enrichmentSearch =
78
+ withEnrichments === undefined
79
+ ? undefined
80
+ : {
81
+ // Enrichments are also looked for among this block's own columns.
82
+ // Built once: every dataset searches the same set.
83
+ sources: [ColumnsCollection(["current_block", pool], { ctx: ctx.ctx })],
84
+ maxHops: opts?.enrichmentMaxHops,
85
+ ...(typeof withEnrichments === "function"
86
+ ? { predicate: withEnrichments }
87
+ : { include: withEnrichments }),
88
+ };
82
89
 
83
- const filterMatches = findFilterColumns({
84
- sources: [filterSource],
85
- anchorSpec: datasetSpec,
86
- }).filter((m) => filterPredicate(m.getSpec()!));
90
+ return datasets.map(({ column, producer }, i): DatasetOption => {
91
+ const datasetSpec = column.getSpec();
92
+ const primary: Option = {
93
+ ref: createPlRef(producer.blockId, producer.name, requireEnrichments),
94
+ label: labels[i],
95
+ };
87
96
 
88
- const filters =
89
- filterMatches.length === 0
90
- ? undefined
91
- : filterMatchesToOptions(filterMatches, {
92
- refsByObjectId: refMap,
93
- datasetSpec,
94
- labelOptions: opts?.labelOptions,
95
- });
97
+ const filters = filterMatchesToOptions(
98
+ findFilterColumns({ sources: [pool], anchorSpec: datasetSpec }).filter((match) =>
99
+ filterPredicate(match.getSpec()),
100
+ ),
101
+ { datasetSpec, labelOptions: opts?.labelOptions },
102
+ );
96
103
 
97
- let enrichments;
98
- if (enrichmentSources && withEnrichments) {
99
- const enrichmentVariants = findEnrichmentColumns({
100
- sources: enrichmentSources,
101
- anchorSpec: datasetSpec,
102
- maxHops: opts?.enrichmentMaxHops,
103
- ...(typeof withEnrichments === "function"
104
- ? { predicate: withEnrichments }
105
- : { include: withEnrichments }),
106
- });
107
- if (enrichmentVariants.length > 0) {
108
- enrichments = enrichmentVariantsToRefs(enrichmentVariants, opts?.labelOptions);
109
- }
110
- }
104
+ const enrichments =
105
+ enrichmentSearch === undefined
106
+ ? []
107
+ : labelEnrichments(
108
+ findEnrichmentColumns({ ...enrichmentSearch, anchorSpec: datasetSpec }),
109
+ opts?.labelOptions,
110
+ );
111
111
 
112
112
  return {
113
113
  primary,
114
- ...(filters !== undefined && filters.length > 0 ? { filters } : {}),
115
- ...(enrichments !== undefined && enrichments.length > 0 ? { enrichments } : {}),
114
+ ...(filters.length > 0 ? { filters } : {}),
115
+ ...(enrichments.length > 0 ? { enrichments } : {}),
116
116
  };
117
117
  });
118
118
  }
119
+
120
+ // Internals
121
+
122
+ type SpecPredicateOption =
123
+ | MultiColumnSelector
124
+ | MultiColumnSelector[]
125
+ | ((spec: PObjectSpec) => boolean);
126
+
127
+ function toPredicate(opt: SpecPredicateOption | undefined): (spec: PObjectSpec) => boolean {
128
+ if (opt === undefined) return () => true;
129
+ return typeof opt === "function" ? opt : multiColumnSelectorsToPredicate(opt);
130
+ }
131
+
132
+ function findDatasets(
133
+ pool: ColumnsCollection,
134
+ primary: SpecPredicateOption | undefined,
135
+ ): ColumnRecipe[] {
136
+ if (primary === undefined) return pool.getColumns();
137
+ if (typeof primary === "function") {
138
+ return pool.getColumns().filter((column) => primary(column.getSpec()));
139
+ }
140
+ return pool.filter({ include: primary }).getColumns();
141
+ }
@@ -1,10 +1,20 @@
1
- import type { LabeledEnrichmentRefs, Option, PrimaryRef } from "@milaboratories/pl-model-common";
1
+ import type { ColumnUniversalId, Option, PrimaryRef } from "@milaboratories/pl-model-common";
2
+
3
+ /**
4
+ * A column joined to a dataset without being picked by the user, with the header
5
+ * it is exported under. The id names a block output reached from the dataset
6
+ * through linker columns, so a workflow can find the column by the id alone.
7
+ */
8
+ export type DatasetEnrichment = {
9
+ readonly id: ColumnUniversalId;
10
+ readonly label: string;
11
+ };
2
12
 
3
13
  /** Dataset picker entry: user picks {@link primary}, gets {@link enrichments} attached. */
4
14
  export type DatasetOption = {
5
15
  readonly primary: Option;
6
16
  readonly filters?: readonly Option[];
7
- readonly enrichments?: LabeledEnrichmentRefs;
17
+ readonly enrichments?: readonly DatasetEnrichment[];
8
18
  };
9
19
 
10
20
  /**
@@ -14,7 +24,7 @@ export type DatasetOption = {
14
24
  export type DatasetSelection = {
15
25
  readonly __isDatasetSelection: "v1";
16
26
  readonly primary: PrimaryRef;
17
- readonly enrichments?: LabeledEnrichmentRefs;
27
+ readonly enrichments?: readonly DatasetEnrichment[];
18
28
  };
19
29
 
20
30
  export function isDatasetSelection(value: unknown): value is DatasetSelection {
@@ -28,7 +38,7 @@ export function isDatasetSelection(value: unknown): value is DatasetSelection {
28
38
 
29
39
  export function createDatasetSelection(
30
40
  primary: PrimaryRef,
31
- enrichments?: LabeledEnrichmentRefs,
41
+ enrichments?: readonly DatasetEnrichment[],
32
42
  ): DatasetSelection {
33
43
  if (enrichments !== undefined && enrichments.length > 0) {
34
44
  return { __isDatasetSelection: "v1", primary, enrichments };