util.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 CIF from 'mol-io/reader/cif'
  7. import { Progress } from 'mol-task'
  8. import { VolumeData, parseDensityServerData } from 'mol-model/volume'
  9. import { DensityServer_Data_Database } from 'mol-io/reader/cif/schema/density-server';
  10. export async function downloadCif(url: string, isBinary: boolean) {
  11. const data = await fetch(url);
  12. return parseCif(isBinary ? new Uint8Array(await data.arrayBuffer()) : await data.text());
  13. }
  14. export async function parseCif(data: string|Uint8Array) {
  15. const comp = CIF.parse(data)
  16. const parsed = await comp.run(Progress.format);
  17. if (parsed.isError) throw parsed;
  18. return parsed.result
  19. }
  20. export type Volume = { source: DensityServer_Data_Database, volume: VolumeData }
  21. export async function getVolumeFromEmdId(emdid: string): Promise<Volume> {
  22. const cif = await downloadCif(`https://webchem.ncbr.muni.cz/DensityServer/em/emd-${emdid}/cell?detail=4`, true)
  23. const data = CIF.schema.densityServer(cif.blocks[1])
  24. return { source: data, volume: await parseDensityServerData(data).run() }
  25. }
  26. export function resizeCanvas (canvas: HTMLCanvasElement, container: Element) {
  27. let w = window.innerWidth
  28. let h = window.innerHeight
  29. if (container !== document.body) {
  30. let bounds = container.getBoundingClientRect()
  31. w = bounds.right - bounds.left
  32. h = bounds.bottom - bounds.top
  33. }
  34. canvas.width = window.devicePixelRatio * w
  35. canvas.height = window.devicePixelRatio * h
  36. Object.assign(canvas.style, { width: `${w}px`, height: `${h}px` })
  37. }