density-server.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { DensityServer_Data_Database } from '../../mol-io/reader/cif/schema/density-server';
  7. import { Volume } from '../../mol-model/volume';
  8. import { Task } from '../../mol-task';
  9. import { SpacegroupCell, Box3D } from '../../mol-math/geometry';
  10. import { Tensor, Vec3 } from '../../mol-math/linear-algebra';
  11. import { ModelFormat } from '../format';
  12. import { CustomProperties } from '../../mol-model/custom-property';
  13. export function volumeFromDensityServerData(source: DensityServer_Data_Database): Task<Volume> {
  14. return Task.create<Volume>('Create Volume', async ctx => {
  15. const { volume_data_3d_info: info, volume_data_3d: values } = source;
  16. const cell = SpacegroupCell.create(
  17. info.spacegroup_number.value(0),
  18. Vec3.ofArray(info.spacegroup_cell_size.value(0)),
  19. Vec3.scale(Vec3.zero(), Vec3.ofArray(info.spacegroup_cell_angles.value(0)), Math.PI / 180)
  20. );
  21. const axis_order_fast_to_slow = info.axis_order.value(0);
  22. const normalizeOrder = Tensor.convertToCanonicalAxisIndicesFastToSlow(axis_order_fast_to_slow);
  23. // sample count is in "axis order" and needs to be reordered
  24. const sample_count = normalizeOrder(info.sample_count.value(0));
  25. const tensorSpace = Tensor.Space(sample_count, Tensor.invertAxisOrder(axis_order_fast_to_slow), Float32Array);
  26. const data = Tensor.create(tensorSpace, Tensor.Data1(values.values.toArray({ array: Float32Array })));
  27. // origin and dimensions are in "axis order" and need to be reordered
  28. const origin = Vec3.ofArray(normalizeOrder(info.origin.value(0)));
  29. const dimensions = Vec3.ofArray(normalizeOrder(info.dimensions.value(0)));
  30. return {
  31. grid: {
  32. transform: { kind: 'spacegroup', cell, fractionalBox: Box3D.create(origin, Vec3.add(Vec3.zero(), origin, dimensions)) },
  33. cells: data,
  34. stats: {
  35. min: info.min_sampled.value(0),
  36. max: info.max_sampled.value(0),
  37. mean: info.mean_sampled.value(0),
  38. sigma: info.sigma_sampled.value(0)
  39. },
  40. },
  41. sourceData: DscifFormat.create(source),
  42. customProperties: new CustomProperties(),
  43. _propertyData: Object.create(null),
  44. };
  45. });
  46. }
  47. //
  48. export { DscifFormat };
  49. type DscifFormat = ModelFormat<DensityServer_Data_Database>
  50. namespace DscifFormat {
  51. export function is(x: ModelFormat): x is DscifFormat {
  52. return x.kind === 'dscif';
  53. }
  54. export function create(dscif: DensityServer_Data_Database): DscifFormat {
  55. return { kind: 'dscif', name: dscif._name, data: dscif };
  56. }
  57. }