component.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 { WaterNames, PolymerNames } from '../../../mol-model/structure/model/types';
  8. import { SetUtils } from '../../../mol-util/set';
  9. import { BasicSchema } from '../basic/schema';
  10. import { mmCIF_chemComp_schema } from '../../../mol-io/reader/cif/schema/mmcif-extras';
  11. type Component = Table.Row<Pick<mmCIF_chemComp_schema, '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. const CharmmIonComponents = (function() {
  68. const map = new Map<string, Component>();
  69. const components: Component[] = [
  70. { id: 'ZN2', name: 'ZINC ION', type: 'Ion' },
  71. { id: 'SOD', name: 'SODIUM ION', type: 'Ion' },
  72. { id: 'CES', name: 'CESIUM ION', type: 'Ion' },
  73. { id: 'CLA', name: 'CHLORIDE ION', type: 'Ion' },
  74. { id: 'CAL', name: 'CALCIUM ION', type: 'Ion' },
  75. { id: 'POT', name: 'POTASSIUM ION', type: 'Ion' },
  76. ];
  77. components.forEach(c => map.set(c.id, c));
  78. return map;
  79. })();
  80. export class ComponentBuilder {
  81. private namesMap = new Map<string, string>()
  82. private comps = new Map<string, Component>()
  83. private ids: string[] = []
  84. private names: string[] = []
  85. private types: mmCIF_chemComp_schema['type']['T'][] = []
  86. private mon_nstd_flags: mmCIF_chemComp_schema['mon_nstd_flag']['T'][] = []
  87. private set(c: Component) {
  88. this.comps.set(c.id, c);
  89. this.ids.push(c.id);
  90. this.names.push(c.name);
  91. this.types.push(c.type);
  92. this.mon_nstd_flags.push(PolymerNames.has(c.id) ? 'y' : 'n');
  93. }
  94. private getAtomIds(index: number) {
  95. const atomIds = new Set<string>();
  96. let prevSeqId = this.seqId.value(index);
  97. while (index < this.seqId.rowCount) {
  98. const seqId = this.seqId.value(index);
  99. if (seqId !== prevSeqId) break;
  100. atomIds.add(this.atomId.value(index));
  101. prevSeqId - seqId;
  102. index += 1;
  103. }
  104. return atomIds;
  105. }
  106. private hasAtomIds (atomIds: Set<string>, atomIdsList: Set<string>[]) {
  107. for (let i = 0, il = atomIdsList.length; i < il; ++i) {
  108. if (!SetUtils.areIntersecting(atomIds, atomIdsList[i])) {
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. private getType(atomIds: Set<string>): Component['type'] {
  115. if (this.hasAtomIds(atomIds, ProteinAtomIdsList)) {
  116. return 'peptide linking';
  117. } else if (this.hasAtomIds(atomIds, RnaAtomIdsList)) {
  118. return 'RNA linking';
  119. } else if (this.hasAtomIds(atomIds, DnaAtomIdsList)) {
  120. return 'DNA linking';
  121. } else {
  122. return 'other';
  123. }
  124. }
  125. has(compId: string) { return this.comps.has(compId); }
  126. get(compId: string) { return this.comps.get(compId); }
  127. add(compId: string, index: number) {
  128. if (!this.has(compId)) {
  129. if (StandardComponents.has(compId)) {
  130. this.set(StandardComponents.get(compId)!);
  131. } else if (WaterNames.has(compId)) {
  132. this.set({ id: compId, name: 'WATER', type: 'non-polymer' });
  133. } else {
  134. const atomIds = this.getAtomIds(index);
  135. if (CharmmIonComponents.has(compId) && atomIds.size === 1) {
  136. this.set(CharmmIonComponents.get(compId)!);
  137. } else {
  138. const type = this.getType(atomIds);
  139. this.set({ id: compId, name: this.namesMap.get(compId) || compId, type });
  140. }
  141. }
  142. }
  143. return this.get(compId)!;
  144. }
  145. getChemCompTable() {
  146. return Table.ofPartialColumns(BasicSchema.chem_comp, {
  147. id: Column.ofStringArray(this.ids),
  148. name: Column.ofStringArray(this.names),
  149. type: Column.ofStringAliasArray(this.types),
  150. mon_nstd_flag: Column.ofStringAliasArray(this.mon_nstd_flags),
  151. }, this.ids.length);
  152. }
  153. setNames(names: [string, string][]) {
  154. names.forEach(n => this.namesMap.set(n[0], n[1]));
  155. }
  156. constructor(private seqId: Column<number>, private atomId: Column<string>) {
  157. }
  158. }