molstar
Version:
A comprehensive macromolecular library.
140 lines (139 loc) • 6.04 kB
JavaScript
/**
* Copyright (c) 2021-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author Adam Midlik <midlik@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityIdColorThemeProvider = exports.EntityIdColorThemeParams = void 0;
exports.getEntityIdColorThemeParams = getEntityIdColorThemeParams;
exports.EntityIdColorTheme = EntityIdColorTheme;
const structure_1 = require("../../mol-model/structure");
const color_1 = require("../../mol-util/color");
const param_definition_1 = require("../../mol-util/param-definition");
const palette_1 = require("../../mol-util/color/palette");
const categories_1 = require("./categories");
const DefaultList = 'many-distinct';
const DefaultColor = (0, color_1.Color)(0xFAFAFA);
const DefaultWaterColor = (0, color_1.Color)(0xFF0D0D);
const Description = 'Gives every chain a color based on its `label_entity_id` value.';
exports.EntityIdColorThemeParams = {
...(0, palette_1.getPaletteParams)({ type: 'colors', colorList: DefaultList }),
overrideWater: param_definition_1.ParamDefinition.Boolean(false, { description: 'Override the color for water molecules.' }),
waterColor: param_definition_1.ParamDefinition.Color(DefaultWaterColor, { hideIf: p => !p.overrideWater, description: 'Color for water molecules (if overrideWater is true).' }),
};
function getEntityIdColorThemeParams(ctx) {
const params = param_definition_1.ParamDefinition.clone(exports.EntityIdColorThemeParams);
return params;
}
function key(entityId, sourceSerial) {
return `${entityId}|${sourceSerial}`;
}
function getSourceSerialMap(structure) {
const map = new WeakMap();
let count = 0;
for (let i = 0, il = structure.models.length; i < il; ++i) {
const sd = structure.models[i].sourceData;
if (!map.has(sd))
map.set(sd, count++);
}
return map;
}
function getEntityIdSerialMap(structure, sourceMap) {
var _a;
const map = new Map();
for (let i = 0, il = structure.models.length; i < il; ++i) {
const sourceSerial = (_a = sourceMap.get(structure.models[i].sourceData)) !== null && _a !== void 0 ? _a : -1;
const { label_entity_id } = structure.models[i].atomicHierarchy.chains;
for (let j = 0, jl = label_entity_id.rowCount; j < jl; ++j) {
const k = key(label_entity_id.value(j), sourceSerial);
if (!map.has(k))
map.set(k, map.size);
}
const { coarseHierarchy } = structure.models[i];
if (coarseHierarchy.isDefined) {
const { entity_id: spheres_entity_id } = coarseHierarchy.spheres;
for (let j = 0, jl = spheres_entity_id.rowCount; j < jl; ++j) {
const k = key(spheres_entity_id.value(j), sourceSerial);
if (!map.has(k))
map.set(k, map.size);
}
const { entity_id: gaussians_entity_id } = coarseHierarchy.gaussians;
for (let j = 0, jl = gaussians_entity_id.rowCount; j < jl; ++j) {
const k = key(gaussians_entity_id.value(j), sourceSerial);
if (!map.has(k))
map.set(k, map.size);
}
}
}
return map;
}
function getEntityId(location) {
switch (location.unit.kind) {
case 0 /* Unit.Kind.Atomic */:
return structure_1.StructureProperties.chain.label_entity_id(location);
case 1 /* Unit.Kind.Spheres */:
case 2 /* Unit.Kind.Gaussians */:
return structure_1.StructureProperties.coarse.entity_id(location);
}
}
function EntityIdColorTheme(ctx, props) {
let color;
let legend;
if (ctx.structure) {
const l = structure_1.StructureElement.Location.create(ctx.structure.root);
const sourceSerialMap = getSourceSerialMap(ctx.structure);
const entityIdSerialMap = getEntityIdSerialMap(ctx.structure.root, sourceSerialMap);
const labelTable = Array.from(entityIdSerialMap.keys());
const valueLabel = (i) => labelTable[i];
const palette = (0, palette_1.getPalette)(entityIdSerialMap.size, props, { valueLabel });
legend = palette.legend;
color = (location) => {
var _a;
let structElemLoc;
if (structure_1.StructureElement.Location.is(location)) {
structElemLoc = location;
}
else if (structure_1.Bond.isLocation(location)) {
l.unit = location.aUnit;
l.element = location.aUnit.elements[location.aIndex];
structElemLoc = l;
}
else {
return DefaultColor;
}
const entityId = getEntityId(structElemLoc);
const sourceSerial = (_a = sourceSerialMap.get(structElemLoc.unit.model.sourceData)) !== null && _a !== void 0 ? _a : -1;
if (props.overrideWater) {
const entities = structElemLoc.unit.model.entities;
const entityType = entities.data.type.value(entities.getEntityIndex(entityId));
if (entityType === 'water')
return props.waterColor;
}
const k = key(entityId, sourceSerial);
const serial = entityIdSerialMap.get(k);
return serial === undefined ? DefaultColor : palette.color(serial);
};
}
else {
color = () => DefaultColor;
}
return {
factory: EntityIdColorTheme,
granularity: 'group',
color,
props,
description: Description,
legend
};
}
exports.EntityIdColorThemeProvider = {
name: 'entity-id',
label: 'Entity Id',
category: categories_1.ColorThemeCategory.Chain,
factory: EntityIdColorTheme,
getParams: getEntityIdColorThemeParams,
defaultValues: param_definition_1.ParamDefinition.getDefaultValues(exports.EntityIdColorThemeParams),
isApplicable: (ctx) => !!ctx.structure
};
;