carbohydrate-symbol.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { StructureElement, Bond, ElementIndex, Unit, Model } from '../../mol-model/structure';
  7. import { SaccharideColors, MonosaccharidesColorTable } from '../../mol-model/structure/structure/carbohydrates/constants';
  8. import { Location } from '../../mol-model/location';
  9. import { ColorTheme, LocationColor } from '../color';
  10. import { Color } from '../../mol-util/color';
  11. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  12. import { ThemeDataContext } from '../theme';
  13. import { TableLegend } from '../../mol-util/legend';
  14. const DefaultColor = Color(0xCCCCCC);
  15. const Description = 'Assigns colors according to the Symbol Nomenclature for Glycans (SNFG).';
  16. export const CarbohydrateSymbolColorThemeParams = { };
  17. export type CarbohydrateSymbolColorThemeParams = typeof CarbohydrateSymbolColorThemeParams
  18. export function getCarbohydrateSymbolColorThemeParams(ctx: ThemeDataContext) {
  19. return CarbohydrateSymbolColorThemeParams; // TODO return copy
  20. }
  21. export function CarbohydrateSymbolColorTheme(ctx: ThemeDataContext, props: PD.Values<CarbohydrateSymbolColorThemeParams>): ColorTheme<CarbohydrateSymbolColorThemeParams> {
  22. let color: LocationColor;
  23. if (ctx.structure) {
  24. const { elements, getElementIndices } = ctx.structure.carbohydrates;
  25. const getColor = (unit: Unit, index: ElementIndex) => {
  26. if (!Unit.isAtomic(unit)) return DefaultColor;
  27. const carbs = getElementIndices(unit, index);
  28. return carbs.length > 0 ? elements[carbs[0]].component.color : DefaultColor;
  29. };
  30. color = (location: Location, isSecondary: boolean) => {
  31. if (isSecondary) {
  32. return SaccharideColors.Secondary;
  33. } else {
  34. if (StructureElement.Location.is(location)) {
  35. return getColor(location.unit, location.element);
  36. } else if (Bond.isLocation(location)) {
  37. return getColor(location.aUnit, location.aUnit.elements[location.aIndex]);
  38. }
  39. }
  40. return DefaultColor;
  41. };
  42. } else {
  43. color = () => DefaultColor;
  44. }
  45. return {
  46. factory: CarbohydrateSymbolColorTheme,
  47. granularity: 'group',
  48. color: color,
  49. props: props,
  50. description: Description,
  51. legend: TableLegend(MonosaccharidesColorTable)
  52. };
  53. }
  54. export const CarbohydrateSymbolColorThemeProvider: ColorTheme.Provider<CarbohydrateSymbolColorThemeParams, 'carbohydrate-symbol'> = {
  55. name: 'carbohydrate-symbol',
  56. label: 'Carbohydrate Symbol',
  57. category: ColorTheme.Category.Residue,
  58. factory: CarbohydrateSymbolColorTheme,
  59. getParams: getCarbohydrateSymbolColorThemeParams,
  60. defaultValues: PD.getDefaultValues(CarbohydrateSymbolColorThemeParams),
  61. isApplicable: (ctx: ThemeDataContext) => {
  62. return !!ctx.structure && ctx.structure.models.some(m => Model.hasCarbohydrate(m));
  63. }
  64. };