diff --git a/src/mol-theme/color.ts b/src/mol-theme/color.ts index 29ea808574de9632d64910191832ab9850974610..18edc8457a92f6972cf8f77cdbf78060bda91c18 100644 --- a/src/mol-theme/color.ts +++ b/src/mol-theme/color.ts @@ -31,6 +31,7 @@ import { EntitySourceColorThemeProvider } from './color/entity-source'; import { IllustrativeColorThemeProvider } from './color/illustrative'; import { HydrophobicityColorThemeProvider } from './color/hydrophobicity'; import { ModelIndexColorThemeProvider } from './color/model-index'; +import { OccupancyColorThemeProvider } from './color/occupancy'; export type LocationColor = (location: Location, isSecondary: boolean) => Color @@ -82,6 +83,7 @@ export const BuiltInColorThemes = { 'illustrative': IllustrativeColorThemeProvider, 'model-index': ModelIndexColorThemeProvider, 'molecule-type': MoleculeTypeColorThemeProvider, + 'occupancy': OccupancyColorThemeProvider, 'polymer-id': PolymerIdColorThemeProvider, 'polymer-index': PolymerIndexColorThemeProvider, 'residue-name': ResidueNameColorThemeProvider, diff --git a/src/mol-theme/color/occupancy.ts b/src/mol-theme/color/occupancy.ts new file mode 100644 index 0000000000000000000000000000000000000000..3f315c5c2c6fa436641a7f66822b731289050ed0 --- /dev/null +++ b/src/mol-theme/color/occupancy.ts @@ -0,0 +1,67 @@ +/** + * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info. + * + * @author Alexander Rose <alexander.rose@weirdbyte.de> + */ + +import { Color, ColorScale } from '../../mol-util/color'; +import { StructureElement, Unit, Link, ElementIndex } from '../../mol-model/structure'; +import { Location } from '../../mol-model/location'; +import { ColorTheme } from '../color'; +import { ParamDefinition as PD } from '../../mol-util/param-definition' +import { ThemeDataContext } from '../theme'; +import { ColorListName, ColorListOptionsScale } from '../../mol-util/color/lists'; + +const DefaultOccupancyColor = Color(0xCCCCCC) +const Description = `Assigns a color based on the occupancy of an atom.` + +export const OccupancyColorThemeParams = { + domain: PD.Interval([0, 1]), + list: PD.ColorList<ColorListName>('purples', ColorListOptionsScale), +} +export type OccupancyColorThemeParams = typeof OccupancyColorThemeParams +export function getOccupancyColorThemeParams(ctx: ThemeDataContext) { + return OccupancyColorThemeParams // TODO return copy +} + +export function getOccupancy(unit: Unit, element: ElementIndex): number { + if (Unit.isAtomic(unit)) { + return unit.model.atomicConformation.occupancy.value(element) + } else { + return 0 + } +} + +export function OccupancyColorTheme(ctx: ThemeDataContext, props: PD.Values<OccupancyColorThemeParams>): ColorTheme<OccupancyColorThemeParams> { + const scale = ColorScale.create({ + reverse: false, + domain: props.domain, + listOrName: props.list, + }) + + function color(location: Location): Color { + if (StructureElement.Location.is(location)) { + return scale.color(getOccupancy(location.unit, location.element)) + } else if (Link.isLocation(location)) { + return scale.color(getOccupancy(location.aUnit, location.aUnit.elements[location.aIndex])) + } + return DefaultOccupancyColor + } + + return { + factory: OccupancyColorTheme, + granularity: 'group', + color, + props, + description: Description, + legend: scale ? scale.legend : undefined + } +} + +export const OccupancyColorThemeProvider: ColorTheme.Provider<OccupancyColorThemeParams> = { + label: 'Occupancy', + factory: OccupancyColorTheme, + getParams: getOccupancyColorThemeParams, + defaultValues: PD.getDefaultValues(OccupancyColorThemeParams), + isApplicable: (ctx: ThemeDataContext) => !!ctx.structure +} \ No newline at end of file