sort.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 { createRangeArray, makeBuckets } from '../../../mol-data/util';
  7. import { Column, Table } from '../../../mol-data/db';
  8. import { RuntimeContext } from '../../../mol-task';
  9. import { AtomSite } from './schema';
  10. import { arrayIsIdentity } from '../../../mol-util/array';
  11. export type SortedAtomSite = {
  12. atom_site: AtomSite
  13. sourceIndex: Column<number>
  14. }
  15. export async function sortAtomSite(ctx: RuntimeContext, atom_site: AtomSite, start: number, end: number): Promise<SortedAtomSite> {
  16. const indices = createRangeArray(start, end - 1);
  17. const { label_entity_id, label_asym_id, label_seq_id } = atom_site;
  18. const entityBuckets = makeBuckets(indices, label_entity_id.value);
  19. if (ctx.shouldUpdate) await ctx.update();
  20. for (let ei = 0, _eI = entityBuckets.length - 1; ei < _eI; ei++) {
  21. const chainBuckets = makeBuckets(indices, label_asym_id.value, { start: entityBuckets[ei], end: entityBuckets[ei + 1] });
  22. for (let cI = 0, _cI = chainBuckets.length - 1; cI < _cI; cI++) {
  23. const aI = chainBuckets[cI];
  24. // are we in HETATM territory?
  25. if (label_seq_id.valueKind(aI) !== Column.ValueKinds.Present) continue;
  26. makeBuckets(indices, label_seq_id.value, { sort: true, start: aI, end: chainBuckets[cI + 1] });
  27. if (ctx.shouldUpdate) await ctx.update();
  28. }
  29. if (ctx.shouldUpdate) await ctx.update();
  30. }
  31. if (arrayIsIdentity(indices) && indices.length === atom_site._rowCount) {
  32. return { atom_site, sourceIndex: Column.ofIntArray(indices) };
  33. }
  34. return {
  35. atom_site: Table.view(atom_site, atom_site._schema, indices) as AtomSite,
  36. sourceIndex: Column.ofIntArray(indices)
  37. };
  38. }