UNPKG

molstar

Version:

A comprehensive macromolecular library.

63 lines (62 loc) 3.55 kB
/** * Copyright (c) 2023 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Adam Midlik <midlik@gmail.com> */ import { SortedArray } from '../../../mol-data/int.js'; import { Structure, StructureElement } from '../../../mol-model/structure.js'; import { StaticStructureComponentTypes, createStructureComponent } from '../../../mol-plugin-state/helpers/structure-component.js'; import { PluginStateObject } from '../../../mol-plugin-state/objects.js'; import { MolScriptBuilder } from '../../../mol-script/language/builder.js'; import { mapArrayToObject, pickObjectKeys } from '../../../mol-util/object.js'; import { Choice } from '../../../mol-util/param-choice.js'; import { ParamDefinition as PD } from '../../../mol-util/param-definition.js'; import { capitalize } from '../../../mol-util/string.js'; import { MVSAnnotationStructureComponentParams, createMVSAnnotationStructureComponent } from './annotation-structure-component.js'; /** Allowed values for a static selector */ export const StaticSelectorChoice = new Choice(mapArrayToObject(StaticStructureComponentTypes, t => capitalize(t)), 'all'); /** Parameter definition for specifying a part of structure (kinda extension of `StructureComponentParams` from mol-plugin-state/helpers/structure-component) */ export const SelectorParams = PD.MappedStatic('static', { static: StaticSelectorChoice.PDSelect(), expression: PD.Value(MolScriptBuilder.struct.generator.all), bundle: PD.Value(StructureElement.Bundle.Empty), script: PD.Script({ language: 'mol-script', expression: '(sel.atom.all)' }), annotation: PD.Group(pickObjectKeys(MVSAnnotationStructureComponentParams, ['annotationId', 'fieldName', 'fieldValues'])), }, { description: 'Define a part of the structure where this layer applies (use Static:all to apply to the whole structure)' }); /** `Selector` for selecting the whole structure */ export const SelectorAll = { name: 'static', params: 'all' }; /** Decide whether a selector is `SelectorAll` */ export function isSelectorAll(props) { return props.name === 'static' && props.params === 'all'; } export const ElementSet = { /** Create an `ElementSet` from a structure */ fromStructure(structure) { if (!structure) return {}; const out = {}; for (const unit of structure.units) { out[unit.id] = unit.elements; } return out; }, /** Create an `ElementSet` from the substructure of `structure` defined by `selector` */ fromSelector(structure, selector) { if (!structure) return {}; const selection = substructureFromSelector(structure, selector); // using `getAtomRangesForRow` might (might not) be faster here return this.fromStructure(selection); }, /** Decide if the element set `set` contains structure element location `location` */ has(set, location) { const array = set[location.unit.id]; return array ? SortedArray.has(array, location.element) : false; }, }; /** Return a substructure of `structure` defined by `selector` */ export function substructureFromSelector(structure, selector) { const pso = (selector.name === 'annotation') ? createMVSAnnotationStructureComponent(structure, { ...selector.params, label: '', nullIfEmpty: false }, {}) : createStructureComponent(structure, { type: selector, label: '', nullIfEmpty: false }, { source: structure }); return PluginStateObject.Molecule.Structure.is(pso) ? pso.data : Structure.Empty; }