residue-name.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 { Color, ColorMap } from 'mol-util/color';
  7. import { StructureElement, Unit, Link, ElementIndex } from 'mol-model/structure';
  8. import { Location } from 'mol-model/location';
  9. import { ColorTheme, TableLegend } from '../color';
  10. import { ParamDefinition as PD } from 'mol-util/param-definition'
  11. import { ThemeDataContext } from 'mol-theme/theme';
  12. // protein colors from Jmol http://jmol.sourceforge.net/jscolors/
  13. const ResidueNameColors = ColorMap({
  14. // standard amino acids
  15. 'ALA': 0x8CFF8C,
  16. 'ARG': 0x00007C,
  17. 'ASN': 0xFF7C70,
  18. 'ASP': 0xA00042,
  19. 'CYS': 0xFFFF70,
  20. 'GLN': 0xFF4C4C,
  21. 'GLU': 0x660000,
  22. 'GLY': 0xEEEEEE,
  23. 'HIS': 0x7070FF,
  24. 'ILE': 0x004C00,
  25. 'LEU': 0x455E45,
  26. 'LYS': 0x4747B8,
  27. 'MET': 0xB8A042,
  28. 'PHE': 0x534C52,
  29. 'PRO': 0x525252,
  30. 'SER': 0xFF7042,
  31. 'THR': 0xB84C00,
  32. 'TRP': 0x4F4600,
  33. 'TYR': 0x8C704C,
  34. 'VAL': 0xFF8CFF,
  35. // rna bases
  36. 'A': 0xDC143C, // Crimson Red
  37. 'G': 0x32CD32, // Lime Green
  38. 'I': 0x9ACD32, // Yellow Green
  39. 'C': 0xFFD700, // Gold Yellow
  40. 'T': 0x4169E1, // Royal Blue
  41. 'U': 0x40E0D0, // Turquoise Cyan
  42. // dna bases
  43. 'DA': 0xDC143C,
  44. 'DG': 0x32CD32,
  45. 'DI': 0x9ACD32,
  46. 'DC': 0xFFD700,
  47. 'DT': 0x4169E1,
  48. 'DU': 0x40E0D0,
  49. // peptide bases
  50. 'APN': 0xDC143C,
  51. 'GPN': 0x32CD32,
  52. 'CPN': 0xFFD700,
  53. 'TPN': 0x4169E1,
  54. })
  55. const DefaultResidueNameColor = Color(0xFF00FF)
  56. const Description = 'Assigns a color to every residue according to its name.'
  57. export const ResidueNameColorThemeParams = {}
  58. export function getResidueNameColorThemeParams(ctx: ThemeDataContext) {
  59. return ResidueNameColorThemeParams // TODO return copy
  60. }
  61. export type ResidueNameColorThemeProps = PD.Values<typeof ResidueNameColorThemeParams>
  62. export function residueNameColor(residueName: string): Color {
  63. const c = (ResidueNameColors as { [k: string]: Color })[residueName];
  64. return c === undefined ? DefaultResidueNameColor : c
  65. }
  66. function getAtomicCompId(unit: Unit.Atomic, element: ElementIndex) {
  67. const { modifiedResidues } = unit.model.properties
  68. const compId = unit.model.atomicHierarchy.residues.auth_comp_id.value(unit.residueIndex[element])
  69. const parentId = modifiedResidues.parentId.get(compId)
  70. return parentId === undefined ? compId : parentId
  71. }
  72. function getCoarseCompId(unit: Unit.Spheres | Unit.Gaussians, element: ElementIndex) {
  73. const seqIdBegin = unit.coarseElements.seq_id_begin.value(element)
  74. const seqIdEnd = unit.coarseElements.seq_id_end.value(element)
  75. if (seqIdBegin === seqIdEnd) {
  76. const { modifiedResidues } = unit.model.properties
  77. const entityKey = unit.coarseElements.entityKey[element]
  78. const seq = unit.model.sequence.byEntityKey[entityKey]
  79. let compId = seq.compId.value(seqIdBegin - 1) // 1-indexed
  80. const parentId = modifiedResidues.parentId.get(compId)
  81. return parentId === undefined ? compId : parentId
  82. }
  83. }
  84. export function ResidueNameColorTheme(ctx: ThemeDataContext, props: ResidueNameColorThemeProps): ColorTheme<ResidueNameColorThemeProps> {
  85. function color(location: Location): Color {
  86. if (StructureElement.isLocation(location)) {
  87. if (Unit.isAtomic(location.unit)) {
  88. return residueNameColor(getAtomicCompId(location.unit, location.element))
  89. } else {
  90. const compId = getCoarseCompId(location.unit, location.element)
  91. if (compId) return residueNameColor(compId)
  92. }
  93. } else if (Link.isLocation(location)) {
  94. if (Unit.isAtomic(location.aUnit)) {
  95. return residueNameColor(getAtomicCompId(location.aUnit, location.aUnit.elements[location.aIndex]))
  96. } else {
  97. const compId = getCoarseCompId(location.aUnit, location.aUnit.elements[location.aIndex])
  98. if (compId) return residueNameColor(compId)
  99. }
  100. }
  101. return DefaultResidueNameColor
  102. }
  103. return {
  104. granularity: 'group',
  105. color,
  106. props,
  107. description: Description,
  108. legend: TableLegend(Object.keys(ResidueNameColors).map(name => {
  109. return [name, (ResidueNameColors as any)[name] as Color] as [string, Color]
  110. }).concat([[ 'Unknown', DefaultResidueNameColor ]]))
  111. }
  112. }
  113. export const ResidueNameColorThemeProvider: ColorTheme.Provider<typeof ResidueNameColorThemeParams> = {
  114. factory: ResidueNameColorTheme, params: getResidueNameColorThemeParams
  115. }