mmcif.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 } from '../structure'
  10. import { _atom_site } from './categories/atom_site';
  11. import CifCategory = CifWriter.Category
  12. import { _struct_conf, _struct_sheet_range } from './categories/secondary-structure';
  13. import { _pdbx_struct_mod_residue } from './categories/modified-residues';
  14. import { _chem_comp, _pdbx_chem_comp_identifier, _pdbx_nonpoly_scheme } from './categories/misc';
  15. import { Model } from '../model';
  16. import { getUniqueEntityIndicesFromStructures, copy_mmCif_category } from './categories/utils';
  17. import { _struct_asym, _entity_poly, _entity_poly_seq } from './categories/sequence';
  18. import { ModelPropertyDescriptor } from '../model/properties/custom';
  19. export interface CifExportContext {
  20. structures: Structure[],
  21. firstModel: Model,
  22. cache: any
  23. }
  24. export namespace CifExportContext {
  25. export function create(structures: Structure | Structure[]): CifExportContext {
  26. const structureArray = Array.isArray(structures) ? structures : [structures];
  27. return {
  28. structures: structureArray,
  29. firstModel: structureArray[0].model,
  30. cache: Object.create(null)
  31. };
  32. }
  33. }
  34. const _entity: CifCategory<CifExportContext> = {
  35. name: 'entity',
  36. instance({ structures }) {
  37. const indices = getUniqueEntityIndicesFromStructures(structures);
  38. return CifCategory.ofTable(structures[0].model.entities.data, indices);
  39. }
  40. }
  41. const Categories = [
  42. // Basics
  43. copy_mmCif_category('entry'),
  44. copy_mmCif_category('exptl'),
  45. _entity,
  46. // Symmetry
  47. copy_mmCif_category('cell'),
  48. copy_mmCif_category('symmetry'),
  49. // Assemblies
  50. copy_mmCif_category('pdbx_struct_assembly'),
  51. copy_mmCif_category('pdbx_struct_assembly_gen'),
  52. copy_mmCif_category('pdbx_struct_oper_list'),
  53. // Secondary structure
  54. _struct_conf,
  55. _struct_sheet_range,
  56. // Sequence
  57. _struct_asym,
  58. _entity_poly,
  59. _entity_poly_seq,
  60. // Branch
  61. copy_mmCif_category('pdbx_entity_branch'),
  62. copy_mmCif_category('pdbx_entity_branch_link'),
  63. copy_mmCif_category('pdbx_branch_scheme'),
  64. // Misc
  65. // TODO: filter for actual present residues?
  66. _chem_comp,
  67. _pdbx_chem_comp_identifier,
  68. copy_mmCif_category('atom_sites'),
  69. _pdbx_nonpoly_scheme,
  70. _pdbx_struct_mod_residue,
  71. // Atoms
  72. _atom_site
  73. ];
  74. namespace _Filters {
  75. export const AtomSitePositionsFieldNames = new Set<string>(<(keyof typeof mmCIF_Schema.atom_site)[]>['id', 'Cartn_x', 'Cartn_y', 'Cartn_z']);
  76. }
  77. export const mmCIF_Export_Filters = {
  78. onlyPositions: <CifWriter.Category.Filter>{
  79. includeCategory(name) { return name === 'atom_site'; },
  80. includeField(cat, field) { return _Filters.AtomSitePositionsFieldNames.has(field); }
  81. }
  82. }
  83. /** Doesn't start a data block */
  84. export function encode_mmCIF_categories(encoder: CifWriter.Encoder, structures: Structure | Structure[], params?: { skipCategoryNames?: Set<string>, exportCtx?: CifExportContext }) {
  85. const first = Array.isArray(structures) ? structures[0] : (structures as Structure);
  86. const models = first.models;
  87. if (models.length !== 1) throw 'Can\'t export stucture composed from multiple models.';
  88. const _params = params || { };
  89. const ctx: CifExportContext = params && params.exportCtx ? params.exportCtx : CifExportContext.create(structures);
  90. for (const cat of Categories) {
  91. if (_params.skipCategoryNames && _params.skipCategoryNames.has(cat.name)) continue;
  92. encoder.writeCategory(cat, ctx);
  93. }
  94. for (const customProp of models[0].customProperties.all) {
  95. if (!customProp.cifExport || customProp.cifExport.categories.length === 0) continue;
  96. const prefix = customProp.cifExport.prefix;
  97. const cats = customProp.cifExport.categories;
  98. let propCtx = ctx;
  99. if (customProp.cifExport.context) {
  100. const propId = ModelPropertyDescriptor.getUUID(customProp);
  101. if (ctx.cache[propId + '__ctx']) propCtx = ctx.cache[propId + '__ctx'];
  102. else {
  103. propCtx = customProp.cifExport.context(ctx) || ctx;
  104. ctx.cache[propId + '__ctx'] = propCtx;
  105. }
  106. }
  107. for (const cat of cats) {
  108. if (_params.skipCategoryNames && _params.skipCategoryNames.has(cat.name)) continue;
  109. if (cat.name.indexOf(prefix) !== 0) throw new Error(`Custom category '${cat.name}' name must start with prefix '${prefix}.'`);
  110. encoder.writeCategory(cat, propCtx);
  111. }
  112. }
  113. }
  114. function to_mmCIF(name: string, structure: Structure, asBinary = false) {
  115. const enc = CifWriter.createEncoder({ binary: asBinary });
  116. enc.startDataBlock(name);
  117. encode_mmCIF_categories(enc, structure);
  118. return enc.getData();
  119. }
  120. export default to_mmCIF