mmcif.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 { _chem_comp, _pdbx_chem_comp_identifier, _pdbx_nonpoly_scheme } from './categories/misc';
  14. import { Model } from '../model';
  15. import { getUniqueEntityIndicesFromStructures, copy_mmCif_category } from './categories/utils';
  16. import { _struct_asym, _entity_poly, _entity_poly_seq } from './categories/sequence';
  17. import { CustomPropertyDescriptor } from '../common/custom-property';
  18. import { atom_site_operator_mapping } from './categories/atom_site_operator_mapping';
  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. function isWithoutSymmetry(structure: Structure) {
  42. return structure.units.every(u => u.conformation.operator.isIdentity)
  43. }
  44. const Categories = [
  45. // Basics
  46. copy_mmCif_category('entry'),
  47. copy_mmCif_category('exptl'),
  48. _entity,
  49. // Symmetry
  50. copy_mmCif_category('cell', isWithoutSymmetry),
  51. copy_mmCif_category('symmetry', isWithoutSymmetry),
  52. // Assemblies
  53. copy_mmCif_category('pdbx_struct_assembly', isWithoutSymmetry),
  54. copy_mmCif_category('pdbx_struct_assembly_gen', isWithoutSymmetry),
  55. copy_mmCif_category('pdbx_struct_oper_list', isWithoutSymmetry),
  56. // Secondary structure
  57. _struct_conf,
  58. _struct_sheet_range,
  59. // Sequence
  60. _struct_asym,
  61. _entity_poly,
  62. _entity_poly_seq,
  63. // Branch
  64. copy_mmCif_category('pdbx_entity_branch'),
  65. copy_mmCif_category('pdbx_entity_branch_link'),
  66. copy_mmCif_category('pdbx_branch_scheme'),
  67. // Misc
  68. // TODO: filter for actual present residues?
  69. _chem_comp,
  70. _pdbx_chem_comp_identifier,
  71. copy_mmCif_category('atom_sites'),
  72. _pdbx_nonpoly_scheme,
  73. // Atoms
  74. _atom_site
  75. ];
  76. namespace _Filters {
  77. export const AtomSitePositionsFieldNames = new Set<string>(<(keyof typeof mmCIF_Schema.atom_site)[]>['id', 'Cartn_x', 'Cartn_y', 'Cartn_z']);
  78. }
  79. export const mmCIF_Export_Filters = {
  80. onlyPositions: <CifWriter.Category.Filter>{
  81. includeCategory(name) { return name === 'atom_site'; },
  82. includeField(cat, field) { return _Filters.AtomSitePositionsFieldNames.has(field); }
  83. }
  84. }
  85. function encodeCustomProp(customProp: CustomPropertyDescriptor, ctx: CifExportContext, encoder: CifWriter.Encoder, params: encode_mmCIF_categories_Params) {
  86. if (!customProp.cifExport || customProp.cifExport.categories.length === 0) return;
  87. const prefix = customProp.cifExport.prefix;
  88. const cats = customProp.cifExport.categories;
  89. let propCtx = ctx;
  90. if (customProp.cifExport.context) {
  91. const propId = CustomPropertyDescriptor.getUUID(customProp);
  92. if (ctx.cache[propId + '__ctx']) propCtx = ctx.cache[propId + '__ctx'];
  93. else {
  94. propCtx = customProp.cifExport.context(ctx) || ctx;
  95. ctx.cache[propId + '__ctx'] = propCtx;
  96. }
  97. }
  98. for (const cat of cats) {
  99. if (params.skipCategoryNames && params.skipCategoryNames.has(cat.name)) continue;
  100. if (cat.name.indexOf(prefix) !== 0) throw new Error(`Custom category '${cat.name}' name must start with prefix '${prefix}.'`);
  101. encoder.writeCategory(cat, propCtx);
  102. }
  103. }
  104. type encode_mmCIF_categories_Params = { skipCategoryNames?: Set<string>, exportCtx?: CifExportContext }
  105. /** Doesn't start a data block */
  106. export function encode_mmCIF_categories(encoder: CifWriter.Encoder, structures: Structure | Structure[], params?: encode_mmCIF_categories_Params) {
  107. const first = Array.isArray(structures) ? structures[0] : (structures as Structure);
  108. const models = first.models;
  109. if (models.length !== 1) throw 'Can\'t export stucture composed from multiple models.';
  110. const _params = params || { };
  111. const ctx: CifExportContext = params && params.exportCtx ? params.exportCtx : CifExportContext.create(structures);
  112. for (const cat of Categories) {
  113. if (_params.skipCategoryNames && _params.skipCategoryNames.has(cat.name)) continue;
  114. encoder.writeCategory(cat, ctx);
  115. }
  116. if ((!_params.skipCategoryNames || !_params.skipCategoryNames.has('atom_site')) && encoder.isCategoryIncluded('atom_site')) {
  117. atom_site_operator_mapping(encoder, ctx);
  118. }
  119. for (const customProp of models[0].customProperties.all) {
  120. encodeCustomProp(customProp, ctx, encoder, _params);
  121. }
  122. const structureCustomProps = new Set<CustomPropertyDescriptor>();
  123. for (const s of ctx.structures) {
  124. if (!s.hasCustomProperties) continue;
  125. for (const p of s.customPropertyDescriptors.all) structureCustomProps.add(p);
  126. }
  127. structureCustomProps.forEach(customProp => encodeCustomProp(customProp, ctx, encoder, _params));
  128. }
  129. function to_mmCIF(name: string, structure: Structure, asBinary = false) {
  130. const enc = CifWriter.createEncoder({ binary: asBinary });
  131. enc.startDataBlock(name);
  132. encode_mmCIF_categories(enc, structure);
  133. return enc.getData();
  134. }
  135. export default to_mmCIF