sifts-mapping.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { Location } from '../../../mol-model/location';
  7. import { Bond, StructureElement, Unit } from '../../../mol-model/structure';
  8. import { ColorTheme, LocationColor } from '../../../mol-theme/color';
  9. import { ThemeDataContext } from '../../../mol-theme/theme';
  10. import { Color } from '../../../mol-util/color';
  11. import { getPalette, getPaletteParams } from '../../../mol-util/color/palette';
  12. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  13. import { CustomProperty } from '../../common/custom-property';
  14. import { SIFTSMapping } from '../sifts-mapping';
  15. const DefaultColor = Color(0xFAFAFA);
  16. const Description = 'Assigns a color based on SIFTS mapping.';
  17. // same colors for same accessions
  18. const globalAccessionMap = new Map<string, number>();
  19. export const SIFTSMappingColorThemeParams = {
  20. ...getPaletteParams({ type: 'colors', colorList: 'set-1' }),
  21. };
  22. export type SIFTSMappingColorThemeParams = typeof SIFTSMappingColorThemeParams
  23. export function getSIFTSMappingColorThemeParams(ctx: ThemeDataContext) {
  24. return SIFTSMappingColorThemeParams; // TODO return copy
  25. }
  26. export function SIFTSMappingColorTheme(ctx: ThemeDataContext, props: PD.Values<SIFTSMappingColorThemeParams>): ColorTheme<SIFTSMappingColorThemeParams> {
  27. let color: LocationColor;
  28. if (ctx.structure) {
  29. for (const m of ctx.structure.models) {
  30. const mapping = SIFTSMapping.Provider.get(m).value;
  31. if (!mapping) continue;
  32. for (const acc of mapping.accession) {
  33. if (!acc || globalAccessionMap.has(acc)) continue;
  34. globalAccessionMap.set(acc, globalAccessionMap.size);
  35. }
  36. }
  37. const l = StructureElement.Location.create(ctx.structure);
  38. const palette = getPalette(globalAccessionMap.size + 1, props, { valueLabel: i => `${i}` });
  39. const colorMap = new Map<string, Color>();
  40. const getColor = (location: StructureElement.Location) => {
  41. const key = SIFTSMapping.getKey(location);
  42. if (!key) return DefaultColor;
  43. if (colorMap.has(key)) return colorMap.get(key)!;
  44. const color = palette.color(globalAccessionMap.get(key)!);
  45. colorMap.set(key, color);
  46. return color;
  47. };
  48. color = (location: Location): Color => {
  49. if (StructureElement.Location.is(location) && Unit.isAtomic(location.unit)) {
  50. return getColor(location);
  51. } else if (Bond.isLocation(location)) {
  52. l.unit = location.aUnit;
  53. l.element = location.aUnit.elements[location.aIndex];
  54. return getColor(l);
  55. }
  56. return DefaultColor;
  57. };
  58. } else {
  59. color = () => DefaultColor;
  60. }
  61. return {
  62. factory: SIFTSMappingColorTheme,
  63. granularity: 'group',
  64. preferSmoothing: true,
  65. color,
  66. props,
  67. description: Description,
  68. };
  69. }
  70. export const SIFTSMappingColorThemeProvider: ColorTheme.Provider<SIFTSMappingColorThemeParams, 'sifts-mapping'> = {
  71. name: 'sifts-mapping',
  72. label: 'SIFTS Mapping',
  73. category: ColorTheme.Category.Residue,
  74. factory: SIFTSMappingColorTheme,
  75. getParams: getSIFTSMappingColorThemeParams,
  76. defaultValues: PD.getDefaultValues(SIFTSMappingColorThemeParams),
  77. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure?.models.some(m => SIFTSMapping.Provider.isApplicable(m)),
  78. ensureCustomProperties: {
  79. attach: async (ctx: CustomProperty.Context, data: ThemeDataContext) => {
  80. if (!data.structure) return;
  81. for (const m of data.structure.models) {
  82. await SIFTSMapping.Provider.attach(ctx, m, void 0, true);
  83. }
  84. },
  85. detach: (data) => {
  86. if (!data.structure) return;
  87. for (const m of data.structure.models) {
  88. SIFTSMapping.Provider.ref(m, false);
  89. }
  90. }
  91. }
  92. };