cross-link.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Link } from 'mol-model/structure';
  7. import { Color, ColorScale, ColorBrewer } from 'mol-util/color';
  8. import { Location } from 'mol-model/location';
  9. import { ColorThemeProps, ColorTheme } from '../color';
  10. import { LocationColor } from 'mol-geo/util/color-data';
  11. import { Vec3 } from 'mol-math/linear-algebra';
  12. const DefaultColor = 0xCCCCCC;
  13. const distVecA = Vec3.zero(), distVecB = Vec3.zero()
  14. function linkDistance(link: Link.Location) {
  15. link.aUnit.conformation.position(link.aIndex, distVecA)
  16. link.bUnit.conformation.position(link.bIndex, distVecB)
  17. return Vec3.distance(distVecA, distVecB)
  18. }
  19. export function CrossLinkColorTheme(props: ColorThemeProps): ColorTheme {
  20. let colorFn: LocationColor
  21. if (props.structure) {
  22. const crosslinks = props.structure.crossLinkRestraints
  23. const scale = ColorScale.create({ domain: [ -10, 10 ], colors: ColorBrewer.RdYlBu })
  24. colorFn = (location: Location): Color => {
  25. if (Link.isLocation(location)) {
  26. const pairs = crosslinks.getPairs(location.aIndex, location.aUnit, location.bIndex, location.bUnit)
  27. if (pairs) {
  28. return scale.color(linkDistance(location) - pairs[0].distanceThreshold)
  29. }
  30. }
  31. return DefaultColor
  32. }
  33. } else {
  34. colorFn = () => DefaultColor
  35. }
  36. return {
  37. kind: 'element',
  38. color: colorFn
  39. }
  40. }