bond.ts 11 KB

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