index.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 './index.html'
  7. import Viewer from 'mol-view/viewer';
  8. import CIF, { CifBlock } from 'mol-io/reader/cif'
  9. // import { parse as parseObj } from 'mol-io/reader/obj/parser'
  10. import { readUrlAs } from 'mol-util/read'
  11. import { Model, Format, Structure, StructureSymmetry } from 'mol-model/structure';
  12. import { CartoonRepresentation } from 'mol-geo/representation/structure/representation/cartoon';
  13. import { BallAndStickRepresentation } from 'mol-geo/representation/structure/representation/ball-and-stick';
  14. import { EveryLoci } from 'mol-model/loci';
  15. import { MarkerAction } from 'mol-geo/util/marker-data';
  16. import { labelFirst } from 'mol-view/label';
  17. import { Queries as Q, StructureProperties as SP, StructureSelection, StructureQuery } from 'mol-model/structure';
  18. import { MeshBuilder } from 'mol-geo/mesh/mesh-builder';
  19. import { ShapeRepresentation } from 'mol-geo/representation/shape';
  20. import { Vec3, Mat4 } from 'mol-math/linear-algebra';
  21. import { Shape } from 'mol-model/shape';
  22. import { Color } from 'mol-util/color';
  23. import { addSphere } from 'mol-geo/mesh/builder/sphere';
  24. import { Box } from 'mol-geo/primitive/box';
  25. const container = document.getElementById('container')
  26. if (!container) throw new Error('Can not find element with id "container".')
  27. const canvas = document.getElementById('canvas') as HTMLCanvasElement
  28. if (!canvas) throw new Error('Can not find element with id "canvas".')
  29. const info = document.getElementById('info') as HTMLCanvasElement
  30. if (!info) throw new Error('Can not find element with id "info".')
  31. const viewer = Viewer.create(canvas, container)
  32. viewer.animate()
  33. viewer.input.resize.subscribe(() => {
  34. // do whatever appropriate
  35. })
  36. viewer.input.move.subscribe(({x, y, inside, buttons}) => {
  37. if (!inside || buttons) return
  38. const p = viewer.identify(x, y)
  39. const loci = viewer.getLoci(p)
  40. viewer.mark(EveryLoci, MarkerAction.RemoveHighlight)
  41. viewer.mark(loci, MarkerAction.Highlight)
  42. const label = labelFirst(loci)
  43. info.innerText = `${label}`
  44. })
  45. // async function getObjFromUrl(url: string) {
  46. // const data = await readUrlAs(url, false) as string
  47. // const comp = parseObj(data)
  48. // const parsed = await comp.run()
  49. // if (parsed.isError) throw parsed
  50. // return parsed.result
  51. // }
  52. async function getCifFromUrl(url: string) {
  53. const data = await readUrlAs(url, false)
  54. const comp = CIF.parse(data)
  55. const parsed = await comp.run()
  56. if (parsed.isError) throw parsed
  57. return parsed.result.blocks[0]
  58. }
  59. async function getModelFromMmcif(cif: CifBlock) {
  60. const models = await Model.create(Format.mmCIF(cif)).run()
  61. return models[0]
  62. }
  63. async function getStructureFromModel(model: Model, assembly = '1') {
  64. const assemblies = model.symmetry.assemblies
  65. if (assemblies.length) {
  66. return await StructureSymmetry.buildAssembly(Structure.ofModel(model), assembly).run()
  67. } else {
  68. return Structure.ofModel(model)
  69. }
  70. }
  71. async function init() {
  72. const cif = await getCifFromUrl('https://files.rcsb.org/download/1crn.cif')
  73. const model = await getModelFromMmcif(cif)
  74. const structure = await getStructureFromModel(model)
  75. viewer.center(structure.boundary.sphere.center)
  76. // cartoon for whole structure
  77. const cartoonRepr = CartoonRepresentation()
  78. await cartoonRepr.create(structure, {
  79. colorTheme: { name: 'chain-id' },
  80. sizeTheme: { name: 'uniform', value: 0.2 },
  81. useFog: false // TODO fog not working properly
  82. }).run()
  83. viewer.add(cartoonRepr)
  84. // create new structure via query
  85. const q1 = Q.generators.atoms({
  86. residueTest: qtx => SP.residue.label_seq_id(qtx.element) < 7
  87. });
  88. const newStructure = StructureSelection.unionStructure(await StructureQuery.run(q1, structure));
  89. // ball+stick for new structure
  90. const ballStickRepr = BallAndStickRepresentation()
  91. await ballStickRepr.create(newStructure, {
  92. colorTheme: { name: 'element-symbol' },
  93. sizeTheme: { name: 'uniform', value: 0.1 },
  94. useFog: false // TODO fog not working properly
  95. }).run()
  96. viewer.add(ballStickRepr)
  97. // create a mesh
  98. const meshBuilder = MeshBuilder.create(256, 128)
  99. const colors: Color[] = []
  100. const labels: string[] = []
  101. // red sphere
  102. meshBuilder.setGroup(0)
  103. colors[0] = Color(0xFF2233)
  104. labels[0] = 'red sphere'
  105. addSphere(meshBuilder, Vec3.create(0, 0, 0), 4, 2)
  106. // green cube
  107. meshBuilder.setGroup(1)
  108. colors[1] = Color(0x2233FF)
  109. labels[1] = 'blue cube'
  110. const t = Mat4.identity()
  111. Mat4.fromTranslation(t, Vec3.create(10, 0, 0))
  112. Mat4.scale(t, t, Vec3.create(3, 3, 3))
  113. meshBuilder.add(t, Box())
  114. const mesh = meshBuilder.getMesh()
  115. // const mesh = getObjFromUrl('mesh.obj')
  116. // create shape from mesh
  117. const shape = Shape.create('myShape', mesh, colors, labels)
  118. // add representation from shape
  119. const customRepr = ShapeRepresentation()
  120. await customRepr.create(shape, {
  121. colorTheme: { name: 'shape-group' },
  122. // colorTheme: { name: 'uniform', value: Color(0xFFCC22) },
  123. useFog: false // TODO fog not working properly
  124. }).run()
  125. viewer.add(customRepr)
  126. // ensure the added representations get rendered, i.e. without mouse input
  127. viewer.requestDraw()
  128. }
  129. init()