123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- /**
- * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
- *
- * @author Joan Segura <joan.segura@rcsb.org>
- */
- import { StructureRef } from 'molstar/lib/mol-plugin-state/manager/structure/hierarchy-state';
- import { Structure } from 'molstar/lib/mol-model/structure/structure';
- import { PluginContext } from 'molstar/lib/mol-plugin/context';
- import { MolScriptBuilder as MS } from 'molstar/lib/mol-script/language/builder';
- import { StructureRepresentationRegistry } from 'molstar/lib/mol-repr/structure/registry';
- import { StructureSelectionQuery } from 'molstar/lib/mol-plugin-state/helpers/structure-selection-query';
- import {
- rangeToTest, SelectBase,
- SelectRange,
- SelectTarget,
- Target,
- targetToLoci,
- toRange
- } from './selection';
- export function setFocusFromRange(plugin: PluginContext, target: SelectRange) {
- const data = getStructureWithModelId(plugin.managers.structure.hierarchy.current.structures, target);
- if (!data) return;
- const loci = targetToLoci(target, data);
- if (!loci) return;
- plugin.managers.structure.focus.setFromLoci(loci);
- }
- function getStructureWithModelId(structures: StructureRef[], target: { modelId: string }): Structure | undefined {
- const structureRef = getStructureRefWithModelId(structures, target);
- if (structureRef) return structureRef.cell?.obj?.data;
- }
- export function getStructureRefWithModelId(structures: StructureRef[], target: { modelId: string }): StructureRef | undefined {
- for (const structure of structures) {
- if (!structure.cell?.obj?.data?.units) continue;
- const unit = structure.cell.obj.data.units[0];
- if (unit.model.id === target.modelId) return structure;
- }
- }
- export function select(plugin: PluginContext, targets: SelectTarget | SelectTarget[], mode: 'select' | 'hover', modifier: 'add' | 'set') {
- (Array.isArray(targets) ? targets : [targets]).forEach((target, n)=>{
- const data = getStructureWithModelId(plugin.managers.structure.hierarchy.current.structures, target);
- if (!data) return;
- const loci = targetToLoci(target, data);
- if (!loci) return;
- if (mode === 'hover') {
- plugin.managers.interactivity.lociHighlights.highlight({ loci });
- } else if (mode === 'select') {
- plugin.managers.structure.selection.fromLoci(n > 0 ? 'add' : modifier, loci);
- }
- });
- }
- export function clearSelection(plugin: PluginContext, mode: 'select' | 'hover', target?: { modelId: string; } & Target) {
- if (mode === 'hover') {
- plugin.managers.interactivity.lociHighlights.clearHighlights();
- return;
- }
- if (!target) {
- plugin.managers.interactivity.lociSelects.deselectAll();
- return;
- }
- const data = getStructureWithModelId(plugin.managers.structure.hierarchy.current.structures, target);
- if (!data) return;
- const loci = targetToLoci(target, data);
- plugin.managers.interactivity.lociSelects.deselect({ loci });
- }
- export async function createComponent(plugin: PluginContext, componentLabel: string, targets: SelectBase | SelectTarget | SelectTarget[], representationType: StructureRepresentationRegistry.BuiltIn) {
- for (const target of (Array.isArray(targets) ? targets : [targets])) {
- const structureRef = getStructureRefWithModelId(plugin.managers.structure.hierarchy.current.structures, target);
- if (!structureRef) throw Error('createComponent error: model not found');
- const residues = toResidues(target);
- const sel = StructureSelectionQuery('innerQuery_' + Math.random().toString(36).substr(2),
- MS.struct.generator.atomGroups(rangeToTest(target.labelAsymId, residues)));
- await plugin.managers.structure.component.add({
- selection: sel,
- options: { checkExisting: false, label: componentLabel },
- representation: representationType,
- }, [structureRef]);
- }
- }
- function toResidues(target: SelectBase | SelectTarget): number[] {
- if ('labelSeqRange' in target) {
- return toRange(target.labelSeqRange.beg, target.labelSeqRange.end);
- }
- if ('labelSeqId' in target) {
- return [target.labelSeqId];
- }
- return [];
- }
- export function removeComponent(plugin: PluginContext, componentLabel: string) {
- plugin.managers.structure.hierarchy.currentComponentGroups.forEach(c => {
- for (const comp of c) {
- if (comp.cell.obj?.label === componentLabel) {
- plugin.managers.structure.hierarchy.remove(c);
- break;
- }
- }
- });
- }
|