molstar
Version:
A comprehensive macromolecular library.
205 lines (204 loc) • 12.4 kB
JavaScript
"use strict";
/**
* Copyright (c) 2018-2024 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author Gianluca Tomasello <giagitom@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PolymerTraceParams = exports.DefaultPolymerTraceMeshProps = exports.PolymerTraceMeshParams = void 0;
exports.PolymerTraceVisual = PolymerTraceVisual;
const param_definition_1 = require("../../../mol-util/param-definition");
const structure_1 = require("../../../mol-model/structure");
const mesh_1 = require("../../../mol-geo/geometry/mesh/mesh");
const mesh_builder_1 = require("../../../mol-geo/geometry/mesh/mesh-builder");
const polymer_1 = require("./util/polymer");
const types_1 = require("../../../mol-model/structure/model/types");
const sheet_1 = require("../../../mol-geo/geometry/mesh/builder/sheet");
const tube_1 = require("../../../mol-geo/geometry/mesh/builder/tube");
const units_visual_1 = require("../units-visual");
const secondary_structure_1 = require("../../../mol-model-props/computed/secondary-structure");
const ribbon_1 = require("../../../mol-geo/geometry/mesh/builder/ribbon");
const sphere_1 = require("../../../mol-geo/geometry/mesh/builder/sphere");
const linear_algebra_1 = require("../../../mol-math/linear-algebra");
const base_1 = require("../../../mol-geo/geometry/base");
const geometry_1 = require("../../../mol-math/geometry");
exports.PolymerTraceMeshParams = {
sizeFactor: param_definition_1.ParamDefinition.Numeric(0.2, { min: 0, max: 10, step: 0.01 }),
aspectRatio: param_definition_1.ParamDefinition.Numeric(5, { min: 0.1, max: 10, step: 0.1 }),
arrowFactor: param_definition_1.ParamDefinition.Numeric(1.5, { min: 0, max: 3, step: 0.1 }, { description: 'Size factor for sheet arrows' }),
tubularHelices: param_definition_1.ParamDefinition.Boolean(false, { description: 'Draw alpha helices as tubes' }),
roundCap: param_definition_1.ParamDefinition.Boolean(false, { description: 'Draw round caps on tubular alpha helices' }),
helixProfile: param_definition_1.ParamDefinition.Select('elliptical', param_definition_1.ParamDefinition.arrayToOptions(['elliptical', 'rounded', 'square']), { description: 'Protein helix trace profile' }),
nucleicProfile: param_definition_1.ParamDefinition.Select('square', param_definition_1.ParamDefinition.arrayToOptions(['elliptical', 'rounded', 'square']), { description: 'Nucleic strand trace profile' }),
detail: param_definition_1.ParamDefinition.Numeric(0, { min: 0, max: 3, step: 1 }, base_1.BaseGeometry.CustomQualityParamInfo),
linearSegments: param_definition_1.ParamDefinition.Numeric(8, { min: 1, max: 48, step: 1 }, base_1.BaseGeometry.CustomQualityParamInfo),
radialSegments: param_definition_1.ParamDefinition.Numeric(16, { min: 2, max: 56, step: 2 }, base_1.BaseGeometry.CustomQualityParamInfo)
};
exports.DefaultPolymerTraceMeshProps = param_definition_1.ParamDefinition.getDefaultValues(exports.PolymerTraceMeshParams);
const tmpV1 = (0, linear_algebra_1.Vec3)();
function createPolymerTraceMesh(ctx, unit, structure, theme, props, mesh) {
const polymerElementCount = unit.polymerElements.length;
if (!polymerElementCount)
return mesh_1.Mesh.createEmpty(mesh);
const { sizeFactor, detail, linearSegments, radialSegments, aspectRatio, arrowFactor, tubularHelices, roundCap, helixProfile, nucleicProfile } = props;
const vertexCount = linearSegments * radialSegments * polymerElementCount + (radialSegments + 1) * polymerElementCount * 2;
const builderState = mesh_builder_1.MeshBuilder.createState(vertexCount, vertexCount / 10, mesh);
const isCoarse = structure_1.Unit.isCoarse(unit);
const state = (0, polymer_1.createCurveSegmentState)(linearSegments);
const { curvePoints, normalVectors, binormalVectors, widthValues, heightValues } = state;
let i = 0;
const polymerTraceIt = (0, polymer_1.PolymerTraceIterator)(unit, structure, { ignoreSecondaryStructure: false, useHelixOrientation: tubularHelices });
while (polymerTraceIt.hasNext) {
const v = polymerTraceIt.move();
builderState.currentGroup = i;
const isNucleicType = (0, types_1.isNucleic)(v.moleculeType);
const isSheet = types_1.SecondaryStructureType.is(v.secStrucType, 4 /* SecondaryStructureType.Flag.Beta */);
const isHelix = types_1.SecondaryStructureType.is(v.secStrucType, 2 /* SecondaryStructureType.Flag.Helix */);
const tension = isHelix && !tubularHelices ? polymer_1.HelixTension : polymer_1.StandardTension;
const shift = isNucleicType ? polymer_1.NucleicShift : polymer_1.StandardShift;
(0, polymer_1.interpolateCurveSegment)(state, v, tension, shift);
let w0 = theme.size.size(v.centerPrev) * sizeFactor;
let w1 = theme.size.size(v.center) * sizeFactor;
let w2 = theme.size.size(v.centerNext) * sizeFactor;
if (isCoarse) {
w0 *= aspectRatio / 2;
w1 *= aspectRatio / 2;
w2 *= aspectRatio / 2;
}
const startCap = v.secStrucFirst || v.coarseBackboneFirst || v.first;
const endCap = v.secStrucLast || v.coarseBackboneLast || v.last;
const hasRoundCap = isHelix && tubularHelices && roundCap;
let segmentCount = linearSegments;
if (v.initial) {
segmentCount = Math.max(Math.round(linearSegments * shift), 1);
const offset = linearSegments - segmentCount;
curvePoints.copyWithin(0, offset * 3);
binormalVectors.copyWithin(0, offset * 3);
normalVectors.copyWithin(0, offset * 3);
linear_algebra_1.Vec3.fromArray(tmpV1, curvePoints, 3);
linear_algebra_1.Vec3.normalize(tmpV1, linear_algebra_1.Vec3.sub(tmpV1, v.p2, tmpV1));
linear_algebra_1.Vec3.scaleAndAdd(tmpV1, v.p2, tmpV1, w1 * polymer_1.OverhangFactor);
linear_algebra_1.Vec3.toArray(tmpV1, curvePoints, 0);
}
else if (v.final) {
segmentCount = Math.max(Math.round(linearSegments * (1 - shift)), 1);
linear_algebra_1.Vec3.fromArray(tmpV1, curvePoints, segmentCount * 3 - 3);
linear_algebra_1.Vec3.normalize(tmpV1, linear_algebra_1.Vec3.sub(tmpV1, v.p2, tmpV1));
linear_algebra_1.Vec3.scaleAndAdd(tmpV1, v.p2, tmpV1, w1 * polymer_1.OverhangFactor);
linear_algebra_1.Vec3.toArray(tmpV1, curvePoints, segmentCount * 3);
}
if (v.initial === true && v.final === true) {
(0, sphere_1.addSphere)(builderState, v.p2, w1 * 2, detail);
}
else if (isSheet) {
const h0 = w0 * aspectRatio;
const h1 = w1 * aspectRatio;
const h2 = w2 * aspectRatio;
const arrowHeight = v.secStrucLast ? h1 * arrowFactor : 0;
(0, polymer_1.interpolateSizes)(state, w0, w1, w2, h0, h1, h2, shift);
if (radialSegments === 2) {
(0, ribbon_1.addRibbon)(builderState, curvePoints, normalVectors, binormalVectors, segmentCount, widthValues, heightValues, arrowHeight);
}
else {
(0, sheet_1.addSheet)(builderState, curvePoints, normalVectors, binormalVectors, segmentCount, widthValues, heightValues, arrowHeight, startCap, endCap);
}
}
else {
let h0, h1, h2;
if (isHelix) {
if (tubularHelices) {
w0 *= aspectRatio * 1.5;
w1 *= aspectRatio * 1.5;
w2 *= aspectRatio * 1.5;
h0 = w0;
h1 = w1;
h2 = w2;
}
else {
h0 = w0 * aspectRatio;
h1 = w1 * aspectRatio;
h2 = w2 * aspectRatio;
}
}
else if (isNucleicType) {
h0 = w0 * aspectRatio;
h1 = w1 * aspectRatio;
h2 = w2 * aspectRatio;
}
else {
h0 = w0;
h1 = w1;
h2 = w2;
}
(0, polymer_1.interpolateSizes)(state, w0, w1, w2, h0, h1, h2, shift);
const [normals, binormals] = isNucleicType ? [binormalVectors, normalVectors] : [normalVectors, binormalVectors];
if (isNucleicType) {
// TODO: find a cleaner way to swap normal and binormal for nucleic types
for (let i = 0, il = normals.length; i < il; i++)
normals[i] *= -1;
}
const profile = isNucleicType ? nucleicProfile : helixProfile;
if (radialSegments === 2) {
if (isNucleicType) {
(0, ribbon_1.addRibbon)(builderState, curvePoints, normals, binormals, segmentCount, heightValues, widthValues, 0);
}
else {
(0, ribbon_1.addRibbon)(builderState, curvePoints, normals, binormals, segmentCount, widthValues, heightValues, 0);
}
}
else if (radialSegments === 4) {
(0, sheet_1.addSheet)(builderState, curvePoints, normals, binormals, segmentCount, widthValues, heightValues, 0, startCap, endCap);
}
else if (h1 === w1) {
(0, tube_1.addTube)(builderState, curvePoints, normals, binormals, segmentCount, radialSegments, widthValues, heightValues, startCap, endCap, 'elliptical', hasRoundCap);
}
else if (profile === 'square') {
(0, sheet_1.addSheet)(builderState, curvePoints, normals, binormals, segmentCount, widthValues, heightValues, 0, startCap, endCap);
}
else {
(0, tube_1.addTube)(builderState, curvePoints, normals, binormals, segmentCount, radialSegments, widthValues, heightValues, startCap, endCap, profile);
}
}
++i;
}
const m = mesh_builder_1.MeshBuilder.getMesh(builderState);
const sphere = geometry_1.Sphere3D.expand((0, geometry_1.Sphere3D)(), unit.boundary.sphere, 1 * props.sizeFactor);
m.setBoundingSphere(sphere);
return m;
}
exports.PolymerTraceParams = {
...units_visual_1.UnitsMeshParams,
...exports.PolymerTraceMeshParams
};
function PolymerTraceVisual(materialId) {
return (0, units_visual_1.UnitsMeshVisual)({
defaultProps: param_definition_1.ParamDefinition.getDefaultValues(exports.PolymerTraceParams),
createGeometry: createPolymerTraceMesh,
createLocationIterator: (structureGroup) => polymer_1.PolymerLocationIterator.fromGroup(structureGroup, { asSecondary: true }),
getLoci: polymer_1.getPolymerElementLoci,
eachLocation: polymer_1.eachPolymerElement,
setUpdateState: (state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup) => {
state.createGeometry = (newProps.sizeFactor !== currentProps.sizeFactor ||
newProps.tubularHelices !== currentProps.tubularHelices ||
newProps.roundCap !== currentProps.roundCap ||
newProps.detail !== currentProps.detail ||
newProps.linearSegments !== currentProps.linearSegments ||
newProps.radialSegments !== currentProps.radialSegments ||
newProps.aspectRatio !== currentProps.aspectRatio ||
newProps.arrowFactor !== currentProps.arrowFactor ||
newProps.helixProfile !== currentProps.helixProfile ||
newProps.nucleicProfile !== currentProps.nucleicProfile);
const secondaryStructureHash = secondary_structure_1.SecondaryStructureProvider.get(newStructureGroup.structure).version;
if (state.info.secondaryStructureHash !== secondaryStructureHash) {
if (state.info.secondaryStructureHash !== undefined)
state.createGeometry = true;
state.info.secondaryStructureHash = secondaryStructureHash;
}
},
initUpdateState: (state, newProps, newTheme, newStructureGroup) => {
const secondaryStructureHash = secondary_structure_1.SecondaryStructureProvider.get(newStructureGroup.structure).version;
state.info.secondaryStructureHash = secondaryStructureHash;
}
}, materialId);
}