chain-id.ts 5.0 KB

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