format.ts 4.1 KB

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