mmcif.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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, _chem_comp_bond, _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 '../../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. // Struct conn
  72. copy_mmCif_category('struct_conn'),
  73. // Misc
  74. _chem_comp,
  75. _chem_comp_bond,
  76. _pdbx_chem_comp_identifier,
  77. copy_mmCif_category('atom_sites'),
  78. _pdbx_nonpoly_scheme,
  79. // Atoms
  80. _atom_site
  81. ];
  82. namespace _Filters {
  83. export const AtomSitePositionsFieldNames = new Set<string>(<(keyof typeof mmCIF_Schema.atom_site)[]>['id', 'Cartn_x', 'Cartn_y', 'Cartn_z']);
  84. }
  85. export const mmCIF_Export_Filters = {
  86. onlyPositions: <CifWriter.Category.Filter>{
  87. includeCategory(name) { return name === 'atom_site'; },
  88. includeField(cat, field) { return _Filters.AtomSitePositionsFieldNames.has(field); }
  89. }
  90. };
  91. function getCustomPropCategories(customProp: CustomPropertyDescriptor, ctx: CifExportContext, params?: encode_mmCIF_categories_Params): CifExportCategoryInfo[] {
  92. if (!customProp.cifExport || customProp.cifExport.categories.length === 0) return [];
  93. const prefix = customProp.cifExport.prefix;
  94. const cats = customProp.cifExport.categories;
  95. let propCtx = ctx;
  96. if (customProp.cifExport.context) {
  97. const propId = CustomPropertyDescriptor.getUUID(customProp);
  98. if (ctx.cache[propId + '__ctx']) propCtx = ctx.cache[propId + '__ctx'];
  99. else {
  100. propCtx = customProp.cifExport.context(ctx) || ctx;
  101. ctx.cache[propId + '__ctx'] = propCtx;
  102. }
  103. }
  104. const ret: CifExportCategoryInfo[] = [];
  105. for (const cat of cats) {
  106. if (params?.skipCategoryNames?.has(cat.name)) continue;
  107. if (cat.name.indexOf(prefix) !== 0) throw new Error(`Custom category '${cat.name}' name must start with prefix '${prefix}.'`);
  108. ret.push([cat, propCtx]);
  109. }
  110. return ret;
  111. }
  112. type encode_mmCIF_categories_Params = {
  113. skipCategoryNames?: Set<string>,
  114. exportCtx?: CifExportContext,
  115. copyAllCategories?: boolean,
  116. customProperties?: CustomPropertyDescriptor[]
  117. }
  118. /** Doesn't start a data block */
  119. export function encode_mmCIF_categories(encoder: CifWriter.Encoder, structures: Structure | Structure[], params?: encode_mmCIF_categories_Params) {
  120. const first = Array.isArray(structures) ? structures[0] : (structures as Structure);
  121. const models = first.models;
  122. if (models.length !== 1) throw new Error('Can\'t export stucture composed from multiple models.');
  123. const ctx: CifExportContext = params?.exportCtx || CifExportContext.create(structures);
  124. if (params?.copyAllCategories && MmcifFormat.is(models[0].sourceData)) {
  125. encode_mmCIF_categories_copyAll(encoder, ctx, params);
  126. } else {
  127. encode_mmCIF_categories_default(encoder, ctx, params);
  128. }
  129. }
  130. function encode_mmCIF_categories_default(encoder: CifWriter.Encoder, ctx: CifExportContext, params?: encode_mmCIF_categories_Params) {
  131. for (const cat of Categories) {
  132. if (params?.skipCategoryNames && params?.skipCategoryNames.has(cat.name)) continue;
  133. encoder.writeCategory(cat, ctx);
  134. }
  135. if (!params?.skipCategoryNames?.has('atom_site') && encoder.isCategoryIncluded('atom_site')) {
  136. const info = atom_site_operator_mapping(ctx);
  137. if (info) encoder.writeCategory(info[0], info[1], info[2]);
  138. }
  139. const _params = params || { };
  140. for (const customProp of ctx.firstModel.customProperties.all) {
  141. for (const [cat, propCtx] of getCustomPropCategories(customProp, ctx, _params)) {
  142. encoder.writeCategory(cat, propCtx);
  143. }
  144. }
  145. if (params?.customProperties) {
  146. for (const customProp of params?.customProperties) {
  147. for (const [cat, propCtx] of getCustomPropCategories(customProp, ctx, _params)) {
  148. encoder.writeCategory(cat, propCtx);
  149. }
  150. }
  151. }
  152. for (const s of ctx.structures) {
  153. if (!s.hasCustomProperties) continue;
  154. for (const customProp of s.customPropertyDescriptors.all) {
  155. for (const [cat, propCtx] of getCustomPropCategories(customProp, ctx, _params)) {
  156. encoder.writeCategory(cat, propCtx);
  157. }
  158. }
  159. }
  160. }
  161. function encode_mmCIF_categories_copyAll(encoder: CifWriter.Encoder, ctx: CifExportContext, params?: encode_mmCIF_categories_Params) {
  162. const providedCategories = new Map<string, CifExportCategoryInfo>();
  163. for (const cat of Categories) {
  164. providedCategories.set(cat.name, [cat, ctx]);
  165. }
  166. const mapping = atom_site_operator_mapping(ctx);
  167. if (mapping) providedCategories.set(mapping[0].name, mapping);
  168. const _params = params || { };
  169. for (const customProp of ctx.firstModel.customProperties.all) {
  170. for (const info of getCustomPropCategories(customProp, ctx, _params)) {
  171. providedCategories.set(info[0].name, info);
  172. }
  173. }
  174. if (params?.customProperties) {
  175. for (const customProp of params?.customProperties) {
  176. for (const info of getCustomPropCategories(customProp, ctx, _params)) {
  177. providedCategories.set(info[0].name, info);
  178. }
  179. }
  180. }
  181. for (const s of ctx.structures) {
  182. if (!s.hasCustomProperties) continue;
  183. for (const customProp of s.customPropertyDescriptors.all) {
  184. for (const info of getCustomPropCategories(customProp, ctx)) {
  185. providedCategories.set(info[0].name, info);
  186. }
  187. }
  188. }
  189. const handled = new Set<string>();
  190. const data = (ctx.firstModel.sourceData as MmcifFormat).data;
  191. for (const catName of data.frame.categoryNames) {
  192. handled.add(catName);
  193. if (providedCategories.has(catName)) {
  194. const info = providedCategories.get(catName)!;
  195. encoder.writeCategory(info[0], info[1], info[2]);
  196. } else {
  197. if ((data.db as any)[catName]) {
  198. const cat = copy_mmCif_category(catName as any);
  199. encoder.writeCategory(cat, ctx);
  200. } else {
  201. const cat = copy_source_mmCifCategory(encoder, ctx, data.frame.categories[catName]);
  202. if (cat) encoder.writeCategory(cat);
  203. }
  204. }
  205. }
  206. providedCategories.forEach((info, name) => {
  207. if (!handled.has(name)) encoder.writeCategory(info[0], info[1], info[2]);
  208. });
  209. }
  210. function to_mmCIF(name: string, structure: Structure, asBinary = false) {
  211. const enc = CifWriter.createEncoder({ binary: asBinary });
  212. enc.startDataBlock(name);
  213. encode_mmCIF_categories(enc, structure);
  214. return enc.getData();
  215. }
  216. export { to_mmCIF };