molstar
Version:
A comprehensive macromolecular library.
151 lines (150 loc) • 7.2 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 { StructureElement, Bond } from '../../../mol-model/structure';
import { Vec3 } from '../../../mol-math/linear-algebra';
import { BitFlags, arrayEqual } from '../../../mol-util';
import { createLinkLines } from './util/link';
import { ComplexLinesVisual, ComplexLinesParams } from '../complex-visual';
import { BondType } from '../../../mol-model/structure/model/types';
import { BondIterator, getInterBondLoci, eachInterBond, BondLineParams, makeInterBondIgnoreTest, hasStructureVisibleBonds } from './util/bond';
import { Lines } from '../../../mol-geo/geometry/lines/lines';
import { Sphere3D } from '../../../mol-math/geometry';
import { EmptyLocationIterator } from '../../../mol-geo/util/location-iterator';
const tmpRefPosBondIt = new Bond.ElementBondIterator();
function setRefPosition(pos, structure, unit, index) {
tmpRefPosBondIt.setElement(structure, unit, index);
while (tmpRefPosBondIt.hasNext) {
const bA = tmpRefPosBondIt.move();
bA.otherUnit.conformation.position(bA.otherUnit.elements[bA.otherIndex], pos);
return pos;
}
return null;
}
export function getInterUnitBondLineBuilderProps(structure, theme, props) {
const bonds = structure.interUnitBonds;
const { edgeCount, edges } = bonds;
const { sizeFactor, aromaticBonds, multipleBonds } = props;
const mbOff = multipleBonds === 'off';
const mbSymmetric = multipleBonds === 'symmetric';
const ref = Vec3();
const loc = StructureElement.Location.create();
return {
linkCount: edgeCount,
referencePosition: (edgeIndex) => {
const b = edges[edgeIndex];
let unitA, unitB;
let indexA, indexB;
if (b.unitA < b.unitB) {
unitA = structure.unitMap.get(b.unitA);
unitB = structure.unitMap.get(b.unitB);
indexA = b.indexA;
indexB = b.indexB;
}
else if (b.unitA > b.unitB) {
unitA = structure.unitMap.get(b.unitB);
unitB = structure.unitMap.get(b.unitA);
indexA = b.indexB;
indexB = b.indexA;
}
else {
throw new Error('same units in createInterUnitBondLines');
}
return setRefPosition(ref, structure, unitA, indexA) || setRefPosition(ref, structure, unitB, indexB);
},
position: (posA, posB, edgeIndex, _adjust) => {
const b = edges[edgeIndex];
const uA = structure.unitMap.get(b.unitA);
const uB = structure.unitMap.get(b.unitB);
uA.conformation.position(uA.elements[b.indexA], posA);
uB.conformation.position(uB.elements[b.indexB], posB);
},
style: (edgeIndex) => {
const o = edges[edgeIndex].props.order;
const f = BitFlags.create(edges[edgeIndex].props.flag);
if (BondType.is(f, 2 /* BondType.Flag.MetallicCoordination */) || BondType.is(f, 4 /* BondType.Flag.HydrogenBond */)) {
// show metallic coordinations and hydrogen bonds with dashed cylinders
return 1 /* LinkStyle.Dashed */;
}
else if (o === 3) {
return mbOff ? 0 /* LinkStyle.Solid */ :
mbSymmetric ? 4 /* LinkStyle.Triple */ :
5 /* LinkStyle.OffsetTriple */;
}
else if (aromaticBonds && BondType.is(f, 16 /* BondType.Flag.Aromatic */)) {
return 7 /* LinkStyle.Aromatic */;
}
return (o !== 2 || mbOff) ? 0 /* LinkStyle.Solid */ :
mbSymmetric ? 2 /* LinkStyle.Double */ :
3 /* LinkStyle.OffsetDouble */;
},
radius: (edgeIndex) => {
const b = edges[edgeIndex];
loc.structure = structure;
loc.unit = structure.unitMap.get(b.unitA);
loc.element = loc.unit.elements[b.indexA];
const sizeA = theme.size.size(loc);
loc.unit = structure.unitMap.get(b.unitB);
loc.element = loc.unit.elements[b.indexB];
const sizeB = theme.size.size(loc);
return Math.min(sizeA, sizeB) * sizeFactor;
},
ignore: makeInterBondIgnoreTest(structure, props)
};
}
function createInterUnitBondLines(ctx, structure, theme, props, lines) {
if (!hasStructureVisibleBonds(structure, props))
return Lines.createEmpty(lines);
if (!structure.interUnitBonds.edgeCount)
return Lines.createEmpty(lines);
const builderProps = getInterUnitBondLineBuilderProps(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 InterUnitBondLineParams = {
...ComplexLinesParams,
...BondLineParams,
includeParent: PD.Boolean(false),
};
export function InterUnitBondLineVisual(materialId) {
return ComplexLinesVisual({
defaultProps: PD.getDefaultValues(InterUnitBondLineParams),
createGeometry: createInterUnitBondLines,
createLocationIterator: (structure, props) => {
return !hasStructureVisibleBonds(structure, props)
? EmptyLocationIterator
: BondIterator.fromStructure(structure);
},
getLoci: getInterBondLoci,
eachLocation: eachInterBond,
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);
}