dcd.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Task } from '../../mol-task';
  7. import { DcdFile } from '../../mol-io/reader/dcd/parser';
  8. import { Coordinates, Frame, Time } from '../../mol-model/structure/coordinates';
  9. import { Vec3 } from '../../mol-math/linear-algebra';
  10. import { degToRad, halfPI } from '../../mol-math/misc';
  11. import { Cell } from '../../mol-math/geometry/spacegroup/cell';
  12. import { Mutable } from '../../mol-util/type-helpers';
  13. import { EPSILON, equalEps } from '../../mol-math/linear-algebra/3d/common';
  14. const charmmTimeUnitFactor = 20.45482949774598;
  15. export function coordinatesFromDcd(dcdFile: DcdFile): Task<Coordinates> {
  16. return Task.create('Parse DCD', async ctx => {
  17. await ctx.update('Converting to coordinates');
  18. const { header } = dcdFile;
  19. const deltaTime = header.DELTA
  20. ? Time(header.DELTA * charmmTimeUnitFactor, 'ps')
  21. : Time(1, 'step');
  22. const offsetTime = header.ISTART >= 1
  23. ? Time((header.ISTART - 1) * deltaTime.value, deltaTime.unit)
  24. : Time(0, deltaTime.unit);
  25. const frames: Frame[] = [];
  26. for (let i = 0, il = dcdFile.frames.length; i < il; ++i) {
  27. const dcdFrame = dcdFile.frames[i];
  28. const frame: Mutable<Frame> = {
  29. elementCount: dcdFrame.elementCount,
  30. time: Time(offsetTime.value + deltaTime.value * i, deltaTime.unit),
  31. x: dcdFrame.x,
  32. y: dcdFrame.y,
  33. z: dcdFrame.z,
  34. xyzOrdering: { isIdentity: true }
  35. };
  36. if (dcdFrame.cell) {
  37. // this is not standardized, using heuristics to handle variants
  38. const c = dcdFrame.cell;
  39. if (c[1] >= -1 && c[1] <= 1 && c[3] >= -1 && c[3] <= 1 && c[4] >= -1 && c[4] <= 1) {
  40. frame.cell = Cell.create(
  41. Vec3.create(c[0], c[2], c[5]),
  42. Vec3.create(
  43. degToRad(90 - Math.asin(c[1]) * 90 / halfPI),
  44. degToRad(90 - Math.asin(c[3]) * 90 / halfPI),
  45. degToRad(90 - Math.asin(c[4]) * 90 / halfPI)
  46. )
  47. );
  48. } else if (
  49. c[0] < 0 || c[1] < 0 || c[2] < 0 || c[3] < 0 || c[4] < 0 || c[5] < 0 ||
  50. c[3] > 180 || c[4] > 180 || c[5] > 180
  51. ) {
  52. frame.cell = Cell.fromBasis(
  53. Vec3.create(c[0], c[1], c[3]),
  54. Vec3.create(c[1], c[2], c[4]),
  55. Vec3.create(c[3], c[4], c[5])
  56. );
  57. } else {
  58. frame.cell = Cell.create(
  59. Vec3.create(c[0], c[2], c[5]),
  60. // interpret angles very close to 0 as 90 deg
  61. Vec3.create(
  62. degToRad(equalEps(c[1], 0, EPSILON) ? 90 : c[1]),
  63. degToRad(equalEps(c[3], 0, EPSILON) ? 90 : c[3]),
  64. degToRad(equalEps(c[4], 0, EPSILON) ? 90 : c[4])
  65. )
  66. );
  67. }
  68. }
  69. frames.push(frame);
  70. }
  71. return Coordinates.create(frames, deltaTime, offsetTime);
  72. });
  73. }