render-structure.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 './index.html'
  7. import { Canvas3D } from 'mol-canvas3d/canvas3d';
  8. import CIF, { CifFrame } from 'mol-io/reader/cif'
  9. import { Model, Structure } from 'mol-model/structure';
  10. import { ColorTheme } from 'mol-theme/color';
  11. import { SizeTheme } from 'mol-theme/size';
  12. import { CartoonRepresentationProvider } from 'mol-repr/structure/representation/cartoon';
  13. import { trajectoryFromMmCIF } from 'mol-model-formats/structure/mmcif';
  14. import { computeModelDSSP } from 'mol-model/structure/model/properties/utils/secondary-structure';
  15. const parent = document.getElementById('app')!
  16. parent.style.width = '100%'
  17. parent.style.height = '100%'
  18. const canvas = document.createElement('canvas')
  19. canvas.style.width = '100%'
  20. canvas.style.height = '100%'
  21. parent.appendChild(canvas)
  22. const canvas3d = Canvas3D.create(canvas, parent)
  23. canvas3d.animate()
  24. async function parseCif(data: string|Uint8Array) {
  25. const comp = CIF.parse(data);
  26. const parsed = await comp.run();
  27. if (parsed.isError) throw parsed;
  28. return parsed.result;
  29. }
  30. async function downloadCif(url: string, isBinary: boolean) {
  31. const data = await fetch(url);
  32. return parseCif(isBinary ? new Uint8Array(await data.arrayBuffer()) : await data.text());
  33. }
  34. async function downloadFromPdb(pdb: string) {
  35. // const parsed = await downloadCif(`https://files.rcsb.org/download/${pdb}.cif`, false);
  36. const parsed = await downloadCif(`https://webchem.ncbr.muni.cz/ModelServer/static/bcif/${pdb}`, true);
  37. return parsed.blocks[0];
  38. }
  39. async function getModels(frame: CifFrame) {
  40. return await trajectoryFromMmCIF(frame).run();
  41. }
  42. async function getStructure(model: Model) {
  43. return Structure.ofModel(model);
  44. }
  45. const reprCtx = {
  46. colorThemeRegistry: ColorTheme.createRegistry(),
  47. sizeThemeRegistry: SizeTheme.createRegistry()
  48. }
  49. function getCartoonRepr() {
  50. return CartoonRepresentationProvider.factory(reprCtx, CartoonRepresentationProvider.getParams)
  51. }
  52. async function init() {
  53. const cif = await downloadFromPdb('3j3q')
  54. const models = await getModels(cif)
  55. console.time('computeModelDSSP')
  56. const secondaryStructure = computeModelDSSP(models[0].atomicHierarchy, models[0].atomicConformation)
  57. console.timeEnd('computeModelDSSP')
  58. ;(models[0].properties as any).secondaryStructure = secondaryStructure
  59. const structure = await getStructure(models[0])
  60. const cartoonRepr = getCartoonRepr()
  61. cartoonRepr.setTheme({
  62. color: reprCtx.colorThemeRegistry.create('secondary-structure', { structure }),
  63. size: reprCtx.sizeThemeRegistry.create('uniform', { structure })
  64. })
  65. await cartoonRepr.createOrUpdate({ ...CartoonRepresentationProvider.defaultValues, quality: 'auto' }, structure).run()
  66. canvas3d.add(cartoonRepr)
  67. canvas3d.resetCamera()
  68. }
  69. init()