chain-id.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 { Unit, StructureProperties, StructureElement, Link } from 'mol-model/structure';
  7. import { Color } from 'mol-util/color';
  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 'mol-theme/theme';
  12. import { ScaleLegend } from 'mol-util/color/scale';
  13. import { Column } from 'mol-data/db';
  14. import { getPaletteParams, getPalette } from './util';
  15. import { TableLegend } from 'mol-util/color/tables';
  16. const DefaultColor = Color(0xCCCCCC)
  17. const Description = 'Gives every chain a color based on its `asym_id` value.'
  18. export const ChainIdColorThemeParams = {
  19. ...getPaletteParams({ scaleList: 'RedYellowBlue' }),
  20. }
  21. export type ChainIdColorThemeParams = typeof ChainIdColorThemeParams
  22. export function getChainIdColorThemeParams(ctx: ThemeDataContext) {
  23. return ChainIdColorThemeParams // TODO return copy
  24. }
  25. function getAsymId(unit: Unit): StructureElement.Property<string> {
  26. switch (unit.kind) {
  27. case Unit.Kind.Atomic:
  28. return StructureProperties.chain.label_asym_id
  29. case Unit.Kind.Spheres:
  30. case Unit.Kind.Gaussians:
  31. return StructureProperties.coarse.asym_id
  32. }
  33. }
  34. function addAsymIds(map: Map<string, number>, data: Column<string>) {
  35. let j = map.size
  36. for (let o = 0, ol = data.rowCount; o < ol; ++o) {
  37. const k = data.value(o)
  38. if (!map.has(k)) {
  39. map.set(k, j)
  40. j += 1
  41. }
  42. }
  43. }
  44. export function ChainIdColorTheme(ctx: ThemeDataContext, props: PD.Values<ChainIdColorThemeParams>): ColorTheme<ChainIdColorThemeParams> {
  45. let color: LocationColor
  46. let legend: ScaleLegend | TableLegend | undefined
  47. if (ctx.structure) {
  48. // TODO same asym ids in different models should get different color
  49. const l = StructureElement.create()
  50. const { models } = ctx.structure
  51. const asymIdSerialMap = new Map<string, number>()
  52. for (let i = 0, il = models.length; i <il; ++i) {
  53. const m = models[i]
  54. addAsymIds(asymIdSerialMap, m.atomicHierarchy.chains.label_asym_id)
  55. if (m.coarseHierarchy.isDefined) {
  56. addAsymIds(asymIdSerialMap, m.coarseHierarchy.spheres.asym_id)
  57. addAsymIds(asymIdSerialMap, m.coarseHierarchy.gaussians.asym_id)
  58. }
  59. }
  60. const palette = getPalette(asymIdSerialMap.size, props)
  61. legend = palette.legend
  62. color = (location: Location): Color => {
  63. if (StructureElement.isLocation(location)) {
  64. const asym_id = getAsymId(location.unit)
  65. return palette.color(asymIdSerialMap.get(asym_id(location)) || 0)
  66. } else if (Link.isLocation(location)) {
  67. const asym_id = getAsymId(location.aUnit)
  68. l.unit = location.aUnit
  69. l.element = location.aUnit.elements[location.aIndex]
  70. return palette.color(asymIdSerialMap.get(asym_id(l)) || 0)
  71. }
  72. return DefaultColor
  73. }
  74. } else {
  75. color = () => DefaultColor
  76. }
  77. return {
  78. factory: ChainIdColorTheme,
  79. granularity: 'group',
  80. color,
  81. props,
  82. description: Description,
  83. legend
  84. }
  85. }
  86. export const ChainIdColorThemeProvider: ColorTheme.Provider<ChainIdColorThemeParams> = {
  87. label: 'Chain Id',
  88. factory: ChainIdColorTheme,
  89. getParams: getChainIdColorThemeParams,
  90. defaultValues: PD.getDefaultValues(ChainIdColorThemeParams),
  91. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  92. }