operator-name.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 } from '../../mol-util/color';
  7. import { StructureElement, Bond, Structure } from '../../mol-model/structure';
  8. import { Location } from '../../mol-model/location';
  9. import { ColorTheme, LocationColor } from '../color';
  10. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  11. import { ThemeDataContext } from '../theme';
  12. import { getPaletteParams, getPalette } from '../../mol-util/color/palette';
  13. import { ScaleLegend, TableLegend } from '../../mol-util/legend';
  14. const DefaultList = 'many-distinct';
  15. const DefaultColor = Color(0xCCCCCC);
  16. const Description = `Assigns a color based on the operator name of a transformed chain.`;
  17. export const OperatorNameColorThemeParams = {
  18. ...getPaletteParams({ type: 'colors', colorList: DefaultList }),
  19. };
  20. export type OperatorNameColorThemeParams = typeof OperatorNameColorThemeParams
  21. export function getOperatorNameColorThemeParams(ctx: ThemeDataContext) {
  22. const params = PD.clone(OperatorNameColorThemeParams);
  23. return params;
  24. }
  25. function getOperatorNameSerialMap(structure: Structure) {
  26. const map = new Map<string, number>();
  27. for (let i = 0, il = structure.units.length; i < il; ++i) {
  28. const name = structure.units[i].conformation.operator.name;
  29. if (!map.has(name)) map.set(name, map.size);
  30. }
  31. return map;
  32. }
  33. export function OperatorNameColorTheme(ctx: ThemeDataContext, props: PD.Values<OperatorNameColorThemeParams>): ColorTheme<OperatorNameColorThemeParams> {
  34. let color: LocationColor;
  35. let legend: ScaleLegend | TableLegend | undefined;
  36. if (ctx.structure) {
  37. const operatorNameSerialMap = getOperatorNameSerialMap(ctx.structure.root);
  38. const labelTable = Array.from(operatorNameSerialMap.keys());
  39. props.palette.params.valueLabel = (i: number) => labelTable[i];
  40. const palette = getPalette(operatorNameSerialMap.size, props);
  41. legend = palette.legend;
  42. color = (location: Location): Color => {
  43. let serial: number | undefined = undefined;
  44. if (StructureElement.Location.is(location)) {
  45. const name = location.unit.conformation.operator.name;
  46. serial = operatorNameSerialMap.get(name);
  47. } else if (Bond.isLocation(location)) {
  48. const name = location.aUnit.conformation.operator.name;
  49. serial = operatorNameSerialMap.get(name);
  50. }
  51. return serial === undefined ? DefaultColor : palette.color(serial);
  52. };
  53. } else {
  54. color = () => DefaultColor;
  55. }
  56. return {
  57. factory: OperatorNameColorTheme,
  58. granularity: 'instance',
  59. color,
  60. props,
  61. description: Description,
  62. legend
  63. };
  64. }
  65. export const OperatorNameColorThemeProvider: ColorTheme.Provider<OperatorNameColorThemeParams, 'operator-name'> = {
  66. name: 'operator-name',
  67. label: 'Operator Name',
  68. category: ColorTheme.Category.Symmetry,
  69. factory: OperatorNameColorTheme,
  70. getParams: getOperatorNameColorThemeParams,
  71. defaultValues: PD.getDefaultValues(OperatorNameColorThemeParams),
  72. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  73. };