gro.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Model } from '../../mol-model/structure/model';
  7. import { Task } from '../../mol-task';
  8. import { ModelFormat } from './format';
  9. import { _parse_mmCif } from './mmcif/parser';
  10. import { GroFile, GroAtoms } from '../../mol-io/reader/gro/schema';
  11. import { CifCategory, CifField } from '../../mol-io/reader/cif';
  12. import { Column } from '../../mol-data/db';
  13. import { mmCIF_Schema } from '../../mol-io/reader/cif/schema/mmcif';
  14. import { guessElementSymbolString } from './util';
  15. import { MoleculeType, getMoleculeType, isPolymer } from '../../mol-model/structure/model/types';
  16. import { ComponentBuilder } from './common/component';
  17. import { getChainId } from './common/util';
  18. // TODO multi model files
  19. class EntityBuilder {
  20. private count = 0
  21. private ids: string[] = []
  22. private types: string[] = []
  23. private descriptions: string[] = []
  24. private heteroMap = new Map<string, string>()
  25. private chainMap = new Map<string, string>()
  26. private waterId?: string
  27. private set(type: string, description: string) {
  28. this.count += 1
  29. this.ids.push(`${this.count}`)
  30. this.types.push(type)
  31. this.descriptions.push(description)
  32. }
  33. getEntityId(compId: string, moleculeType: MoleculeType, chainId: string): string {
  34. if (moleculeType === MoleculeType.water) {
  35. if (this.waterId === undefined) {
  36. this.set('water', 'Water')
  37. this.waterId = `${this.count}`
  38. }
  39. return this.waterId;
  40. } else if (isPolymer(moleculeType)) {
  41. if (!this.chainMap.has(chainId)) {
  42. this.set('polymer', `Polymer ${this.chainMap.size + 1}`)
  43. this.chainMap.set(chainId, `${this.count}`)
  44. }
  45. return this.chainMap.get(chainId)!
  46. } else {
  47. if (!this.heteroMap.has(compId)) {
  48. this.set('non-polymer', compId)
  49. this.heteroMap.set(compId, `${this.count}`)
  50. }
  51. return this.heteroMap.get(compId)!
  52. }
  53. }
  54. getEntityCategory() {
  55. const entity: CifCategory.SomeFields<mmCIF_Schema['entity']> = {
  56. id: CifField.ofStrings(this.ids),
  57. type: CifField.ofStrings(this.types),
  58. pdbx_description: CifField.ofStrings(this.descriptions),
  59. }
  60. return CifCategory.ofFields('entity', entity)
  61. }
  62. }
  63. function getCategories(atoms: GroAtoms) {
  64. const auth_atom_id = CifField.ofColumn(atoms.atomName)
  65. const auth_comp_id = CifField.ofColumn(atoms.residueName)
  66. const entityIds = new Array<string>(atoms.count)
  67. const asymIds = new Array<string>(atoms.count)
  68. const seqIds = new Uint32Array(atoms.count)
  69. const ids = new Uint32Array(atoms.count)
  70. const entityBuilder = new EntityBuilder()
  71. const componentBuilder = new ComponentBuilder(atoms.residueNumber, atoms.atomName)
  72. let currentEntityId = ''
  73. let currentAsymIndex = 0
  74. let currentAsymId = ''
  75. let currentSeqId = 0
  76. let prevMoleculeType = MoleculeType.unknown
  77. let prevResidueNumber = -1
  78. for (let i = 0, il = atoms.count; i < il; ++i) {
  79. const residueNumber = atoms.residueNumber.value(i)
  80. if (residueNumber !== prevResidueNumber) {
  81. const compId = atoms.residueName.value(i)
  82. const moleculeType = getMoleculeType(componentBuilder.add(compId, i).type, compId)
  83. if (moleculeType !== prevMoleculeType || (
  84. residueNumber !== prevResidueNumber + 1 && !(
  85. // gro format allows only for 5 character residueNumbers, handle overflow here
  86. prevResidueNumber === 99999 && residueNumber === 0
  87. )
  88. )) {
  89. currentAsymId = getChainId(currentAsymIndex)
  90. currentAsymIndex += 1
  91. currentSeqId = 0
  92. }
  93. currentEntityId = entityBuilder.getEntityId(compId, moleculeType, currentAsymId)
  94. currentSeqId += 1
  95. prevResidueNumber = residueNumber
  96. prevMoleculeType = moleculeType
  97. }
  98. entityIds[i] = currentEntityId
  99. asymIds[i] = currentAsymId
  100. seqIds[i] = currentSeqId
  101. ids[i] = i
  102. }
  103. const auth_asym_id = CifField.ofColumn(Column.ofStringArray(asymIds))
  104. const atom_site: CifCategory.SomeFields<mmCIF_Schema['atom_site']> = {
  105. auth_asym_id,
  106. auth_atom_id,
  107. auth_comp_id,
  108. auth_seq_id: CifField.ofColumn(atoms.residueNumber),
  109. B_iso_or_equiv: CifField.ofColumn(Column.Undefined(atoms.count, Column.Schema.float)),
  110. Cartn_x: CifField.ofNumbers(Column.mapToArray(atoms.x, x => x * 10, Float32Array)),
  111. Cartn_y: CifField.ofNumbers(Column.mapToArray(atoms.y, y => y * 10, Float32Array)),
  112. Cartn_z: CifField.ofNumbers(Column.mapToArray(atoms.z, z => z * 10, Float32Array)),
  113. group_PDB: CifField.ofColumn(Column.Undefined(atoms.count, Column.Schema.str)),
  114. id: CifField.ofColumn(Column.ofIntArray(ids)),
  115. label_alt_id: CifField.ofColumn(Column.Undefined(atoms.count, Column.Schema.str)),
  116. label_asym_id: auth_asym_id,
  117. label_atom_id: auth_atom_id,
  118. label_comp_id: auth_comp_id,
  119. label_seq_id: CifField.ofColumn(Column.ofIntArray(seqIds)),
  120. label_entity_id: CifField.ofColumn(Column.ofStringArray(entityIds)),
  121. occupancy: CifField.ofColumn(Column.ofConst(1, atoms.count, Column.Schema.float)),
  122. type_symbol: CifField.ofStrings(Column.mapToArray(atoms.atomName, s => guessElementSymbolString(s))),
  123. pdbx_PDB_ins_code: CifField.ofColumn(Column.Undefined(atoms.count, Column.Schema.str)),
  124. pdbx_PDB_model_num: CifField.ofColumn(Column.ofConst('1', atoms.count, Column.Schema.str)),
  125. }
  126. return {
  127. entity: entityBuilder.getEntityCategory(),
  128. chem_comp: componentBuilder.getChemCompCategory(),
  129. atom_site: CifCategory.ofFields('atom_site', atom_site)
  130. }
  131. }
  132. async function groToMmCif(gro: GroFile) {
  133. const categories = getCategories(gro.structures[0].atoms)
  134. return {
  135. header: gro.structures[0].header.title,
  136. categoryNames: Object.keys(categories),
  137. categories
  138. };
  139. }
  140. export function trajectoryFromGRO(gro: GroFile): Task<Model.Trajectory> {
  141. return Task.create('Parse GRO', async ctx => {
  142. await ctx.update('Converting to mmCIF');
  143. const cif = await groToMmCif(gro);
  144. const format = ModelFormat.mmCIF(cif);
  145. return _parse_mmCif(format, ctx);
  146. })
  147. }