format.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Copyright (c) 2018-2020 Mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Model } from '../../../mol-model/structure/model/model';
  7. import { Table } from '../../../mol-data/db';
  8. import { mmCIF_Schema } from '../../../mol-io/reader/cif/schema/mmcif';
  9. import { Unit } from '../../../mol-model/structure';
  10. import { ElementIndex } from '../../../mol-model/structure/model/indexing';
  11. import { FormatPropertyProvider } from '../../../mol-model-formats/structure/common/property';
  12. import { CustomPropertyDescriptor } from '../../../mol-model/custom-property';
  13. export { ModelCrossLinkRestraint };
  14. interface ModelCrossLinkRestraint {
  15. getIndicesByElement: (element: ElementIndex, kind: Unit.Kind) => number[]
  16. data: Table<mmCIF_Schema['ihm_cross_link_restraint']>
  17. }
  18. namespace ModelCrossLinkRestraint {
  19. export const Descriptor: CustomPropertyDescriptor = {
  20. name: 'ihm_cross_link_restraint',
  21. // TODO cifExport
  22. };
  23. export const Provider = FormatPropertyProvider.create<ModelCrossLinkRestraint>(Descriptor);
  24. export function fromTable(table: Table<mmCIF_Schema['ihm_cross_link_restraint']>, model: Model): ModelCrossLinkRestraint {
  25. const p1 = {
  26. entity_id: table.entity_id_1,
  27. asym_id: table.asym_id_1,
  28. seq_id: table.seq_id_1,
  29. atom_id: table.atom_id_1,
  30. };
  31. const p2: typeof p1 = {
  32. entity_id: table.entity_id_2,
  33. asym_id: table.asym_id_2,
  34. seq_id: table.seq_id_2,
  35. atom_id: table.atom_id_2,
  36. };
  37. function _add(map: Map<ElementIndex, number[]>, element: ElementIndex, row: number) {
  38. const indices = map.get(element);
  39. if (indices) indices.push(row);
  40. else map.set(element, [row]);
  41. }
  42. function add(row: number, ps: typeof p1) {
  43. const entityId = ps.entity_id.value(row);
  44. const asymId = ps.asym_id.value(row);
  45. const seqId = ps.seq_id.value(row);
  46. if (table.model_granularity.value(row) === 'by-atom') {
  47. const atomicElement = model.atomicHierarchy.index.findAtom({
  48. auth_seq_id: seqId,
  49. label_asym_id: asymId,
  50. label_atom_id: ps.atom_id.value(row),
  51. label_entity_id: entityId,
  52. });
  53. if (atomicElement >= 0) _add(atomicElementMap, atomicElement as ElementIndex, row);
  54. } else if (model.coarseHierarchy.isDefined) {
  55. const sphereElement = model.coarseHierarchy.spheres.findSequenceKey(entityId, asymId, seqId);
  56. if (sphereElement >= 0) {
  57. _add(sphereElementMap, sphereElement, row);
  58. } else {
  59. const gaussianElement = model.coarseHierarchy.gaussians.findSequenceKey(entityId, asymId, seqId);
  60. if (gaussianElement >= 0) _add(gaussianElementMap, gaussianElement, row);
  61. }
  62. }
  63. }
  64. function getMapByKind(kind: Unit.Kind) {
  65. switch (kind) {
  66. case Unit.Kind.Atomic: return atomicElementMap;
  67. case Unit.Kind.Spheres: return sphereElementMap;
  68. case Unit.Kind.Gaussians: return gaussianElementMap;
  69. }
  70. }
  71. /** map from atomic element to cross link indices */
  72. const atomicElementMap: Map<ElementIndex, number[]> = new Map();
  73. /** map from sphere element to cross link indices */
  74. const sphereElementMap: Map<ElementIndex, number[]> = new Map();
  75. /** map from gaussian element to cross link indices */
  76. const gaussianElementMap: Map<ElementIndex, number[]> = new Map();
  77. const emptyIndexArray: number[] = [];
  78. for (let i = 0; i < table._rowCount; ++i) {
  79. add(i, p1);
  80. add(i, p2);
  81. }
  82. return {
  83. getIndicesByElement: (element: ElementIndex, kind: Unit.Kind) => {
  84. const map = getMapByKind(kind);
  85. const idx = map.get(element);
  86. return idx !== undefined ? idx : emptyIndexArray;
  87. },
  88. data: table
  89. };
  90. }
  91. }