chain-id.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 { ColorThemeProps, ColorTheme, LocationColor } from '../color';
  10. const DefaultColor = Color(0xCCCCCC)
  11. const Description = 'Gives every chain a color based on its `asym_id` value.'
  12. function getAsymId(unit: Unit): StructureElement.Property<string> {
  13. switch (unit.kind) {
  14. case Unit.Kind.Atomic:
  15. return StructureProperties.chain.label_asym_id
  16. case Unit.Kind.Spheres:
  17. case Unit.Kind.Gaussians:
  18. return StructureProperties.coarse.asym_id
  19. }
  20. }
  21. export function ChainIdColorTheme(props: ColorThemeProps): ColorTheme {
  22. let color: LocationColor
  23. let scale: ColorScale | undefined = undefined
  24. // const table: [string, Color][] = []
  25. if (props.structure) {
  26. const l = StructureElement.create()
  27. const { models } = props.structure
  28. const asymIdSerialMap = new Map<string, number>()
  29. let j = 0
  30. for (let i = 0, il = models.length; i <il; ++i) {
  31. models[i].properties.asymIdSerialMap.forEach((v, k) => {
  32. if (!asymIdSerialMap.has(k)) {
  33. asymIdSerialMap.set(k, j)
  34. j += 1
  35. }
  36. })
  37. }
  38. scale = ColorScale.create({ domain: [ 0, asymIdSerialMap.size - 1 ] })
  39. const scaleColor = scale.color
  40. // asymIdSerialMap.forEach((v, k) => table.push([k, scaleColor(v)]))
  41. color = (location: Location): Color => {
  42. if (StructureElement.isLocation(location)) {
  43. const asym_id = getAsymId(location.unit)
  44. return scaleColor(asymIdSerialMap.get(asym_id(location)) || 0)
  45. } else if (Link.isLocation(location)) {
  46. const asym_id = getAsymId(location.aUnit)
  47. l.unit = location.aUnit
  48. l.element = location.aUnit.elements[location.aIndex]
  49. return scaleColor(asymIdSerialMap.get(asym_id(l)) || 0)
  50. }
  51. return DefaultColor
  52. }
  53. } else {
  54. color = () => DefaultColor
  55. }
  56. return {
  57. granularity: 'group',
  58. color,
  59. description: Description,
  60. // legend: scale ? TableLegend(table) : undefined
  61. legend: scale ? scale.legend : undefined
  62. }
  63. }