polymer-id.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 { Unit, StructureProperties, StructureElement, Link } from 'mol-model/structure';
  7. import { ColorScale, 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 { ColorListOptions, ColorListName } from 'mol-util/color/scale';
  13. import { Column } from 'mol-data/db';
  14. import { Entities } from 'mol-model/structure/model/properties/common';
  15. const DefaultColor = Color(0xCCCCCC)
  16. const Description = 'Gives every polymer chain a color based on its `asym_id` value.'
  17. export const PolymerIdColorThemeParams = {
  18. list: PD.ColorScale<ColorListName>('RedYellowBlue', ColorListOptions),
  19. }
  20. export type PolymerIdColorThemeParams = typeof PolymerIdColorThemeParams
  21. export function getPolymerIdColorThemeParams(ctx: ThemeDataContext) {
  22. return PolymerIdColorThemeParams // TODO return copy
  23. }
  24. function getAsymId(unit: Unit): StructureElement.Property<string> {
  25. switch (unit.kind) {
  26. case Unit.Kind.Atomic:
  27. return StructureProperties.chain.label_asym_id
  28. case Unit.Kind.Spheres:
  29. case Unit.Kind.Gaussians:
  30. return StructureProperties.coarse.asym_id
  31. }
  32. }
  33. function addPolymerAsymIds(map: Map<string, number>, asymId: Column<string>, entityId: Column<string>, entities: Entities) {
  34. let j = map.size
  35. for (let o = 0, ol = asymId.rowCount; o < ol; ++o) {
  36. const e = entityId.value(o)
  37. const eI = entities.getEntityIndex(e)
  38. if (entities.data.type.value(eI) === 'polymer') {
  39. const k = asymId.value(o)
  40. if (!map.has(k)) {
  41. map.set(k, j)
  42. j += 1
  43. }
  44. }
  45. }
  46. }
  47. export function PolymerIdColorTheme(ctx: ThemeDataContext, props: PD.Values<PolymerIdColorThemeParams>): ColorTheme<PolymerIdColorThemeParams> {
  48. let color: LocationColor
  49. const scale = ColorScale.create({ listOrName: props.list, minLabel: 'Start', maxLabel: 'End' })
  50. if (ctx.structure) {
  51. const l = StructureElement.create()
  52. const { models } = ctx.structure
  53. const polymerAsymIdSerialMap = new Map<string, number>()
  54. for (let i = 0, il = models.length; i <il; ++i) {
  55. for (let i = 0, il = models.length; i <il; ++i) {
  56. const m = models[i]
  57. addPolymerAsymIds(polymerAsymIdSerialMap, m.atomicHierarchy.chains.label_asym_id, m.atomicHierarchy.chains.label_entity_id, m.entities)
  58. if (m.coarseHierarchy.isDefined) {
  59. addPolymerAsymIds(polymerAsymIdSerialMap, m.coarseHierarchy.spheres.asym_id, m.coarseHierarchy.spheres.entity_id, m.entities)
  60. addPolymerAsymIds(polymerAsymIdSerialMap, m.coarseHierarchy.gaussians.asym_id, m.coarseHierarchy.spheres.entity_id, m.entities)
  61. }
  62. }
  63. }
  64. scale.setDomain(0, polymerAsymIdSerialMap.size - 1)
  65. const scaleColor = scale.color
  66. color = (location: Location): Color => {
  67. if (StructureElement.isLocation(location)) {
  68. const asym_id = getAsymId(location.unit)
  69. return scaleColor(polymerAsymIdSerialMap.get(asym_id(location)) || 0)
  70. } else if (Link.isLocation(location)) {
  71. const asym_id = getAsymId(location.aUnit)
  72. l.unit = location.aUnit
  73. l.element = location.aUnit.elements[location.aIndex]
  74. return scaleColor(polymerAsymIdSerialMap.get(asym_id(l)) || 0)
  75. }
  76. return DefaultColor
  77. }
  78. } else {
  79. color = () => DefaultColor
  80. }
  81. return {
  82. factory: PolymerIdColorTheme,
  83. granularity: 'group',
  84. color,
  85. props,
  86. description: Description,
  87. legend: scale ? scale.legend : undefined
  88. }
  89. }
  90. export const PolymerIdColorThemeProvider: ColorTheme.Provider<PolymerIdColorThemeParams> = {
  91. label: 'Polymer Id',
  92. factory: PolymerIdColorTheme,
  93. getParams: getPolymerIdColorThemeParams,
  94. defaultValues: PD.getDefaultValues(PolymerIdColorThemeParams),
  95. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  96. }