gro.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 } from '../../mol-model/structure/model/types';
  16. import { ComponentBuilder } from './common/component';
  17. import { getChainId } from './common/util';
  18. import { EntityBuilder } from './common/entity';
  19. // TODO multi model files
  20. function getCategories(atoms: GroAtoms) {
  21. const auth_atom_id = CifField.ofColumn(atoms.atomName)
  22. const auth_comp_id = CifField.ofColumn(atoms.residueName)
  23. const entityIds = new Array<string>(atoms.count)
  24. const asymIds = new Array<string>(atoms.count)
  25. const seqIds = new Uint32Array(atoms.count)
  26. const ids = new Uint32Array(atoms.count)
  27. const entityBuilder = new EntityBuilder()
  28. const componentBuilder = new ComponentBuilder(atoms.residueNumber, atoms.atomName)
  29. let currentEntityId = ''
  30. let currentAsymIndex = 0
  31. let currentAsymId = ''
  32. let currentSeqId = 0
  33. let prevMoleculeType = MoleculeType.Unknown
  34. let prevResidueNumber = -1
  35. for (let i = 0, il = atoms.count; i < il; ++i) {
  36. const residueNumber = atoms.residueNumber.value(i)
  37. if (residueNumber !== prevResidueNumber) {
  38. const compId = atoms.residueName.value(i)
  39. const moleculeType = getMoleculeType(componentBuilder.add(compId, i).type, compId)
  40. if (moleculeType !== prevMoleculeType || (
  41. residueNumber !== prevResidueNumber + 1 && !(
  42. // gro format allows only for 5 character residueNumbers, handle overflow here
  43. prevResidueNumber === 99999 && residueNumber === 0
  44. )
  45. )) {
  46. currentAsymId = getChainId(currentAsymIndex)
  47. currentAsymIndex += 1
  48. currentSeqId = 0
  49. }
  50. currentEntityId = entityBuilder.getEntityId(compId, moleculeType, currentAsymId)
  51. currentSeqId += 1
  52. prevResidueNumber = residueNumber
  53. prevMoleculeType = moleculeType
  54. }
  55. entityIds[i] = currentEntityId
  56. asymIds[i] = currentAsymId
  57. seqIds[i] = currentSeqId
  58. ids[i] = i
  59. }
  60. const auth_asym_id = CifField.ofColumn(Column.ofStringArray(asymIds))
  61. const atom_site: CifCategory.SomeFields<mmCIF_Schema['atom_site']> = {
  62. auth_asym_id,
  63. auth_atom_id,
  64. auth_comp_id,
  65. auth_seq_id: CifField.ofColumn(atoms.residueNumber),
  66. B_iso_or_equiv: CifField.ofColumn(Column.Undefined(atoms.count, Column.Schema.float)),
  67. Cartn_x: CifField.ofNumbers(Column.mapToArray(atoms.x, x => x * 10, Float32Array)),
  68. Cartn_y: CifField.ofNumbers(Column.mapToArray(atoms.y, y => y * 10, Float32Array)),
  69. Cartn_z: CifField.ofNumbers(Column.mapToArray(atoms.z, z => z * 10, Float32Array)),
  70. group_PDB: CifField.ofColumn(Column.Undefined(atoms.count, Column.Schema.str)),
  71. id: CifField.ofColumn(Column.ofIntArray(ids)),
  72. label_alt_id: CifField.ofColumn(Column.Undefined(atoms.count, Column.Schema.str)),
  73. label_asym_id: auth_asym_id,
  74. label_atom_id: auth_atom_id,
  75. label_comp_id: auth_comp_id,
  76. label_seq_id: CifField.ofColumn(Column.ofIntArray(seqIds)),
  77. label_entity_id: CifField.ofColumn(Column.ofStringArray(entityIds)),
  78. occupancy: CifField.ofColumn(Column.ofConst(1, atoms.count, Column.Schema.float)),
  79. type_symbol: CifField.ofStrings(Column.mapToArray(atoms.atomName, s => guessElementSymbolString(s))),
  80. pdbx_PDB_ins_code: CifField.ofColumn(Column.Undefined(atoms.count, Column.Schema.str)),
  81. pdbx_PDB_model_num: CifField.ofColumn(Column.ofConst('1', atoms.count, Column.Schema.str)),
  82. }
  83. return {
  84. entity: entityBuilder.getEntityCategory(),
  85. chem_comp: componentBuilder.getChemCompCategory(),
  86. atom_site: CifCategory.ofFields('atom_site', atom_site)
  87. }
  88. }
  89. function groToMmCif(gro: GroFile) {
  90. const categories = getCategories(gro.structures[0].atoms)
  91. return {
  92. header: gro.structures[0].header.title,
  93. categoryNames: Object.keys(categories),
  94. categories
  95. };
  96. }
  97. export function trajectoryFromGRO(gro: GroFile): Task<Model.Trajectory> {
  98. return Task.create('Parse GRO', async ctx => {
  99. await ctx.update('Converting to mmCIF');
  100. const cif = groToMmCif(gro);
  101. const format = ModelFormat.mmCIF(cif);
  102. return _parse_mmCif(format, ctx);
  103. })
  104. }