hydrophobicity.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. */
  6. import { Color, ColorScale } from '../../mol-util/color';
  7. import { StructureElement, Unit, Bond, ElementIndex } from '../../mol-model/structure';
  8. import { Location } from '../../mol-model/location';
  9. import { ColorTheme } from '../color';
  10. import { ParamDefinition as PD } from '../../mol-util/param-definition'
  11. import { ThemeDataContext } from '../theme';
  12. import { ResidueHydrophobicity } from '../../mol-model/structure/model/types';
  13. const Description = 'Assigns a color to every amino acid according to the "Experimentally determined hydrophobicity scale for proteins at membrane interfaces" by Wimely and White (doi:10.1038/nsb1096-842).'
  14. export const HydrophobicityColorThemeParams = {
  15. list: PD.ColorList('red-yellow-green', { presetKind: 'scale' }),
  16. scale: PD.Select('DGwif', [['DGwif', 'DG water-membrane'], ['DGwoct', 'DG water-octanol'], ['Oct-IF', 'DG difference']] as const)
  17. }
  18. export type HydrophobicityColorThemeParams = typeof HydrophobicityColorThemeParams
  19. export function getHydrophobicityColorThemeParams(ctx: ThemeDataContext) {
  20. return HydrophobicityColorThemeParams // TODO return copy
  21. }
  22. const scaleIndexMap = { 'DGwif': 0, 'DGwoct': 1, 'Oct-IF': 2 }
  23. export function hydrophobicity(compId: string, scaleIndex: number): number {
  24. const c = (ResidueHydrophobicity as { [k: string]: number[] })[compId];
  25. return c === undefined ? 0 : c[scaleIndex]
  26. }
  27. function getAtomicCompId(unit: Unit.Atomic, element: ElementIndex) {
  28. return unit.model.atomicHierarchy.residues.label_comp_id.value(unit.residueIndex[element])
  29. }
  30. function getCoarseCompId(unit: Unit.Spheres | Unit.Gaussians, element: ElementIndex) {
  31. const seqIdBegin = unit.coarseElements.seq_id_begin.value(element)
  32. const seqIdEnd = unit.coarseElements.seq_id_end.value(element)
  33. if (seqIdBegin === seqIdEnd) {
  34. const entityKey = unit.coarseElements.entityKey[element]
  35. const seq = unit.model.sequence.byEntityKey[entityKey].sequence
  36. return seq.compId.value(seqIdBegin - 1) // 1-indexed
  37. }
  38. }
  39. export function HydrophobicityColorTheme(ctx: ThemeDataContext, props: PD.Values<HydrophobicityColorThemeParams>): ColorTheme<HydrophobicityColorThemeParams> {
  40. const scaleIndex = scaleIndexMap[props.scale]
  41. // get domain
  42. let min = Infinity
  43. let max = -Infinity
  44. for (const name in ResidueHydrophobicity) {
  45. const val = (ResidueHydrophobicity as { [k: string]: number[] })[name][scaleIndex]
  46. min = Math.min(min, val)
  47. max = Math.max(max, val)
  48. }
  49. const scale = ColorScale.create({
  50. listOrName: props.list.colors,
  51. domain: [ max, min ],
  52. minLabel: 'Hydrophobic',
  53. maxLabel: 'Hydrophilic'
  54. })
  55. function color(location: Location): Color {
  56. let compId: string | undefined
  57. if (StructureElement.Location.is(location)) {
  58. if (Unit.isAtomic(location.unit)) {
  59. compId = getAtomicCompId(location.unit, location.element)
  60. } else {
  61. compId = getCoarseCompId(location.unit, location.element)
  62. }
  63. } else if (Bond.isLocation(location)) {
  64. if (Unit.isAtomic(location.aUnit)) {
  65. compId = getAtomicCompId(location.aUnit, location.aUnit.elements[location.aIndex])
  66. } else {
  67. compId = getCoarseCompId(location.aUnit, location.aUnit.elements[location.aIndex])
  68. }
  69. }
  70. return scale.color(compId ? hydrophobicity(compId, scaleIndex) : 0)
  71. }
  72. return {
  73. factory: HydrophobicityColorTheme,
  74. granularity: 'group',
  75. color,
  76. props,
  77. description: Description,
  78. legend: scale ? scale.legend : undefined
  79. }
  80. }
  81. export const HydrophobicityColorThemeProvider: ColorTheme.Provider<HydrophobicityColorThemeParams, 'hydrophobicity'> = {
  82. name: 'hydrophobicity',
  83. label: 'Hydrophobicity',
  84. category: ColorTheme.Category.Residue,
  85. factory: HydrophobicityColorTheme,
  86. getParams: getHydrophobicityColorThemeParams,
  87. defaultValues: PD.getDefaultValues(HydrophobicityColorThemeParams),
  88. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  89. }