polymer-index.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 { ColorScale, Color } from 'mol-util/color';
  7. import { Location } from 'mol-model/location';
  8. import { StructureElement, Link } 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 { ColorListName, ColorListOptions } from 'mol-util/color/scale';
  13. const DefaultColor = Color(0xCCCCCC)
  14. const Description = 'Gives every polymer a unique color based on the position (index) of the polymer in the list of polymers in the structure.'
  15. export const PolymerIndexColorThemeParams = {
  16. list: PD.Select<ColorListName>('RdYlBu', ColorListOptions),
  17. }
  18. export function getPolymerIndexColorThemeParams(ctx: ThemeDataContext) {
  19. return PolymerIndexColorThemeParams // TODO return copy
  20. }
  21. export type PolymerIndexColorThemeProps = PD.Values<typeof PolymerIndexColorThemeParams>
  22. export function PolymerIndexColorTheme(ctx: ThemeDataContext, props: PolymerIndexColorThemeProps): ColorTheme<PolymerIndexColorThemeProps> {
  23. let color: LocationColor
  24. const scale = ColorScale.create({ listOrName: props.list, minLabel: 'Start', maxLabel: 'End' })
  25. if (ctx.structure) {
  26. const { units } = ctx.structure
  27. let polymerCount = 0
  28. for (let i = 0, il = units.length; i <il; ++i) {
  29. if (units[i].polymerElements.length > 0) ++polymerCount
  30. }
  31. scale.setDomain(0, polymerCount - 1)
  32. const unitIdColor = new Map<number, Color>()
  33. for (let i = 0, j = 0, il = units.length; i <il; ++i) {
  34. if (units[i].polymerElements.length > 0) {
  35. unitIdColor.set(units[i].id, scale.color(j))
  36. ++j
  37. }
  38. }
  39. color = (location: Location): Color => {
  40. let color: Color | undefined
  41. if (StructureElement.isLocation(location)) {
  42. color = unitIdColor.get(location.unit.id)
  43. } else if (Link.isLocation(location)) {
  44. color = unitIdColor.get(location.aUnit.id)
  45. }
  46. return color !== undefined ? color : DefaultColor
  47. }
  48. } else {
  49. color = () => DefaultColor
  50. }
  51. return {
  52. granularity: 'instance',
  53. color,
  54. props,
  55. description: Description,
  56. legend: scale ? scale.legend : undefined
  57. }
  58. }
  59. export const PolymerIndexColorThemeProvider: ColorTheme.Provider<typeof PolymerIndexColorThemeParams> = {
  60. label: 'Polymer Index',
  61. factory: PolymerIndexColorTheme,
  62. getParams: getPolymerIndexColorThemeParams
  63. }