mmcif.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Copyright (c) 2017-2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { CifWriter } from 'mol-io/writer/cif'
  8. // import { mmCIF_Schema } from 'mol-io/reader/cif/schema/mmcif'
  9. import { Structure, Element } from '../structure'
  10. import { Model } from '../model'
  11. import P from '../query/properties'
  12. interface Context {
  13. structure: Structure,
  14. model: Model
  15. }
  16. import CifField = CifWriter.Field
  17. import CifCategory = CifWriter.Category
  18. const atom_site_fields: CifField<Element.Location>[] = [
  19. CifField.str('group_PDB', P.residue.group_PDB),
  20. CifField.int('id', P.atom.id),
  21. CifField.str('type_symbol', P.atom.type_symbol as any),
  22. CifField.str('label_atom_id', P.atom.label_atom_id),
  23. CifField.str('label_alt_id', P.atom.label_alt_id),
  24. CifField.str('label_comp_id', P.residue.label_comp_id),
  25. CifField.int('label_seq_id', P.residue.label_seq_id),
  26. CifField.str('pdbx_PDB_ins_code', P.residue.pdbx_PDB_ins_code),
  27. CifField.str('label_asym_id', P.chain.label_asym_id),
  28. CifField.str('label_entity_id', P.chain.label_entity_id),
  29. CifField.float('Cartn_x', P.atom.x),
  30. CifField.float('Cartn_y', P.atom.y),
  31. CifField.float('Cartn_z', P.atom.z),
  32. CifField.float('occupancy', P.atom.occupancy),
  33. CifField.int('pdbx_formal_charge', P.atom.pdbx_formal_charge),
  34. CifField.str('auth_atom_id', P.atom.auth_atom_id),
  35. CifField.str('auth_comp_id', P.residue.auth_comp_id),
  36. CifField.int('auth_seq_id', P.residue.auth_seq_id),
  37. CifField.str('auth_asym_id', P.chain.auth_asym_id),
  38. CifField.int('pdbx_PDB_model_num', P.unit.model_num),
  39. CifField.str('operator_name', P.unit.operator_name)
  40. ];
  41. function entityProvider({ model }: Context): CifCategory {
  42. return CifCategory.ofTable('entity', model.entities.data);
  43. }
  44. function atomSiteProvider({ structure }: Context): CifCategory {
  45. return {
  46. data: void 0,
  47. name: 'atom_site',
  48. fields: atom_site_fields,
  49. rowCount: structure.elementCount,
  50. keys: () => structure.elementLocations()
  51. }
  52. }
  53. /** Doesn't start a data block */
  54. export function encode_mmCIF_categories(encoder: CifWriter.Encoder, structure: Structure) {
  55. const models = Structure.getModels(structure);
  56. if (models.length !== 1) throw 'Can\'t export stucture composed from multiple models.';
  57. const model = models[0];
  58. const ctx: Context = { structure, model };
  59. encoder.writeCategory(entityProvider, [ctx]);
  60. encoder.writeCategory(atomSiteProvider, [ctx]);
  61. }
  62. function to_mmCIF(name: string, structure: Structure, asBinary = false) {
  63. const enc = CifWriter.createEncoder({ binary: asBinary });
  64. enc.startDataBlock(name);
  65. encode_mmCIF_categories(enc, structure);
  66. return enc.getData();
  67. }
  68. export default to_mmCIF