export.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { PluginContext } from 'molstar/lib/mol-plugin/context';
  2. import { StateSelection } from 'molstar/lib/mol-state';
  3. import { PluginStateObject } from 'molstar/lib/mol-plugin-state/objects';
  4. import { StructureSelection, Structure } from 'molstar/lib/mol-model/structure';
  5. import { CifExportContext, encode_mmCIF_categories } from 'molstar/lib/mol-model/structure/export/mmcif';
  6. import { utf8ByteCount, utf8Write } from 'molstar/lib/mol-io/common/utf8';
  7. import { zip } from 'molstar/lib/mol-util/zip/zip';
  8. import { getFormattedTime } from 'molstar/lib/mol-util/date';
  9. import { download } from 'molstar/lib/mol-util/download';
  10. import { CustomPropertyDescriptor } from 'molstar/lib/mol-model/custom-property';
  11. import { CifWriter } from 'molstar/lib/mol-io/writer/cif';
  12. type encode_mmCIF_categories_Params = {
  13. skipCategoryNames?: Set<string>,
  14. exportCtx?: CifExportContext,
  15. copyAllCategories?: boolean,
  16. customProperties?: CustomPropertyDescriptor[]
  17. }
  18. function exportParams(): encode_mmCIF_categories_Params {
  19. const skipCategories: Set<string> = new Set();
  20. skipCategories
  21. // Basics
  22. .add('entry')
  23. // Symmetry
  24. .add('cell')
  25. .add('symmetry')
  26. // Secondary structure
  27. .add('struct_conf')
  28. .add('struct_sheet_range')
  29. // Assemblies
  30. .add('pdbx_struct_assembly')
  31. .add('pdbx_struct_assembly_gen')
  32. .add('pdbx_struct_oper_list');
  33. const params: encode_mmCIF_categories_Params = {
  34. skipCategoryNames: skipCategories
  35. };
  36. return params;
  37. }
  38. function to_mmCIF(name: string, structure: Structure, asBinary = false) {
  39. const enc = CifWriter.createEncoder({ binary: asBinary });
  40. enc.startDataBlock(name);
  41. encode_mmCIF_categories(enc, structure, exportParams());
  42. return enc.getData();
  43. }
  44. function getDecorator(plugin: PluginContext, root: string): string {
  45. const tree = plugin.state.data.tree;
  46. const children = tree.children.get(root);
  47. if (children.size !== 1) return root;
  48. const child = children.first();
  49. if (tree.transforms.get(child).transformer.definition.isDecorator) {
  50. return getDecorator(plugin, child);
  51. }
  52. return root;
  53. }
  54. function extractStructureDataFromState(plugin: PluginContext): { [k: string]: Structure } {
  55. const content: { [k: string]: Structure } = Object.create(null);
  56. const cells = plugin.state.data.select(StateSelection.Generators.rootsOfType(PluginStateObject.Molecule.Structure));
  57. for (let i = 0; i < cells.length; i++) {
  58. const c = cells[i];
  59. const nodeRef = getDecorator(plugin, c.transform.ref);
  60. const children = plugin.state.data.select(StateSelection.Generators.byRef(nodeRef))
  61. .map(child => child.obj!.data);
  62. const sele = StructureSelection.Sequence(c.obj!.data, children);
  63. const structure = StructureSelection.unionStructure(sele);
  64. const name = `${i + 1}-${structure.model.entryId}`;
  65. content[name] = structure;
  66. }
  67. return content;
  68. }
  69. export function encodeStructureData(plugin: PluginContext): { [k: string]: Uint8Array } {
  70. const content: { [k: string]: Uint8Array } = Object.create(null);
  71. const structures = extractStructureDataFromState(plugin);
  72. for (const [key, structure] of Object.entries(structures)) {
  73. const filename = `${key}.cif`;
  74. const str = to_mmCIF(filename, structure, false) as string;
  75. const data = new Uint8Array(utf8ByteCount(str));
  76. utf8Write(data, 0, str);
  77. content[filename] = data;
  78. }
  79. return content;
  80. }
  81. export function downloadAsZipFile(content: { [k: string]: Uint8Array }) {
  82. const filename = `mol-star_download_${getFormattedTime()}.zip`;
  83. const buf = zip(content);
  84. download(new Blob([buf]), filename);
  85. }