molstar
Version:
A comprehensive macromolecular library.
164 lines (163 loc) • 8.62 kB
JavaScript
/**
* Copyright (c) 2020-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { __assign } from "tslib";
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 } from './util/link';
import { UnitsLinesParams, UnitsLinesVisual } from '../units-visual';
import { BondType } from '../../../mol-model/structure/model/types';
import { BondIterator, BondLineParams, getIntraBondLoci, eachIntraBond, makeIntraBondIgnoreTest, ignoreBondType } 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';
// avoiding namespace lookup improved performance in Chrome (Aug 2020)
var isBondType = BondType.is;
function createIntraUnitBondLines(ctx, unit, structure, theme, props, lines) {
if (!Unit.isAtomic(unit))
return Lines.createEmpty(lines);
var child = structure.child;
var childUnit = child === null || child === void 0 ? void 0 : child.unitMap.get(unit.id);
if (child && !childUnit)
return Lines.createEmpty(lines);
var location = StructureElement.Location.create(structure, unit);
var elements = unit.elements;
var bonds = unit.bonds;
var edgeCount = bonds.edgeCount, a = bonds.a, b = bonds.b, edgeProps = bonds.edgeProps, offset = bonds.offset;
if (!edgeCount)
return Lines.createEmpty(lines);
var _order = edgeProps.order, _flags = edgeProps.flags;
var sizeFactor = props.sizeFactor, aromaticBonds = props.aromaticBonds, includeTypes = props.includeTypes, excludeTypes = props.excludeTypes, multipleBonds = props.multipleBonds;
var mbOff = multipleBonds === 'off';
var mbSymmetric = multipleBonds === 'symmetric';
var include = BondType.fromNames(includeTypes);
var exclude = BondType.fromNames(excludeTypes);
var ignoreComputedAromatic = ignoreBondType(include, exclude, 32 /* BondType.Flag.Computed */);
var vRef = Vec3();
var pos = unit.conformation.invariantPosition;
var _a = unit.rings, elementRingIndices = _a.elementRingIndices, elementAromaticRingIndices = _a.elementAromaticRingIndices;
var deloTriplets = aromaticBonds ? unit.resonance.delocalizedTriplets : undefined;
var builderProps = {
linkCount: edgeCount * 2,
referencePosition: function (edgeIndex) {
var _a, _b;
var aI = a[edgeIndex], bI = b[edgeIndex];
var rI = deloTriplets === null || deloTriplets === void 0 ? void 0 : deloTriplets.getThirdElement(aI, bI);
if (rI !== undefined)
return pos(elements[rI], vRef);
if (aI > bI)
_a = [bI, aI], aI = _a[0], bI = _a[1];
if (offset[aI + 1] - offset[aI] === 1)
_b = [bI, aI], aI = _b[0], bI = _b[1];
var aR = elementAromaticRingIndices.get(aI) || elementRingIndices.get(aI);
var maxSize = 0;
for (var i = offset[aI], il = offset[aI + 1]; i < il; ++i) {
var _bI = b[i];
if (_bI !== bI && _bI !== aI) {
if (aR) {
var _bR = elementAromaticRingIndices.get(_bI) || elementRingIndices.get(_bI);
if (!_bR)
continue;
var size = arrayIntersectionSize(aR, _bR);
if (size > maxSize) {
maxSize = size;
pos(elements[_bI], vRef);
}
}
else {
return pos(elements[_bI], vRef);
}
}
}
return maxSize > 0 ? vRef : null;
},
position: function (posA, posB, edgeIndex) {
pos(elements[a[edgeIndex]], posA);
pos(elements[b[edgeIndex]], posB);
},
style: function (edgeIndex) {
var o = _order[edgeIndex];
var f = _flags[edgeIndex];
if (isBondType(f, 2 /* BondType.Flag.MetallicCoordination */) || isBondType(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) {
var aI = a[edgeIndex], bI = b[edgeIndex];
var aR = elementAromaticRingIndices.get(aI);
var bR = elementAromaticRingIndices.get(bI);
var 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: function (edgeIndex) {
location.element = elements[a[edgeIndex]];
var sizeA = theme.size.size(location);
location.element = elements[b[edgeIndex]];
var sizeB = theme.size.size(location);
return Math.min(sizeA, sizeB) * sizeFactor;
},
ignore: makeIntraBondIgnoreTest(structure, unit, props)
};
var _b = createLinkLines(ctx, builderProps, props, lines), l = _b.lines, boundingSphere = _b.boundingSphere;
if (boundingSphere) {
l.setBoundingSphere(boundingSphere);
}
else if (l.lineCount > 0) {
var sphere = Sphere3D.expand(Sphere3D(), (childUnit !== null && childUnit !== void 0 ? childUnit : unit).boundary.sphere, 1 * sizeFactor);
l.setBoundingSphere(sphere);
}
return l;
}
export var IntraUnitBondLineParams = __assign(__assign(__assign({}, UnitsLinesParams), BondLineParams), { includeParent: PD.Boolean(false) });
export function IntraUnitBondLineVisual(materialId) {
return UnitsLinesVisual({
defaultProps: PD.getDefaultValues(IntraUnitBondLineParams),
createGeometry: createIntraUnitBondLines,
createLocationIterator: BondIterator.fromGroup,
getLoci: getIntraBondLoci,
eachLocation: eachIntraBond,
setUpdateState: function (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 ||
!arrayEqual(newProps.includeTypes, currentProps.includeTypes) ||
!arrayEqual(newProps.excludeTypes, currentProps.excludeTypes) ||
newProps.aromaticBonds !== currentProps.aromaticBonds ||
newProps.multipleBonds !== currentProps.multipleBonds);
var newUnit = newStructureGroup.group.units[0];
var 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);
}