nucleotide-ring-mesh.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  7. import { Vec3 } from '../../../mol-math/linear-algebra';
  8. import { NumberArray } from '../../../mol-util/type-helpers';
  9. import { VisualContext } from '../../visual';
  10. import { Unit, Structure, ElementIndex } from '../../../mol-model/structure';
  11. import { Theme } from '../../../mol-theme/theme';
  12. import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
  13. import { MeshBuilder } from '../../../mol-geo/geometry/mesh/mesh-builder';
  14. import { Segmentation } from '../../../mol-data/int';
  15. import { CylinderProps } from '../../../mol-geo/primitive/cylinder';
  16. import { isNucleic, isPurineBase, isPyrimidineBase } from '../../../mol-model/structure/model/types';
  17. import { addCylinder } from '../../../mol-geo/geometry/mesh/builder/cylinder';
  18. import { addSphere } from '../../../mol-geo/geometry/mesh/builder/sphere';
  19. import { UnitsMeshParams, UnitsVisual, UnitsMeshVisual } from '../units-visual';
  20. import { NucleotideLocationIterator, getNucleotideElementLoci, eachNucleotideElement } from './util/nucleotide';
  21. import { VisualUpdateState } from '../../util';
  22. const pTrace = Vec3.zero()
  23. const pN1 = Vec3.zero()
  24. const pC2 = Vec3.zero()
  25. const pN3 = Vec3.zero()
  26. const pC4 = Vec3.zero()
  27. const pC5 = Vec3.zero()
  28. const pC6 = Vec3.zero()
  29. const pN7 = Vec3.zero()
  30. const pC8 = Vec3.zero()
  31. const pN9 = Vec3.zero()
  32. const normal = Vec3.zero()
  33. export const NucleotideRingMeshParams = {
  34. sizeFactor: PD.Numeric(0.2, { min: 0, max: 10, step: 0.01 }),
  35. radialSegments: PD.Numeric(16, { min: 2, max: 56, step: 2 }),
  36. detail: PD.Numeric(0, { min: 0, max: 3, step: 1 }),
  37. }
  38. export const DefaultNucleotideRingMeshProps = PD.getDefaultValues(NucleotideRingMeshParams)
  39. export type NucleotideRingProps = typeof DefaultNucleotideRingMeshProps
  40. const positionsRing5_6 = new Float32Array(2 * 9 * 3)
  41. const stripIndicesRing5_6 = new Uint32Array([0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 14, 15, 12, 13, 8, 9, 10, 11, 0, 1])
  42. const fanIndicesTopRing5_6 = new Uint32Array([8, 12, 14, 16, 6, 4, 2, 0, 10])
  43. const fanIndicesBottomRing5_6 = new Uint32Array([9, 11, 1, 3, 5, 7, 17, 15, 13])
  44. const positionsRing6 = new Float32Array(2 * 6 * 3)
  45. const stripIndicesRing6 = new Uint32Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1])
  46. const fanIndicesTopRing6 = new Uint32Array([0, 10, 8, 6, 4, 2])
  47. const fanIndicesBottomRing6 = new Uint32Array([1, 3, 5, 7, 9, 11])
  48. const tmpShiftV = Vec3.zero()
  49. function shiftPositions(out: NumberArray, dir: Vec3, ...positions: Vec3[]) {
  50. for (let i = 0, il = positions.length; i < il; ++i) {
  51. const v = positions[i]
  52. Vec3.toArray(Vec3.add(tmpShiftV, v, dir), out, (i * 2) * 3)
  53. Vec3.toArray(Vec3.sub(tmpShiftV, v, dir), out, (i * 2 + 1) * 3)
  54. }
  55. }
  56. function createNucleotideRingMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: NucleotideRingProps, mesh?: Mesh) {
  57. if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh)
  58. const nucleotideElementCount = unit.nucleotideElements.length
  59. if (!nucleotideElementCount) return Mesh.createEmpty(mesh)
  60. const { sizeFactor, radialSegments, detail } = props
  61. const vertexCount = nucleotideElementCount * (26 + radialSegments * 2)
  62. const builderState = MeshBuilder.createState(vertexCount, vertexCount / 4, mesh)
  63. const { elements, model } = unit
  64. const { modifiedResidues } = model.properties
  65. const { chainAtomSegments, residueAtomSegments, residues, index: atomicIndex } = model.atomicHierarchy
  66. const { moleculeType, traceElementIndex } = model.atomicHierarchy.derived.residue
  67. const { label_comp_id } = residues
  68. const pos = unit.conformation.invariantPosition
  69. const chainIt = Segmentation.transientSegments(chainAtomSegments, elements)
  70. const residueIt = Segmentation.transientSegments(residueAtomSegments, elements)
  71. const radius = 1 * sizeFactor
  72. const halfThickness = 1.25 * sizeFactor
  73. const cylinderProps: CylinderProps = { radiusTop: 1 * sizeFactor, radiusBottom: 1 * sizeFactor, radialSegments }
  74. let i = 0
  75. while (chainIt.hasNext) {
  76. residueIt.setSegment(chainIt.move());
  77. while (residueIt.hasNext) {
  78. const { index: residueIndex } = residueIt.move();
  79. if (isNucleic(moleculeType[residueIndex])) {
  80. let compId = label_comp_id.value(residueIndex)
  81. const parentId = modifiedResidues.parentId.get(compId)
  82. if (parentId !== undefined) compId = parentId
  83. let idxTrace: ElementIndex | -1 = -1, idxN1: ElementIndex | -1 = -1, idxC2: ElementIndex | -1 = -1, idxN3: ElementIndex | -1 = -1, idxC4: ElementIndex | -1 = -1, idxC5: ElementIndex | -1 = -1, idxC6: ElementIndex | -1 = -1, idxN7: ElementIndex | -1 = -1, idxC8: ElementIndex | -1 = -1, idxN9: ElementIndex | -1 = -1
  84. builderState.currentGroup = i
  85. let isPurine = isPurineBase(compId)
  86. let isPyrimidine = isPyrimidineBase(compId)
  87. if (!isPurine && !isPyrimidine) {
  88. // detect Purine or Pyrimidin based on geometry
  89. const idxC4 = atomicIndex.findAtomOnResidue(residueIndex, 'C4')
  90. const idxN9 = atomicIndex.findAtomOnResidue(residueIndex, 'N9')
  91. if (idxC4 !== -1 && idxN9 !== -1 && Vec3.distance(pos(idxC4, pC4), pos(idxN9, pN9)) < 1.6) {
  92. isPurine = true
  93. } else {
  94. isPyrimidine = true
  95. }
  96. }
  97. if (isPurine) {
  98. idxTrace = traceElementIndex[residueIndex]
  99. idxN1 = atomicIndex.findAtomOnResidue(residueIndex, 'N1')
  100. idxC2 = atomicIndex.findAtomOnResidue(residueIndex, 'C2')
  101. idxN3 = atomicIndex.findAtomOnResidue(residueIndex, 'N3')
  102. idxC4 = atomicIndex.findAtomOnResidue(residueIndex, 'C4')
  103. idxC5 = atomicIndex.findAtomOnResidue(residueIndex, 'C5')
  104. if (idxC5 === -1) {
  105. // modified ring, e.g. DP
  106. idxC5 = atomicIndex.findAtomOnResidue(residueIndex, 'N5')
  107. }
  108. idxC6 = atomicIndex.findAtomOnResidue(residueIndex, 'C6')
  109. idxN7 = atomicIndex.findAtomOnResidue(residueIndex, 'N7')
  110. if (idxN7 === -1) {
  111. // modified ring, e.g. DP
  112. idxN7 = atomicIndex.findAtomOnResidue(residueIndex, 'C7')
  113. }
  114. idxC8 = atomicIndex.findAtomOnResidue(residueIndex, 'C8')
  115. idxN9 = atomicIndex.findAtomOnResidue(residueIndex, 'N9')
  116. if (idxN9 !== -1 && idxTrace !== -1) {
  117. pos(idxN9, pN9); pos(idxTrace, pTrace)
  118. builderState.currentGroup = i
  119. addCylinder(builderState, pN9, pTrace, 1, cylinderProps)
  120. addSphere(builderState, pN9, radius, detail)
  121. }
  122. if (idxN1 !== -1 && idxC2 !== -1 && idxN3 !== -1 && idxC4 !== -1 && idxC5 !== -1 && idxC6 !== -1 && idxN7 !== -1 && idxC8 !== -1 && idxN9 !== -1 ) {
  123. pos(idxN1, pN1); pos(idxC2, pC2); pos(idxN3, pN3); pos(idxC4, pC4); pos(idxC5, pC5); pos(idxC6, pC6); pos(idxN7, pN7); pos(idxC8, pC8)
  124. Vec3.triangleNormal(normal, pN1, pC4, pC5)
  125. Vec3.scale(normal, normal, halfThickness)
  126. shiftPositions(positionsRing5_6, normal, pN1, pC2, pN3, pC4, pC5, pC6, pN7, pC8, pN9)
  127. MeshBuilder.addTriangleStrip(builderState, positionsRing5_6, stripIndicesRing5_6)
  128. MeshBuilder.addTriangleFan(builderState, positionsRing5_6, fanIndicesTopRing5_6)
  129. MeshBuilder.addTriangleFan(builderState, positionsRing5_6, fanIndicesBottomRing5_6)
  130. }
  131. } else if (isPyrimidine) {
  132. idxTrace = traceElementIndex[residueIndex]
  133. idxN1 = atomicIndex.findAtomOnResidue(residueIndex, 'N1')
  134. if (idxN1 === -1) {
  135. // modified ring, e.g. DZ
  136. idxN1 = atomicIndex.findAtomOnResidue(residueIndex, 'C1')
  137. }
  138. idxC2 = atomicIndex.findAtomOnResidue(residueIndex, 'C2')
  139. idxN3 = atomicIndex.findAtomOnResidue(residueIndex, 'N3')
  140. idxC4 = atomicIndex.findAtomOnResidue(residueIndex, 'C4')
  141. idxC5 = atomicIndex.findAtomOnResidue(residueIndex, 'C5')
  142. idxC6 = atomicIndex.findAtomOnResidue(residueIndex, 'C6')
  143. if (idxN1 !== -1 && idxTrace !== -1) {
  144. pos(idxN1, pN1); pos(idxTrace, pTrace)
  145. builderState.currentGroup = i
  146. addCylinder(builderState, pN1, pTrace, 1, cylinderProps)
  147. addSphere(builderState, pN1, radius, detail)
  148. }
  149. if (idxN1 !== -1 && idxC2 !== -1 && idxN3 !== -1 && idxC4 !== -1 && idxC5 !== -1 && idxC6 !== -1) {
  150. pos(idxC2, pC2); pos(idxN3, pN3); pos(idxC4, pC4); pos(idxC5, pC5); pos(idxC6, pC6);
  151. Vec3.triangleNormal(normal, pN1, pC4, pC5)
  152. Vec3.scale(normal, normal, halfThickness)
  153. shiftPositions(positionsRing6, normal, pN1, pC2, pN3, pC4, pC5, pC6)
  154. MeshBuilder.addTriangleStrip(builderState, positionsRing6, stripIndicesRing6)
  155. MeshBuilder.addTriangleFan(builderState, positionsRing6, fanIndicesTopRing6)
  156. MeshBuilder.addTriangleFan(builderState, positionsRing6, fanIndicesBottomRing6)
  157. }
  158. }
  159. ++i
  160. }
  161. }
  162. }
  163. return MeshBuilder.getMesh(builderState)
  164. }
  165. export const NucleotideRingParams = {
  166. ...UnitsMeshParams,
  167. ...NucleotideRingMeshParams
  168. }
  169. export type NucleotideRingParams = typeof NucleotideRingParams
  170. export function NucleotideRingVisual(materialId: number): UnitsVisual<NucleotideRingParams> {
  171. return UnitsMeshVisual<NucleotideRingParams>({
  172. defaultProps: PD.getDefaultValues(NucleotideRingParams),
  173. createGeometry: createNucleotideRingMesh,
  174. createLocationIterator: NucleotideLocationIterator.fromGroup,
  175. getLoci: getNucleotideElementLoci,
  176. eachLocation: eachNucleotideElement,
  177. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<NucleotideRingParams>, currentProps: PD.Values<NucleotideRingParams>) => {
  178. state.createGeometry = (
  179. newProps.sizeFactor !== currentProps.sizeFactor ||
  180. newProps.radialSegments !== currentProps.radialSegments
  181. )
  182. }
  183. }, materialId)
  184. }