hydrophobicity.ts 4.2 KB

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