mmcif.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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, copy_source_mmCifCategory } 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. import { MmcifFormat } from '../../../mol-model-formats/structure/mmcif';
  20. export interface CifExportContext {
  21. structures: Structure[],
  22. firstModel: Model,
  23. cache: any
  24. }
  25. export type CifExportCategoryInfo =
  26. | [CifWriter.Category, any /** context */, CifWriter.Encoder.WriteCategoryOptions]
  27. | [CifWriter.Category, any /** context */]
  28. export namespace CifExportContext {
  29. export function create(structures: Structure | Structure[]): CifExportContext {
  30. const structureArray = Array.isArray(structures) ? structures : [structures];
  31. return {
  32. structures: structureArray,
  33. firstModel: structureArray[0].model,
  34. cache: Object.create(null)
  35. };
  36. }
  37. }
  38. const _entity: CifCategory<CifExportContext> = {
  39. name: 'entity',
  40. instance({ structures }) {
  41. const indices = getUniqueEntityIndicesFromStructures(structures);
  42. return CifCategory.ofTable(structures[0].model.entities.data, indices);
  43. }
  44. };
  45. function isWithoutSymmetry(structure: Structure) {
  46. return structure.units.every(u => u.conformation.operator.isIdentity);
  47. }
  48. const Categories = [
  49. // Basics
  50. copy_mmCif_category('entry'),
  51. copy_mmCif_category('exptl'),
  52. _entity,
  53. // Symmetry
  54. copy_mmCif_category('cell', isWithoutSymmetry),
  55. copy_mmCif_category('symmetry', isWithoutSymmetry),
  56. // Assemblies
  57. copy_mmCif_category('pdbx_struct_assembly', isWithoutSymmetry),
  58. copy_mmCif_category('pdbx_struct_assembly_gen', isWithoutSymmetry),
  59. copy_mmCif_category('pdbx_struct_oper_list', isWithoutSymmetry),
  60. // Secondary structure
  61. _struct_conf,
  62. _struct_sheet_range,
  63. // Sequence
  64. _struct_asym,
  65. _entity_poly,
  66. _entity_poly_seq,
  67. // Branch
  68. copy_mmCif_category('pdbx_entity_branch'),
  69. copy_mmCif_category('pdbx_entity_branch_link'),
  70. copy_mmCif_category('pdbx_branch_scheme'),
  71. // Misc
  72. // TODO: filter for actual present residues?
  73. _chem_comp,
  74. _pdbx_chem_comp_identifier,
  75. copy_mmCif_category('atom_sites'),
  76. _pdbx_nonpoly_scheme,
  77. // Atoms
  78. _atom_site
  79. ];
  80. namespace _Filters {
  81. export const AtomSitePositionsFieldNames = new Set<string>(<(keyof typeof mmCIF_Schema.atom_site)[]>['id', 'Cartn_x', 'Cartn_y', 'Cartn_z']);
  82. }
  83. export const mmCIF_Export_Filters = {
  84. onlyPositions: <CifWriter.Category.Filter>{
  85. includeCategory(name) { return name === 'atom_site'; },
  86. includeField(cat, field) { return _Filters.AtomSitePositionsFieldNames.has(field); }
  87. }
  88. };
  89. function getCustomPropCategories(customProp: CustomPropertyDescriptor, ctx: CifExportContext, params?: encode_mmCIF_categories_Params): CifExportCategoryInfo[] {
  90. if (!customProp.cifExport || customProp.cifExport.categories.length === 0) return [];
  91. const prefix = customProp.cifExport.prefix;
  92. const cats = customProp.cifExport.categories;
  93. let propCtx = ctx;
  94. if (customProp.cifExport.context) {
  95. const propId = CustomPropertyDescriptor.getUUID(customProp);
  96. if (ctx.cache[propId + '__ctx']) propCtx = ctx.cache[propId + '__ctx'];
  97. else {
  98. propCtx = customProp.cifExport.context(ctx) || ctx;
  99. ctx.cache[propId + '__ctx'] = propCtx;
  100. }
  101. }
  102. const ret: CifExportCategoryInfo[] = [];
  103. for (const cat of cats) {
  104. if (params?.skipCategoryNames?.has(cat.name)) continue;
  105. if (cat.name.indexOf(prefix) !== 0) throw new Error(`Custom category '${cat.name}' name must start with prefix '${prefix}.'`);
  106. ret.push([cat, propCtx]);
  107. }
  108. return ret;
  109. }
  110. type encode_mmCIF_categories_Params = {
  111. skipCategoryNames?: Set<string>,
  112. exportCtx?: CifExportContext,
  113. copyAllCategories?: boolean
  114. }
  115. /** Doesn't start a data block */
  116. export function encode_mmCIF_categories(encoder: CifWriter.Encoder, structures: Structure | Structure[], params?: encode_mmCIF_categories_Params) {
  117. const first = Array.isArray(structures) ? structures[0] : (structures as Structure);
  118. const models = first.models;
  119. if (models.length !== 1) throw 'Can\'t export stucture composed from multiple models.';
  120. const ctx: CifExportContext = params?.exportCtx || CifExportContext.create(structures);
  121. if (params?.copyAllCategories && MmcifFormat.is(models[0].sourceData)) {
  122. encode_mmCIF_categories_copyAll(encoder, ctx);
  123. } else {
  124. encode_mmCIF_categories_default(encoder, ctx, params);
  125. }
  126. }
  127. function encode_mmCIF_categories_default(encoder: CifWriter.Encoder, ctx: CifExportContext, params?: encode_mmCIF_categories_Params) {
  128. for (const cat of Categories) {
  129. if (params?.skipCategoryNames && params?.skipCategoryNames.has(cat.name)) continue;
  130. encoder.writeCategory(cat, ctx);
  131. }
  132. if (!params?.skipCategoryNames?.has('atom_site') && encoder.isCategoryIncluded('atom_site')) {
  133. const info = atom_site_operator_mapping(ctx);
  134. if (info) encoder.writeCategory(info[0], info[1], info[2]);
  135. }
  136. const _params = params || { };
  137. for (const customProp of ctx.firstModel.customProperties.all) {
  138. for (const [cat, propCtx] of getCustomPropCategories(customProp, ctx, _params)) {
  139. encoder.writeCategory(cat, propCtx);
  140. }
  141. }
  142. for (const s of ctx.structures) {
  143. if (!s.hasCustomProperties) continue;
  144. for (const customProp of s.customPropertyDescriptors.all) {
  145. for (const [cat, propCtx] of getCustomPropCategories(customProp, ctx, _params)) {
  146. encoder.writeCategory(cat, propCtx);
  147. }
  148. }
  149. }
  150. }
  151. function encode_mmCIF_categories_copyAll(encoder: CifWriter.Encoder, ctx: CifExportContext) {
  152. const providedCategories = new Map<string, CifExportCategoryInfo>();
  153. for (const cat of Categories) {
  154. providedCategories.set(cat.name, [cat, ctx]);
  155. }
  156. const mapping = atom_site_operator_mapping(ctx);
  157. if (mapping) providedCategories.set(mapping[0].name, mapping);
  158. for (const customProp of ctx.firstModel.customProperties.all) {
  159. for (const info of getCustomPropCategories(customProp, ctx)) {
  160. providedCategories.set(info[0].name, info);
  161. }
  162. }
  163. for (const s of ctx.structures) {
  164. if (!s.hasCustomProperties) continue;
  165. for (const customProp of s.customPropertyDescriptors.all) {
  166. for (const info of getCustomPropCategories(customProp, ctx)) {
  167. providedCategories.set(info[0].name, info);
  168. }
  169. }
  170. }
  171. const handled = new Set<string>();
  172. const data = (ctx.firstModel.sourceData as MmcifFormat).data;
  173. for (const catName of data.frame.categoryNames) {
  174. handled.add(catName);
  175. if (providedCategories.has(catName)) {
  176. const info = providedCategories.get(catName)!;
  177. encoder.writeCategory(info[0], info[1], info[2]);
  178. } else {
  179. if ((data.db as any)[catName]) {
  180. const cat = copy_mmCif_category(catName as any);
  181. encoder.writeCategory(cat, ctx);
  182. } else {
  183. const cat = copy_source_mmCifCategory(encoder, ctx, data.frame.categories[catName]);
  184. if (cat) encoder.writeCategory(cat);
  185. }
  186. }
  187. }
  188. providedCategories.forEach((info, name) => {
  189. if (!handled.has(name)) encoder.writeCategory(info[0], info[1], info[2]);
  190. });
  191. }
  192. function to_mmCIF(name: string, structure: Structure, asBinary = false) {
  193. const enc = CifWriter.createEncoder({ binary: asBinary });
  194. enc.startDataBlock(name);
  195. encode_mmCIF_categories(enc, structure);
  196. return enc.getData();
  197. }
  198. export default to_mmCIF;