geometry-quality.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Copyright (c) 2020 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 { Color } from '../../../mol-util/color';
  10. import { StructureElement } from '../../../mol-model/structure';
  11. import { Location } from '../../../mol-model/location';
  12. import { CustomProperty } from '../../common/custom-property';
  13. import { ValidationReportProvider, ValidationReport } from '../validation-report';
  14. import { TableLegend } from '../../../mol-util/legend';
  15. import { PolymerType } from '../../../mol-model/structure/model/types';
  16. const DefaultColor = Color(0x909090)
  17. const NoIssuesColor = Color(0x2166ac)
  18. const OneIssueColor = Color(0xfee08b)
  19. const TwoIssuesColor = Color(0xf46d43)
  20. const ThreeOrMoreIssuesColor = Color(0xa50026)
  21. const ColorLegend = TableLegend([
  22. ['No issues', NoIssuesColor],
  23. ['One issue', OneIssueColor],
  24. ['Two issues', TwoIssuesColor],
  25. ['Three or more issues', ThreeOrMoreIssuesColor]
  26. ])
  27. export function GeometryQualityColorTheme(ctx: ThemeDataContext, props: {}): ColorTheme<{}> {
  28. let color: LocationColor = () => DefaultColor
  29. const validationReport = ctx.structure && ValidationReportProvider.get(ctx.structure.models[0])
  30. const contextHash = validationReport?.version
  31. const value = validationReport?.value
  32. const model = ctx.structure?.models[0]
  33. if (value && model) {
  34. const { geometryIssues, clashes, bondOutliers, angleOutliers } = value
  35. const residueIndex = model.atomicHierarchy.residueAtomSegments.index
  36. const { polymerType } = model.atomicHierarchy.derived.residue
  37. color = (location: Location): Color => {
  38. if (StructureElement.Location.is(location) && location.unit.model === model) {
  39. const { element } = location
  40. const rI = residueIndex[element]
  41. let value = geometryIssues.get(rI)?.size
  42. if (value !== undefined && polymerType[rI] === PolymerType.NA) {
  43. value = 0
  44. if (clashes.getVertexEdgeCount(element) > 0) value += 1
  45. if (bondOutliers.index.has(element)) value += 1
  46. if (angleOutliers.index.has(element)) value += 1
  47. }
  48. switch (value) {
  49. case undefined: return DefaultColor
  50. case 0: return NoIssuesColor
  51. case 1: return OneIssueColor
  52. case 2: return TwoIssuesColor
  53. default: return ThreeOrMoreIssuesColor
  54. }
  55. }
  56. return DefaultColor
  57. }
  58. }
  59. return {
  60. factory: GeometryQualityColorTheme,
  61. granularity: 'group',
  62. color,
  63. props,
  64. contextHash,
  65. description: 'Assigns residue colors according to the number of geometry issues.',
  66. legend: ColorLegend
  67. }
  68. }
  69. export const GeometryQualityColorThemeProvider: ColorTheme.Provider<{}> = {
  70. label: 'RCSB Geometry Quality',
  71. factory: GeometryQualityColorTheme,
  72. getParams: () => ({}),
  73. defaultValues: PD.getDefaultValues({}),
  74. isApplicable: (ctx: ThemeDataContext) => ValidationReport.isApplicable(ctx.structure?.models[0]),
  75. ensureCustomProperties: (ctx: CustomProperty.Context, data: ThemeDataContext) => {
  76. return data.structure ? ValidationReportProvider.attach(ctx, data.structure.models[0]) : Promise.resolve()
  77. }
  78. }