bond.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { BondType } from '../../../../mol-model/structure/model/types';
  7. import { Unit, StructureElement, Structure, Bond } from '../../../../mol-model/structure';
  8. import { ParamDefinition as PD } from '../../../../mol-util/param-definition';
  9. import { LocationIterator } from '../../../../mol-geo/util/location-iterator';
  10. import { LinkCylinderParams, LinkLineParams } from './link';
  11. import { ObjectKeys } from '../../../../mol-util/type-helpers';
  12. import { PickingId } from '../../../../mol-geo/geometry/picking';
  13. import { EmptyLoci, Loci } from '../../../../mol-model/loci';
  14. import { Interval, OrderedSet, SortedArray } from '../../../../mol-data/int';
  15. import { isH, isHydrogen, StructureGroup } from './common';
  16. export const BondParams = {
  17. includeTypes: PD.MultiSelect(ObjectKeys(BondType.Names), PD.objectToOptions(BondType.Names)),
  18. excludeTypes: PD.MultiSelect([] as BondType.Names[], PD.objectToOptions(BondType.Names)),
  19. ignoreHydrogens: PD.Boolean(false),
  20. aromaticBonds: PD.Boolean(true, { description: 'Display aromatic bonds with dashes' }),
  21. multipleBonds: PD.Select('symmetric', PD.arrayToOptions(['off', 'symmetric', 'offset'] as const)),
  22. };
  23. export const DefaultBondProps = PD.getDefaultValues(BondParams);
  24. export type BondProps = typeof DefaultBondProps
  25. export const BondCylinderParams = {
  26. ...LinkCylinderParams,
  27. ...BondParams,
  28. adjustCylinderLength: PD.Boolean(false, { description: 'Shorten cylinders to reduce overlap with spheres. Useful for for transparent bonds. Not working well with aromatic bonds.' })
  29. };
  30. export const DefaultBondCylinderProps = PD.getDefaultValues(BondCylinderParams);
  31. export type BondCylinderProps = typeof DefaultBondCylinderProps
  32. export const BondLineParams = {
  33. ...LinkLineParams,
  34. ...BondParams
  35. };
  36. export const DefaultBondLineProps = PD.getDefaultValues(BondLineParams);
  37. export type BondLineProps = typeof DefaultBondLineProps
  38. export function ignoreBondType(include: BondType.Flag, exclude: BondType.Flag, f: BondType.Flag) {
  39. return !BondType.is(include, f) || BondType.is(exclude, f);
  40. }
  41. export function makeIntraBondIgnoreTest(structure: Structure, unit: Unit.Atomic, props: BondProps): undefined | ((edgeIndex: number) => boolean) {
  42. const elements = unit.elements;
  43. const { atomicNumber } = unit.model.atomicHierarchy.derived.atom;
  44. const bonds = unit.bonds;
  45. const { a, b, edgeProps } = bonds;
  46. const { flags: _flags } = edgeProps;
  47. const { ignoreHydrogens, includeTypes, excludeTypes } = props;
  48. const include = BondType.fromNames(includeTypes);
  49. const exclude = BondType.fromNames(excludeTypes);
  50. const allBondTypes = BondType.isAll(include) && BondType.Flag.None === exclude;
  51. const { child } = structure;
  52. const childUnit = child?.unitMap.get(unit.id);
  53. if (child && !childUnit) throw new Error('expected childUnit to exist if child exists');
  54. if (allBondTypes && !ignoreHydrogens && !child) return;
  55. return (edgeIndex: number) => {
  56. return (
  57. (!!childUnit && !SortedArray.has(childUnit.elements, elements[a[edgeIndex]])) ||
  58. (ignoreHydrogens && (isH(atomicNumber, elements[a[edgeIndex]]) || isH(atomicNumber, elements[b[edgeIndex]]))) ||
  59. (!allBondTypes && ignoreBondType(include, exclude, _flags[edgeIndex]))
  60. );
  61. };
  62. }
  63. export function makeInterBondIgnoreTest(structure: Structure, props: BondProps): undefined | ((edgeIndex: number) => boolean) {
  64. const bonds = structure.interUnitBonds;
  65. const { edges } = bonds;
  66. const { ignoreHydrogens, includeTypes, excludeTypes } = props;
  67. const include = BondType.fromNames(includeTypes);
  68. const exclude = BondType.fromNames(excludeTypes);
  69. const allBondTypes = BondType.isAll(include) && BondType.Flag.None === exclude;
  70. const { child } = structure;
  71. if (allBondTypes && !ignoreHydrogens && !child) return;
  72. return (edgeIndex: number) => {
  73. if (child) {
  74. const b = edges[edgeIndex];
  75. const childUnitA = child.unitMap.get(b.unitA);
  76. if (!childUnitA) return true;
  77. const unitA = structure.unitMap.get(b.unitA);
  78. const eA = unitA.elements[b.indexA];
  79. if (!SortedArray.has(childUnitA.elements, eA)) return true;
  80. }
  81. if (ignoreHydrogens) {
  82. const b = edges[edgeIndex];
  83. const uA = structure.unitMap.get(b.unitA);
  84. const uB = structure.unitMap.get(b.unitB);
  85. if (isHydrogen(uA, uA.elements[b.indexA]) || isHydrogen(uB, uB.elements[b.indexB])) return true;
  86. }
  87. if (!allBondTypes) {
  88. if (ignoreBondType(include, exclude, edges[edgeIndex].props.flag)) return true;
  89. }
  90. return false;
  91. };
  92. }
  93. export namespace BondIterator {
  94. export function fromGroup(structureGroup: StructureGroup): LocationIterator {
  95. const { group, structure } = structureGroup;
  96. const unit = group.units[0] as Unit.Atomic;
  97. const groupCount = Unit.isAtomic(unit) ? unit.bonds.edgeCount * 2 : 0;
  98. const instanceCount = group.units.length;
  99. const location = Bond.Location(structure, undefined, undefined, structure, undefined, undefined);
  100. const getLocation = (groupIndex: number, instanceIndex: number) => {
  101. const unit = group.units[instanceIndex] as Unit.Atomic;
  102. location.aUnit = unit;
  103. location.bUnit = unit;
  104. location.aIndex = unit.bonds.a[groupIndex];
  105. location.bIndex = unit.bonds.b[groupIndex];
  106. return location;
  107. };
  108. return LocationIterator(groupCount, instanceCount, 1, getLocation);
  109. }
  110. export function fromStructure(structure: Structure): LocationIterator {
  111. const groupCount = structure.interUnitBonds.edgeCount;
  112. const instanceCount = 1;
  113. const location = Bond.Location(structure, undefined, undefined, structure, undefined, undefined);
  114. const getLocation = (groupIndex: number) => {
  115. const bond = structure.interUnitBonds.edges[groupIndex];
  116. location.aUnit = structure.unitMap.get(bond.unitA);
  117. location.aIndex = bond.indexA;
  118. location.bUnit = structure.unitMap.get(bond.unitB);
  119. location.bIndex = bond.indexB;
  120. return location;
  121. };
  122. return LocationIterator(groupCount, instanceCount, 1, getLocation, true);
  123. }
  124. }
  125. //
  126. export function getIntraBondLoci(pickingId: PickingId, structureGroup: StructureGroup, id: number) {
  127. const { objectId, instanceId, groupId } = pickingId;
  128. if (id === objectId) {
  129. const { structure, group } = structureGroup;
  130. const unit = group.units[instanceId];
  131. if (Unit.isAtomic(unit)) {
  132. const { target } = structure;
  133. const iA = unit.bonds.a[groupId];
  134. const iB = unit.bonds.b[groupId];
  135. return Bond.Loci(target, [
  136. Bond.Location(target, unit, iA, target, unit, iB),
  137. Bond.Location(target, unit, iB, target, unit, iA)
  138. ]);
  139. }
  140. }
  141. return EmptyLoci;
  142. }
  143. export function eachIntraBond(loci: Loci, structureGroup: StructureGroup, apply: (interval: Interval) => boolean, isMarking: boolean) {
  144. let changed = false;
  145. if (Bond.isLoci(loci)) {
  146. const { structure, group } = structureGroup;
  147. if (!Structure.areEquivalent(loci.structure, structure)) return false;
  148. const unit = group.units[0];
  149. if (!Unit.isAtomic(unit)) return false;
  150. const groupCount = unit.bonds.edgeCount * 2;
  151. for (const b of loci.bonds) {
  152. if (b.aUnit !== b.bUnit) continue;
  153. const unitIdx = group.unitIndexMap.get(b.aUnit.id);
  154. if (unitIdx !== undefined) {
  155. const idx = unit.bonds.getDirectedEdgeIndex(b.aIndex, b.bIndex);
  156. if (idx !== -1) {
  157. if (apply(Interval.ofSingleton(unitIdx * groupCount + idx))) changed = true;
  158. }
  159. }
  160. }
  161. } else if (StructureElement.Loci.is(loci)) {
  162. const { structure, group } = structureGroup;
  163. if (!Structure.areEquivalent(loci.structure, structure)) return false;
  164. const unit = group.units[0];
  165. if (!Unit.isAtomic(unit)) return false;
  166. const groupCount = unit.bonds.edgeCount * 2;
  167. for (const e of loci.elements) {
  168. const unitIdx = group.unitIndexMap.get(e.unit.id);
  169. if (unitIdx !== undefined) {
  170. const { offset, b } = unit.bonds;
  171. OrderedSet.forEach(e.indices, v => {
  172. for (let t = offset[v], _t = offset[v + 1]; t < _t; t++) {
  173. if (!isMarking || OrderedSet.has(e.indices, b[t])) {
  174. if (apply(Interval.ofSingleton(unitIdx * groupCount + t))) changed = true;
  175. }
  176. }
  177. });
  178. }
  179. }
  180. }
  181. return changed;
  182. }
  183. //
  184. export function getInterBondLoci(pickingId: PickingId, structure: Structure, id: number) {
  185. const { objectId, groupId } = pickingId;
  186. if (id === objectId) {
  187. const { target } = structure;
  188. const b = structure.interUnitBonds.edges[groupId];
  189. const uA = structure.unitMap.get(b.unitA);
  190. const uB = structure.unitMap.get(b.unitB);
  191. return Bond.Loci(target, [
  192. Bond.Location(target, uA, b.indexA, target, uB, b.indexB),
  193. Bond.Location(target, uB, b.indexB, target, uA, b.indexA)
  194. ]);
  195. }
  196. return EmptyLoci;
  197. }
  198. const __unitMap = new Map<number, OrderedSet<StructureElement.UnitIndex>>();
  199. export function eachInterBond(loci: Loci, structure: Structure, apply: (interval: Interval) => boolean, isMarking: boolean) {
  200. let changed = false;
  201. if (Bond.isLoci(loci)) {
  202. if (!Structure.areEquivalent(loci.structure, structure)) return false;
  203. for (const b of loci.bonds) {
  204. const idx = structure.interUnitBonds.getBondIndexFromLocation(b);
  205. if (idx !== -1) {
  206. if (apply(Interval.ofSingleton(idx))) changed = true;
  207. }
  208. }
  209. } else if (StructureElement.Loci.is(loci)) {
  210. if (!Structure.areEquivalent(loci.structure, structure)) return false;
  211. if (isMarking && loci.elements.length === 1) return false; // only a single unit
  212. for (const e of loci.elements) __unitMap.set(e.unit.id, e.indices);
  213. for (const e of loci.elements) {
  214. const { unit } = e;
  215. if (!Unit.isAtomic(unit)) continue;
  216. structure.interUnitBonds.getConnectedUnits(unit.id).forEach(b => {
  217. const otherLociIndices = __unitMap.get(b.unitB);
  218. if (!isMarking || otherLociIndices) {
  219. OrderedSet.forEach(e.indices, v => {
  220. if (!b.connectedIndices.includes(v)) return;
  221. b.getEdges(v).forEach(bi => {
  222. if (!isMarking || (otherLociIndices && OrderedSet.has(otherLociIndices, bi.indexB))) {
  223. const idx = structure.interUnitBonds.getEdgeIndex(v, unit.id, bi.indexB, b.unitB);
  224. if (apply(Interval.ofSingleton(idx))) changed = true;
  225. }
  226. });
  227. });
  228. }
  229. });
  230. }
  231. __unitMap.clear();
  232. }
  233. return changed;
  234. }