UNPKG

molstar

Version:

A comprehensive macromolecular library.

144 lines (143 loc) 7.65 kB
/** * Copyright (c) 2018-2019 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 { Vec3, Mat4 } from '../../../mol-math/linear-algebra'; import { Box } from '../../../mol-geo/primitive/box'; import { Unit } from '../../../mol-model/structure'; import { Mesh } from '../../../mol-geo/geometry/mesh/mesh'; import { MeshBuilder } from '../../../mol-geo/geometry/mesh/mesh-builder'; import { Segmentation } from '../../../mol-data/int'; import { isNucleic, isPurineBase, isPyrimidineBase } from '../../../mol-model/structure/model/types'; import { addCylinder } from '../../../mol-geo/geometry/mesh/builder/cylinder'; import { UnitsMeshParams, UnitsMeshVisual } from '../units-visual'; import { NucleotideLocationIterator, getNucleotideElementLoci, eachNucleotideElement } from './util/nucleotide'; import { BaseGeometry } from '../../../mol-geo/geometry/base'; import { Sphere3D } from '../../../mol-math/geometry'; // TODO support blocks for multiple locations (including from microheterogeneity) var p1 = Vec3(); var p2 = Vec3(); var p3 = Vec3(); var p4 = Vec3(); var p5 = Vec3(); var p6 = Vec3(); var v12 = Vec3(); var v34 = Vec3(); var vC = Vec3(); var center = Vec3(); var t = Mat4.identity(); var sVec = Vec3(); var box = Box(); export var NucleotideBlockMeshParams = { sizeFactor: PD.Numeric(0.2, { min: 0, max: 10, step: 0.01 }), radialSegments: PD.Numeric(16, { min: 2, max: 56, step: 2 }, BaseGeometry.CustomQualityParamInfo), }; export var DefaultNucleotideBlockMeshProps = PD.getDefaultValues(NucleotideBlockMeshParams); function createNucleotideBlockMesh(ctx, unit, structure, theme, props, mesh) { if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh); var nucleotideElementCount = unit.nucleotideElements.length; if (!nucleotideElementCount) return Mesh.createEmpty(mesh); var sizeFactor = props.sizeFactor, radialSegments = props.radialSegments; var vertexCount = nucleotideElementCount * (box.vertices.length / 3 + radialSegments * 2); var builderState = MeshBuilder.createState(vertexCount, vertexCount / 4, mesh); var elements = unit.elements, model = unit.model; var _a = model.atomicHierarchy, chainAtomSegments = _a.chainAtomSegments, residueAtomSegments = _a.residueAtomSegments, atoms = _a.atoms, atomicIndex = _a.index; var _b = model.atomicHierarchy.derived.residue, moleculeType = _b.moleculeType, traceElementIndex = _b.traceElementIndex; var label_comp_id = atoms.label_comp_id; var pos = unit.conformation.invariantPosition; var chainIt = Segmentation.transientSegments(chainAtomSegments, elements); var residueIt = Segmentation.transientSegments(residueAtomSegments, elements); var cylinderProps = { radiusTop: 1 * sizeFactor, radiusBottom: 1 * sizeFactor, radialSegments: radialSegments, bottomCap: true }; var i = 0; while (chainIt.hasNext) { residueIt.setSegment(chainIt.move()); while (residueIt.hasNext) { var residueIndex = residueIt.move().index; if (isNucleic(moleculeType[residueIndex])) { var compId = label_comp_id.value(residueAtomSegments.offsets[residueIndex]); var idx1 = -1, idx2 = -1, idx3 = -1, idx4 = -1, idx5 = -1, idx6 = -1; var width = 4.5, depth = 2.5 * sizeFactor; var height = 4.5; var isPurine = isPurineBase(compId); var isPyrimidine = isPyrimidineBase(compId); if (!isPurine && !isPyrimidine) { // detect Purine or Pyrimidin based on geometry var idxC4 = atomicIndex.findAtomOnResidue(residueIndex, 'C4'); var idxN9 = atomicIndex.findAtomOnResidue(residueIndex, 'N9'); if (idxC4 !== -1 && idxN9 !== -1 && Vec3.distance(pos(idxC4, p1), pos(idxN9, p2)) < 1.6) { isPurine = true; } else { isPyrimidine = true; } } if (isPurine) { height = 4.5; idx1 = atomicIndex.findAtomOnResidue(residueIndex, 'N1'); idx2 = atomicIndex.findAtomOnResidue(residueIndex, 'C4'); idx3 = atomicIndex.findAtomOnResidue(residueIndex, 'C6'); idx4 = atomicIndex.findAtomOnResidue(residueIndex, 'C2'); idx5 = atomicIndex.findAtomOnResidue(residueIndex, 'N9'); idx6 = traceElementIndex[residueIndex]; } else if (isPyrimidine) { height = 3.0; idx1 = atomicIndex.findAtomOnResidue(residueIndex, 'N3'); idx2 = atomicIndex.findAtomOnResidue(residueIndex, 'C6'); idx3 = atomicIndex.findAtomOnResidue(residueIndex, 'C4'); idx4 = atomicIndex.findAtomOnResidue(residueIndex, 'C2'); idx5 = atomicIndex.findAtomOnResidue(residueIndex, 'N1'); if (idx5 === -1) { // modified ring, e.g. DZ idx5 = atomicIndex.findAtomOnResidue(residueIndex, 'C1'); } idx6 = traceElementIndex[residueIndex]; } if (idx5 !== -1 && idx6 !== -1) { pos(idx5, p5); pos(idx6, p6); builderState.currentGroup = i; addCylinder(builderState, p5, p6, 1, cylinderProps); if (idx1 !== -1 && idx2 !== -1 && idx3 !== -1 && idx4 !== -1) { pos(idx1, p1); pos(idx2, p2); pos(idx3, p3); pos(idx4, p4); Vec3.normalize(v12, Vec3.sub(v12, p2, p1)); Vec3.normalize(v34, Vec3.sub(v34, p4, p3)); Vec3.normalize(vC, Vec3.cross(vC, v12, v34)); Mat4.targetTo(t, p1, p2, vC); Vec3.scaleAndAdd(center, p1, v12, height / 2 - 0.2); Mat4.scale(t, t, Vec3.set(sVec, width, depth, height)); Mat4.setTranslation(t, center); MeshBuilder.addPrimitive(builderState, t, box); } } ++i; } } } var m = MeshBuilder.getMesh(builderState); var sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor); m.setBoundingSphere(sphere); return m; } export var NucleotideBlockParams = __assign(__assign({}, UnitsMeshParams), NucleotideBlockMeshParams); export function NucleotideBlockVisual(materialId) { return UnitsMeshVisual({ defaultProps: PD.getDefaultValues(NucleotideBlockParams), createGeometry: createNucleotideBlockMesh, createLocationIterator: NucleotideLocationIterator.fromGroup, getLoci: getNucleotideElementLoci, eachLocation: eachNucleotideElement, setUpdateState: function (state, newProps, currentProps) { state.createGeometry = (newProps.sizeFactor !== currentProps.sizeFactor || newProps.radialSegments !== currentProps.radialSegments); } }, materialId); }