wrapper.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 { CifWriter } from '../../mol-io/writer/cif';
  7. import { Model } from '../../mol-model/structure';
  8. import { dateToUtcString } from '../../mol-util/date';
  9. import { MmcifFormat } from '../../mol-model-formats/structure/mmcif';
  10. interface PropertyWrapper<Data> {
  11. info: PropertyWrapper.Info,
  12. data: Data
  13. }
  14. namespace PropertyWrapper {
  15. export interface Info {
  16. timestamp_utc: string
  17. }
  18. export function createInfo(): Info {
  19. return { timestamp_utc: dateToUtcString(new Date()) };
  20. }
  21. export function defaultInfoCategory<Ctx>(name: string, getter: (ctx: Ctx) => Info | undefined): CifWriter.Category<Ctx> {
  22. return {
  23. name,
  24. instance(ctx) {
  25. const info = getter(ctx);
  26. return {
  27. fields: _info_fields,
  28. source: [{ data: info, rowCount: 1 }]
  29. };
  30. }
  31. };
  32. }
  33. const _info_fields: CifWriter.Field<number, Info>[] = [
  34. CifWriter.Field.str('updated_datetime_utc', (_, date) => date.timestamp_utc)
  35. ];
  36. export function tryGetInfoFromCif(categoryName: string, model: Model): Info | undefined {
  37. if (!MmcifFormat.is(model.sourceData) || !model.sourceData.data.frame.categoryNames.includes(categoryName)) {
  38. return;
  39. }
  40. const timestampField = model.sourceData.data.frame.categories[categoryName].getField('updated_datetime_utc');
  41. if (!timestampField || timestampField.rowCount === 0) return;
  42. return { timestamp_utc: timestampField.str(0) || dateToUtcString(new Date()) };
  43. }
  44. }
  45. export { PropertyWrapper };