interactions.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  7. import { Structure, Unit } from '../../../mol-model/structure';
  8. import { RuntimeContext } from '../../../mol-task';
  9. import { Features, FeaturesBuilder } from './features';
  10. import { ValenceModelProvider } from '../valence-model';
  11. import { InteractionsIntraContacts, InteractionsInterContacts, FeatureType } from './common';
  12. import { IntraContactsBuilder, InterContactsBuilder } from './contacts-builder';
  13. import { IntMap } from '../../../mol-data/int';
  14. import { Vec3 } from '../../../mol-math/linear-algebra';
  15. import { addUnitContacts, ContactTester, addStructureContacts, ContactProvider, ContactsParams, ContactsProps } from './contacts';
  16. import { HalogenDonorProvider, HalogenAcceptorProvider, HalogenBondsProvider } from './halogen-bonds';
  17. import { HydrogenDonorProvider, WeakHydrogenDonorProvider, HydrogenAcceptorProvider, HydrogenBondsProvider, WeakHydrogenBondsProvider } from './hydrogen-bonds';
  18. import { NegativChargeProvider, PositiveChargeProvider, AromaticRingProvider, IonicProvider, PiStackingProvider, CationPiProvider } from './charged';
  19. import { HydrophobicAtomProvider, HydrophobicProvider } from './hydrophobic';
  20. import { SetUtils } from '../../../mol-util/set';
  21. import { MetalCoordinationProvider, MetalProvider, MetalBindingProvider } from './metal';
  22. import { refineInteractions } from './refine';
  23. export { Interactions }
  24. interface Interactions {
  25. /** Features of each unit */
  26. unitsFeatures: IntMap<Features>
  27. /** Interactions of each unit */
  28. unitsContacts: IntMap<InteractionsIntraContacts>
  29. /** Interactions between units */
  30. contacts: InteractionsInterContacts
  31. }
  32. namespace Interactions {
  33. export interface Location {
  34. readonly kind: 'interaction-location'
  35. interactions: Interactions
  36. unitA: Unit
  37. /** Index into features of unitA */
  38. indexA: Features.FeatureIndex
  39. unitB: Unit
  40. /** Index into features of unitB */
  41. indexB: Features.FeatureIndex
  42. }
  43. export function Location(interactions?: Interactions, unitA?: Unit, indexA?: Features.FeatureIndex, unitB?: Unit, indexB?: Features.FeatureIndex): Location {
  44. return { kind: 'interaction-location', interactions: interactions as any, unitA: unitA as any, indexA: indexA as any, unitB: unitB as any, indexB: indexB as any };
  45. }
  46. export function isLocation(x: any): x is Location {
  47. return !!x && x.kind === 'interaction-location';
  48. }
  49. export function areLocationsEqual(locA: Location, locB: Location) {
  50. return (
  51. locA.interactions === locB.interactions &&
  52. locA.indexA === locB.indexA && locA.indexB === locB.indexB &&
  53. locA.unitA === locB.unitA && locA.unitB === locB.unitB
  54. )
  55. }
  56. export interface Loci {
  57. readonly kind: 'interaction-loci'
  58. readonly structure: Structure
  59. readonly interactions: Interactions
  60. readonly links: ReadonlyArray<{
  61. unitA: Unit
  62. /** Index into features of unitA */
  63. indexA: Features.FeatureIndex
  64. unitB: Unit
  65. /** Index into features of unitB */
  66. indexB: Features.FeatureIndex
  67. }>
  68. }
  69. export function Loci(structure: Structure, interactions: Interactions, links: Loci['links']): Loci {
  70. return { kind: 'interaction-loci', structure, interactions, links };
  71. }
  72. export function isLoci(x: any): x is Loci {
  73. return !!x && x.kind === 'interaction-loci';
  74. }
  75. export function areLociEqual(a: Loci, b: Loci) {
  76. if (a.structure !== b.structure) return false
  77. if (a.interactions !== b.interactions) return false
  78. if (a.links.length !== b.links.length) return false
  79. for (let i = 0, il = a.links.length; i < il; ++i) {
  80. const linkA = a.links[i]
  81. const linkB = b.links[i]
  82. if (linkA.unitA !== linkB.unitA) return false
  83. if (linkA.unitB !== linkB.unitB) return false
  84. if (linkA.indexA !== linkB.indexA) return false
  85. if (linkA.indexB !== linkB.indexB) return false
  86. }
  87. return true
  88. }
  89. export function isLociEmpty(loci: Loci) {
  90. return loci.links.length === 0 ? true : false
  91. }
  92. }
  93. const FeatureProviders = [
  94. HydrogenDonorProvider, WeakHydrogenDonorProvider, HydrogenAcceptorProvider,
  95. NegativChargeProvider, PositiveChargeProvider, AromaticRingProvider,
  96. HalogenDonorProvider, HalogenAcceptorProvider,
  97. HydrophobicAtomProvider,
  98. MetalProvider, MetalBindingProvider,
  99. ]
  100. const ContactProviders = {
  101. 'ionic': IonicProvider,
  102. 'pi-stacking': PiStackingProvider,
  103. 'cation-pi': CationPiProvider,
  104. 'halogen-bonds': HalogenBondsProvider,
  105. 'hydrogen-bonds': HydrogenBondsProvider,
  106. 'weak-hydrogen-bonds': WeakHydrogenBondsProvider,
  107. 'hydrophobic': HydrophobicProvider,
  108. 'metal-coordination': MetalCoordinationProvider,
  109. }
  110. type ContactProviders = typeof ContactProviders
  111. function getProvidersParams() {
  112. const params: { [k in keyof ContactProviders]: PD.Group<ContactProviders[k]['params']> } = Object.create(null)
  113. Object.keys(ContactProviders).forEach(k => {
  114. (params as any)[k] = PD.Group(ContactProviders[k as keyof ContactProviders].params)
  115. })
  116. return params
  117. }
  118. export const InteractionsParams = {
  119. types: PD.MultiSelect([
  120. 'ionic',
  121. 'cation-pi',
  122. 'pi-stacking',
  123. 'hydrogen-bonds',
  124. 'halogen-bonds',
  125. // 'hydrophobic',
  126. 'metal-coordination',
  127. // 'weak-hydrogen-bonds',
  128. ], PD.objectToOptions(ContactProviders)),
  129. contacts: PD.Group(ContactsParams, { isFlat: true }),
  130. ...getProvidersParams()
  131. }
  132. export type InteractionsParams = typeof InteractionsParams
  133. export type InteractionsProps = PD.Values<InteractionsParams>
  134. export async function computeInteractions(runtime: RuntimeContext, structure: Structure, props: Partial<InteractionsProps>): Promise<Interactions> {
  135. const p = { ...PD.getDefaultValues(InteractionsParams), ...props }
  136. await ValenceModelProvider.attach(structure).runInContext(runtime)
  137. const contactProviders: ContactProvider<any>[] = []
  138. Object.keys(ContactProviders).forEach(k => {
  139. if (p.types.includes(k)) contactProviders.push(ContactProviders[k as keyof typeof ContactProviders])
  140. })
  141. const contactTesters = contactProviders.map(l => l.createTester(p[l.name as keyof InteractionsProps]))
  142. const requiredFeatures = new Set<FeatureType>()
  143. contactTesters.forEach(l => SetUtils.add(requiredFeatures, l.requiredFeatures))
  144. const featureProviders = FeatureProviders.filter(f => SetUtils.areIntersecting(requiredFeatures, f.types))
  145. const unitsFeatures = IntMap.Mutable<Features>()
  146. const unitsContacts = IntMap.Mutable<InteractionsIntraContacts>()
  147. for (let i = 0, il = structure.unitSymmetryGroups.length; i < il; ++i) {
  148. const group = structure.unitSymmetryGroups[i]
  149. if (runtime.shouldUpdate) {
  150. await runtime.update({ message: 'computing interactions', current: i, max: il })
  151. }
  152. const features = findUnitFeatures(structure, group.units[0], featureProviders)
  153. const intraUnitContacts = findIntraUnitContacts(structure, group.units[0], features, contactTesters, p.contacts)
  154. for (let j = 0, jl = group.units.length; j < jl; ++j) {
  155. const u = group.units[j]
  156. unitsFeatures.set(u.id, features)
  157. unitsContacts.set(u.id, intraUnitContacts)
  158. }
  159. }
  160. const contacts = findInterUnitContacts(structure, unitsFeatures, contactTesters, p.contacts)
  161. const interactions = { unitsFeatures, unitsContacts, contacts }
  162. refineInteractions(structure, interactions)
  163. return interactions
  164. }
  165. function findUnitFeatures(structure: Structure, unit: Unit, featureProviders: Features.Provider[]) {
  166. const count = unit.elements.length
  167. const featuresBuilder = FeaturesBuilder.create(count, count / 2)
  168. if (Unit.isAtomic(unit)) {
  169. for (const fp of featureProviders) {
  170. fp.add(structure, unit, featuresBuilder)
  171. }
  172. }
  173. return featuresBuilder.getFeatures(count)
  174. }
  175. function findIntraUnitContacts(structure: Structure, unit: Unit, features: Features, contactTesters: ReadonlyArray<ContactTester>, props: ContactsProps) {
  176. const builder = IntraContactsBuilder.create(features, unit.elements.length)
  177. if (Unit.isAtomic(unit)) {
  178. addUnitContacts(structure, unit, features, builder, contactTesters, props)
  179. }
  180. return builder.getContacts()
  181. }
  182. function findInterUnitContacts(structure: Structure, unitsFeatures: IntMap<Features>, contactTesters: ReadonlyArray<ContactTester>, props: ContactsProps) {
  183. const builder = InterContactsBuilder.create()
  184. const maxDistance = Math.max(...contactTesters.map(t => t.maxDistance))
  185. const lookup = structure.lookup3d;
  186. const imageCenter = Vec3.zero();
  187. for (const unitA of structure.units) {
  188. if (!Unit.isAtomic(unitA)) continue;
  189. const featuresA = unitsFeatures.get(unitA.id)
  190. const bs = unitA.lookup3d.boundary.sphere;
  191. Vec3.transformMat4(imageCenter, bs.center, unitA.conformation.operator.matrix);
  192. const closeUnits = lookup.findUnitIndices(imageCenter[0], imageCenter[1], imageCenter[2], bs.radius + maxDistance);
  193. for (let i = 0; i < closeUnits.count; i++) {
  194. const unitB = structure.units[closeUnits.indices[i]];
  195. if (!Unit.isAtomic(unitB) || unitA.id >= unitB.id || !Structure.validUnitPair(structure, unitA, unitB)) continue;
  196. const featuresB = unitsFeatures.get(unitB.id)
  197. if (unitB.elements.length >= unitA.elements.length) {
  198. addStructureContacts(structure, unitA, featuresA, unitB, featuresB, builder, contactTesters, props)
  199. } else {
  200. addStructureContacts(structure, unitB, featuresB, unitA, featuresA, builder, contactTesters, props)
  201. }
  202. }
  203. }
  204. return builder.getContacts(unitsFeatures)
  205. }