nucleotide-block-mesh.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Unit, Structure, ElementIndex } from 'mol-model/structure';
  7. import { UnitsVisual } from '../representation';
  8. import { Vec3, Mat4 } from 'mol-math/linear-algebra';
  9. import { Segmentation } from 'mol-data/int';
  10. import { MoleculeType, isNucleic, isPurinBase, isPyrimidineBase } from 'mol-model/structure/model/types';
  11. import { getElementIndexForAtomRole } from 'mol-model/structure/util';
  12. import { UnitsMeshVisual, UnitsMeshParams } from '../units-visual';
  13. import { NucleotideLocationIterator, markNucleotideElement, getNucleotideElementLoci } from './util/nucleotide';
  14. import { ParamDefinition as PD } from 'mol-util/param-definition';
  15. import { Box } from 'mol-geo/primitive/box';
  16. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  17. import { MeshBuilder } from 'mol-geo/geometry/mesh/mesh-builder';
  18. import { addCylinder } from 'mol-geo/geometry/mesh/builder/cylinder';
  19. import { VisualContext } from 'mol-repr/representation';
  20. import { Theme } from 'mol-theme/theme';
  21. import { VisualUpdateState } from 'mol-repr/util';
  22. const p1 = Vec3.zero()
  23. const p2 = Vec3.zero()
  24. const p3 = Vec3.zero()
  25. const p4 = Vec3.zero()
  26. const p5 = Vec3.zero()
  27. const p6 = Vec3.zero()
  28. const v12 = Vec3.zero()
  29. const v34 = Vec3.zero()
  30. const vC = Vec3.zero()
  31. const center = Vec3.zero()
  32. const t = Mat4.identity()
  33. const sVec = Vec3.zero()
  34. const box = Box()
  35. export const NucleotideBlockMeshParams = {
  36. sizeFactor: PD.Numeric(0.2, { min: 0, max: 10, step: 0.01 }),
  37. }
  38. export const DefaultNucleotideBlockMeshProps = PD.getDefaultValues(NucleotideBlockMeshParams)
  39. export type NucleotideBlockMeshProps = typeof DefaultNucleotideBlockMeshProps
  40. function createNucleotideBlockMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: NucleotideBlockMeshProps, mesh?: Mesh) {
  41. if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh)
  42. const { sizeFactor } = props
  43. // TODO better vertex count estimate
  44. const builder = MeshBuilder.create(256, 128, mesh)
  45. const { elements, model } = unit
  46. const { chemicalComponentMap, modifiedResidues } = model.properties
  47. const { chainAtomSegments, residueAtomSegments, residues, index: atomicIndex } = model.atomicHierarchy
  48. const { label_comp_id } = residues
  49. const pos = unit.conformation.invariantPosition
  50. const chainIt = Segmentation.transientSegments(chainAtomSegments, elements)
  51. const residueIt = Segmentation.transientSegments(residueAtomSegments, elements)
  52. let i = 0
  53. while (chainIt.hasNext) {
  54. residueIt.setSegment(chainIt.move());
  55. while (residueIt.hasNext) {
  56. const { index: residueIndex } = residueIt.move();
  57. let compId = label_comp_id.value(residueIndex)
  58. const cc = chemicalComponentMap.get(compId)
  59. const moleculeType = cc ? cc.moleculeType : MoleculeType.unknown
  60. if (isNucleic(moleculeType)) {
  61. const parentId = modifiedResidues.parentId.get(compId)
  62. if (parentId !== undefined) compId = parentId
  63. let idx1: ElementIndex | -1 = -1, idx2: ElementIndex | -1 = -1, idx3: ElementIndex | -1 = -1, idx4: ElementIndex | -1 = -1, idx5: ElementIndex | -1 = -1, idx6: ElementIndex | -1 = -1
  64. let width = 4.5, height = 4.5, depth = 2.5 * sizeFactor
  65. if (isPurinBase(compId)) {
  66. height = 4.5
  67. idx1 = atomicIndex.findAtomOnResidue(residueIndex, 'N1')
  68. idx2 = atomicIndex.findAtomOnResidue(residueIndex, 'C4')
  69. idx3 = atomicIndex.findAtomOnResidue(residueIndex, 'C6')
  70. idx4 = atomicIndex.findAtomOnResidue(residueIndex, 'C2')
  71. idx5 = atomicIndex.findAtomOnResidue(residueIndex, 'N9')
  72. idx6 = getElementIndexForAtomRole(model, residueIndex, 'trace')
  73. } else if (isPyrimidineBase(compId)) {
  74. height = 3.0
  75. idx1 = atomicIndex.findAtomOnResidue(residueIndex, 'N3')
  76. idx2 = atomicIndex.findAtomOnResidue(residueIndex, 'C6')
  77. idx3 = atomicIndex.findAtomOnResidue(residueIndex, 'C4')
  78. idx4 = atomicIndex.findAtomOnResidue(residueIndex, 'C2')
  79. idx5 = atomicIndex.findAtomOnResidue(residueIndex, 'N1')
  80. idx6 = getElementIndexForAtomRole(model, residueIndex, 'trace')
  81. }
  82. if (idx5 !== -1 && idx6 !== -1) {
  83. pos(idx5, p5); pos(idx6, p6)
  84. builder.setGroup(i)
  85. addCylinder(builder, p5, p6, 1, { radiusTop: 1 * sizeFactor, radiusBottom: 1 * sizeFactor })
  86. if (idx1 !== -1 && idx2 !== -1 && idx3 !== -1 && idx4 !== -1) {
  87. pos(idx1, p1); pos(idx2, p2); pos(idx3, p3); pos(idx4, p4);
  88. Vec3.normalize(v12, Vec3.sub(v12, p2, p1))
  89. Vec3.normalize(v34, Vec3.sub(v34, p4, p3))
  90. Vec3.normalize(vC, Vec3.cross(vC, v12, v34))
  91. Mat4.targetTo(t, p1, p2, vC)
  92. Vec3.scaleAndAdd(center, p1, v12, height / 2 - 0.2)
  93. Mat4.scale(t, t, Vec3.set(sVec, width, depth, height))
  94. Mat4.setTranslation(t, center)
  95. builder.add(t, box)
  96. }
  97. }
  98. ++i
  99. }
  100. }
  101. }
  102. return builder.getMesh()
  103. }
  104. export const NucleotideBlockParams = {
  105. ...UnitsMeshParams,
  106. ...NucleotideBlockMeshParams
  107. }
  108. export type NucleotideBlockParams = typeof NucleotideBlockParams
  109. export function NucleotideBlockVisual(): UnitsVisual<NucleotideBlockParams> {
  110. return UnitsMeshVisual<NucleotideBlockParams>({
  111. defaultProps: PD.getDefaultValues(NucleotideBlockParams),
  112. createGeometry: createNucleotideBlockMesh,
  113. createLocationIterator: NucleotideLocationIterator.fromGroup,
  114. getLoci: getNucleotideElementLoci,
  115. mark: markNucleotideElement,
  116. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<NucleotideBlockParams>, currentProps: PD.Values<NucleotideBlockParams>) => {
  117. state.createGeometry = (
  118. newProps.sizeFactor !== currentProps.sizeFactor
  119. )
  120. }
  121. })
  122. }