component.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Table, Column } from '../../../mol-data/db';
  7. import { mmCIF_Schema } from '../../../mol-io/reader/cif/schema/mmcif';
  8. import { WaterNames } from '../../../mol-model/structure/model/types';
  9. import { SetUtils } from '../../../mol-util/set';
  10. import { BasicSchema } from '../basic/schema';
  11. type Component = Table.Row<Pick<mmCIF_Schema['chem_comp'], 'id' | 'name' | 'type'>>
  12. const ProteinAtomIdsList = [
  13. new Set([ 'CA' ]),
  14. new Set([ 'C' ]),
  15. new Set([ 'N' ])
  16. ]
  17. const RnaAtomIdsList = [
  18. new Set([ 'P', 'O3\'', 'O3*' ]),
  19. new Set([ 'C4\'', 'C4*' ]),
  20. new Set([ 'O2\'', 'O2*', 'F2\'', 'F2*' ])
  21. ]
  22. const DnaAtomIdsList = [
  23. new Set([ 'P', 'O3\'', 'O3*' ]),
  24. new Set([ 'C3\'', 'C3*' ]),
  25. new Set([ 'O2\'', 'O2*', 'F2\'', 'F2*' ])
  26. ]
  27. const StandardComponents = (function() {
  28. const map = new Map<string, Component>()
  29. const components: Component[] = [
  30. { id: 'HIS', name: 'HISTIDINE', type: 'L-peptide linking' },
  31. { id: 'ARG', name: 'ARGININE', type: 'L-peptide linking' },
  32. { id: 'LYS', name: 'LYSINE', type: 'L-peptide linking' },
  33. { id: 'ILE', name: 'ISOLEUCINE', type: 'L-peptide linking' },
  34. { id: 'PHE', name: 'PHENYLALANINE', type: 'L-peptide linking' },
  35. { id: 'LEU', name: 'LEUCINE', type: 'L-peptide linking' },
  36. { id: 'TRP', name: 'TRYPTOPHAN', type: 'L-peptide linking' },
  37. { id: 'ALA', name: 'ALANINE', type: 'L-peptide linking' },
  38. { id: 'MET', name: 'METHIONINE', type: 'L-peptide linking' },
  39. { id: 'CYS', name: 'CYSTEINE', type: 'L-peptide linking' },
  40. { id: 'ASN', name: 'ASPARAGINE', type: 'L-peptide linking' },
  41. { id: 'VAL', name: 'VALINE', type: 'L-peptide linking' },
  42. { id: 'GLY', name: 'GLYCINE', type: 'peptide linking' },
  43. { id: 'SER', name: 'SERINE', type: 'L-peptide linking' },
  44. { id: 'GLN', name: 'GLUTAMINE', type: 'L-peptide linking' },
  45. { id: 'TYR', name: 'TYROSINE', type: 'L-peptide linking' },
  46. { id: 'ASP', name: 'ASPARTIC ACID', type: 'L-peptide linking' },
  47. { id: 'GLU', name: 'GLUTAMIC ACID', type: 'L-peptide linking' },
  48. { id: 'THR', name: 'THREONINE', type: 'L-peptide linking' },
  49. { id: 'SEC', name: 'SELENOCYSTEINE', type: 'L-peptide linking' },
  50. { id: 'PYL', name: 'PYRROLYSINE', type: 'L-peptide linking' },
  51. { id: 'A', name: 'ADENOSINE-5\'-MONOPHOSPHATE', type: 'RNA linking' },
  52. { id: 'C', name: 'CYTIDINE-5\'-MONOPHOSPHATE', type: 'RNA linking' },
  53. { id: 'T', name: 'THYMIDINE-5\'-MONOPHOSPHATE', type: 'RNA linking' },
  54. { id: 'G', name: 'GUANOSINE-5\'-MONOPHOSPHATE', type: 'RNA linking' },
  55. { id: 'I', name: 'INOSINIC ACID', type: 'RNA linking' },
  56. { id: 'U', name: 'URIDINE-5\'-MONOPHOSPHATE', type: 'RNA linking' },
  57. { id: 'DA', name: '2\'-DEOXYADENOSINE-5\'-MONOPHOSPHATE', type: 'DNA linking' },
  58. { id: 'DC', name: '2\'-DEOXYCYTIDINE-5\'-MONOPHOSPHATE', type: 'DNA linking' },
  59. { id: 'DT', name: 'THYMIDINE-5\'-MONOPHOSPHATE', type: 'DNA linking' },
  60. { id: 'DG', name: '2\'-DEOXYGUANOSINE-5\'-MONOPHOSPHATE', type: 'DNA linking' },
  61. { id: 'DI', name: '2\'-DEOXYINOSINE-5\'-MONOPHOSPHATE', type: 'DNA linking' },
  62. { id: 'DU', name: '2\'-DEOXYURIDINE-5\'-MONOPHOSPHATE', type: 'DNA linking' },
  63. ]
  64. components.forEach(c => map.set(c.id, c))
  65. return map
  66. })()
  67. export class ComponentBuilder {
  68. private namesMap = new Map<string, string>()
  69. private comps = new Map<string, Component>()
  70. private ids: string[] = []
  71. private names: string[] = []
  72. private types: mmCIF_Schema['chem_comp']['type']['T'][] = []
  73. private set(c: Component) {
  74. this.comps.set(c.id, c)
  75. this.ids.push(c.id)
  76. this.names.push(c.name)
  77. this.types.push(c.type)
  78. }
  79. private getAtomIds(index: number) {
  80. const atomIds = new Set<string>()
  81. let prevSeqId = this.seqId.value(index)
  82. while (index < this.seqId.rowCount) {
  83. const seqId = this.seqId.value(index)
  84. if (seqId !== prevSeqId) break
  85. atomIds.add(this.atomId.value(index))
  86. prevSeqId - seqId
  87. index += 1
  88. }
  89. return atomIds
  90. }
  91. private hasAtomIds (atomIds: Set<string>, atomIdsList: Set<string>[]) {
  92. for (let i = 0, il = atomIdsList.length; i < il; ++i) {
  93. if (!SetUtils.areIntersecting(atomIds, atomIdsList[i])) {
  94. return false
  95. }
  96. }
  97. return true
  98. }
  99. private getType(atomIds: Set<string>): Component['type'] {
  100. if (this.hasAtomIds(atomIds, ProteinAtomIdsList)) {
  101. return 'peptide linking'
  102. } else if (this.hasAtomIds(atomIds, RnaAtomIdsList)) {
  103. return 'RNA linking'
  104. } else if (this.hasAtomIds(atomIds, DnaAtomIdsList)) {
  105. return 'DNA linking'
  106. } else {
  107. return 'other'
  108. }
  109. }
  110. has(compId: string) { return this.comps.has(compId) }
  111. get(compId: string) { return this.comps.get(compId) }
  112. add(compId: string, index: number) {
  113. if (!this.has(compId)) {
  114. if (StandardComponents.has(compId)) {
  115. this.set(StandardComponents.get(compId)!)
  116. } else if (WaterNames.has(compId)) {
  117. this.set({ id: compId, name: 'WATER', type: 'non-polymer' })
  118. } else {
  119. const type = this.getType(this.getAtomIds(index))
  120. this.set({ id: compId, name: this.namesMap.get(compId) || compId, type })
  121. }
  122. }
  123. return this.get(compId)!
  124. }
  125. getChemCompTable() {
  126. return Table.ofPartialColumns(BasicSchema.chem_comp, {
  127. id: Column.ofStringArray(this.ids),
  128. name: Column.ofStringArray(this.names),
  129. type: Column.ofStringAliasArray(this.types),
  130. }, this.ids.length)
  131. }
  132. setNames(names: [string, string][]) {
  133. names.forEach(n => this.namesMap.set(n[0], n[1]))
  134. }
  135. constructor(private seqId: Column<number>, private atomId: Column<string>) {
  136. }
  137. }