encoder.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Sebastian Bittrich <sebastian.bittrich@rcsb.org>
  5. */
  6. import { StringBuilder } from '../../../mol-util';
  7. import { Category } from '../cif/encoder';
  8. import { getCategoryInstanceData } from '../cif/encoder/util';
  9. import { LigandEncoder } from '../ligand-encoder';
  10. // specification: http://c4.cabrillo.edu/404/ctfile.pdf
  11. // SDF wraps MOL and allows for multiple molecules per file as well as additional properties
  12. export class MolEncoder extends LigandEncoder {
  13. _writeCategory<Ctx>(category: Category<Ctx>, context?: Ctx) {
  14. // use separate builder because we still need to write Counts and Bonds line
  15. const ctab = StringBuilder.create();
  16. const bonds = StringBuilder.create();
  17. // write Atom block and gather data for Bonds and Charges
  18. const { instance, source } = getCategoryInstanceData(category, context);
  19. // write header
  20. const name = this.getName(instance, source);
  21. // 3rd lines must be present and can contain comments
  22. StringBuilder.writeSafe(this.builder, `${name}\n ${this.encoder}\n\n`);
  23. const atomMap = this.componentAtomData.entries.get(name)!;
  24. const bondMap = this.componentBondData.entries.get(name)!;
  25. // happens for the unknown ligands (UNL)
  26. if (!atomMap) throw Error(`The Chemical Component Dictionary doesn't hold any atom data for ${name}`);
  27. let bondCount = 0;
  28. let chiral = false;
  29. // traverse once to determine all actually present atoms
  30. const atoms = this.getAtoms(instance, source);
  31. atoms.forEach((atom1, label_atom_id1) => {
  32. const { index: i1, type_symbol: type_symbol1 } = atom1;
  33. const atomMapData1 = atomMap.map.get(label_atom_id1);
  34. if (!atomMapData1) {
  35. if (this.isHydrogen(type_symbol1)) {
  36. return;
  37. } else {
  38. throw Error(`Unknown atom ${label_atom_id1} for component ${name}`);
  39. }
  40. }
  41. const { charge, stereo_config } = atomMapData1;
  42. StringBuilder.writePadLeft(ctab, atom1.Cartn_x.toFixed(4), 10);
  43. StringBuilder.writePadLeft(ctab, atom1.Cartn_y.toFixed(4), 10);
  44. StringBuilder.writePadLeft(ctab, atom1.Cartn_z.toFixed(4), 10);
  45. StringBuilder.whitespace1(ctab);
  46. StringBuilder.writePadRight(ctab, atom1.type_symbol, 2);
  47. StringBuilder.writeSafe(ctab, ' 0');
  48. StringBuilder.writeIntegerPadLeft(ctab, this.mapCharge(charge), 3);
  49. StringBuilder.writeSafe(ctab, ' 0 0 0 0 0 0 0 0 0 0\n');
  50. if (stereo_config !== 'n') chiral = true;
  51. // no data for metal ions
  52. if (!bondMap?.map) return;
  53. bondMap.map.get(label_atom_id1)!.forEach((bond, label_atom_id2) => {
  54. const atom2 = atoms.get(label_atom_id2);
  55. if (!atom2) return;
  56. const { index: i2 } = atom2;
  57. if (i1 < i2) {
  58. const { order } = bond;
  59. StringBuilder.writeIntegerPadLeft(bonds, i1 + 1, 3);
  60. StringBuilder.writeIntegerPadLeft(bonds, i2 + 1, 3);
  61. StringBuilder.writeIntegerPadLeft(bonds, order, 3);
  62. StringBuilder.writeSafe(bonds, ' 0 0 0 0\n');
  63. bondCount++;
  64. }
  65. });
  66. });
  67. // write counts line
  68. StringBuilder.writeIntegerPadLeft(this.builder, atoms.size, 3);
  69. StringBuilder.writeIntegerPadLeft(this.builder, bondCount, 3);
  70. StringBuilder.writeSafe(this.builder, ` 0 0 ${chiral ? 1 : 0} 0 0 0 0 0 0\n`);
  71. StringBuilder.writeSafe(this.builder, StringBuilder.getString(ctab));
  72. StringBuilder.writeSafe(this.builder, StringBuilder.getString(bonds));
  73. StringBuilder.writeSafe(this.builder, 'M END\n');
  74. }
  75. private mapCharge(raw: number): number {
  76. // 0 = uncharged or value other than these, 1 = +3, 2 = +2, 3 = +1, 4 = doublet radical, 5 = -1, 6 = -2, 7 = -3
  77. switch (raw) {
  78. case 3: return 1;
  79. case 2: return 2;
  80. case 1: return 3;
  81. case -1: return 5;
  82. case -2: return 6;
  83. case -3: return 7;
  84. default: return 0;
  85. }
  86. }
  87. protected writeFullCategory<Ctx>(sb: StringBuilder, category: Category<Ctx>, context?: Ctx) {
  88. const { instance, source } = getCategoryInstanceData(category, context);
  89. const fields = instance.fields;
  90. const src = source[0];
  91. if (!src) return;
  92. const data = src.data;
  93. const it = src.keys();
  94. const key = it.move();
  95. for (let _f = 0; _f < fields.length; _f++) {
  96. const f = fields[_f]!;
  97. StringBuilder.writeSafe(sb, `> <${category.name}.${f.name}>\n`);
  98. const val = f.value(key, data, 0);
  99. StringBuilder.writeSafe(sb, val as string);
  100. StringBuilder.writeSafe(sb, '\n\n');
  101. }
  102. }
  103. encode() {
  104. // write meta-information, do so after ctab
  105. if (this.error || this.metaInformation) {
  106. StringBuilder.writeSafe(this.builder, StringBuilder.getString(this.meta));
  107. }
  108. // terminate file (needed for SDF only)
  109. if (!!this.terminator) {
  110. StringBuilder.writeSafe(this.builder, `${this.terminator}\n`);
  111. }
  112. this.encoded = true;
  113. }
  114. constructor(encoder: string, metaInformation: boolean, hydrogens: boolean, readonly terminator: string = '') {
  115. super(encoder, metaInformation, hydrogens);
  116. if (metaInformation && !terminator) {
  117. throw new Error('meta-information cannot be written for MOL files');
  118. }
  119. }
  120. }