hierarchy.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * Copyright (c) 2017-2019 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 { Column, Table } from '../../../../../mol-data/db'
  8. import { Segmentation } from '../../../../../mol-data/int'
  9. import { mmCIF_Schema as mmCIF } from '../../../../../mol-io/reader/cif/schema/mmcif'
  10. import { ElementSymbol, MoleculeType } from '../../types'
  11. import { ChainIndex, EntityIndex, ResidueIndex, ElementIndex } from '../../indexing';
  12. import SortedRanges from '../../../../../mol-data/int/sorted-ranges';
  13. export const AtomsSchema = {
  14. /**
  15. * The chemical element of this atom site.
  16. * For mmCIF files, this points to atom_type.symbol in the ATOM_TYPE category.
  17. */
  18. type_symbol: Column.Schema.Aliased<ElementSymbol>(mmCIF.atom_site.type_symbol),
  19. /**
  20. * A component of the identifier for this atom site.
  21. * This is a standardized name for the atom within its residue.
  22. * For mmCIF files, this points to chem_comp_atom.atom_id in the CHEM_COMP_ATOM category.
  23. */
  24. label_atom_id: mmCIF.atom_site.label_atom_id,
  25. /**
  26. * An alternative identifier for label_atom_id that may be provided by an author
  27. * in order to match the identification used in the publication that describes the structure.
  28. */
  29. auth_atom_id: mmCIF.atom_site.auth_atom_id,
  30. /**
  31. * A component of the identifier for this atom site.
  32. * Identifies an alternative conformation for this atom site.
  33. */
  34. label_alt_id: mmCIF.atom_site.label_alt_id,
  35. /**
  36. * The net integer charge assigned to this atom.
  37. * This is the formal charge assignment normally found in chemical diagrams.
  38. */
  39. pdbx_formal_charge: mmCIF.atom_site.pdbx_formal_charge,
  40. /**
  41. * The index of this atom in the input data.
  42. * Required because of sorting of atoms.
  43. */
  44. sourceIndex: Column.Schema.int
  45. // id, occupancy and B_iso_or_equiv are part of conformation
  46. };
  47. export type AtomsSchema = typeof AtomsSchema
  48. export interface Atoms extends Table<AtomsSchema> { }
  49. export const ResiduesSchema = {
  50. /**
  51. * The group of atoms to which the atom site belongs. This data item is provided for
  52. * compatibility with the original Protein Data Bank format, and only for that purpose.
  53. */
  54. group_PDB: mmCIF.atom_site.group_PDB,
  55. /**
  56. * A component of the identifier for this atom site.
  57. * For mmCIF files, this points to chem_comp.id in the CHEM_COMP category.
  58. */
  59. label_comp_id: mmCIF.atom_site.label_comp_id,
  60. /**
  61. * An alternative identifier for atom_site.label_comp_id that may be provided by an author
  62. * in order to match the identification used in the publication that describes the structure.
  63. */
  64. auth_comp_id: mmCIF.atom_site.auth_comp_id,
  65. /**
  66. * For mmCIF files, this points to entity_poly_seq.num in the ENTITY_POLY_SEQ category.
  67. */
  68. label_seq_id: mmCIF.atom_site.label_seq_id,
  69. /**
  70. * An alternative identifier for atom_site.label_seq_id that may be provided by an author
  71. * in order to match the identification used in the publication that describes the structure.
  72. */
  73. auth_seq_id: mmCIF.atom_site.auth_seq_id,
  74. /**
  75. * PDB insertion code.
  76. */
  77. pdbx_PDB_ins_code: mmCIF.atom_site.pdbx_PDB_ins_code,
  78. };
  79. export type ResiduesSchema = typeof ResiduesSchema
  80. export interface Residues extends Table<ResiduesSchema> { }
  81. export const ChainsSchema = {
  82. /**
  83. * A component of the identifier for this atom site.
  84. * For mmCIF files, this points to struct_asym.id in the STRUCT_ASYM category.
  85. */
  86. label_asym_id: mmCIF.atom_site.label_asym_id,
  87. /**
  88. * An alternative identifier for atomsite.label_asym_id that may be provided by an author
  89. * in order to match the identification used in the publication that describes the structure.
  90. */
  91. auth_asym_id: mmCIF.atom_site.auth_asym_id,
  92. /**
  93. * For mmCIF files, this points to _entity.id in the ENTITY category.
  94. */
  95. label_entity_id: mmCIF.atom_site.label_entity_id
  96. }
  97. export type ChainsSchema = typeof ChainsSchema
  98. export interface Chains extends Table<ChainsSchema> { }
  99. export interface AtomicData {
  100. atoms: Atoms,
  101. residues: Residues,
  102. chains: Chains
  103. }
  104. export interface AtomicDerivedData {
  105. readonly residue: {
  106. readonly traceElementIndex: ArrayLike<ElementIndex | -1>
  107. readonly directionFromElementIndex: ArrayLike<ElementIndex | -1>
  108. readonly directionToElementIndex: ArrayLike<ElementIndex | -1>
  109. readonly moleculeType: ArrayLike<MoleculeType>
  110. }
  111. }
  112. export interface AtomicSegments {
  113. /** Maps residueIndex to a range of atoms [segments[rI], segments[rI + 1]) */
  114. residueAtomSegments: Segmentation<ElementIndex, ResidueIndex>,
  115. /**
  116. * Maps chainIndex to a range of atoms [segments[cI], segments[cI + 1]),
  117. *
  118. * residues of i-th chain are accessed like this:
  119. * const rI = residueAtomSegments.index, offsets = chainAtomSegments.offsets;
  120. * const start = rI[offsets[i]], const end = rI[offsets[i + 1] - 1] + 1;
  121. * for (let j = start; j < end; i++) { }
  122. */
  123. chainAtomSegments: Segmentation<ElementIndex, ChainIndex>,
  124. // TODO: include entity segments?
  125. }
  126. export interface AtomicIndex {
  127. /** @returns index or -1 if not present. */
  128. getEntityFromChain(cI: ChainIndex): EntityIndex,
  129. /**
  130. * Find chain using label_ mmCIF properties
  131. * @returns index or -1 if not present.
  132. */
  133. findChainLabel(key: AtomicIndex.ChainLabelKey): ChainIndex,
  134. /**
  135. * Find chain using auth_ mmCIF properties
  136. * @returns index or -1 if not present.
  137. */
  138. findChainAuth(key: AtomicIndex.ChainAuthKey): ChainIndex,
  139. /**
  140. * Index of the 1st occurence of this residue.
  141. * auth_seq_id is used because label_seq_id is undefined for "ligands" in mmCIF.
  142. * @param key.pdbx_PDB_ins_code Empty string for undefined
  143. * @returns index or -1 if not present.
  144. */
  145. findResidue(key: AtomicIndex.ResidueKey): ResidueIndex,
  146. findResidue(label_entity_id: string, label_asym_id: string, auth_seq_id: number, pdbx_PDB_ins_code?: string): ResidueIndex,
  147. /**
  148. * Index of the 1st occurence of this residue.
  149. * @param key.pdbx_PDB_ins_code Empty string for undefined
  150. * @returns index or -1 if not present.
  151. */
  152. findResidueAuth(key: AtomicIndex.ResidueAuthKey): ResidueIndex,
  153. /**
  154. * Find the residue index where the spefied residue should be inserted to maintain the ordering (entity_id, asym_id, seq_id, ins_code).
  155. * Useful for determining ranges for sequence-level annotations.
  156. * @param key.pdbx_PDB_ins_code Use empty string for undefined
  157. */
  158. findResidueInsertion(key: AtomicIndex.ResidueLabelKey): ResidueIndex,
  159. /**
  160. * Find element index of an atom.
  161. * @param key
  162. * @returns index or -1 if the atom is not present.
  163. */
  164. findAtom(key: AtomicIndex.AtomKey): ElementIndex,
  165. /**
  166. * Find element index of an atom.
  167. * @param key
  168. * @returns index or -1 if the atom is not present.
  169. */
  170. findAtomAuth(key: AtomicIndex.AtomAuthKey): ElementIndex,
  171. /**
  172. * Find element index of an atom on a given residue.
  173. * @returns index or -1 if the atom is not present.
  174. */
  175. findAtomOnResidue(residueIndex: ResidueIndex, label_atom_id: string, label_alt_id?: string): ElementIndex
  176. /**
  177. * Find element index of any given atom on a given residue.
  178. * @returns first found index or -1 if none of the given atoms are present.
  179. */
  180. findAtomsOnResidue(residueIndex: ResidueIndex, label_atom_ids: Set<string>): ElementIndex
  181. // TODO: add indices that support comp_id?
  182. }
  183. export namespace AtomicIndex {
  184. export interface ChainLabelKey { label_entity_id: string, label_asym_id: string }
  185. export interface ChainAuthKey { auth_asym_id: string }
  186. export interface ResidueKey { label_entity_id: string, label_asym_id: string, auth_seq_id: number, pdbx_PDB_ins_code?: string }
  187. export function EmptyResidueKey(): ResidueKey { return { label_entity_id: '', label_asym_id: '', auth_seq_id: 0, pdbx_PDB_ins_code: void 0 }; }
  188. export interface ResidueAuthKey { auth_asym_id: string, auth_comp_id: string, auth_seq_id: number, pdbx_PDB_ins_code?: string }
  189. export interface ResidueLabelKey { label_entity_id: string, label_asym_id: string, label_seq_id: number, pdbx_PDB_ins_code?: string }
  190. export interface AtomKey extends ResidueKey { label_atom_id: string, label_alt_id?: string }
  191. export interface AtomAuthKey extends ResidueAuthKey { auth_atom_id: string, label_alt_id?: string }
  192. }
  193. export interface AtomicRanges {
  194. polymerRanges: SortedRanges<ElementIndex>
  195. gapRanges: SortedRanges<ElementIndex>
  196. cyclicPolymerMap: Map<ResidueIndex, ResidueIndex>
  197. }
  198. type _Hierarchy = AtomicData & AtomicSegments & AtomicRanges
  199. export interface AtomicHierarchy extends _Hierarchy {
  200. index: AtomicIndex
  201. derived: AtomicDerivedData
  202. }
  203. export namespace AtomicHierarchy {
  204. /** Start residue inclusive */
  205. export function chainStartResidueIndex(segs: AtomicSegments, cI: ChainIndex) {
  206. return segs.residueAtomSegments.index[segs.chainAtomSegments.offsets[cI]];
  207. }
  208. /** End residue exclusive */
  209. export function chainEndResidueIndexExcl(segs: AtomicSegments, cI: ChainIndex) {
  210. return segs.residueAtomSegments.index[segs.chainAtomSegments.offsets[cI + 1] - 1] + 1 as ResidueIndex;
  211. }
  212. }