nucleotide-block-mesh.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 } from 'mol-model/structure';
  7. import { UnitsVisual } from '..';
  8. import { RuntimeContext } from 'mol-task'
  9. import { Mesh } from '../../../geometry/mesh/mesh';
  10. import { MeshBuilder } from '../../../geometry/mesh/mesh-builder';
  11. import { Vec3, Mat4 } from 'mol-math/linear-algebra';
  12. import { Segmentation } from 'mol-data/int';
  13. import { MoleculeType, isNucleic, isPurinBase, isPyrimidineBase } from 'mol-model/structure/model/types';
  14. import { getElementIndexForAtomId, getElementIndexForAtomRole } from 'mol-model/structure/util';
  15. import { UnitsMeshVisual, UnitsMeshParams } from '../units-visual';
  16. import { addCylinder } from '../../../geometry/mesh/builder/cylinder';
  17. import { Box } from '../../../primitive/box';
  18. import { NucleotideLocationIterator, markNucleotideElement, getNucleotideElementLoci } from './util/nucleotide';
  19. import { paramDefaultValues } from 'mol-util/parameter';
  20. const p1 = Vec3.zero()
  21. const p2 = Vec3.zero()
  22. const p3 = Vec3.zero()
  23. const p4 = Vec3.zero()
  24. const p5 = Vec3.zero()
  25. const p6 = Vec3.zero()
  26. const v12 = Vec3.zero()
  27. const v34 = Vec3.zero()
  28. const vC = Vec3.zero()
  29. const center = Vec3.zero()
  30. const t = Mat4.identity()
  31. const sVec = Vec3.zero()
  32. const box = Box()
  33. // TODO define props, should be scalable
  34. async function createNucleotideBlockMesh(ctx: RuntimeContext, unit: Unit, structure: Structure, props: {}, mesh?: Mesh) {
  35. if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh)
  36. // TODO better vertex count estimate
  37. const builder = MeshBuilder.create(256, 128, mesh)
  38. const { elements, model } = unit
  39. const { chemicalComponentMap, modifiedResidues } = model.properties
  40. const { chainAtomSegments, residueAtomSegments, residues } = model.atomicHierarchy
  41. const { label_comp_id } = residues
  42. const pos = unit.conformation.invariantPosition
  43. const chainIt = Segmentation.transientSegments(chainAtomSegments, elements)
  44. const residueIt = Segmentation.transientSegments(residueAtomSegments, elements)
  45. let i = 0
  46. while (chainIt.hasNext) {
  47. residueIt.setSegment(chainIt.move());
  48. while (residueIt.hasNext) {
  49. const { index: residueIndex } = residueIt.move();
  50. let compId = label_comp_id.value(residueIndex)
  51. const cc = chemicalComponentMap.get(compId)
  52. const moleculeType = cc ? cc.moleculeType : MoleculeType.unknown
  53. if (isNucleic(moleculeType)) {
  54. const parentId = modifiedResidues.parentId.get(compId)
  55. if (parentId !== undefined) compId = parentId
  56. let idx1 = -1, idx2 = -1, idx3 = -1, idx4 = -1, idx5 = -1, idx6 = -1
  57. let width = 4.5, height = 4.5, depth = 0.5
  58. if (isPurinBase(compId)) {
  59. height = 4.5
  60. idx1 = getElementIndexForAtomId(model, residueIndex, 'N1')
  61. idx2 = getElementIndexForAtomId(model, residueIndex, 'C4')
  62. idx3 = getElementIndexForAtomId(model, residueIndex, 'C6')
  63. idx4 = getElementIndexForAtomId(model, residueIndex, 'C2')
  64. idx5 = getElementIndexForAtomId(model, residueIndex, 'N9')
  65. idx6 = getElementIndexForAtomRole(model, residueIndex, 'trace')
  66. } else if (isPyrimidineBase(compId)) {
  67. height = 3.0
  68. idx1 = getElementIndexForAtomId(model, residueIndex, 'N3')
  69. idx2 = getElementIndexForAtomId(model, residueIndex, 'C6')
  70. idx3 = getElementIndexForAtomId(model, residueIndex, 'C4')
  71. idx4 = getElementIndexForAtomId(model, residueIndex, 'C2')
  72. idx5 = getElementIndexForAtomId(model, residueIndex, 'N1')
  73. idx6 = getElementIndexForAtomRole(model, residueIndex, 'trace')
  74. }
  75. if (idx5 !== -1 && idx6 !== -1) {
  76. pos(idx5, p5); pos(idx6, p6)
  77. builder.setGroup(i)
  78. addCylinder(builder, p5, p6, 1, { radiusTop: 0.2, radiusBottom: 0.2 })
  79. if (idx1 !== -1 && idx2 !== -1 && idx3 !== -1 && idx4 !== -1) {
  80. pos(idx1, p1); pos(idx2, p2); pos(idx3, p3); pos(idx4, p4);
  81. Vec3.normalize(v12, Vec3.sub(v12, p2, p1))
  82. Vec3.normalize(v34, Vec3.sub(v34, p4, p3))
  83. Vec3.normalize(vC, Vec3.cross(vC, v12, v34))
  84. Mat4.targetTo(t, p1, p2, vC)
  85. Vec3.scaleAndAdd(center, p1, v12, height / 2 - 0.2)
  86. Mat4.scale(t, t, Vec3.set(sVec, width, depth, height))
  87. Mat4.setTranslation(t, center)
  88. builder.add(t, box)
  89. }
  90. }
  91. if (i % 10000 === 0 && ctx.shouldUpdate) {
  92. await ctx.update({ message: 'Nucleotide block mesh', current: i });
  93. }
  94. ++i
  95. }
  96. }
  97. }
  98. return builder.getMesh()
  99. }
  100. export const NucleotideBlockParams = {
  101. ...UnitsMeshParams
  102. }
  103. export const DefaultNucleotideBlockProps = paramDefaultValues(NucleotideBlockParams)
  104. export type NucleotideBlockProps = typeof DefaultNucleotideBlockProps
  105. export function NucleotideBlockVisual(): UnitsVisual<NucleotideBlockProps> {
  106. return UnitsMeshVisual<NucleotideBlockProps>({
  107. defaultProps: DefaultNucleotideBlockProps,
  108. createGeometry: createNucleotideBlockMesh,
  109. createLocationIterator: NucleotideLocationIterator.fromGroup,
  110. getLoci: getNucleotideElementLoci,
  111. mark: markNucleotideElement,
  112. setUpdateState: () => {}
  113. })
  114. }