coloring.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import { Unit, StructureProperties, StructureElement, Bond } from '../../mol-model/structure';
  8. import { Color } from '../../mol-util/color';
  9. import { Location } from '../../mol-model/location';
  10. import { ColorTheme, LocationColor } from '../../mol-theme/color';
  11. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  12. import { ThemeDataContext } from '../../mol-theme/theme';
  13. import { Column } from '../../mol-data/db';
  14. const Description = 'Gives every chain a color from a list based on its `asym_id` value.';
  15. export function createProteopediaCustomTheme(colors: number[]) {
  16. const ProteopediaCustomColorThemeParams = {
  17. colors: PD.ObjectList({ color: PD.Color(Color(0xffffff)) }, ({ color }) => Color.toHexString(color),
  18. { defaultValue: colors.map(c => ({ color: Color(c) })) })
  19. };
  20. type ProteopediaCustomColorThemeParams = typeof ProteopediaCustomColorThemeParams
  21. function getChainIdColorThemeParams(ctx: ThemeDataContext) {
  22. return ProteopediaCustomColorThemeParams; // 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 addAsymIds(map: Map<string, number>, data: Column<string>) {
  34. let j = map.size;
  35. for (let o = 0, ol = data.rowCount; o < ol; ++o) {
  36. const k = data.value(o);
  37. if (!map.has(k)) {
  38. map.set(k, j);
  39. j += 1;
  40. }
  41. }
  42. }
  43. function ProteopediaCustomColorTheme(ctx: ThemeDataContext, props: PD.Values<ProteopediaCustomColorThemeParams>): ColorTheme<ProteopediaCustomColorThemeParams> {
  44. let color: LocationColor;
  45. const colors = props.colors, colorCount = colors.length, defaultColor = colors[0].color;
  46. if (ctx.structure) {
  47. const l = StructureElement.Location.create(ctx.structure);
  48. const { models } = ctx.structure;
  49. const asymIdSerialMap = new Map<string, number>();
  50. for (let i = 0, il = models.length; i < il; ++i) {
  51. const m = models[i];
  52. addAsymIds(asymIdSerialMap, m.atomicHierarchy.chains.label_asym_id);
  53. if (m.coarseHierarchy.isDefined) {
  54. addAsymIds(asymIdSerialMap, m.coarseHierarchy.spheres.asym_id);
  55. addAsymIds(asymIdSerialMap, m.coarseHierarchy.gaussians.asym_id);
  56. }
  57. }
  58. color = (location: Location): Color => {
  59. if (StructureElement.Location.is(location)) {
  60. const asym_id = getAsymId(location.unit);
  61. const o = asymIdSerialMap.get(asym_id(location)) || 0;
  62. return colors[o % colorCount].color;
  63. } else if (Bond.isLocation(location)) {
  64. const asym_id = getAsymId(location.aUnit);
  65. l.unit = location.aUnit;
  66. l.element = location.aUnit.elements[location.aIndex];
  67. const o = asymIdSerialMap.get(asym_id(l)) || 0;
  68. return colors[o % colorCount].color;
  69. }
  70. return defaultColor;
  71. };
  72. } else {
  73. color = () => defaultColor;
  74. }
  75. return {
  76. factory: ProteopediaCustomColorTheme,
  77. granularity: 'group',
  78. color,
  79. props,
  80. description: Description,
  81. legend: undefined
  82. };
  83. }
  84. return {
  85. name: 'proteopedia-custom',
  86. label: 'Proteopedia Custom',
  87. category: 'Custom',
  88. factory: ProteopediaCustomColorTheme,
  89. getParams: getChainIdColorThemeParams,
  90. defaultValues: PD.getDefaultValues(ProteopediaCustomColorThemeParams),
  91. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  92. };
  93. }