bond.ts 12 KB

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