color.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright (c) 2020-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Sebastian Bittrich <sebastian.bittrich@rcsb.org>
  5. */
  6. import { ThemeDataContext } from 'molstar/lib/mol-theme/theme';
  7. import { ColorTheme, LocationColor } from 'molstar/lib/mol-theme/color';
  8. import { ParamDefinition as PD } from 'molstar/lib/mol-util/param-definition';
  9. import { Color } from 'molstar/lib/mol-util/color';
  10. import { StructureElement, Model, Bond } from 'molstar/lib/mol-model/structure';
  11. import { Location } from 'molstar/lib/mol-model/location';
  12. import { CustomProperty } from 'molstar/lib/mol-model-props/common/custom-property';
  13. import { ValidationReport } from 'molstar/lib/extensions/rcsb/validation-report/prop';
  14. import { TableLegend } from 'molstar/lib/mol-util/legend';
  15. import { RSCC, RSCCProvider } from './prop';
  16. const DefaultColor = Color(0xaaaaaa);
  17. const Colors = [DefaultColor, Color(0xff7d45), Color(0xffdb13), Color(0x65cbf3), Color(0x0053d6)];
  18. const ConfidenceColors: { [k: string]: Color } = {
  19. 'No Score': Colors[0],
  20. 'Very low confidence': Colors[1],
  21. 'Low confidence': Colors[2],
  22. 'Well resolved': Colors[3],
  23. 'Very well resolved': Colors[4]
  24. };
  25. const ConfidenceColorLegend = TableLegend(Object.entries(ConfidenceColors));
  26. export function getRSCCColorThemeParams(ctx: ThemeDataContext) {
  27. const categories = RSCC.getCategories(ctx.structure);
  28. if (categories.length === 0) {
  29. return {
  30. type: PD.MappedStatic('score', {
  31. 'score': PD.Group({})
  32. })
  33. };
  34. }
  35. return {
  36. type: PD.MappedStatic('score', {
  37. 'score': PD.Group({}),
  38. 'category': PD.Group({
  39. kind: PD.Select(categories[0], PD.arrayToOptions(categories))
  40. }, { isFlat: true })
  41. })
  42. };
  43. }
  44. export type RSCCColorThemeParams = ReturnType<typeof getRSCCColorThemeParams>
  45. export function RSCCColorTheme(ctx: ThemeDataContext, props: PD.Values<RSCCColorThemeParams>): ColorTheme<RSCCColorThemeParams> {
  46. let color: LocationColor = () => DefaultColor;
  47. if (ctx.structure && ctx.structure.models.length > 0 && ctx.structure.models[0].customProperties.has(RSCCProvider.descriptor)) {
  48. const l = StructureElement.Location.create(ctx.structure.root);
  49. const getColor = (location: StructureElement.Location): Color => {
  50. const score: string = RSCC.getScore(location)[1];
  51. if (props.type.name !== 'score') {
  52. const categoryProp = props.type.params.kind;
  53. if (score === categoryProp) return ConfidenceColors[score];
  54. }
  55. return ConfidenceColors[score];
  56. };
  57. color = (location: Location) => {
  58. if (StructureElement.Location.is(location)) {
  59. return getColor(location);
  60. } else if (Bond.isLocation(location)) {
  61. l.unit = location.aUnit;
  62. l.element = location.aUnit.elements[location.aIndex];
  63. return getColor(l);
  64. }
  65. return DefaultColor;
  66. };
  67. }
  68. return {
  69. factory: RSCCColorTheme,
  70. granularity: 'group',
  71. preferSmoothing: true,
  72. color,
  73. props,
  74. description: 'Assigns residue colors according to the real-space correlation coefficient (RSCC) for polymer residues. Colors range from orange (very low confidence) and yellow (low confidence) to cyan (well resolved) and blue (very well resolved). Categories were obtained by archive-wide statistical analysis. Data from wwPDB Validation Report, obtained via RCSB PDB.',
  75. legend: ConfidenceColorLegend
  76. };
  77. }
  78. export const RSCCColorThemeProvider: ColorTheme.Provider<RSCCColorThemeParams, 'rscc'> = {
  79. name: 'rscc',
  80. label: 'Experimental Support Confidence',
  81. category: ColorTheme.Category.Validation,
  82. factory: RSCCColorTheme,
  83. getParams: getRSCCColorThemeParams,
  84. defaultValues: PD.getDefaultValues(getRSCCColorThemeParams({})),
  85. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure && ValidationReport.isApplicable(ctx.structure.models[0]) && Model.isFromXray(ctx.structure.models[0]) && Model.probablyHasDensityMap(ctx.structure.models[0]),
  86. ensureCustomProperties: {
  87. attach: (ctx: CustomProperty.Context, data: ThemeDataContext) => data.structure ? RSCCProvider.attach(ctx, data.structure.models[0], void 0, true) : Promise.resolve(),
  88. detach: (data) => data.structure && RSCCProvider.ref(data.structure.models[0], false)
  89. }
  90. };