export.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 export_Params(): encode_mmCIF_categories_Params {
  19. const skipCategories: Set<string> = new Set();
  20. skipCategories.add('pdbx_struct_assembly').add('pdbx_struct_assembly_gen').add('pdbx_struct_oper_list');
  21. const params: encode_mmCIF_categories_Params = {
  22. skipCategoryNames: skipCategories
  23. };
  24. return params;
  25. }
  26. function to_mmCIF(name: string, structure: Structure, asBinary = false) {
  27. const enc = CifWriter.createEncoder({ binary: asBinary });
  28. enc.startDataBlock(name);
  29. encode_mmCIF_categories(enc, structure, export_Params());
  30. return enc.getData();
  31. }
  32. function extract_structure_data_from_state(plugin: PluginContext): { [k: string]: Structure } {
  33. const content: { [k: string]: Structure } = {};
  34. const cells = plugin.state.data.select(StateSelection.Generators.rootsOfType(PluginStateObject.Molecule.Structure));
  35. for (const c of cells) {
  36. const children = plugin.state.data.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure, c.transform.ref))
  37. .filter(child => (child !== c && !child.transform.transformer.definition.isDecorator))
  38. .map(child => child.obj!.data);
  39. const sele = StructureSelection.Sequence(c.obj!.data, children);
  40. const structure = StructureSelection.unionStructure(sele);
  41. const name = structure.model.entryId;
  42. content[name] = structure;
  43. }
  44. return content;
  45. }
  46. export function encodeStructureData(plugin: PluginContext): { [k: string]: Uint8Array } {
  47. const content: { [k: string]: Uint8Array } = {};
  48. const structures = extract_structure_data_from_state(plugin);
  49. for (const [key, structure] of Object.entries(structures)) {
  50. const filename = `${key}.cif`;
  51. const str = to_mmCIF(filename, structure, false) as string;
  52. const data = new Uint8Array(utf8ByteCount(str));
  53. utf8Write(data, 0, str);
  54. content[filename] = data;
  55. }
  56. return content;
  57. }
  58. export function downloadAsZipFile(content: { [k: string]: Uint8Array }) {
  59. const filename = `mol-star_download_${getFormattedTime()}.zip`;
  60. const buf = zip(content)
  61. download(new Blob([buf]), filename);
  62. }