struct_conn.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 '../../../model'
  8. import { Structure } from '../../../../structure'
  9. import { LinkType } from '../../../types'
  10. import { findEntityIdByAsymId, findAtomIndexByLabelName } from '../util'
  11. import { Column } from 'mol-data/db'
  12. import { ModelPropertyDescriptor } from '../../../properties/custom';
  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 } from '../../../indexing';
  17. export interface StructConn {
  18. getResidueEntries(residueAIndex: number, residueBIndex: number): ReadonlyArray<StructConn.Entry>,
  19. getAtomEntries(atomIndex: number): ReadonlyArray<StructConn.Entry>,
  20. readonly entries: ReadonlyArray<StructConn.Entry>
  21. }
  22. export namespace StructConn {
  23. export const Descriptor: ModelPropertyDescriptor = {
  24. isStatic: true,
  25. name: 'struct_conn',
  26. cifExport: {
  27. prefix: '',
  28. categories: [{
  29. name: 'struct_conn',
  30. instance(ctx) {
  31. const struct_conn = getStructConn(ctx.model);
  32. if (!struct_conn) return CifWriter.Category.Empty;
  33. const strConn = get(ctx.model);
  34. if (!strConn || strConn.entries.length === 0) return CifWriter.Category.Empty;
  35. const foundAtoms = new Set<ElementIndex>();
  36. const indices: number[] = [];
  37. for (const entry of strConn.entries) {
  38. const { partners } = entry;
  39. let hasAll = true;
  40. for (let i = 0, _i = partners.length; i < _i; i++) {
  41. const atom = partners[i].atomIndex;
  42. if (foundAtoms.has(atom)) continue;
  43. if (hasAtom(ctx.structure, atom)) {
  44. foundAtoms.add(atom);
  45. } else {
  46. hasAll = false;
  47. break;
  48. }
  49. }
  50. if (hasAll) {
  51. indices[indices.length] = entry.rowIndex;
  52. }
  53. }
  54. return CifWriter.Category.ofTable(struct_conn, indices);
  55. }
  56. }]
  57. }
  58. }
  59. function hasAtom({ units }: Structure, element: ElementIndex) {
  60. for (let i = 0, _i = units.length; i < _i; i++) {
  61. if (SortedArray.indexOf(units[i].elements, element) >= 0) return true;
  62. }
  63. return false;
  64. }
  65. function _resKey(rA: number, rB: number) {
  66. if (rA < rB) return `${rA}-${rB}`;
  67. return `${rB}-${rA}`;
  68. }
  69. const _emptyEntry: Entry[] = [];
  70. class StructConnImpl implements StructConn {
  71. private _residuePairIndex: Map<string, StructConn.Entry[]> | undefined = void 0;
  72. private _atomIndex: Map<number, StructConn.Entry[]> | undefined = void 0;
  73. private getResiduePairIndex() {
  74. if (this._residuePairIndex) return this._residuePairIndex;
  75. this._residuePairIndex = new Map();
  76. for (const e of this.entries) {
  77. const ps = e.partners;
  78. const l = ps.length;
  79. for (let i = 0; i < l - 1; i++) {
  80. for (let j = i + i; j < l; j++) {
  81. const key = _resKey(ps[i].residueIndex, ps[j].residueIndex);
  82. if (this._residuePairIndex.has(key)) {
  83. this._residuePairIndex.get(key)!.push(e);
  84. } else {
  85. this._residuePairIndex.set(key, [e]);
  86. }
  87. }
  88. }
  89. }
  90. return this._residuePairIndex;
  91. }
  92. private getAtomIndex() {
  93. if (this._atomIndex) return this._atomIndex;
  94. this._atomIndex = new Map();
  95. for (const e of this.entries) {
  96. for (const p of e.partners) {
  97. const key = p.atomIndex;
  98. if (this._atomIndex.has(key)) {
  99. this._atomIndex.get(key)!.push(e);
  100. } else {
  101. this._atomIndex.set(key, [e]);
  102. }
  103. }
  104. }
  105. return this._atomIndex;
  106. }
  107. getResidueEntries(residueAIndex: number, residueBIndex: number): ReadonlyArray<StructConn.Entry> {
  108. return this.getResiduePairIndex().get(_resKey(residueAIndex, residueBIndex)) || _emptyEntry;
  109. }
  110. getAtomEntries(atomIndex: number): ReadonlyArray<StructConn.Entry> {
  111. return this.getAtomIndex().get(atomIndex) || _emptyEntry;
  112. }
  113. constructor(public entries: StructConn.Entry[]) {
  114. }
  115. }
  116. export interface Entry {
  117. rowIndex: number,
  118. distance: number,
  119. order: number,
  120. flags: number,
  121. partners: { residueIndex: number, atomIndex: ElementIndex, symmetry: string }[]
  122. }
  123. type StructConnType = typeof mmCIF_Schema.struct_conn.conn_type_id.T
  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_comp_id: struct_conn.ptnr1_label_comp_id,
  145. label_seq_id: struct_conn.ptnr1_label_seq_id,
  146. auth_seq_id: struct_conn.ptnr1_auth_seq_id,
  147. label_atom_id: struct_conn.ptnr1_label_atom_id,
  148. label_alt_id: struct_conn.pdbx_ptnr1_label_alt_id,
  149. ins_code: struct_conn.pdbx_ptnr1_PDB_ins_code,
  150. symmetry: struct_conn.ptnr1_symmetry
  151. };
  152. const p2: typeof p1 = {
  153. label_asym_id: struct_conn.ptnr2_label_asym_id,
  154. label_comp_id: struct_conn.ptnr2_label_comp_id,
  155. label_seq_id: struct_conn.ptnr2_label_seq_id,
  156. auth_seq_id: struct_conn.ptnr2_auth_seq_id,
  157. label_atom_id: struct_conn.ptnr2_label_atom_id,
  158. label_alt_id: struct_conn.pdbx_ptnr2_label_alt_id,
  159. ins_code: struct_conn.pdbx_ptnr2_PDB_ins_code,
  160. symmetry: struct_conn.ptnr2_symmetry
  161. };
  162. const _p = (row: number, ps: typeof p1) => {
  163. if (ps.label_asym_id.valueKind(row) !== Column.ValueKind.Present) return void 0;
  164. const asymId = ps.label_asym_id.value(row)
  165. const residueIndex = model.atomicHierarchy.findResidueKey(
  166. findEntityIdByAsymId(model, asymId),
  167. asymId,
  168. ps.label_comp_id.value(row),
  169. ps.auth_seq_id.value(row),
  170. ps.ins_code.value(row)
  171. );
  172. if (residueIndex < 0) return void 0;
  173. const atomName = ps.label_atom_id.value(row);
  174. // turns out "mismat" records might not have atom name value
  175. if (!atomName) return void 0;
  176. const atomIndex = findAtomIndexByLabelName(model, residueIndex, atomName, ps.label_alt_id.value(row));
  177. if (atomIndex < 0) return void 0;
  178. return { residueIndex, atomIndex, symmetry: ps.symmetry.value(row) || '1_555' };
  179. }
  180. const _ps = (row: number) => {
  181. const ret = [];
  182. let p = _p(row, p1);
  183. if (p) ret.push(p);
  184. p = _p(row, p2);
  185. if (p) ret.push(p);
  186. return ret;
  187. }
  188. const entries: StructConn.Entry[] = [];
  189. for (let i = 0; i < struct_conn._rowCount; i++) {
  190. const partners = _ps(i);
  191. if (partners.length < 2) continue;
  192. const type = conn_type_id.value(i)! as StructConnType;
  193. const orderType = (pdbx_value_order.value(i) || '').toLowerCase();
  194. let flags = LinkType.Flag.None;
  195. let order = 1;
  196. switch (orderType) {
  197. case 'sing': order = 1; break;
  198. case 'doub': order = 2; break;
  199. case 'trip': order = 3; break;
  200. case 'quad': order = 4; break;
  201. }
  202. switch (type) {
  203. case 'covale':
  204. case 'covale_base':
  205. case 'covale_phosphate':
  206. case 'covale_sugar':
  207. case 'modres':
  208. flags = LinkType.Flag.Covalent;
  209. break;
  210. case 'disulf': flags = LinkType.Flag.Covalent | LinkType.Flag.Sulfide; break;
  211. case 'hydrog': flags = LinkType.Flag.Hydrogen; 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. }