molstar
Version:
A comprehensive macromolecular library.
278 lines (277 loc) • 14 kB
JavaScript
/**
* Copyright (c) 2020-2024 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { ParamDefinition as PD } from '../../../mol-util/param-definition';
import { Unit, StructureElement } from '../../../mol-model/structure';
import { Vec3 } from '../../../mol-math/linear-algebra';
import { arrayEqual } from '../../../mol-util';
import { createLinkLines, EmptyLinkBuilderProps } from './util/link';
import { UnitsLinesParams, UnitsLinesVisual } from '../units-visual';
import { BondType } from '../../../mol-model/structure/model/types';
import { BondIterator, BondLineParams, getIntraBondLoci, eachIntraBond, makeIntraBondIgnoreTest, ignoreBondType, hasUnitVisibleBonds, hasStructureVisibleBonds, getStructureGroupsBondLoci, eachStructureGroupsBond } from './util/bond';
import { Sphere3D } from '../../../mol-math/geometry';
import { Lines } from '../../../mol-geo/geometry/lines/lines';
import { IntAdjacencyGraph } from '../../../mol-math/graph';
import { arrayIntersectionSize } from '../../../mol-util/array';
import { ComplexLinesParams, ComplexLinesVisual } from '../complex-visual';
import { EmptyLocationIterator } from '../../../mol-geo/util/location-iterator';
// avoiding namespace lookup improved performance in Chrome (Aug 2020)
const isBondType = BondType.is;
function getIntraUnitBondLineBuilderProps(unit, structure, theme, props) {
const location = StructureElement.Location.create(structure, unit);
const elements = unit.elements;
const bonds = unit.bonds;
const { edgeCount, a, b, edgeProps, offset } = bonds;
const { order: _order, flags: _flags } = edgeProps;
const { sizeFactor, aromaticBonds, includeTypes, excludeTypes, multipleBonds } = props;
const mbOff = multipleBonds === 'off';
const mbSymmetric = multipleBonds === 'symmetric';
const include = BondType.fromNames(includeTypes);
const exclude = BondType.fromNames(excludeTypes);
const ignoreComputedAromatic = ignoreBondType(include, exclude, 32 /* BondType.Flag.Computed */);
const vRef = Vec3();
const c = unit.conformation;
const { elementRingIndices, elementAromaticRingIndices } = unit.rings;
const deloTriplets = aromaticBonds ? unit.resonance.delocalizedTriplets : undefined;
return {
linkCount: edgeCount * 2,
referencePosition: (edgeIndex) => {
let aI = a[edgeIndex], bI = b[edgeIndex];
const rI = deloTriplets === null || deloTriplets === void 0 ? void 0 : deloTriplets.getThirdElement(aI, bI);
if (rI !== undefined)
return c.invariantPosition(elements[rI], vRef);
if (aI > bI)
[aI, bI] = [bI, aI];
if (offset[aI + 1] - offset[aI] === 1)
[aI, bI] = [bI, aI];
const aR = elementAromaticRingIndices.get(aI) || elementRingIndices.get(aI);
let maxSize = 0;
for (let i = offset[aI], il = offset[aI + 1]; i < il; ++i) {
const _bI = b[i];
if (_bI !== bI && _bI !== aI) {
if (aR) {
const _bR = elementAromaticRingIndices.get(_bI) || elementRingIndices.get(_bI);
if (!_bR)
continue;
const size = arrayIntersectionSize(aR, _bR);
if (size > maxSize) {
maxSize = size;
c.invariantPosition(elements[_bI], vRef);
}
}
else {
return c.invariantPosition(elements[_bI], vRef);
}
}
}
return maxSize > 0 ? vRef : null;
},
position: (posA, posB, edgeIndex, _adjust) => {
c.invariantPosition(elements[a[edgeIndex]], posA);
c.invariantPosition(elements[b[edgeIndex]], posB);
},
style: (edgeIndex) => {
const o = _order[edgeIndex];
const f = _flags[edgeIndex];
if (isBondType(f, 2 /* BondType.Flag.MetallicCoordination */) || isBondType(f, 4 /* BondType.Flag.HydrogenBond */)) {
// show metallic coordinations and hydrogen bonds with dashed lines
return 1 /* LinkStyle.Dashed */;
}
else if (o === 3) {
return mbOff ? 0 /* LinkStyle.Solid */ :
mbSymmetric ? 4 /* LinkStyle.Triple */ :
5 /* LinkStyle.OffsetTriple */;
}
else if (aromaticBonds) {
const aI = a[edgeIndex], bI = b[edgeIndex];
const aR = elementAromaticRingIndices.get(aI);
const bR = elementAromaticRingIndices.get(bI);
const arCount = (aR && bR) ? arrayIntersectionSize(aR, bR) : 0;
if (isBondType(f, 16 /* BondType.Flag.Aromatic */) || (arCount && !ignoreComputedAromatic)) {
if (arCount === 2) {
return 8 /* LinkStyle.MirroredAromatic */;
}
else {
return 7 /* LinkStyle.Aromatic */;
}
}
}
return (o !== 2 || mbOff) ? 0 /* LinkStyle.Solid */ :
mbSymmetric ? 2 /* LinkStyle.Double */ :
3 /* LinkStyle.OffsetDouble */;
},
radius: (edgeIndex) => {
location.element = elements[a[edgeIndex]];
const sizeA = theme.size.size(location);
location.element = elements[b[edgeIndex]];
const sizeB = theme.size.size(location);
return Math.min(sizeA, sizeB) * sizeFactor;
},
ignore: makeIntraBondIgnoreTest(structure, unit, props)
};
}
function createIntraUnitBondLines(ctx, unit, structure, theme, props, lines) {
if (!Unit.isAtomic(unit))
return Lines.createEmpty(lines);
if (!hasUnitVisibleBonds(unit, props))
return Lines.createEmpty(lines);
if (!unit.bonds.edgeCount)
return Lines.createEmpty(lines);
const { child } = structure;
const childUnit = child === null || child === void 0 ? void 0 : child.unitMap.get(unit.id);
if (child && !childUnit)
return Lines.createEmpty(lines);
const builderProps = getIntraUnitBondLineBuilderProps(unit, structure, theme, props);
const { lines: l, boundingSphere } = createLinkLines(ctx, builderProps, props, lines);
if (boundingSphere) {
l.setBoundingSphere(boundingSphere);
}
else if (l.lineCount > 0) {
const sphere = Sphere3D.expand(Sphere3D(), (childUnit !== null && childUnit !== void 0 ? childUnit : unit).boundary.sphere, 1 * props.sizeFactor);
l.setBoundingSphere(sphere);
}
return l;
}
export const IntraUnitBondLineParams = {
...UnitsLinesParams,
...BondLineParams,
includeParent: PD.Boolean(false),
};
export function IntraUnitBondLineVisual(materialId) {
return UnitsLinesVisual({
defaultProps: PD.getDefaultValues(IntraUnitBondLineParams),
createGeometry: createIntraUnitBondLines,
createLocationIterator: (structureGroup) => BondIterator.fromGroup(structureGroup),
getLoci: getIntraBondLoci,
eachLocation: eachIntraBond,
setUpdateState: (state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup) => {
state.createGeometry = (newProps.sizeFactor !== currentProps.sizeFactor ||
newProps.linkScale !== currentProps.linkScale ||
newProps.linkSpacing !== currentProps.linkSpacing ||
newProps.aromaticDashCount !== currentProps.aromaticDashCount ||
newProps.dashCount !== currentProps.dashCount ||
newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
newProps.ignoreHydrogensVariant !== currentProps.ignoreHydrogensVariant ||
!arrayEqual(newProps.includeTypes, currentProps.includeTypes) ||
!arrayEqual(newProps.excludeTypes, currentProps.excludeTypes) ||
newProps.aromaticBonds !== currentProps.aromaticBonds ||
newProps.multipleBonds !== currentProps.multipleBonds);
const newUnit = newStructureGroup.group.units[0];
const currentUnit = currentStructureGroup.group.units[0];
if (Unit.isAtomic(newUnit) && Unit.isAtomic(currentUnit)) {
if (!IntAdjacencyGraph.areEqual(newUnit.bonds, currentUnit.bonds)) {
state.createGeometry = true;
state.updateTransform = true;
state.updateColor = true;
state.updateSize = true;
}
}
}
}, materialId);
}
//
function getStructureIntraUnitBondLineBuilderProps(structure, theme, props) {
const intraUnitProps = [];
const { bondCount, unitIndex, unitEdgeIndex, unitGroupIndex } = structure.intraUnitBondMapping;
const { child } = structure;
for (const ug of structure.unitSymmetryGroups) {
const unit = ug.units[0];
const childUnit = child === null || child === void 0 ? void 0 : child.unitMap.get(unit.id);
const p = Unit.isAtomic(unit) && !(child && !childUnit)
? getIntraUnitBondLineBuilderProps(unit, structure, theme, props)
: EmptyLinkBuilderProps;
intraUnitProps.push({ group: ug, props: p });
}
return {
linkCount: bondCount,
referencePosition: (edgeIndex) => {
const { group, props } = intraUnitProps[unitIndex[edgeIndex]];
if (!props.referencePosition)
return null;
const v = props.referencePosition(unitEdgeIndex[edgeIndex]);
if (!v)
return null;
const u = group.units[unitGroupIndex[edgeIndex]];
Vec3.transformMat4(v, v, u.conformation.operator.matrix);
return v;
},
position: (posA, posB, edgeIndex, adjust) => {
const { group, props } = intraUnitProps[unitIndex[edgeIndex]];
props.position(posA, posB, unitEdgeIndex[edgeIndex], adjust);
const u = group.units[unitGroupIndex[edgeIndex]];
Vec3.transformMat4(posA, posA, u.conformation.operator.matrix);
Vec3.transformMat4(posB, posB, u.conformation.operator.matrix);
},
style: (edgeIndex) => {
const { props } = intraUnitProps[unitIndex[edgeIndex]];
return props.style ? props.style(unitEdgeIndex[edgeIndex]) : 0 /* LinkStyle.Solid */;
},
radius: (edgeIndex) => {
const { props } = intraUnitProps[unitIndex[edgeIndex]];
return props.radius(unitEdgeIndex[edgeIndex]);
},
ignore: (edgeIndex) => {
const { props } = intraUnitProps[unitIndex[edgeIndex]];
return props.ignore ? props.ignore(unitEdgeIndex[edgeIndex]) : false;
},
stub: (edgeIndex) => {
const { props } = intraUnitProps[unitIndex[edgeIndex]];
return props.stub ? props.stub(unitEdgeIndex[edgeIndex]) : false;
}
};
}
function createStructureIntraUnitBondLines(ctx, structure, theme, props, lines) {
if (!hasStructureVisibleBonds(structure, props))
return Lines.createEmpty(lines);
if (!structure.intraUnitBondMapping.bondCount)
return Lines.createEmpty(lines);
const builderProps = getStructureIntraUnitBondLineBuilderProps(structure, theme, props);
const { lines: l, boundingSphere } = createLinkLines(ctx, builderProps, props, lines);
if (boundingSphere) {
l.setBoundingSphere(boundingSphere);
}
else if (l.lineCount > 0) {
const { child } = structure;
const sphere = Sphere3D.expand(Sphere3D(), (child !== null && child !== void 0 ? child : structure).boundary.sphere, 1 * props.sizeFactor);
l.setBoundingSphere(sphere);
}
return l;
}
export const StructureIntraUnitBondLineParams = {
...ComplexLinesParams,
...BondLineParams,
includeParent: PD.Boolean(false),
};
export function StructureIntraUnitBondLineVisual(materialId) {
return ComplexLinesVisual({
defaultProps: PD.getDefaultValues(StructureIntraUnitBondLineParams),
createGeometry: createStructureIntraUnitBondLines,
createLocationIterator: (structure, props) => {
return !hasStructureVisibleBonds(structure, props)
? EmptyLocationIterator
: BondIterator.fromStructureGroups(structure);
},
getLoci: getStructureGroupsBondLoci,
eachLocation: eachStructureGroupsBond,
setUpdateState: (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) => {
state.createGeometry = (newProps.sizeFactor !== currentProps.sizeFactor ||
newProps.linkScale !== currentProps.linkScale ||
newProps.linkSpacing !== currentProps.linkSpacing ||
newProps.aromaticDashCount !== currentProps.aromaticDashCount ||
newProps.dashCount !== currentProps.dashCount ||
newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
newProps.ignoreHydrogensVariant !== currentProps.ignoreHydrogensVariant ||
!arrayEqual(newProps.includeTypes, currentProps.includeTypes) ||
!arrayEqual(newProps.excludeTypes, currentProps.excludeTypes) ||
newProps.multipleBonds !== currentProps.multipleBonds);
if (hasStructureVisibleBonds(newStructure, newProps) && newStructure.interUnitBonds !== currentStructure.interUnitBonds) {
state.createGeometry = true;
state.updateTransform = true;
state.updateColor = true;
state.updateSize = true;
}
}
}, materialId);
}