lookup3d.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import * as util from 'util'
  2. import * as fs from 'fs'
  3. import CIF from 'mol-io/reader/cif'
  4. import { Structure, Model, Format } from 'mol-model/structure'
  5. import { GridLookup3D } from 'mol-math/geometry';
  6. // import { sortArray } from 'mol-data/util';
  7. import { OrderedSet } from 'mol-data/int';
  8. require('util.promisify').shim();
  9. const readFileAsync = util.promisify(fs.readFile);
  10. async function readData(path: string) {
  11. if (path.match(/\.bcif$/)) {
  12. const input = await readFileAsync(path)
  13. const data = new Uint8Array(input.byteLength);
  14. for (let i = 0; i < input.byteLength; i++) data[i] = input[i];
  15. return data;
  16. } else {
  17. return readFileAsync(path, 'utf8');
  18. }
  19. }
  20. export async function readCIF(path: string) {
  21. const input = await readData(path)
  22. const comp = typeof input === 'string' ? CIF.parseText(input) : CIF.parseBinary(input);
  23. const parsed = await comp.run();
  24. if (parsed.isError) {
  25. throw parsed;
  26. }
  27. const mmcif = Format.mmCIF(parsed.result.blocks[0]);
  28. const models = await Model.create(mmcif).run();
  29. const structures = models.map(Structure.ofModel);
  30. return { mmcif: mmcif.data, models, structures };
  31. }
  32. export async function test() {
  33. const { mmcif, structures } = await readCIF('e:/test/quick/1tqn_updated.cif');
  34. const lookup = GridLookup3D({ x: mmcif.atom_site.Cartn_x.toArray(), y: mmcif.atom_site.Cartn_y.toArray(), z: mmcif.atom_site.Cartn_z.toArray(),
  35. indices: OrderedSet.ofBounds(0, mmcif.atom_site._rowCount),
  36. //radius: [1, 1, 1, 1]
  37. //indices: [1]
  38. });
  39. console.log(lookup.boundary.box, lookup.boundary.sphere);
  40. const result = lookup.find(-30.07, 8.178, -13.897, 10);
  41. console.log(result.count)//, sortArray(result.indices));
  42. // const sl = structures[0].lookup3d;
  43. // const result1 = sl.find(-30.07, 8.178, -13.897, 10);
  44. // console.log(result1.count);//, result1.indices);
  45. console.log(structures[0].boundary);
  46. console.log(lookup.boundary);
  47. }
  48. test();