color.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Yana Rose
  5. */
  6. import { ThemeDataContext } from 'molstar/lib/mol-theme/theme';
  7. import { ColorTheme } 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, StructureProperties, Bond } from 'molstar/lib/mol-model/structure';
  11. import { Location } from 'molstar/lib/mol-model/location';
  12. export function SuperposeColorTheme(ctx: ThemeDataContext, props: {}): ColorTheme<{}> {
  13. const colorLookup = ctx.structure?.inheritedPropertyData.colors;
  14. const defaultColorLookup: Map<string, Color> = new Map();
  15. for (const [asymId, seqIds] of Object.entries(colorLookup)) {
  16. const colorValue = (seqIds as Map<number, Color>).values().next().value;
  17. const defaultColor = Color.desaturate(Color.lighten(colorValue, 1.7), 1.2);
  18. defaultColorLookup.set(asymId, defaultColor);
  19. }
  20. let DefaultColor = Color(0xCCCCCC);
  21. const colorValues: Color[] = Array.from(defaultColorLookup.values());
  22. if (colorValues.every((val, i, arr) => val === arr[0])) {
  23. DefaultColor = colorValues[0];
  24. }
  25. const l = StructureElement.Location.create();
  26. const _color = (location: StructureElement.Location) => {
  27. const asymId = StructureProperties.chain.label_asym_id(location);
  28. const seqId = StructureProperties.residue.label_seq_id(location);
  29. if (colorLookup?.[asymId]?.has(seqId)) {
  30. if (colorLookup[asymId]?.get(seqId) !== undefined) {
  31. return colorLookup[asymId]?.get(seqId);
  32. }
  33. } else if (colorLookup?.[asymId]) {
  34. return defaultColorLookup.get(asymId)!;
  35. }
  36. return DefaultColor;
  37. };
  38. const color = (location: Location): Color => {
  39. if (StructureElement.Location.is(location)) {
  40. return _color(location);
  41. } else if (Bond.isLocation(location)) {
  42. l.structure = location.aStructure;
  43. l.unit = location.aUnit;
  44. l.element = location.aUnit.elements[location.aIndex];
  45. return _color(l);
  46. }
  47. return DefaultColor;
  48. };
  49. return {
  50. factory: SuperposeColorTheme,
  51. granularity: 'group',
  52. color,
  53. props,
  54. description: 'Superpose coloring',
  55. };
  56. }
  57. export const SuperposeColorThemeProvider: ColorTheme.Provider<{}, 'superpose'> = {
  58. name: 'superpose',
  59. label: 'Superpose',
  60. category: ColorTheme.Category.Misc,
  61. factory: SuperposeColorTheme,
  62. getParams: () => ({}),
  63. defaultValues: PD.getDefaultValues({}),
  64. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure && !!ctx.structure.inheritedPropertyData.colors,
  65. };