mol2.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * Copyright (c) 2020-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Column, Table } from '../../mol-data/db';
  7. import { Model, Symmetry } from '../../mol-model/structure/model';
  8. import { BondType, MoleculeType } from '../../mol-model/structure/model/types';
  9. import { RuntimeContext, Task } from '../../mol-task';
  10. import { createModels } from './basic/parser';
  11. import { BasicSchema, createBasic } from './basic/schema';
  12. import { ComponentBuilder } from './common/component';
  13. import { EntityBuilder } from './common/entity';
  14. import { ModelFormat } from '../format';
  15. import { IndexPairBonds } from './property/bonds/index-pair';
  16. import { Mol2Crysin, Mol2File } from '../../mol-io/reader/mol2/schema';
  17. import { AtomPartialCharge } from './property/partial-charge';
  18. import { Trajectory, ArrayTrajectory } from '../../mol-model/structure';
  19. import { guessElementSymbolString } from './util';
  20. import { ModelSymmetry } from './property/symmetry';
  21. import { Spacegroup, SpacegroupCell } from '../../mol-math/geometry';
  22. import { Vec3 } from '../../mol-math/linear-algebra';
  23. async function getModels(mol2: Mol2File, ctx: RuntimeContext) {
  24. const models: Model[] = [];
  25. for (let i = 0, il = mol2.structures.length; i < il; ++i) {
  26. const { molecule, atoms, bonds, crysin } = mol2.structures[i];
  27. const A = Column.ofConst('A', atoms.count, Column.Schema.str);
  28. const type_symbol = new Array<string>(atoms.count);
  29. let hasAtomType = false;
  30. for (let i = 0; i < atoms.count; ++i) {
  31. if (atoms.atom_type.value(i).includes('.')) {
  32. hasAtomType = true;
  33. break;
  34. }
  35. }
  36. for (let i = 0; i < atoms.count; ++i) {
  37. type_symbol[i] = hasAtomType
  38. ? atoms.atom_type.value(i).split('.')[0].toUpperCase()
  39. : guessElementSymbolString(atoms.atom_name.value(i), atoms.subst_name.value(i));
  40. }
  41. const atom_site = Table.ofPartialColumns(BasicSchema.atom_site, {
  42. auth_asym_id: A,
  43. auth_atom_id: Column.asArrayColumn(atoms.atom_name),
  44. auth_comp_id: atoms.subst_name,
  45. auth_seq_id: atoms.subst_id,
  46. Cartn_x: Column.asArrayColumn(atoms.x, Float32Array),
  47. Cartn_y: Column.asArrayColumn(atoms.y, Float32Array),
  48. Cartn_z: Column.asArrayColumn(atoms.z, Float32Array),
  49. id: Column.asArrayColumn(atoms.atom_id),
  50. label_asym_id: A,
  51. label_atom_id: Column.asArrayColumn(atoms.atom_name),
  52. label_comp_id: atoms.subst_name,
  53. label_seq_id: atoms.subst_id,
  54. label_entity_id: Column.ofConst('1', atoms.count, Column.Schema.str),
  55. occupancy: Column.ofConst(1, atoms.count, Column.Schema.float),
  56. type_symbol: Column.ofStringArray(type_symbol),
  57. pdbx_PDB_model_num: Column.ofConst(i, atoms.count, Column.Schema.int),
  58. }, atoms.count);
  59. const entityBuilder = new EntityBuilder();
  60. entityBuilder.setNames([['MOL', molecule.mol_name || 'Unknown Entity']]);
  61. entityBuilder.getEntityId('MOL', MoleculeType.Unknown, 'A');
  62. const componentBuilder = new ComponentBuilder(atoms.subst_id, atoms.atom_name);
  63. for (let i = 0, il = atoms.subst_name.rowCount; i < il; ++i) {
  64. componentBuilder.add(atoms.subst_name.value(i), i);
  65. }
  66. const basic = createBasic({
  67. entity: entityBuilder.getEntityTable(),
  68. chem_comp: componentBuilder.getChemCompTable(),
  69. atom_site
  70. });
  71. const _models = await createModels(basic, Mol2Format.create(mol2), ctx);
  72. if (_models.frameCount > 0) {
  73. const indexA = Column.ofIntArray(Column.mapToArray(bonds.origin_atom_id, x => x - 1, Int32Array));
  74. const indexB = Column.ofIntArray(Column.mapToArray(bonds.target_atom_id, x => x - 1, Int32Array));
  75. const key = bonds.bond_id;
  76. const order = Column.ofIntArray(Column.mapToArray(bonds.bond_type, x => {
  77. switch (x) {
  78. case 'ar': // aromatic
  79. case 'am': // amide
  80. case 'un': // unknown
  81. return 1;
  82. case 'du': // dummy
  83. case 'nc': // not connected
  84. return 0;
  85. default:
  86. return parseInt(x);
  87. }
  88. }, Int8Array));
  89. const flag = Column.ofIntArray(Column.mapToArray(bonds.bond_type, x => {
  90. switch (x) {
  91. case 'ar': // aromatic
  92. case 'am': // amide
  93. return BondType.Flag.Aromatic | BondType.Flag.Covalent;
  94. case 'du': // dummy
  95. case 'nc': // not connected
  96. return BondType.Flag.None;
  97. case 'un': // unknown
  98. default:
  99. return BondType.Flag.Covalent;
  100. }
  101. }, Int8Array));
  102. const pairBonds = IndexPairBonds.fromData(
  103. { pairs: { key, indexA, indexB, order, flag }, count: atoms.count },
  104. { maxDistance: crysin ? -1 : Infinity }
  105. );
  106. const first = _models.representative;
  107. IndexPairBonds.Provider.set(first, pairBonds);
  108. AtomPartialCharge.Provider.set(first, {
  109. data: atoms.charge,
  110. type: molecule.charge_type
  111. });
  112. if (crysin) {
  113. const symmetry = getSymmetry(crysin);
  114. if (symmetry) ModelSymmetry.Provider.set(first, symmetry);
  115. }
  116. models.push(first);
  117. }
  118. }
  119. return new ArrayTrajectory(models);
  120. }
  121. function getSymmetry(crysin: Mol2Crysin): Symmetry | undefined {
  122. // TODO handle `crysin.setting`
  123. if (crysin.setting !== 1) return;
  124. const spaceCell = SpacegroupCell.create(
  125. crysin.spaceGroup,
  126. Vec3.create(crysin.a, crysin.b, crysin.c),
  127. Vec3.scale(Vec3(), Vec3.create(crysin.alpha, crysin.beta, crysin.gamma), Math.PI / 180)
  128. );
  129. return {
  130. spacegroup: Spacegroup.create(spaceCell),
  131. assemblies: [],
  132. isNonStandardCrystalFrame: false,
  133. ncsOperators: []
  134. };
  135. }
  136. //
  137. export { Mol2Format };
  138. type Mol2Format = ModelFormat<Mol2File>
  139. namespace Mol2Format {
  140. export function is(x?: ModelFormat): x is Mol2Format {
  141. return x?.kind === 'mol2';
  142. }
  143. export function create(mol2: Mol2File): Mol2Format {
  144. return { kind: 'mol2', name: mol2.name, data: mol2 };
  145. }
  146. }
  147. export function trajectoryFromMol2(mol2: Mol2File): Task<Trajectory> {
  148. return Task.create('Parse MOL2', ctx => getModels(mol2, ctx));
  149. }