preprocess.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { readStructure } from '../server/structure-wrapper';
  7. import { classifyCif } from './converter';
  8. // import { ConsoleLogger } from 'mol-util/console-logger';
  9. import { Structure } from 'mol-model/structure';
  10. import { CifWriter } from 'mol-io/writer/cif';
  11. import Writer from 'mol-io/writer/writer';
  12. import { wrapFileToWriter } from '../server/api-local';
  13. import { Task/*, now*/ } from 'mol-task';
  14. import { /*showProgress, clearLine */ } from './util';
  15. import { encode_mmCIF_categories, CifExportContext } from 'mol-model/structure/export/mmcif';
  16. // TODO: error handling
  17. // let linearId = 0;
  18. export async function preprocessFile(filename: string, outputCif?: string, outputBcif?: string) {
  19. // linearId++;
  20. //const started = now();
  21. //ConsoleLogger.log(`${linearId}`, `Reading '${filename}'...`);
  22. // TODO: support the custom prop provider list here.
  23. const input = await readStructure('entry', '_local_', filename, void 0);
  24. //ConsoleLogger.log(`${linearId}`, `Classifying CIF categories...`);
  25. const categories = await classifyCif(input.cifFrame);
  26. //clearLine();
  27. const exportCtx = CifExportContext.create(input.structure, input.structure.models[0]);
  28. if (outputCif) {
  29. //ConsoleLogger.log(`${linearId}`, `Encoding CIF...`);
  30. const writer = wrapFileToWriter(outputCif);
  31. const encoder = CifWriter.createEncoder({ binary: false });
  32. await encode(input.structure, input.cifFrame.header, categories, encoder, exportCtx, writer);
  33. // clearLine();
  34. writer.end();
  35. }
  36. if (outputBcif) {
  37. // ConsoleLogger.log(`${linearId}`, `Encoding BinaryCIF...`);
  38. const writer = wrapFileToWriter(outputBcif);
  39. const encoder = CifWriter.createEncoder({ binary: true, binaryAutoClassifyEncoding: true });
  40. await encode(input.structure, input.cifFrame.header, categories, encoder, exportCtx, writer);
  41. //clearLine();
  42. writer.end();
  43. }
  44. // ConsoleLogger.log(`${linearId}`, `Finished '${filename}' in ${Math.round(now() - started)}ms`);
  45. }
  46. function encode(structure: Structure, header: string, categories: CifWriter.Category[], encoder: CifWriter.Encoder, exportCtx: CifExportContext, writer: Writer) {
  47. return Task.create('Encode', async ctx => {
  48. const skipCategoryNames = new Set<string>(categories.map(c => c.name));
  49. encoder.startDataBlock(header);
  50. // let current = 0;
  51. for (const cat of categories){
  52. encoder.writeCategory(cat);
  53. // current++;
  54. // if (ctx.shouldUpdate) await ctx.update({ message: 'Encoding...', current, max: categories.length });
  55. }
  56. encode_mmCIF_categories(encoder, structure, { skipCategoryNames, exportCtx });
  57. encoder.encode();
  58. encoder.writeTo(writer);
  59. }).run();
  60. }