lookup3d.ts 2.2 KB

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