residue-name.ts 4.0 KB

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