provider.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * Copyright (c) 2019-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import { decodeMsgPack } from '../../mol-io/common/msgpack/decode';
  8. import { PluginContext } from '../../mol-plugin/context';
  9. import { StateObjectRef } from '../../mol-state';
  10. import { FileNameInfo } from '../../mol-util/file-info';
  11. import { PluginStateObject } from '../objects';
  12. export interface DataFormatProvider<P = any, R = any, V = any> {
  13. label: string,
  14. description: string,
  15. category?: string,
  16. stringExtensions?: string[],
  17. binaryExtensions?: string[],
  18. isApplicable?(info: FileNameInfo, data: string | Uint8Array): boolean,
  19. parse(plugin: PluginContext, data: StateObjectRef<PluginStateObject.Data.Binary | PluginStateObject.Data.String>, params?: P): Promise<R>,
  20. visuals?(plugin: PluginContext, data: R): Promise<V> | undefined
  21. }
  22. export function DataFormatProvider<P extends DataFormatProvider>(provider: P): P { return provider; }
  23. type cifVariants = 'dscif' | 'segcif' | 'coreCif' | -1
  24. export function guessCifVariant(info: FileNameInfo, data: Uint8Array | string): cifVariants {
  25. if (info.ext === 'bcif') {
  26. try {
  27. // TODO: find a way to run msgpackDecode only once
  28. // now it is run twice, here and during file parsing
  29. const { encoder } = decodeMsgPack(data as Uint8Array);
  30. if (encoder.startsWith('VolumeServer')) return 'dscif';
  31. // TODO: assumes volseg-volume-server only serves segments
  32. if (encoder.startsWith('volseg-volume-server')) return 'segcif';
  33. } catch (e) {
  34. console.error(e);
  35. }
  36. } else if (info.ext === 'cif') {
  37. const str = data as string;
  38. if (str.startsWith('data_SERVER\n#\n_density_server_result')) return 'dscif';
  39. if (str.startsWith('data_SERVER\n#\ndata_SEGMENTATION_DATA')) return 'segcif';
  40. if (str.includes('atom_site_fract_x') || str.includes('atom_site.fract_x')) return 'coreCif';
  41. }
  42. return -1;
  43. }