gro.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 { Model } from '../../mol-model/structure/model';
  7. import { Task } from '../../mol-task';
  8. import { ModelFormat } from '../format';
  9. import { GroFile, GroAtoms } from '../../mol-io/reader/gro/schema';
  10. import { Column, Table } from '../../mol-data/db';
  11. import { guessElementSymbolString } from './util';
  12. import { MoleculeType, getMoleculeType } from '../../mol-model/structure/model/types';
  13. import { ComponentBuilder } from './common/component';
  14. import { getChainId } from './common/util';
  15. import { EntityBuilder } from './common/entity';
  16. import { BasicData, BasicSchema, createBasic } from './basic/schema';
  17. import { createModels } from './basic/parser';
  18. import { Trajectory } from '../../mol-model/structure';
  19. import { ArrayTrajectory } from '../../mol-model/structure/trajectory';
  20. function getBasic(atoms: GroAtoms, modelNum: number): BasicData {
  21. const auth_atom_id = atoms.atomName;
  22. const auth_comp_id = 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 = Column.ofStringArray(asymIds);
  61. const atom_site = Table.ofPartialColumns(BasicSchema.atom_site, {
  62. auth_asym_id,
  63. auth_atom_id,
  64. auth_comp_id,
  65. auth_seq_id: atoms.residueNumber,
  66. Cartn_x: Column.ofFloatArray(Column.mapToArray(atoms.x, x => x * 10, Float32Array)),
  67. Cartn_y: Column.ofFloatArray(Column.mapToArray(atoms.y, y => y * 10, Float32Array)),
  68. Cartn_z: Column.ofFloatArray(Column.mapToArray(atoms.z, z => z * 10, Float32Array)),
  69. id: Column.ofIntArray(ids),
  70. label_asym_id: auth_asym_id,
  71. label_atom_id: auth_atom_id,
  72. label_comp_id: auth_comp_id,
  73. label_seq_id: Column.ofIntArray(seqIds),
  74. label_entity_id: Column.ofStringArray(entityIds),
  75. occupancy: Column.ofConst(1, atoms.count, Column.Schema.float),
  76. type_symbol: Column.ofStringArray(Column.mapToArray(atoms.atomName, s => guessElementSymbolString(s))),
  77. pdbx_PDB_model_num: Column.ofConst(modelNum, atoms.count, Column.Schema.int),
  78. }, atoms.count);
  79. return createBasic({
  80. entity: entityBuilder.getEntityTable(),
  81. chem_comp: componentBuilder.getChemCompTable(),
  82. atom_site
  83. });
  84. }
  85. //
  86. export { GroFormat };
  87. type GroFormat = ModelFormat<GroFile>
  88. namespace GroFormat {
  89. export function is(x?: ModelFormat): x is GroFormat {
  90. return x?.kind === 'gro';
  91. }
  92. export function fromGro(gro: GroFile): GroFormat {
  93. return { kind: 'gro', name: gro.structures[0].header.title, data: gro };
  94. }
  95. }
  96. // TODO reuse static model parts when hierarchy is identical
  97. // need to pass all gro.structures as one table into createModels
  98. export function trajectoryFromGRO(gro: GroFile): Task<Trajectory> {
  99. return Task.create('Parse GRO', async ctx => {
  100. const format = GroFormat.fromGro(gro);
  101. const models: Model[] = [];
  102. for (let i = 0, il = gro.structures.length; i < il; ++i) {
  103. const basic = getBasic(gro.structures[i].atoms, i + 1);
  104. const m = await createModels(basic, format, ctx);
  105. if (m.frameCount === 1) {
  106. models.push(m.representative);
  107. }
  108. }
  109. return new ArrayTrajectory(models);
  110. });
  111. }