assembly-symmetry-cluster.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ThemeDataContext } from '../../../mol-theme/theme';
  7. import { ColorTheme, LocationColor } from '../../../mol-theme/color';
  8. import { ParamDefinition as PD } from '../../../mol-util/param-definition'
  9. import { Table } from '../../../mol-data/db';
  10. import { AssemblySymmetry } from '../assembly-symmetry';
  11. import { Color } from '../../../mol-util/color';
  12. import { Unit, StructureElement, StructureProperties } from '../../../mol-model/structure';
  13. import { Location } from '../../../mol-model/location';
  14. import { ScaleLegend } from '../../../mol-util/color/scale';
  15. import { getSymmetrySelectParam } from '../util';
  16. import { getPalette, getPaletteParams } from '../../../mol-util/color/palette';
  17. import { TableLegend } from '../../../mol-util/color/lists';
  18. const DefaultColor = Color(0xCCCCCC)
  19. function getAsymId(unit: Unit): StructureElement.Property<string> {
  20. switch (unit.kind) {
  21. case Unit.Kind.Atomic:
  22. return StructureProperties.chain.label_asym_id
  23. case Unit.Kind.Spheres:
  24. case Unit.Kind.Gaussians:
  25. return StructureProperties.coarse.asym_id
  26. }
  27. }
  28. function clusterMemberKey(assemblyId: string, asymId: string, operList: string[]) {
  29. return `${assemblyId}-${asymId}-${operList.join('|')}`
  30. }
  31. export const AssemblySymmetryClusterColorThemeParams = {
  32. ...getPaletteParams({ scaleList: 'red-yellow-blue' }),
  33. symmetryId: getSymmetrySelectParam(),
  34. }
  35. export type AssemblySymmetryClusterColorThemeParams = typeof AssemblySymmetryClusterColorThemeParams
  36. export function getAssemblySymmetryClusterColorThemeParams(ctx: ThemeDataContext) {
  37. const params = PD.clone(AssemblySymmetryClusterColorThemeParams)
  38. params.symmetryId = getSymmetrySelectParam(ctx.structure)
  39. return params
  40. }
  41. export function AssemblySymmetryClusterColorTheme(ctx: ThemeDataContext, props: PD.Values<AssemblySymmetryClusterColorThemeParams>): ColorTheme<AssemblySymmetryClusterColorThemeParams> {
  42. let color: LocationColor = () => DefaultColor
  43. let legend: ScaleLegend | TableLegend | undefined
  44. const { symmetryId } = props
  45. if (ctx.structure && !ctx.structure.isEmpty && ctx.structure.models[0].customProperties.has(AssemblySymmetry.Descriptor)) {
  46. const assemblySymmetry = AssemblySymmetry.get(ctx.structure.models[0])!
  47. const s = assemblySymmetry.db.rcsb_assembly_symmetry
  48. const symmetry = Table.pickRow(s, i => s.id.value(i) === symmetryId)
  49. if (symmetry) {
  50. const clusters = assemblySymmetry.getClusters(symmetryId)
  51. if (clusters._rowCount) {
  52. const clusterByMember = new Map<string, number>()
  53. for (let i = 0, il = clusters._rowCount; i < il; ++i) {
  54. const clusterMembers = assemblySymmetry.getClusterMembers(clusters.id.value(i))
  55. for (let j = 0, jl = clusterMembers._rowCount; j < jl; ++j) {
  56. const asymId = clusterMembers.asym_id.value(j)
  57. const operList = clusterMembers.pdbx_struct_oper_list_ids.value(j)
  58. if (operList.length === 0) operList.push('1') // TODO hack assuming '1' is the id of the identity operator
  59. clusterByMember.set(clusterMemberKey(symmetry.assembly_id, asymId, operList), i)
  60. }
  61. }
  62. const palette = getPalette(clusters._rowCount, props)
  63. legend = palette.legend
  64. color = (location: Location): Color => {
  65. if (StructureElement.Location.is(location)) {
  66. const { assembly } = location.unit.conformation.operator
  67. if (assembly && assembly.id === symmetry.assembly_id) {
  68. const asymId = getAsymId(location.unit)(location)
  69. const cluster = clusterByMember.get(clusterMemberKey(assembly.id, asymId, assembly.operList))
  70. return cluster !== undefined ? palette.color(cluster) : DefaultColor
  71. }
  72. }
  73. return DefaultColor
  74. }
  75. }
  76. }
  77. }
  78. return {
  79. factory: AssemblySymmetryClusterColorTheme,
  80. granularity: 'instance',
  81. color,
  82. props,
  83. description: 'Assigns chain colors according to assembly symmetry cluster membership.',
  84. legend
  85. }
  86. }
  87. export const AssemblySymmetryClusterColorThemeProvider: ColorTheme.Provider<AssemblySymmetryClusterColorThemeParams> = {
  88. label: 'RCSB Assembly Symmetry Cluster',
  89. factory: AssemblySymmetryClusterColorTheme,
  90. getParams: getAssemblySymmetryClusterColorThemeParams,
  91. defaultValues: PD.getDefaultValues(AssemblySymmetryClusterColorThemeParams),
  92. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure && !ctx.structure.isEmpty && ctx.structure.models[0].customProperties.has(AssemblySymmetry.Descriptor)
  93. }