chain-id.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright (c) 2018-2020 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, Model } 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. const DefaultList = 'many-distinct';
  15. const DefaultColor = Color(0xFAFAFA);
  16. const Description = 'Gives every chain a color based on its `asym_id` value.';
  17. export const ChainIdColorThemeParams = {
  18. asymId: PD.Select('auth', PD.arrayToOptions<AsymIdType>(['auth', 'label'])),
  19. ...getPaletteParams({ type: 'colors', colorList: DefaultList }),
  20. };
  21. export type ChainIdColorThemeParams = typeof ChainIdColorThemeParams
  22. export function getChainIdColorThemeParams(ctx: ThemeDataContext) {
  23. const params = PD.clone(ChainIdColorThemeParams);
  24. if (ctx.structure?.models.some(m => m.coarseHierarchy.isDefined)) {
  25. params.asymId.defaultValue = 'label';
  26. }
  27. return params;
  28. }
  29. type AsymIdType = 'auth' | 'label';
  30. function getAsymId(unit: Unit, type: AsymIdType): StructureElement.Property<string> {
  31. switch (unit.kind) {
  32. case Unit.Kind.Atomic:
  33. return type === 'auth'
  34. ? StructureProperties.chain.auth_asym_id
  35. : 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 getAsymIdKey(location: StructureElement.Location, type: AsymIdType) {
  42. const asymId = getAsymId(location.unit, type)(location);
  43. return location.structure.root.models.length > 1
  44. ? getKey(location.unit.model, asymId)
  45. : asymId;
  46. }
  47. function getKey(model: Model, asymId: string) {
  48. return `${asymId}|${(Model.Index.get(model).value || 0) + 1}`;
  49. }
  50. function getAsymIdSerialMap(structure: Structure, type: AsymIdType) {
  51. const map = new Map<string, number>();
  52. for (const m of structure.models) {
  53. const asymIdOffset = Model.AsymIdOffset.get(m).value;
  54. const offset = (type === 'auth' ? asymIdOffset?.auth : asymIdOffset?.label) || 0;
  55. let count = 0;
  56. m.properties.structAsymMap.forEach(({ auth_id }, label_id) => {
  57. const asymId = type === 'auth' ? auth_id : label_id;
  58. const k = structure.models.length > 1
  59. ? getKey(m, asymId)
  60. : asymId;
  61. if (!map.has(k)) {
  62. map.set(k, count + offset);
  63. ++count;
  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(ctx.structure.root);
  74. const asymIdSerialMap = getAsymIdSerialMap(ctx.structure.root, props.asymId);
  75. const labelTable = Array.from(asymIdSerialMap.keys());
  76. const valueLabel = (i: number) => labelTable[i];
  77. const palette = getPalette(asymIdSerialMap.size, props, { valueLabel });
  78. legend = palette.legend;
  79. color = (location: Location): Color => {
  80. let serial: number | undefined = undefined;
  81. if (StructureElement.Location.is(location)) {
  82. const k = getAsymIdKey(location, props.asymId);
  83. serial = asymIdSerialMap.get(k);
  84. } else if (Bond.isLocation(location)) {
  85. l.unit = location.aUnit;
  86. l.element = location.aUnit.elements[location.aIndex];
  87. const k = getAsymIdKey(l, props.asymId);
  88. serial = asymIdSerialMap.get(k);
  89. }
  90. return serial === undefined ? DefaultColor : palette.color(serial);
  91. };
  92. } else {
  93. color = () => DefaultColor;
  94. }
  95. return {
  96. factory: ChainIdColorTheme,
  97. granularity: 'group',
  98. color,
  99. props,
  100. description: Description,
  101. legend
  102. };
  103. }
  104. export const ChainIdColorThemeProvider: ColorTheme.Provider<ChainIdColorThemeParams, 'chain-id'> = {
  105. name: 'chain-id',
  106. label: 'Chain Id',
  107. category: ColorTheme.Category.Chain,
  108. factory: ChainIdColorTheme,
  109. getParams: getChainIdColorThemeParams,
  110. defaultValues: PD.getDefaultValues(ChainIdColorThemeParams),
  111. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  112. };