-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathome.ts
332 lines (296 loc) · 10.9 KB
/
ome.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import type { Readable } from "@zarrita/storage";
import pMap from "p-map";
import * as zarr from "zarrita";
import type { ImageLayerConfig, OnClickData, SourceData } from "./state";
import { ZarrPixelSource } from "./ZarrPixelSource";
import * as utils from "./utils";
export async function loadWell(
config: ImageLayerConfig,
grp: zarr.Group<Readable>,
wellAttrs: Ome.Well,
): Promise<SourceData> {
// Can filter Well fields by URL query ?acquisition=ID
const acquisitionId: number | undefined = config.acquisition ? Number.parseInt(config.acquisition) : undefined;
let acquisitions: Ome.Acquisition[] = [];
utils.assert(wellAttrs?.images, "Well .zattrs missing images");
utils.assert(grp.path, "Cannot inspect zarr path to open well.");
const [row, col] = grp.path.split("/").filter(Boolean).slice(-2);
let { images } = wellAttrs;
// Do we have more than 1 Acquisition?
const acqIds = images.flatMap((img) => (img.acquisition ? [img.acquisition] : []));
if (acqIds.length > 1) {
// Need to get acquisitions metadata from parent Plate
const platePath = grp.path.replace(`${row}/${col}`, "");
const plate = await zarr.open(grp.resolve(platePath));
const plateAttrs = utils.resolveAttrs(plate.attrs) as { plate: Ome.Plate };
acquisitions = plateAttrs.plate.acquisitions ?? [];
// filter imagePaths by acquisition
if (acquisitionId && acqIds.includes(acquisitionId)) {
images = images.filter((img) => img.acquisition === acquisitionId);
}
}
const imgPaths = images.map((img) => img.path);
const cols = Math.ceil(Math.sqrt(imgPaths.length));
const rows = Math.ceil(imgPaths.length / cols);
// Use first image for rendering settings, resolutions etc.
const first = await zarr.open(grp.resolve(imgPaths[0]), { kind: "group" });
const imgAttrs = utils.resolveAttrs(first.attrs);
utils.assert(utils.isMultiscales(imgAttrs), "Path for image is not valid.");
let resolution = imgAttrs.multiscales[0].datasets[0].path;
// Create loader for every Image.
const promises = imgPaths.map((p) => {
const loc = grp.resolve(utils.join(p, resolution));
// @ts-expect-error - ok flag to avoid loading unused attrs
const arr: zarr.Array<zarr.DataType, Readable> = zarr.open(loc, { kind: "array", attrs: false });
return arr;
});
const data = await Promise.all(promises);
const axes = utils.getNgffAxes(imgAttrs.multiscales);
const axis_labels = utils.getNgffAxisLabels(axes);
const tileSize = utils.guessTileSize(data[0]);
const loaders = utils.range(rows).flatMap((row) => {
// filter to remove any empty row/col position
return utils
.range(cols)
.filter((col) => col + row * cols < data.length)
.map((col) => {
const offset = col + row * cols;
return {
name: String(offset),
row,
col,
loader: new ZarrPixelSource(data[offset], { labels: axis_labels, tileSize }),
};
});
});
let meta: Meta;
if (utils.isOmeroMultiscales(imgAttrs)) {
meta = parseOmeroMeta(imgAttrs.omero, axes);
} else {
meta = await defaultMeta(loaders[0].loader, axis_labels);
}
const sourceData: SourceData = {
loaders,
...meta,
axis_labels,
loader: [loaders[0].loader],
model_matrix: utils.parseMatrix(config.model_matrix),
defaults: {
selection: meta.defaultSelection,
colormap: config.colormap ?? "",
opacity: config.opacity ?? 1,
},
name: `Well ${row}${col}`,
};
if (acquisitions.length > 0) {
// To show acquisition chooser in UI
sourceData.acquisitions = acquisitions;
sourceData.acquisitionId = acquisitionId || -1;
}
sourceData.rows = rows;
sourceData.columns = cols;
sourceData.onClick = (info: OnClickData) => {
let gridCoord = info.gridCoord;
if (!gridCoord) {
return;
}
const { row, column } = gridCoord;
let imgSource = undefined;
if (typeof config.source === "string" && grp.path && !Number.isNaN(row) && !Number.isNaN(column)) {
const field = row * cols + column;
imgSource = utils.join(config.source, imgPaths[field]);
}
if (config.onClick) {
info.layer = undefined;
info.imageSource = imgSource;
config.onClick(info);
} else if (imgSource) {
window.open(`${window.location.origin + window.location.pathname}?source=${imgSource}`);
}
};
return sourceData;
}
export async function loadPlate(
config: ImageLayerConfig,
grp: zarr.Group<Readable>,
plateAttrs: Ome.Plate,
): Promise<SourceData> {
utils.assert(plateAttrs?.rows || plateAttrs?.columns, "Plate .zattrs missing rows, columns or wells");
const rows = plateAttrs.rows.map((row) => row.name);
const columns = plateAttrs.columns.map((row) => row.name);
// Fields are by index and we assume at least 1 per Well
const wellPaths = plateAttrs.wells.map((well) => well.path);
const zarrVersion = await utils.guessZarrVersion(grp);
// Use first image as proxy for others.
const wellAttrs = await utils.getAttrsOnly<{ well: Ome.Well }>(grp, {
path: wellPaths[0],
zarrVersion,
});
utils.assert("well" in wellAttrs, "Path for image is not valid, not a well.");
const imgPath = wellAttrs.well.images[0].path;
const imgAttrs = await utils.getAttrsOnly<Ome.Attrs>(grp, {
path: utils.join(wellPaths[0], imgPath),
zarrVersion,
});
utils.assert("multiscales" in imgAttrs, "Path for image is not valid.");
// Lowest resolution is the 'path' of the last 'dataset' from the first multiscales
const { datasets } = imgAttrs.multiscales[0];
const resolution = datasets[datasets.length - 1].path;
async function getImgPath(wellPath: string) {
const wellAttrs = await utils.getAttrsOnly<{ well: Ome.Well }>(grp, {
path: wellPath,
zarrVersion,
});
utils.assert("well" in wellAttrs, "Path for image is not valid, not a well.");
return utils.join(wellPath, wellAttrs.well.images[0].path);
}
const wellImagePaths = await Promise.all(wellPaths.map(getImgPath));
// Create loader for every Well. Some loaders may be undefined if Wells are missing.
const mapper = async ([key, path]: string[]) => {
// @ts-expect-error - we don't need the meta for these arrays
let arr: zarr.Array<zarr.DataType, Readable> = await zarr.open(grp.resolve(path), {
kind: "array",
attrs: false,
});
return [key, arr] as const;
};
const promises = await pMap(
wellImagePaths.map((p) => [p, utils.join(p, resolution)]),
mapper,
{ concurrency: 10 },
);
const data = await Promise.all(promises);
const axes = utils.getNgffAxes(imgAttrs.multiscales);
const axis_labels = utils.getNgffAxisLabels(axes);
const tileSize = utils.guessTileSize(data[0][1]);
const loaders = data.map((d) => {
const [row, col] = d[0].split("/");
return {
name: `${row}${col}`,
row: rows.indexOf(row),
col: columns.indexOf(col),
loader: new ZarrPixelSource(d[1], { labels: axis_labels, tileSize }),
};
});
let meta: Meta;
if ("omero" in imgAttrs) {
meta = parseOmeroMeta(imgAttrs.omero, axes);
} else {
meta = await defaultMeta(loaders[0].loader, axis_labels);
}
// Load Image to use for channel names, rendering settings, sizeZ, sizeT etc.
const sourceData: SourceData = {
loaders,
...meta,
axis_labels,
loader: [loaders[0].loader],
model_matrix: utils.parseMatrix(config.model_matrix),
defaults: {
selection: meta.defaultSelection,
colormap: config.colormap ?? "",
opacity: config.opacity ?? 1,
},
name: plateAttrs.name || "Plate",
rows: rows.length,
columns: columns.length,
};
// Us onClick from image config or Open Well in new window
sourceData.onClick = (info: OnClickData) => {
let gridCoord = info.gridCoord;
if (!gridCoord) {
return;
}
const { row, column } = gridCoord;
let imgSource = undefined;
if (typeof config.source === "string" && grp.path && !Number.isNaN(row) && !Number.isNaN(column)) {
imgSource = utils.join(config.source, rows[row], columns[column]);
}
if (config.onClick) {
info.layer = undefined;
info.imageSource = imgSource;
config.onClick(info);
} else if (imgSource) {
window.open(`${window.location.origin + window.location.pathname}?source=${imgSource}`);
}
};
return sourceData;
}
export async function loadOmeroMultiscales(
config: ImageLayerConfig,
grp: zarr.Group<Readable>,
attrs: { multiscales: Ome.Multiscale[]; omero: Ome.Omero },
): Promise<SourceData> {
const { name, opacity = 1, colormap = "" } = config;
const data = await utils.loadMultiscales(grp, attrs.multiscales);
const axes = utils.getNgffAxes(attrs.multiscales);
const axis_labels = utils.getNgffAxisLabels(axes);
const meta = parseOmeroMeta(attrs.omero, axes);
const tileSize = utils.guessTileSize(data[0]);
const loader = data.map((arr) => new ZarrPixelSource(arr, { labels: axis_labels, tileSize }));
return {
loader: loader,
axis_labels,
model_matrix: utils.parseMatrix(config.model_matrix),
defaults: {
selection: meta.defaultSelection,
colormap,
opacity,
},
...meta,
name: meta.name ?? name,
};
}
type Meta = {
name: string | undefined;
names: Array<string>;
colors: Array<string>;
contrast_limits: Array<[number, number] | undefined>;
visibilities: Array<boolean>;
channel_axis: number | null;
defaultSelection: Array<number>;
};
async function defaultMeta(loader: ZarrPixelSource<string[]>, axis_labels: string[]): Promise<Meta> {
const channel_axis = axis_labels.indexOf("c");
const channel_count = channel_axis === -1 ? 1 : loader.shape[channel_axis];
const visibilities = utils.getDefaultVisibilities(channel_count);
const contrast_limits = await utils.calcConstrastLimits(loader, channel_axis, visibilities);
const colors = utils.getDefaultColors(channel_count, visibilities);
return {
name: "Image",
names: utils.range(channel_count).map((i) => `channel_${i}`),
colors,
contrast_limits,
visibilities,
channel_axis: axis_labels.includes("c") ? axis_labels.indexOf("c") : null,
defaultSelection: axis_labels.map(() => 0),
};
}
function parseOmeroMeta({ rdefs, channels, name }: Ome.Omero, axes: Ome.Axis[]): Meta {
const t = rdefs?.defaultT ?? 0;
const z = rdefs?.defaultZ ?? 0;
const colors: string[] = [];
const contrast_limits: [min: number, max: number][] = [];
const visibilities: boolean[] = [];
const names: string[] = [];
channels.forEach((c, index) => {
colors.push(c.color);
contrast_limits.push([c.window.start, c.window.end]);
visibilities.push(c.active);
names.push(c.label || `${index}`);
});
const defaultSelection = axes.map((axis) => {
if (axis.type === "time") return t;
if (axis.name === "z") return z;
return 0;
});
const channel_axis = axes.findIndex((axis) => axis.type === "channel");
return {
name,
names,
colors,
contrast_limits,
visibilities,
channel_axis,
defaultSelection,
};
}