nucleotide-block-mesh.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 { ValueCell } from 'mol-util/value-cell'
  7. import { createMeshRenderObject, MeshRenderObject } from 'mol-gl/render-object'
  8. import { Unit } from 'mol-model/structure';
  9. import { DefaultStructureProps, UnitsVisual } from '..';
  10. import { RuntimeContext } from 'mol-task'
  11. import { createTransforms, createColors } from './util/common';
  12. import { deepEqual } from 'mol-util';
  13. import { MeshValues } from 'mol-gl/renderable';
  14. import { getMeshData } from '../../../util/mesh-data';
  15. import { Mesh } from '../../../shape/mesh';
  16. import { PickingId } from '../../../util/picking';
  17. import { createMarkers, MarkerAction } from '../../../util/marker-data';
  18. import { Loci } from 'mol-model/loci';
  19. import { SizeTheme } from '../../../theme';
  20. import { createMeshValues, updateMeshValues, updateRenderableState, createRenderableState, DefaultMeshProps } from '../../util';
  21. import { MeshBuilder } from '../../../shape/mesh-builder';
  22. import { getElementLoci, markElement } from './util/element';
  23. import { Vec3, Mat4 } from 'mol-math/linear-algebra';
  24. import { Segmentation, SortedArray } from 'mol-data/int';
  25. import { MoleculeType, isNucleic, isPurinBase, isPyrimidineBase } from 'mol-model/structure/model/types';
  26. import { getElementIndexForAtomId, getElementIndexForAtomRole } from 'mol-model/structure/util';
  27. import { StructureElementIterator } from './util/location-iterator';
  28. const p1 = Vec3.zero()
  29. const p2 = Vec3.zero()
  30. const p3 = Vec3.zero()
  31. const p4 = Vec3.zero()
  32. const p5 = Vec3.zero()
  33. const p6 = Vec3.zero()
  34. const v12 = Vec3.zero()
  35. const v34 = Vec3.zero()
  36. const vC = Vec3.zero()
  37. const center = Vec3.zero()
  38. const t = Mat4.identity()
  39. const sVec = Vec3.zero()
  40. async function createNucleotideBlockMesh(ctx: RuntimeContext, unit: Unit, mesh?: Mesh) {
  41. if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh)
  42. const builder = MeshBuilder.create(256, 128, mesh)
  43. const { elements, model } = unit
  44. const { chemicalComponentMap, modifiedResidues } = model.properties
  45. const { chainAtomSegments, residueAtomSegments, residues } = model.atomicHierarchy
  46. const { label_comp_id } = residues
  47. const pos = unit.conformation.invariantPosition
  48. const chainIt = Segmentation.transientSegments(chainAtomSegments, elements)
  49. const residueIt = Segmentation.transientSegments(residueAtomSegments, elements)
  50. let i = 0
  51. while (chainIt.hasNext) {
  52. residueIt.setSegment(chainIt.move());
  53. while (residueIt.hasNext) {
  54. const { index: residueIndex } = residueIt.move();
  55. const cc = chemicalComponentMap.get(label_comp_id.value(residueIndex))
  56. const moleculeType = cc ? cc.moleculeType : MoleculeType.unknown
  57. if (isNucleic(moleculeType)) {
  58. let compId = label_comp_id.value(residueIndex)
  59. const parentId = modifiedResidues.parentId.get(compId)
  60. if (parentId !== undefined) compId = parentId
  61. let idx1 = -1, idx2 = -1, idx3 = -1, idx4 = -1, idx5 = -1, idx6 = -1
  62. let width = 4.5, height = 4.5, depth = 0.5
  63. if (isPurinBase(compId)) {
  64. height = 4.5
  65. idx1 = getElementIndexForAtomId(model, residueIndex, 'N1')
  66. idx2 = getElementIndexForAtomId(model, residueIndex, 'C4')
  67. idx3 = getElementIndexForAtomId(model, residueIndex, 'C6')
  68. idx4 = getElementIndexForAtomId(model, residueIndex, 'C2')
  69. idx5 = getElementIndexForAtomId(model, residueIndex, 'N9')
  70. idx6 = getElementIndexForAtomRole(model, residueIndex, 'trace')
  71. } else if (isPyrimidineBase(compId)) {
  72. height = 3.0
  73. idx1 = getElementIndexForAtomId(model, residueIndex, 'N3')
  74. idx2 = getElementIndexForAtomId(model, residueIndex, 'C6')
  75. idx3 = getElementIndexForAtomId(model, residueIndex, 'C4')
  76. idx4 = getElementIndexForAtomId(model, residueIndex, 'C2')
  77. idx5 = getElementIndexForAtomId(model, residueIndex, 'N1')
  78. idx6 = getElementIndexForAtomRole(model, residueIndex, 'trace')
  79. }
  80. if (idx1 !== -1 && idx2 !== -1 && idx3 !== -1 && idx4 !== -1 && idx5 !== -1 && idx6 !== -1) {
  81. pos(idx1, p1); pos(idx2, p2); pos(idx3, p3); pos(idx4, p4); pos(idx5, p5); pos(idx6, p6)
  82. Vec3.normalize(v12, Vec3.sub(v12, p2, p1))
  83. Vec3.normalize(v34, Vec3.sub(v34, p4, p3))
  84. Vec3.normalize(vC, Vec3.cross(vC, v12, v34))
  85. Mat4.targetTo(t, p1, p2, vC)
  86. Vec3.scaleAndAdd(center, p1, v12, height / 2 - 0.2)
  87. Mat4.scale(t, t, Vec3.set(sVec, width, depth, height))
  88. Mat4.setTranslation(t, center)
  89. builder.setId(SortedArray.findPredecessorIndex(elements, idx6))
  90. builder.addBox(t)
  91. builder.addCylinder(p5, p6, 1, { radiusTop: 0.2, radiusBottom: 0.2 })
  92. }
  93. }
  94. if (i % 10000 === 0 && ctx.shouldUpdate) {
  95. await ctx.update({ message: 'Gap mesh', current: i });
  96. }
  97. ++i
  98. }
  99. }
  100. return builder.getMesh()
  101. }
  102. export const DefaultNucleotideBlockProps = {
  103. ...DefaultMeshProps,
  104. ...DefaultStructureProps,
  105. sizeTheme: { name: 'physical', factor: 1 } as SizeTheme,
  106. detail: 0,
  107. unitKinds: [ Unit.Kind.Atomic, Unit.Kind.Spheres ] as Unit.Kind[]
  108. }
  109. export type NucleotideBlockProps = Partial<typeof DefaultNucleotideBlockProps>
  110. export function NucleotideBlockVisual(): UnitsVisual<NucleotideBlockProps> {
  111. let renderObject: MeshRenderObject
  112. let currentProps: typeof DefaultNucleotideBlockProps
  113. let mesh: Mesh
  114. let currentGroup: Unit.SymmetryGroup
  115. return {
  116. get renderObject () { return renderObject },
  117. async create(ctx: RuntimeContext, group: Unit.SymmetryGroup, props: NucleotideBlockProps = {}) {
  118. currentProps = Object.assign({}, DefaultNucleotideBlockProps, props)
  119. currentGroup = group
  120. const { colorTheme, unitKinds } = { ...DefaultNucleotideBlockProps, ...props }
  121. const instanceCount = group.units.length
  122. const elementCount = group.elements.length
  123. const unit = group.units[0]
  124. mesh = unitKinds.includes(unit.kind)
  125. ? await createNucleotideBlockMesh(ctx, unit, mesh)
  126. : Mesh.createEmpty(mesh)
  127. // console.log(mesh)
  128. const transforms = createTransforms(group)
  129. const color = createColors(StructureElementIterator.fromGroup(group), colorTheme)
  130. const marker = createMarkers(instanceCount * elementCount)
  131. const counts = { drawCount: mesh.triangleCount * 3, elementCount, instanceCount }
  132. const values: MeshValues = {
  133. ...getMeshData(mesh),
  134. ...color,
  135. ...marker,
  136. aTransform: transforms,
  137. elements: mesh.indexBuffer,
  138. ...createMeshValues(currentProps, counts),
  139. aColor: ValueCell.create(new Float32Array(mesh.vertexCount * 3))
  140. }
  141. const state = createRenderableState(currentProps)
  142. renderObject = createMeshRenderObject(values, state)
  143. },
  144. async update(ctx: RuntimeContext, props: NucleotideBlockProps) {
  145. const newProps = Object.assign({}, currentProps, props)
  146. if (!renderObject) return false
  147. let updateColor = false
  148. if (newProps.detail !== currentProps.detail) {
  149. const unit = currentGroup.units[0]
  150. mesh = await createNucleotideBlockMesh(ctx, unit, mesh)
  151. ValueCell.update(renderObject.values.drawCount, mesh.triangleCount * 3)
  152. updateColor = true
  153. }
  154. if (!deepEqual(newProps.colorTheme, currentProps.colorTheme)) {
  155. updateColor = true
  156. }
  157. if (updateColor) {
  158. if (ctx.shouldUpdate) await ctx.update('Computing nucleotide block colors');
  159. createColors(StructureElementIterator.fromGroup(currentGroup), newProps.colorTheme, renderObject.values)
  160. }
  161. updateMeshValues(renderObject.values, newProps)
  162. updateRenderableState(renderObject.state, newProps)
  163. currentProps = newProps
  164. return true
  165. },
  166. getLoci(pickingId: PickingId) {
  167. return getElementLoci(renderObject.id, currentGroup, pickingId)
  168. },
  169. mark(loci: Loci, action: MarkerAction) {
  170. markElement(renderObject.values.tMarker, currentGroup, loci, action)
  171. },
  172. destroy() {
  173. // TODO
  174. }
  175. }
  176. }