symmetry.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import Structure from './structure'
  7. import AtomSet from './atom/set'
  8. import Unit from './unit'
  9. import { Selection } from '../query'
  10. import { ModelSymmetry } from '../model'
  11. namespace Symmetry {
  12. export const buildAssembly = buildAssemblyImpl;
  13. }
  14. export default Symmetry;
  15. function buildAssemblyImpl(structure: Structure, name: string) {
  16. const models = Structure.getModels(structure);
  17. if (models.length !== 1) throw new Error('Can only build assemblies from structures based on 1 model.');
  18. const assembly = ModelSymmetry.findAssembly(models[0], name);
  19. if (!assembly) throw new Error(`Assembly '${name}' is not defined.`);
  20. const assembler = Structure.Builder();
  21. let unitId = 0;
  22. for (const g of assembly.operatorGroups) {
  23. const selection = g.selector(structure);
  24. if (Selection.structureCount(selection) === 0) continue;
  25. const { units, atoms } = Selection.union(selection);
  26. const unitIds = AtomSet.unitIds(atoms);
  27. for (const oper of g.operators) {
  28. for (let uI = 0, _uI = unitIds.length; uI < _uI; uI++) {
  29. const unit = units.get(unitIds[uI]);
  30. assembler.add(Unit.create(unitId++, unit.model, oper), AtomSet.unitGetByIndex(atoms, uI));
  31. }
  32. }
  33. }
  34. return assembler.getStructure();
  35. }