uncertainty.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 { Color, ColorScale } from '../../mol-util/color';
  7. import { StructureElement, Unit, Bond, ElementIndex } from '../../mol-model/structure';
  8. import { Location } from '../../mol-model/location';
  9. import { ColorTheme } from '../color';
  10. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  11. import { ThemeDataContext } from '../theme';
  12. const DefaultUncertaintyColor = Color(0xffff99);
  13. const Description = `Assigns a color based on the uncertainty or disorder of an element's position, e.g. B-factor or RMSF, depending on the data availability and experimental technique.`;
  14. export const UncertaintyColorThemeParams = {
  15. domain: PD.Interval([0, 100]),
  16. list: PD.ColorList('red-white-blue', { presetKind: 'scale' }),
  17. };
  18. export type UncertaintyColorThemeParams = typeof UncertaintyColorThemeParams
  19. export function getUncertaintyColorThemeParams(ctx: ThemeDataContext) {
  20. return UncertaintyColorThemeParams; // TODO return copy
  21. }
  22. export function getUncertainty(unit: Unit, element: ElementIndex): number {
  23. if (Unit.isAtomic(unit)) {
  24. return unit.model.atomicConformation.B_iso_or_equiv.value(element);
  25. } else if (Unit.isSpheres(unit)) {
  26. return unit.model.coarseConformation.spheres.rmsf[element];
  27. } else {
  28. return 0;
  29. }
  30. }
  31. export function UncertaintyColorTheme(ctx: ThemeDataContext, props: PD.Values<UncertaintyColorThemeParams>): ColorTheme<UncertaintyColorThemeParams> {
  32. const scale = ColorScale.create({
  33. reverse: true,
  34. domain: props.domain,
  35. listOrName: props.list.colors,
  36. });
  37. // TODO calc domain based on data, set min/max as 10/90 percentile to be robust against outliers
  38. function color(location: Location): Color {
  39. if (StructureElement.Location.is(location)) {
  40. return scale.color(getUncertainty(location.unit, location.element));
  41. } else if (Bond.isLocation(location)) {
  42. return scale.color(getUncertainty(location.aUnit, location.aUnit.elements[location.aIndex]));
  43. }
  44. return DefaultUncertaintyColor;
  45. }
  46. return {
  47. factory: UncertaintyColorTheme,
  48. granularity: 'group',
  49. preferSmoothing: true,
  50. color,
  51. props,
  52. description: Description,
  53. legend: scale ? scale.legend : undefined
  54. };
  55. }
  56. export const UncertaintyColorThemeProvider: ColorTheme.Provider<UncertaintyColorThemeParams, 'uncertainty'> = {
  57. name: 'uncertainty',
  58. label: 'Uncertainty/Disorder',
  59. category: ColorTheme.Category.Atom,
  60. factory: UncertaintyColorTheme,
  61. getParams: getUncertaintyColorThemeParams,
  62. defaultValues: PD.getDefaultValues(UncertaintyColorThemeParams),
  63. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure && ctx.structure.models.some(m => m.atomicConformation.B_iso_or_equiv.isDefined || m.coarseHierarchy.isDefined)
  64. };