nucleotide-atomic-bond.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /**
  2. * Copyright (c) 2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Gianluca Tomasello <giagitom@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  8. import { Vec3 } from '../../../mol-math/linear-algebra';
  9. import { VisualContext } from '../../visual';
  10. import { Unit, Structure } 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 } from '../../../mol-model/structure/model/types';
  17. import { addCylinder } from '../../../mol-geo/geometry/mesh/builder/cylinder';
  18. import { UnitsMeshParams, UnitsVisual, UnitsMeshVisual, UnitsCylindersParams, UnitsCylindersVisual } from '../units-visual';
  19. import { NucleotideLocationIterator, getNucleotideElementLoci, eachNucleotideElement, getNucleotideBaseType, createNucleicIndices, setSugarIndices, hasSugarIndices, setPurinIndices, hasPurinIndices, setPyrimidineIndices, hasPyrimidineIndices } from './util/nucleotide';
  20. import { VisualUpdateState } from '../../util';
  21. import { BaseGeometry } from '../../../mol-geo/geometry/base';
  22. import { Sphere3D } from '../../../mol-math/geometry';
  23. import { WebGLContext } from '../../../mol-gl/webgl/context';
  24. import { Cylinders } from '../../../mol-geo/geometry/cylinders/cylinders';
  25. import { CylindersBuilder } from '../../../mol-geo/geometry/cylinders/cylinders-builder';
  26. import { StructureGroup } from './util/common';
  27. const pTrace = Vec3();
  28. const pN1 = Vec3();
  29. const pC2 = Vec3();
  30. const pN3 = Vec3();
  31. const pC4 = Vec3();
  32. const pC5 = Vec3();
  33. const pC6 = Vec3();
  34. const pN7 = Vec3();
  35. const pC8 = Vec3();
  36. const pN9 = Vec3();
  37. const pC1_1 = Vec3();
  38. const pC2_1 = Vec3();
  39. const pC3_1 = Vec3();
  40. const pC4_1 = Vec3();
  41. const pO4_1 = Vec3();
  42. export const NucleotideAtomicBondParams = {
  43. ...UnitsMeshParams,
  44. ...UnitsCylindersParams,
  45. sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
  46. radialSegments: PD.Numeric(16, { min: 2, max: 56, step: 2 }, BaseGeometry.CustomQualityParamInfo),
  47. tryUseImpostor: PD.Boolean(true)
  48. };
  49. export type NucleotideAtomicBondParams = typeof NucleotideAtomicBondParams
  50. interface NucleotideAtomicBondImpostorProps {
  51. sizeFactor: number,
  52. }
  53. export function NucleotideAtomicBondVisual(materialId: number, structure: Structure, props: PD.Values<NucleotideAtomicBondParams>, webgl?: WebGLContext) {
  54. return props.tryUseImpostor && webgl && webgl.extensions.fragDepth
  55. ? NucleotideAtomicBondImpostorVisual(materialId)
  56. : NucleotideAtomicBondMeshVisual(materialId);
  57. }
  58. function createNucleotideAtomicBondImpostor(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: NucleotideAtomicBondImpostorProps, cylinders?: Cylinders) {
  59. if (!Unit.isAtomic(unit)) return Cylinders.createEmpty(cylinders);
  60. const nucleotideElementCount = unit.nucleotideElements.length;
  61. if (!nucleotideElementCount) return Cylinders.createEmpty(cylinders);
  62. const cylindersCountEstimate = nucleotideElementCount * 15; // 15 is the average purine (17) & pirimidine (13) bonds
  63. const builder = CylindersBuilder.create(cylindersCountEstimate, cylindersCountEstimate / 4, cylinders);
  64. const { elements, model } = unit;
  65. const { chainAtomSegments, residueAtomSegments } = model.atomicHierarchy;
  66. const { moleculeType } = model.atomicHierarchy.derived.residue;
  67. const pos = unit.conformation.invariantPosition;
  68. const chainIt = Segmentation.transientSegments(chainAtomSegments, elements);
  69. const residueIt = Segmentation.transientSegments(residueAtomSegments, elements);
  70. let i = 0;
  71. while (chainIt.hasNext) {
  72. residueIt.setSegment(chainIt.move());
  73. while (residueIt.hasNext) {
  74. const { index: residueIndex } = residueIt.move();
  75. if (isNucleic(moleculeType[residueIndex])) {
  76. const idx = createNucleicIndices();
  77. setSugarIndices(idx, unit, residueIndex);
  78. if (hasSugarIndices(idx)) {
  79. pos(idx.C1_1, pC1_1); pos(idx.C2_1, pC2_1); pos(idx.C3_1, pC3_1); pos(idx.C4_1, pC4_1); pos(idx.O4_1, pO4_1);
  80. // trace cylinder
  81. pos(idx.trace, pTrace);
  82. builder.add(pC3_1[0], pC3_1[1], pC3_1[2], pTrace[0], pTrace[1], pTrace[2], 1, true, true, i);
  83. // sugar ring
  84. builder.add(pC3_1[0], pC3_1[1], pC3_1[2], pC4_1[0], pC4_1[1], pC4_1[2], 1, true, true, i);
  85. builder.add(pC4_1[0], pC4_1[1], pC4_1[2], pO4_1[0], pO4_1[1], pO4_1[2], 1, true, true, i);
  86. builder.add(pO4_1[0], pO4_1[1], pO4_1[2], pC1_1[0], pC1_1[1], pC1_1[2], 1, true, true, i);
  87. builder.add(pC1_1[0], pC1_1[1], pC1_1[2], pC2_1[0], pC2_1[1], pC2_1[2], 1, true, true, i);
  88. builder.add(pC2_1[0], pC2_1[1], pC2_1[2], pC3_1[0], pC3_1[1], pC3_1[2], 1, true, true, i);
  89. }
  90. const { isPurine, isPyrimidine } = getNucleotideBaseType(unit, residueIndex);
  91. if (isPurine) {
  92. setPurinIndices(idx, unit, residueIndex);
  93. if (idx.C1_1 !== -1 && idx.N9 !== -1) {
  94. pos(idx.C1_1, pC1_1); pos(idx.N9, pN9);
  95. builder.add(pN9[0], pN9[1], pN9[2], pC1_1[0], pC1_1[1], pC1_1[2], 1, true, true, i);
  96. } else if (idx.N9 !== -1 && idx.trace !== -1) {
  97. pos(idx.N9, pN9); pos(idx.trace, pTrace);
  98. builder.add(pN9[0], pN9[1], pN9[2], pTrace[0], pTrace[1], pTrace[2], 1, true, true, i);
  99. }
  100. if (hasPurinIndices(idx)) {
  101. pos(idx.N1, pN1); pos(idx.C2, pC2); pos(idx.N3, pN3); pos(idx.C4, pC4); pos(idx.C5, pC5); pos(idx.C6, pC6); pos(idx.N7, pN7); pos(idx.C8, pC8); pos(idx.N9, pN9);
  102. // base ring
  103. builder.add(pN9[0], pN9[1], pN9[2], pC8[0], pC8[1], pC8[2], 1, true, true, i);
  104. builder.add(pC8[0], pC8[1], pC8[2], pN7[0], pN7[1], pN7[2], 1, true, true, i);
  105. builder.add(pN7[0], pN7[1], pN7[2], pC5[0], pC5[1], pC5[2], 1, true, true, i);
  106. builder.add(pC5[0], pC5[1], pC5[2], pC6[0], pC6[1], pC6[2], 1, true, true, i);
  107. builder.add(pC6[0], pC6[1], pC6[2], pN1[0], pN1[1], pN1[2], 1, true, true, i);
  108. builder.add(pN1[0], pN1[1], pN1[2], pC2[0], pC2[1], pC2[2], 1, true, true, i);
  109. builder.add(pC2[0], pC2[1], pC2[2], pN3[0], pN3[1], pN3[2], 1, true, true, i);
  110. builder.add(pN3[0], pN3[1], pN3[2], pC4[0], pC4[1], pC4[2], 1, true, true, i);
  111. builder.add(pC4[0], pC4[1], pC4[2], pC5[0], pC5[1], pC5[2], 1, true, true, i);
  112. builder.add(pC4[0], pC4[1], pC4[2], pN9[0], pN9[1], pN9[2], 1, true, true, i);
  113. }
  114. } else if (isPyrimidine) {
  115. setPyrimidineIndices(idx, unit, residueIndex);
  116. if (idx.C1_1 !== -1 && idx.N1 !== -1) {
  117. pos(idx.N1, pN1); pos(idx.C1_1, pC1_1);
  118. builder.add(pN1[0], pN1[1], pN1[2], pC1_1[0], pC1_1[1], pC1_1[2], 1, true, true, i);
  119. } else if (idx.N1 !== -1 && idx.trace !== -1) {
  120. pos(idx.N1, pN1); pos(idx.trace, pTrace);
  121. builder.add(pN1[0], pN1[1], pN1[2], pTrace[0], pTrace[1], pTrace[2], 1, true, true, i);
  122. }
  123. if (hasPyrimidineIndices(idx)) {
  124. pos(idx.N1, pN1); pos(idx.C2, pC2); pos(idx.N3, pN3); pos(idx.C4, pC4); pos(idx.C5, pC5); pos(idx.C6, pC6);
  125. // base ring
  126. builder.add(pN1[0], pN1[1], pN1[2], pC6[0], pC6[1], pC6[2], 1, true, true, i);
  127. builder.add(pC6[0], pC6[1], pC6[2], pC5[0], pC5[1], pC5[2], 1, true, true, i);
  128. builder.add(pC5[0], pC5[1], pC5[2], pC4[0], pC4[1], pC4[2], 1, true, true, i);
  129. builder.add(pC4[0], pC4[1], pC4[2], pN3[0], pN3[1], pN3[2], 1, true, true, i);
  130. builder.add(pN3[0], pN3[1], pN3[2], pC2[0], pC2[1], pC2[2], 1, true, true, i);
  131. builder.add(pC2[0], pC2[1], pC2[2], pN1[0], pN1[1], pN1[2], 1, true, true, i);
  132. }
  133. }
  134. ++i;
  135. }
  136. }
  137. }
  138. const c = builder.getCylinders();
  139. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor);
  140. c.setBoundingSphere(sphere);
  141. return c;
  142. }
  143. export function NucleotideAtomicBondImpostorVisual(materialId: number): UnitsVisual<NucleotideAtomicBondParams> {
  144. return UnitsCylindersVisual<NucleotideAtomicBondParams>({
  145. defaultProps: PD.getDefaultValues(NucleotideAtomicBondParams),
  146. createGeometry: createNucleotideAtomicBondImpostor,
  147. createLocationIterator: NucleotideLocationIterator.fromGroup,
  148. getLoci: getNucleotideElementLoci,
  149. eachLocation: eachNucleotideElement,
  150. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<NucleotideAtomicBondParams>, currentProps: PD.Values<NucleotideAtomicBondParams>) => {
  151. state.createGeometry = (
  152. newProps.sizeFactor !== currentProps.sizeFactor
  153. );
  154. },
  155. mustRecreate: (structureGroup: StructureGroup, props: PD.Values<NucleotideAtomicBondParams>, webgl?: WebGLContext) => {
  156. return !props.tryUseImpostor || !webgl;
  157. }
  158. }, materialId);
  159. }
  160. interface NucleotideAtomicBondMeshProps {
  161. radialSegments: number,
  162. sizeFactor: number,
  163. }
  164. function createNucleotideAtomicBondMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: NucleotideAtomicBondMeshProps, mesh?: Mesh) {
  165. if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh);
  166. const nucleotideElementCount = unit.nucleotideElements.length;
  167. if (!nucleotideElementCount) return Mesh.createEmpty(mesh);
  168. const { sizeFactor, radialSegments } = props;
  169. const vertexCount = nucleotideElementCount * (radialSegments * 15); // 15 is the average purine (17) & pirimidine (13) bonds
  170. const builderState = MeshBuilder.createState(vertexCount, vertexCount / 4, mesh);
  171. const { elements, model } = unit;
  172. const { chainAtomSegments, residueAtomSegments } = model.atomicHierarchy;
  173. const { moleculeType } = model.atomicHierarchy.derived.residue;
  174. const pos = unit.conformation.invariantPosition;
  175. const chainIt = Segmentation.transientSegments(chainAtomSegments, elements);
  176. const residueIt = Segmentation.transientSegments(residueAtomSegments, elements);
  177. const cylinderProps: CylinderProps = { radiusTop: 1 * sizeFactor, radiusBottom: 1 * sizeFactor, radialSegments };
  178. let i = 0;
  179. while (chainIt.hasNext) {
  180. residueIt.setSegment(chainIt.move());
  181. while (residueIt.hasNext) {
  182. const { index: residueIndex } = residueIt.move();
  183. if (isNucleic(moleculeType[residueIndex])) {
  184. const idx = createNucleicIndices();
  185. builderState.currentGroup = i;
  186. setSugarIndices(idx, unit, residueIndex);
  187. if (hasSugarIndices(idx)) {
  188. pos(idx.C1_1, pC1_1); pos(idx.C2_1, pC2_1); pos(idx.C3_1, pC3_1); pos(idx.C4_1, pC4_1); pos(idx.O4_1, pO4_1);
  189. // trace cylinder
  190. pos(idx.trace, pTrace);
  191. addCylinder(builderState, pC3_1, pTrace, 1, cylinderProps);
  192. // sugar ring
  193. addCylinder(builderState, pC3_1, pC4_1, 1, cylinderProps);
  194. addCylinder(builderState, pC4_1, pO4_1, 1, cylinderProps);
  195. addCylinder(builderState, pO4_1, pC1_1, 1, cylinderProps);
  196. addCylinder(builderState, pC1_1, pC2_1, 1, cylinderProps);
  197. addCylinder(builderState, pC2_1, pC3_1, 1, cylinderProps);
  198. }
  199. const { isPurine, isPyrimidine } = getNucleotideBaseType(unit, residueIndex);
  200. if (isPurine) {
  201. setPurinIndices(idx, unit, residueIndex);
  202. if (idx.C1_1 !== -1 && idx.N9 !== -1) {
  203. pos(idx.C1_1, pC1_1); pos(idx.N9, pN9);
  204. addCylinder(builderState, pN9, pC1_1, 1, cylinderProps);
  205. } else if (idx.N9 !== -1 && idx.trace !== -1) {
  206. pos(idx.N9, pN9); pos(idx.trace, pTrace);
  207. addCylinder(builderState, pN9, pTrace, 1, cylinderProps);
  208. }
  209. if (hasPurinIndices(idx)) {
  210. pos(idx.N1, pN1); pos(idx.C2, pC2); pos(idx.N3, pN3); pos(idx.C4, pC4); pos(idx.C5, pC5); pos(idx.C6, pC6); pos(idx.N7, pN7); pos(idx.C8, pC8); pos(idx.N9, pN9);
  211. // base ring
  212. addCylinder(builderState, pN9, pC8, 1, cylinderProps);
  213. addCylinder(builderState, pC8, pN7, 1, cylinderProps);
  214. addCylinder(builderState, pN7, pC5, 1, cylinderProps);
  215. addCylinder(builderState, pC5, pC6, 1, cylinderProps);
  216. addCylinder(builderState, pC6, pN1, 1, cylinderProps);
  217. addCylinder(builderState, pN1, pC2, 1, cylinderProps);
  218. addCylinder(builderState, pC2, pN3, 1, cylinderProps);
  219. addCylinder(builderState, pN3, pC4, 1, cylinderProps);
  220. addCylinder(builderState, pC4, pC5, 1, cylinderProps);
  221. addCylinder(builderState, pC4, pN9, 1, cylinderProps);
  222. }
  223. } else if (isPyrimidine) {
  224. setPyrimidineIndices(idx, unit, residueIndex);
  225. if (idx.C1_1 !== -1 && idx.N1 !== -1) {
  226. pos(idx.N1, pN1); pos(idx.C1_1, pC1_1);
  227. addCylinder(builderState, pN1, pC1_1, 1, cylinderProps);
  228. } else if (idx.N1 !== -1 && idx.trace !== -1) {
  229. pos(idx.N1, pN1); pos(idx.trace, pTrace);
  230. addCylinder(builderState, pN1, pTrace, 1, cylinderProps);
  231. }
  232. if (hasPyrimidineIndices(idx)) {
  233. pos(idx.N1, pN1); pos(idx.C2, pC2); pos(idx.N3, pN3); pos(idx.C4, pC4); pos(idx.C5, pC5); pos(idx.C6, pC6);
  234. // base ring
  235. addCylinder(builderState, pN1, pC6, 1, cylinderProps);
  236. addCylinder(builderState, pC6, pC5, 1, cylinderProps);
  237. addCylinder(builderState, pC5, pC4, 1, cylinderProps);
  238. addCylinder(builderState, pC4, pN3, 1, cylinderProps);
  239. addCylinder(builderState, pN3, pC2, 1, cylinderProps);
  240. addCylinder(builderState, pC2, pN1, 1, cylinderProps);
  241. }
  242. }
  243. ++i;
  244. }
  245. }
  246. }
  247. const m = MeshBuilder.getMesh(builderState);
  248. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor);
  249. m.setBoundingSphere(sphere);
  250. return m;
  251. }
  252. export function NucleotideAtomicBondMeshVisual(materialId: number): UnitsVisual<NucleotideAtomicBondParams> {
  253. return UnitsMeshVisual<NucleotideAtomicBondParams>({
  254. defaultProps: PD.getDefaultValues(NucleotideAtomicBondParams),
  255. createGeometry: createNucleotideAtomicBondMesh,
  256. createLocationIterator: NucleotideLocationIterator.fromGroup,
  257. getLoci: getNucleotideElementLoci,
  258. eachLocation: eachNucleotideElement,
  259. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<NucleotideAtomicBondParams>, currentProps: PD.Values<NucleotideAtomicBondParams>) => {
  260. state.createGeometry = (
  261. newProps.sizeFactor !== currentProps.sizeFactor ||
  262. newProps.radialSegments !== currentProps.radialSegments
  263. );
  264. },
  265. mustRecreate: (structureGroup: StructureGroup, props: PD.Values<NucleotideAtomicBondParams>, webgl?: WebGLContext) => {
  266. return props.tryUseImpostor && !!webgl;
  267. }
  268. }, materialId);
  269. }