struct_conn.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * Copyright (c) 2017-2018 Mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { Model } from 'mol-model/structure/model/model'
  8. import { Structure } from 'mol-model/structure'
  9. import { LinkType } from 'mol-model/structure/model/types'
  10. import { findEntityIdByAsymId, findAtomIndexByLabelName } from '../util'
  11. import { Column } from 'mol-data/db'
  12. import { CustomPropertyDescriptor } from 'mol-model/structure';
  13. import { mmCIF_Database, mmCIF_Schema } from 'mol-io/reader/cif/schema/mmcif';
  14. import { SortedArray } from 'mol-data/int';
  15. import { CifWriter } from 'mol-io/writer/cif'
  16. import { ElementIndex, ResidueIndex } from 'mol-model/structure/model/indexing';
  17. export interface StructConn {
  18. getResidueEntries(residueAIndex: ResidueIndex, residueBIndex: ResidueIndex): ReadonlyArray<StructConn.Entry>,
  19. getAtomEntries(atomIndex: ElementIndex): ReadonlyArray<StructConn.Entry>,
  20. readonly entries: ReadonlyArray<StructConn.Entry>
  21. }
  22. export namespace StructConn {
  23. export const Descriptor: CustomPropertyDescriptor = {
  24. isStatic: true,
  25. name: 'struct_conn',
  26. cifExport: {
  27. prefix: '',
  28. categories: [{
  29. name: 'struct_conn',
  30. instance(ctx) {
  31. const structure = ctx.structures[0], model = structure.model;
  32. const struct_conn = getStructConn(model);
  33. if (!struct_conn) return CifWriter.Category.Empty;
  34. const strConn = get(model);
  35. if (!strConn || strConn.entries.length === 0) return CifWriter.Category.Empty;
  36. const foundAtoms = new Set<ElementIndex>();
  37. const indices: number[] = [];
  38. for (const entry of strConn.entries) {
  39. const { partners } = entry;
  40. let hasAll = true;
  41. for (let i = 0, _i = partners.length; i < _i; i++) {
  42. const atom = partners[i].atomIndex;
  43. if (foundAtoms.has(atom)) continue;
  44. if (hasAtom(structure, atom)) {
  45. foundAtoms.add(atom);
  46. } else {
  47. hasAll = false;
  48. break;
  49. }
  50. }
  51. if (hasAll) {
  52. indices[indices.length] = entry.rowIndex;
  53. }
  54. }
  55. return CifWriter.Category.ofTable(struct_conn, indices);
  56. }
  57. }]
  58. }
  59. }
  60. function hasAtom({ units }: Structure, element: ElementIndex) {
  61. for (let i = 0, _i = units.length; i < _i; i++) {
  62. if (SortedArray.indexOf(units[i].elements, element) >= 0) return true;
  63. }
  64. return false;
  65. }
  66. function _resKey(rA: number, rB: number) {
  67. if (rA < rB) return `${rA}-${rB}`;
  68. return `${rB}-${rA}`;
  69. }
  70. const _emptyEntry: Entry[] = [];
  71. class StructConnImpl implements StructConn {
  72. private _residuePairIndex: Map<string, StructConn.Entry[]> | undefined = void 0;
  73. private _atomIndex: Map<number, StructConn.Entry[]> | undefined = void 0;
  74. private getResiduePairIndex() {
  75. if (this._residuePairIndex) return this._residuePairIndex;
  76. this._residuePairIndex = new Map();
  77. for (const e of this.entries) {
  78. const ps = e.partners;
  79. const l = ps.length;
  80. for (let i = 0; i < l - 1; i++) {
  81. for (let j = i + i; j < l; j++) {
  82. const key = _resKey(ps[i].residueIndex, ps[j].residueIndex);
  83. if (this._residuePairIndex.has(key)) {
  84. this._residuePairIndex.get(key)!.push(e);
  85. } else {
  86. this._residuePairIndex.set(key, [e]);
  87. }
  88. }
  89. }
  90. }
  91. return this._residuePairIndex;
  92. }
  93. private getAtomIndex() {
  94. if (this._atomIndex) return this._atomIndex;
  95. this._atomIndex = new Map();
  96. for (const e of this.entries) {
  97. for (const p of e.partners) {
  98. const key = p.atomIndex;
  99. if (this._atomIndex.has(key)) {
  100. this._atomIndex.get(key)!.push(e);
  101. } else {
  102. this._atomIndex.set(key, [e]);
  103. }
  104. }
  105. }
  106. return this._atomIndex;
  107. }
  108. getResidueEntries(residueAIndex: ResidueIndex, residueBIndex: ResidueIndex): ReadonlyArray<StructConn.Entry> {
  109. return this.getResiduePairIndex().get(_resKey(residueAIndex, residueBIndex)) || _emptyEntry;
  110. }
  111. getAtomEntries(atomIndex: ElementIndex): ReadonlyArray<StructConn.Entry> {
  112. return this.getAtomIndex().get(atomIndex) || _emptyEntry;
  113. }
  114. constructor(public entries: StructConn.Entry[]) {
  115. }
  116. }
  117. export interface Entry {
  118. rowIndex: number,
  119. distance: number,
  120. order: number,
  121. flags: number,
  122. partners: { residueIndex: ResidueIndex, atomIndex: ElementIndex, symmetry: string }[]
  123. }
  124. export function attachFromMmCif(model: Model): boolean {
  125. if (model.customProperties.has(Descriptor)) return true;
  126. if (model.sourceData.kind !== 'mmCIF') return false;
  127. const { struct_conn } = model.sourceData.data;
  128. if (struct_conn._rowCount === 0) return false;
  129. model.customProperties.add(Descriptor);
  130. model._staticPropertyData.__StructConnData__ = struct_conn;
  131. return true;
  132. }
  133. function getStructConn(model: Model) {
  134. return model._staticPropertyData.__StructConnData__ as mmCIF_Database['struct_conn'];
  135. }
  136. export const PropName = '__StructConn__';
  137. export function get(model: Model): StructConn | undefined {
  138. if (model._staticPropertyData[PropName]) return model._staticPropertyData[PropName];
  139. if (!model.customProperties.has(Descriptor)) return void 0;
  140. const struct_conn = getStructConn(model);
  141. const { conn_type_id, pdbx_dist_value, pdbx_value_order } = struct_conn;
  142. const p1 = {
  143. label_asym_id: struct_conn.ptnr1_label_asym_id,
  144. label_seq_id: struct_conn.ptnr1_label_seq_id,
  145. auth_seq_id: struct_conn.ptnr1_auth_seq_id,
  146. label_atom_id: struct_conn.ptnr1_label_atom_id,
  147. label_alt_id: struct_conn.pdbx_ptnr1_label_alt_id,
  148. ins_code: struct_conn.pdbx_ptnr1_PDB_ins_code,
  149. symmetry: struct_conn.ptnr1_symmetry
  150. };
  151. const p2: typeof p1 = {
  152. label_asym_id: struct_conn.ptnr2_label_asym_id,
  153. label_seq_id: struct_conn.ptnr2_label_seq_id,
  154. auth_seq_id: struct_conn.ptnr2_auth_seq_id,
  155. label_atom_id: struct_conn.ptnr2_label_atom_id,
  156. label_alt_id: struct_conn.pdbx_ptnr2_label_alt_id,
  157. ins_code: struct_conn.pdbx_ptnr2_PDB_ins_code,
  158. symmetry: struct_conn.ptnr2_symmetry
  159. };
  160. const _p = (row: number, ps: typeof p1) => {
  161. if (ps.label_asym_id.valueKind(row) !== Column.ValueKind.Present) return void 0;
  162. const asymId = ps.label_asym_id.value(row);
  163. const residueIndex = model.atomicHierarchy.index.findResidue(
  164. findEntityIdByAsymId(model, asymId),
  165. asymId,
  166. ps.auth_seq_id.value(row),
  167. ps.ins_code.value(row)
  168. );
  169. if (residueIndex < 0) return void 0;
  170. const atomName = ps.label_atom_id.value(row);
  171. // turns out "mismat" records might not have atom name value
  172. if (!atomName) return void 0;
  173. const atomIndex = findAtomIndexByLabelName(model, residueIndex, atomName, ps.label_alt_id.value(row));
  174. if (atomIndex < 0) return void 0;
  175. return { residueIndex, atomIndex, symmetry: ps.symmetry.value(row) || '1_555' };
  176. }
  177. const _ps = (row: number) => {
  178. const ret = [];
  179. let p = _p(row, p1);
  180. if (p) ret.push(p);
  181. p = _p(row, p2);
  182. if (p) ret.push(p);
  183. return ret;
  184. }
  185. const entries: StructConn.Entry[] = [];
  186. for (let i = 0; i < struct_conn._rowCount; i++) {
  187. const partners = _ps(i);
  188. if (partners.length < 2) continue;
  189. const type = conn_type_id.value(i) as typeof mmCIF_Schema.struct_conn_type.id.T; // TODO workaround for dictionary inconsistency
  190. const orderType = (pdbx_value_order.value(i) || '').toLowerCase();
  191. let flags = LinkType.Flag.None;
  192. let order = 1;
  193. switch (orderType) {
  194. case 'sing': order = 1; break;
  195. case 'doub': order = 2; break;
  196. case 'trip': order = 3; break;
  197. case 'quad': order = 4; break;
  198. }
  199. switch (type) {
  200. case 'covale':
  201. case 'covale_base':
  202. case 'covale_phosphate':
  203. case 'covale_sugar':
  204. case 'modres':
  205. flags = LinkType.Flag.Covalent;
  206. break;
  207. case 'disulf': flags = LinkType.Flag.Covalent | LinkType.Flag.Sulfide; break;
  208. case 'hydrog':
  209. case 'mismat':
  210. flags = LinkType.Flag.Hydrogen;
  211. break;
  212. case 'metalc': flags = LinkType.Flag.MetallicCoordination; break;
  213. case 'saltbr': flags = LinkType.Flag.Ionic; break;
  214. }
  215. entries.push({ rowIndex: i, flags, order, distance: pdbx_dist_value.value(i), partners });
  216. }
  217. const ret = new StructConnImpl(entries);
  218. model._staticPropertyData[PropName] = ret;
  219. return ret;
  220. }
  221. }