UNPKG

pdbe-molstar

Version:
106 lines (105 loc) 5.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomSequenceColorTheme = void 0; const selector_1 = require("molstar/lib/extensions/mvs/components/selector"); const structure_1 = require("molstar/lib/mol-model/structure"); const color_1 = require("molstar/lib/mol-util/color"); const param_definition_1 = require("molstar/lib/mol-util/param-definition"); const sequence_color_annotations_prop_1 = require("./sequence-color-annotations-prop"); const sequence_color_theme_prop_1 = require("./sequence-color-theme-prop"); /** Special color value meaning "no color assigned" */ const NoColor = (0, color_1.Color)(-1); var CustomSequenceColorTheme; (function (CustomSequenceColorTheme) { /** Provider name (key) for this color theme */ CustomSequenceColorTheme.Name = 'custom-sequence-color'; /** Parameter definition for this color theme */ CustomSequenceColorTheme.Params = {}; function Theme(ctx, props, colorThemeRegistry) { return { factory: (ctx_, props_) => Theme(ctx_, props_, colorThemeRegistry), granularity: 'groupInstance', color: colorFn(ctx, colorThemeRegistry), props: props, description: 'Assigns colors based on custom structure property `SequenceColorProperty` and `SequenceColorBackgroundProperty`.', }; } CustomSequenceColorTheme.Theme = Theme; /** Create a provider for this color theme */ function makeProvider(colorThemeRegistry) { return { name: CustomSequenceColorTheme.Name, label: 'Custom Sequence Color', category: 'Miscellaneous', factory: (ctx, props) => Theme(ctx, props, colorThemeRegistry), getParams: ctx => CustomSequenceColorTheme.Params, defaultValues: param_definition_1.ParamDefinition.getDefaultValues(CustomSequenceColorTheme.Params), isApplicable: (ctx) => !!ctx.structure, }; } CustomSequenceColorTheme.makeProvider = makeProvider; })(CustomSequenceColorTheme || (exports.CustomSequenceColorTheme = CustomSequenceColorTheme = {})); /** Create color function based on `SequenceColorAnnotationsProperty` and `SequenceColorThemeProperty` */ function colorFn(ctx, colorThemeRegistry) { const background = themeColorFn(ctx, colorThemeRegistry); const foreground = annotColorFn(ctx); if (foreground && background) { return (location, isSecondary) => { const fgColor = foreground(location, isSecondary); if (fgColor >= 0) return fgColor; else return background(location, isSecondary); }; } if (foreground) return foreground; if (background) return background; return () => NoColor; } /** Create color function based on `SequenceColorThemeProperty` */ function themeColorFn(ctx, colorThemeRegistry) { var _a; if (!colorThemeRegistry) return undefined; if (!ctx.structure || ctx.structure.isEmpty) return undefined; const data = sequence_color_theme_prop_1.SequenceColorThemeProperty.Provider.get(ctx.structure).value; if (!(data === null || data === void 0 ? void 0 : data.useTheme)) return undefined; const theme = (_a = colorThemeRegistry.get(data.theme.name)) === null || _a === void 0 ? void 0 : _a.factory(ctx, data.theme.params); if (!theme || !('color' in theme)) return undefined; if (data.themeStrength === 1) return theme.color; if (data.themeStrength === 0) return () => data.dilutionColor; return (location, isSecondary) => color_1.Color.interpolate(data.dilutionColor, theme.color(location, isSecondary), data.themeStrength); } /** Create color function based on `SequenceColorAnnotationsProperty` */ function annotColorFn(ctx) { if (!ctx.structure || ctx.structure.isEmpty) return undefined; const colorData = sequence_color_annotations_prop_1.SequenceColorAnnotationsProperty.Provider.get(ctx.structure).value; if (!colorData || colorData.items.length === 0) return undefined; return location => structure_1.StructureElement.Location.is(location) ? sequenceColorForLocation(colorData, location) : NoColor; } function sequenceColorForLocation(colorData, location) { var _a, _b; var _c, _d, _e; const unitCache = (_a = (_c = colorData.colorCache)[_d = location.unit.id]) !== null && _a !== void 0 ? _a : (_c[_d] = {}); return (_b = unitCache[_e = location.element]) !== null && _b !== void 0 ? _b : (unitCache[_e] = findSequenceColorForLocation(colorData, location)); } function findSequenceColorForLocation(colorData, location) { var _a; for (let i = colorData.items.length - 1; i >= 0; i--) { // last color matters const item = colorData.items[i]; const elements = (_a = item.elementSet) !== null && _a !== void 0 ? _a : (item.elementSet = selector_1.ElementSet.fromSelector(location.structure, item.selector)); if (selector_1.ElementSet.has(elements, location)) { return item.color; } } return NoColor; }