chain-id.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, Structure } 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 { getPaletteParams, getPalette } from '../../mol-util/color/palette';
  14. import { TableLegend } from '../../mol-util/color/lists';
  15. import { Segmentation } from '../../mol-data/int';
  16. const DefaultColor = Color(0xFAFAFA)
  17. const Description = 'Gives every chain a color based on its `asym_id` value.'
  18. export const ChainIdColorThemeParams = {
  19. ...getPaletteParams({ type: 'set', setList: 'set-3' }),
  20. }
  21. export type ChainIdColorThemeParams = typeof ChainIdColorThemeParams
  22. export function getChainIdColorThemeParams(ctx: ThemeDataContext) {
  23. const params = PD.clone(ChainIdColorThemeParams)
  24. if (ctx.structure) {
  25. if (getAsymIdSerialMap(ctx.structure.root).size > 12) {
  26. params.palette.defaultValue.name = 'scale'
  27. params.palette.defaultValue.params = { list: 'red-yellow-blue' }
  28. }
  29. }
  30. return params
  31. }
  32. function getAsymId(unit: Unit): StructureElement.Property<string> {
  33. switch (unit.kind) {
  34. case Unit.Kind.Atomic:
  35. return StructureProperties.chain.label_asym_id
  36. case Unit.Kind.Spheres:
  37. case Unit.Kind.Gaussians:
  38. return StructureProperties.coarse.asym_id
  39. }
  40. }
  41. function getAsymIdSerialMap(structure: Structure) {
  42. const map = new Map<string, number>()
  43. for (let i = 0, il = structure.unitSymmetryGroups.length; i < il; ++i) {
  44. const unit = structure.unitSymmetryGroups[i].units[0]
  45. const { model } = unit
  46. if (Unit.isAtomic(unit)) {
  47. const { chainAtomSegments, chains } = model.atomicHierarchy
  48. const chainIt = Segmentation.transientSegments(chainAtomSegments, unit.elements)
  49. while (chainIt.hasNext) {
  50. const { index: chainIndex } = chainIt.move()
  51. const asymId = chains.label_asym_id.value(chainIndex)
  52. if (!map.has(asymId)) map.set(asymId, map.size)
  53. }
  54. } else if (Unit.isCoarse(unit)) {
  55. const { chainElementSegments, asym_id } = Unit.isSpheres(unit)
  56. ? model.coarseHierarchy.spheres
  57. : model.coarseHierarchy.gaussians
  58. const chainIt = Segmentation.transientSegments(chainElementSegments, unit.elements)
  59. while (chainIt.hasNext) {
  60. const { index: chainIndex } = chainIt.move()
  61. const elementIndex = chainElementSegments.offsets[chainIndex]
  62. const asymId = asym_id.value(elementIndex)
  63. if (!map.has(asymId)) map.set(asymId, map.size)
  64. }
  65. }
  66. }
  67. return map
  68. }
  69. export function ChainIdColorTheme(ctx: ThemeDataContext, props: PD.Values<ChainIdColorThemeParams>): ColorTheme<ChainIdColorThemeParams> {
  70. let color: LocationColor
  71. let legend: ScaleLegend | TableLegend | undefined
  72. if (ctx.structure) {
  73. const l = StructureElement.Location.create()
  74. const asymIdSerialMap = getAsymIdSerialMap(ctx.structure.root)
  75. const palette = getPalette(asymIdSerialMap.size, props)
  76. legend = palette.legend
  77. color = (location: Location): Color => {
  78. let serial: number | undefined = undefined
  79. if (StructureElement.Location.is(location)) {
  80. const asym_id = getAsymId(location.unit)
  81. serial = asymIdSerialMap.get(asym_id(location))
  82. } else if (Link.isLocation(location)) {
  83. const asym_id = getAsymId(location.aUnit)
  84. l.unit = location.aUnit
  85. l.element = location.aUnit.elements[location.aIndex]
  86. serial = asymIdSerialMap.get(asym_id(l))
  87. }
  88. return serial === undefined ? DefaultColor : palette.color(serial)
  89. }
  90. } else {
  91. color = () => DefaultColor
  92. }
  93. return {
  94. factory: ChainIdColorTheme,
  95. granularity: 'group',
  96. color,
  97. props,
  98. description: Description,
  99. legend
  100. }
  101. }
  102. export const ChainIdColorThemeProvider: ColorTheme.Provider<ChainIdColorThemeParams> = {
  103. label: 'Chain Id',
  104. factory: ChainIdColorTheme,
  105. getParams: getChainIdColorThemeParams,
  106. defaultValues: PD.getDefaultValues(ChainIdColorThemeParams),
  107. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  108. }