carbohydrate-symbol.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { StructureElement, Link, ElementIndex, Unit } 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/color/tables';
  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, getElementIndex, getAnomericCarbon } = ctx.structure.carbohydrates
  25. const getColor = (unit: Unit, index: ElementIndex) => {
  26. const residueIndex = unit.model.atomicHierarchy.residueAtomSegments.index[index]
  27. const anomericCarbon = getAnomericCarbon(unit, residueIndex)
  28. if (anomericCarbon !== undefined) {
  29. const idx = getElementIndex(unit, anomericCarbon)
  30. if (idx !== undefined) return elements[idx].component.color
  31. }
  32. return DefaultColor
  33. }
  34. color = (location: Location, isSecondary: boolean) => {
  35. if (isSecondary) {
  36. return SaccharideColors.Secondary
  37. } else {
  38. if (StructureElement.isLocation(location)) {
  39. return getColor(location.unit, location.element)
  40. } else if (Link.isLocation(location)) {
  41. return getColor(location.aUnit, location.aUnit.elements[location.aIndex])
  42. }
  43. }
  44. return DefaultColor
  45. }
  46. } else {
  47. color = () => DefaultColor
  48. }
  49. return {
  50. factory: CarbohydrateSymbolColorTheme,
  51. granularity: 'group',
  52. color: color,
  53. props: props,
  54. description: Description,
  55. legend: TableLegend(MonosaccharidesColorTable)
  56. }
  57. }
  58. export const CarbohydrateSymbolColorThemeProvider: ColorTheme.Provider<CarbohydrateSymbolColorThemeParams> = {
  59. label: 'Carbohydrate Symbol',
  60. factory: CarbohydrateSymbolColorTheme,
  61. getParams: getCarbohydrateSymbolColorThemeParams,
  62. defaultValues: PD.getDefaultValues(CarbohydrateSymbolColorThemeParams),
  63. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  64. }