UNPKG

molstar

Version:

A comprehensive macromolecular library.

160 lines (159 loc) 7.11 kB
/** * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal <david.sehnal@gmail.com> */ import { HashSet } from '../../../mol-data/generic'; import { Structure, StructureElement } from '../structure'; import { structureUnion } from './utils/structure-set'; import { OrderedSet, SortedArray } from '../../../mol-data/int'; var StructureSelection; (function (StructureSelection) { function Singletons(source, structure) { return { kind: 'singletons', source: source, structure: structure }; } StructureSelection.Singletons = Singletons; function Sequence(source, structures) { return { kind: 'sequence', source: source, structures: structures }; } StructureSelection.Sequence = Sequence; function Empty(source) { return Singletons(source, Structure.Empty); } StructureSelection.Empty = Empty; ; function isSingleton(s) { return s.kind === 'singletons'; } StructureSelection.isSingleton = isSingleton; function isEmpty(s) { return isSingleton(s) ? s.structure.units.length === 0 : s.structures.length === 0; } StructureSelection.isEmpty = isEmpty; function structureCount(sel) { if (isSingleton(sel)) return sel.structure.elementCount; return sel.structures.length; } StructureSelection.structureCount = structureCount; function unionStructure(sel) { if (isEmpty(sel)) return Structure.Empty; if (isSingleton(sel)) return sel.structure; return structureUnion(sel.source, sel.structures); } StructureSelection.unionStructure = unionStructure; /** Convert selection to loci and use "current structure units" in Loci elements */ function toLociWithCurrentUnits(sel) { var elements = []; var unitMap = sel.source.unitMap; for (var _a = 0, _b = unionStructure(sel).units; _a < _b.length; _a++) { var unit = _b[_a]; if (unit === unitMap.get(unit.id)) { elements[elements.length] = { unit: unit, indices: OrderedSet.ofBounds(0, unit.elements.length) }; } else { elements[elements.length] = { unit: unit, indices: OrderedSet.ofSortedArray(SortedArray.indicesOf(unitMap.get(unit.id).elements, unit.elements)) }; } } return StructureElement.Loci(sel.source, elements); } StructureSelection.toLociWithCurrentUnits = toLociWithCurrentUnits; /** use source unit in loci.elements */ function toLociWithSourceUnits(sel) { var elements = []; var unitMap = sel.source.unitMap; for (var _a = 0, _b = unionStructure(sel).units; _a < _b.length; _a++) { var _unit = _b[_a]; var unit = unitMap.get(_unit.id); if (unit === _unit) { elements[elements.length] = { unit: unit, indices: OrderedSet.ofBounds(0, unit.elements.length) }; } else { elements[elements.length] = { unit: unit, indices: OrderedSet.ofSortedArray(SortedArray.indicesOf(unit.elements, _unit.elements)) }; } } return StructureElement.Loci(sel.source, elements); } StructureSelection.toLociWithSourceUnits = toLociWithSourceUnits; function getSelection(source, structures, allSingletons) { var len = structures.length; if (len === 0) return Empty(source); if (allSingletons) return Singletons(source, structureUnion(source, structures)); return Sequence(source, structures); } var LinearBuilderImpl = /** @class */ (function () { function LinearBuilderImpl(source) { this.source = source; this.structures = []; this.allSingletons = true; } LinearBuilderImpl.prototype.add = function (structure) { var elementCount = structure.elementCount; if (elementCount === 0) return; this.structures[this.structures.length] = structure; if (elementCount !== 1) this.allSingletons = false; }; LinearBuilderImpl.prototype.getSelection = function () { return getSelection(this.source, this.structures, this.allSingletons); }; return LinearBuilderImpl; }()); var HashBuilderImpl = /** @class */ (function () { function HashBuilderImpl(structure) { this.structure = structure; this.structures = []; this.allSingletons = true; this.uniqueSets = HashSet(Structure.hashCode, Structure.areUnitIdsAndIndicesEqual); } HashBuilderImpl.prototype.add = function (structure) { var atomCount = structure.elementCount; if (atomCount === 0 || !this.uniqueSets.add(structure)) return; this.structures[this.structures.length] = structure; if (atomCount !== 1) this.allSingletons = false; }; HashBuilderImpl.prototype.getSelection = function () { return getSelection(this.structure, this.structures, this.allSingletons); }; return HashBuilderImpl; }()); function LinearBuilder(structure) { return new LinearBuilderImpl(structure); } StructureSelection.LinearBuilder = LinearBuilder; function UniqueBuilder(structure) { return new HashBuilderImpl(structure); } StructureSelection.UniqueBuilder = UniqueBuilder; // TODO: build timeout checking into this? function forEach(sel, fn) { var idx = 0; if (StructureSelection.isSingleton(sel)) { for (var _a = 0, _b = sel.structure.units; _a < _b.length; _a++) { var unit = _b[_a]; var elements = unit.elements; for (var i = 0, _i = elements.length; i < _i; i++) { // TODO: optimize this somehow??? var s = Structure.create([unit.getChild(SortedArray.ofSingleton(elements[i]))], { parent: sel.source }); fn(s, idx++); } } } else { for (var _c = 0, _d = sel.structures; _c < _d.length; _c++) { var s = _d[_c]; fn(s, idx++); } } } StructureSelection.forEach = forEach; function withInputStructure(selection, structure) { if (isSingleton(selection)) return Singletons(structure, selection.structure); return Sequence(structure, selection.structures); } StructureSelection.withInputStructure = withInputStructure; // TODO: spatial lookup? })(StructureSelection || (StructureSelection = {})); export { StructureSelection };