polymer-index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * Copyright (c) 2018-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 { Location } from '../../mol-model/location';
  8. import { StructureElement, Link, Structure } from '../../mol-model/structure';
  9. import { ColorTheme, LocationColor } from '../color';
  10. import { ParamDefinition as PD } from '../../mol-util/param-definition'
  11. import { ThemeDataContext } from '../../mol-theme/theme';
  12. import { TableLegend, ScaleLegend } from '../../mol-util/legend';
  13. import { getPaletteParams, getPalette } from '../../mol-util/color/palette';
  14. const DefaultColor = Color(0xCCCCCC)
  15. const Description = 'Gives every polymer a unique color based on the position (index) of the polymer in the list of polymers in the structure.'
  16. export const PolymerIndexColorThemeParams = {
  17. ...getPaletteParams({ type: 'set', setList: 'set-3' }),
  18. }
  19. export type PolymerIndexColorThemeParams = typeof PolymerIndexColorThemeParams
  20. export function getPolymerIndexColorThemeParams(ctx: ThemeDataContext) {
  21. const params = PD.clone(PolymerIndexColorThemeParams)
  22. if (ctx.structure) {
  23. if (getPolymerChainCount(ctx.structure.root) > 12) {
  24. params.palette.defaultValue.name = 'scale'
  25. params.palette.defaultValue.params = { list: 'red-yellow-blue' }
  26. }
  27. }
  28. return params
  29. }
  30. export type PolymerIndexColorThemeProps = PD.Values<typeof PolymerIndexColorThemeParams>
  31. function getPolymerChainCount(structure: Structure) {
  32. let polymerChainCount = 0
  33. const { units } = structure
  34. for (let i = 0, il = units.length; i <il; ++i) {
  35. if (units[i].polymerElements.length > 0) ++polymerChainCount
  36. }
  37. return polymerChainCount
  38. }
  39. export function PolymerIndexColorTheme(ctx: ThemeDataContext, props: PD.Values<PolymerIndexColorThemeParams>): ColorTheme<PolymerIndexColorThemeParams> {
  40. let color: LocationColor
  41. let legend: ScaleLegend | TableLegend | undefined
  42. if (ctx.structure) {
  43. const palette = getPalette(getPolymerChainCount(ctx.structure.root), props)
  44. legend = palette.legend
  45. const { units } = ctx.structure.root
  46. const unitIdColor = new Map<number, Color>()
  47. for (let i = 0, j = 0, il = units.length; i <il; ++i) {
  48. if (units[i].polymerElements.length > 0) {
  49. unitIdColor.set(units[i].id, palette.color(j))
  50. ++j
  51. }
  52. }
  53. color = (location: Location): Color => {
  54. let color: Color | undefined
  55. if (StructureElement.Location.is(location)) {
  56. color = unitIdColor.get(location.unit.id)
  57. } else if (Link.isLocation(location)) {
  58. color = unitIdColor.get(location.aUnit.id)
  59. }
  60. return color !== undefined ? color : DefaultColor
  61. }
  62. } else {
  63. color = () => DefaultColor
  64. }
  65. return {
  66. factory: PolymerIndexColorTheme,
  67. granularity: 'instance',
  68. color,
  69. props,
  70. description: Description,
  71. legend
  72. }
  73. }
  74. export const PolymerIndexColorThemeProvider: ColorTheme.Provider<PolymerIndexColorThemeParams> = {
  75. label: 'Polymer Index',
  76. factory: PolymerIndexColorTheme,
  77. getParams: getPolymerIndexColorThemeParams,
  78. defaultValues: PD.getDefaultValues(PolymerIndexColorThemeParams),
  79. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  80. }