molstar
Version:
A comprehensive macromolecular library.
72 lines (71 loc) • 4.03 kB
JavaScript
/**
* Copyright (c) 2023-2024 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Adam Midlik <midlik@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MVSAnnotationColorThemeProvider = exports.MVSAnnotationColorThemeParams = void 0;
exports.MVSAnnotationColorTheme = MVSAnnotationColorTheme;
const structure_1 = require("../../../mol-model/structure");
const names_1 = require("../../../mol-util/color/names");
const param_definition_1 = require("../../../mol-util/param-definition");
const utils_1 = require("../helpers/utils");
const annotation_prop_1 = require("./annotation-prop");
const is_mvs_model_prop_1 = require("./is-mvs-model-prop");
/** Parameter definition for color theme "MVS Annotation" */
exports.MVSAnnotationColorThemeParams = {
annotationId: param_definition_1.ParamDefinition.Text('', { description: 'Reference to "Annotation" custom model property' }),
fieldName: param_definition_1.ParamDefinition.Text('color', { description: 'Annotation field (column) from which to take color values' }),
background: param_definition_1.ParamDefinition.Color(names_1.ColorNames.gainsboro, { description: 'Color for elements without annotation' }),
};
/** Return color theme that assigns colors based on an annotation file.
* The annotation file itself is handled by a custom model property (`MVSAnnotationsProvider`),
* the color theme then just uses this property. */
function MVSAnnotationColorTheme(ctx, props) {
let color = () => props.background;
if (ctx.structure && !ctx.structure.isEmpty) {
const { annotation } = (0, annotation_prop_1.getMVSAnnotationForStructure)(ctx.structure, props.annotationId);
if (annotation) {
const colorForStructureElementLocation = (location) => {
var _a;
// if (annot.getAnnotationForLocation(location)?.color !== annot.getAnnotationForLocation_Reference(location)?.color) throw new Error('AssertionError');
return (_a = (0, utils_1.decodeColor)(annotation === null || annotation === void 0 ? void 0 : annotation.getValueForLocation(location, props.fieldName))) !== null && _a !== void 0 ? _a : props.background;
};
const auxLocation = structure_1.StructureElement.Location.create(ctx.structure);
color = (location) => {
if (structure_1.StructureElement.Location.is(location)) {
return colorForStructureElementLocation(location);
}
else if (structure_1.Bond.isLocation(location)) {
// this will be applied for each bond twice, to get color of each half (a* refers to the adjacent atom, b* to the opposite atom)
auxLocation.unit = location.aUnit;
auxLocation.element = location.aUnit.elements[location.aIndex];
return colorForStructureElementLocation(auxLocation);
}
return props.background;
};
}
else {
console.error(`Annotation source "${props.annotationId}" not present`);
}
}
return {
factory: MVSAnnotationColorTheme,
granularity: 'group',
preferSmoothing: true,
color: color,
props: props,
description: 'Assigns colors based on custom MolViewSpec annotation data.',
};
}
/** A thingy that is needed to register color theme "MVS Annotation" */
exports.MVSAnnotationColorThemeProvider = {
name: 'mvs-annotation',
label: 'MVS Annotation',
category: 'Miscellaneous', // ColorTheme.Category.Misc can cause webpack build error due to import ordering
factory: MVSAnnotationColorTheme,
getParams: ctx => exports.MVSAnnotationColorThemeParams,
defaultValues: param_definition_1.ParamDefinition.getDefaultValues(exports.MVSAnnotationColorThemeParams),
isApplicable: (ctx) => !!ctx.structure && (0, is_mvs_model_prop_1.isMVSStructure)(ctx.structure),
};
;