UNPKG

molstar

Version:

A comprehensive macromolecular library.

135 lines (134 loc) 6.1 kB
"use strict"; /** * Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal <david.sehnal@gmail.com> * @author Alexander Rose <alexander.rose@weirdbyte.de> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.SegcifFormat = void 0; exports.volumeFromSegmentationData = volumeFromSegmentationData; const volume_1 = require("../../mol-model/volume"); const mol_task_1 = require("../../mol-task"); const geometry_1 = require("../../mol-math/geometry"); const linear_algebra_1 = require("../../mol-math/linear-algebra"); const custom_property_1 = require("../../mol-model/custom-property"); const object_1 = require("../../mol-util/object"); function volumeFromSegmentationData(source, params) { return mol_task_1.Task.create('Create Segmentation Volume', async (ctx) => { var _a; const { volume_data_3d_info: info, segmentation_data_3d: values } = source; const cell = geometry_1.SpacegroupCell.create(info.spacegroup_number.value(0), linear_algebra_1.Vec3.ofArray(info.spacegroup_cell_size.value(0)), linear_algebra_1.Vec3.scale((0, linear_algebra_1.Vec3)(), linear_algebra_1.Vec3.ofArray(info.spacegroup_cell_angles.value(0)), Math.PI / 180)); const axis_order_fast_to_slow = info.axis_order.value(0); const normalizeOrder = linear_algebra_1.Tensor.convertToCanonicalAxisIndicesFastToSlow(axis_order_fast_to_slow); // sample count is in "axis order" and needs to be reordered const sample_count = normalizeOrder(info.sample_count.value(0)); const tensorSpace = linear_algebra_1.Tensor.Space(sample_count, linear_algebra_1.Tensor.invertAxisOrder(axis_order_fast_to_slow), Float32Array); const t = linear_algebra_1.Tensor.create(tensorSpace, linear_algebra_1.Tensor.Data1(values.values.toArray({ array: Float32Array }))); // origin and dimensions are in "axis order" and need to be reordered const origin = linear_algebra_1.Vec3.ofArray(normalizeOrder(info.origin.value(0))); const dimensions = linear_algebra_1.Vec3.ofArray(normalizeOrder(info.dimensions.value(0))); const v = { label: params === null || params === void 0 ? void 0 : params.label, entryId: undefined, grid: { transform: { kind: 'spacegroup', cell, fractionalBox: geometry_1.Box3D.create(origin, linear_algebra_1.Vec3.add((0, linear_algebra_1.Vec3)(), origin, dimensions)) }, cells: t, stats: { min: 0, max: 1, mean: 0, sigma: 1 }, }, sourceData: SegcifFormat.create(source), customProperties: new custom_property_1.CustomProperties(), _propertyData: { ownerId: params === null || params === void 0 ? void 0 : params.ownerId }, }; volume_1.Volume.PickingGranularity.set(v, 'object'); const segments = new Map(); const sets = new Map(); const { segment_id, set_id } = source.segmentation_data_table; for (let i = 0, il = segment_id.rowCount; i < il; ++i) { const segment = segment_id.value(i); const set = set_id.value(i); if (set === 0 || segment === 0) continue; if (!sets.has(set)) sets.set(set, new Set()); sets.get(set).add(segment); } sets.forEach((segs, set) => { segs.forEach(seg => { if (!segments.has(seg)) segments.set(seg, new Set()); segments.get(seg).add(set); }); }); const c = [0, 0, 0]; const getCoords = t.space.getCoords; const d = t.data; const [xn, yn, zn] = v.grid.cells.space.dimensions; const xn1 = xn - 1; const yn1 = yn - 1; const zn1 = zn - 1; const setBounds = {}; sets.forEach((v, k) => { setBounds[k] = [xn1, yn1, zn1, -1, -1, -1]; }); for (let i = 0, il = d.length; i < il; ++i) { const v = d[i]; if (v === 0) continue; getCoords(i, c); const b = setBounds[v]; if (c[0] < b[0]) b[0] = c[0]; if (c[1] < b[1]) b[1] = c[1]; if (c[2] < b[2]) b[2] = c[2]; if (c[0] > b[3]) b[3] = c[0]; if (c[1] > b[4]) b[4] = c[1]; if (c[2] > b[5]) b[5] = c[2]; } const bounds = {}; segments.forEach((v, k) => { bounds[k] = geometry_1.Box3D.create(linear_algebra_1.Vec3.create(xn1, yn1, zn1), linear_algebra_1.Vec3.create(-1, -1, -1)); }); (0, object_1.objectForEach)(setBounds, (b, s) => { sets.get(parseInt(s)).forEach(seg => { const sb = bounds[seg]; if (b[0] < sb.min[0]) sb.min[0] = b[0]; if (b[1] < sb.min[1]) sb.min[1] = b[1]; if (b[2] < sb.min[2]) sb.min[2] = b[2]; if (b[3] > sb.max[0]) sb.max[0] = b[3]; if (b[4] > sb.max[1]) sb.max[1] = b[4]; if (b[5] > sb.max[2]) sb.max[2] = b[5]; }); }); volume_1.Volume.Segmentation.set(v, { segments, sets, bounds, labels: (_a = params === null || params === void 0 ? void 0 : params.segmentLabels) !== null && _a !== void 0 ? _a : {} }); return v; }); } var SegcifFormat; (function (SegcifFormat) { function is(x) { return (x === null || x === void 0 ? void 0 : x.kind) === 'segcif'; } SegcifFormat.is = is; function create(segcif) { return { kind: 'segcif', name: segcif._name, data: segcif }; } SegcifFormat.create = create; })(SegcifFormat || (exports.SegcifFormat = SegcifFormat = {}));