molstar
Version:
A comprehensive macromolecular library.
108 lines (107 loc) • 4.63 kB
JavaScript
/**
* Copyright (c) 2021-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { __assign } from "tslib";
import { StructureProperties, StructureElement, Bond } from '../../mol-model/structure';
import { Color } from '../../mol-util/color';
import { ParamDefinition as PD } from '../../mol-util/param-definition';
import { getPaletteParams, getPalette } from '../../mol-util/color/palette';
var DefaultList = 'many-distinct';
var DefaultColor = Color(0xFAFAFA);
var Description = 'Gives every chain a color based on its `label_entity_id` value.';
export var EntityIdColorThemeParams = __assign({}, getPaletteParams({ type: 'colors', colorList: DefaultList }));
export function getEntityIdColorThemeParams(ctx) {
var params = PD.clone(EntityIdColorThemeParams);
return params;
}
function key(entityId, modelIndex) {
return "".concat(entityId, "|").concat(modelIndex);
}
function getEntityIdSerialMap(structure) {
var map = new Map();
for (var i = 0, il = structure.models.length; i < il; ++i) {
var label_entity_id = structure.models[i].atomicHierarchy.chains.label_entity_id;
for (var j = 0, jl = label_entity_id.rowCount; j < jl; ++j) {
var k = key(label_entity_id.value(j), i);
if (!map.has(k))
map.set(k, map.size);
}
var coarseHierarchy = structure.models[i].coarseHierarchy;
if (coarseHierarchy.isDefined) {
var spheres_entity_id = coarseHierarchy.spheres.entity_id;
for (var j = 0, jl = spheres_entity_id.rowCount; j < jl; ++j) {
var k = key(spheres_entity_id.value(j), i);
if (!map.has(k))
map.set(k, map.size);
}
var gaussians_entity_id = coarseHierarchy.gaussians.entity_id;
for (var j = 0, jl = gaussians_entity_id.rowCount; j < jl; ++j) {
var k = key(gaussians_entity_id.value(j), i);
if (!map.has(k))
map.set(k, map.size);
}
}
}
return map;
}
function getEntityId(location) {
switch (location.unit.kind) {
case 0 /* Unit.Kind.Atomic */:
return StructureProperties.chain.label_entity_id(location);
case 1 /* Unit.Kind.Spheres */:
case 2 /* Unit.Kind.Gaussians */:
return StructureProperties.coarse.entity_id(location);
}
}
export function EntityIdColorTheme(ctx, props) {
var color;
var legend;
if (ctx.structure) {
var l_1 = StructureElement.Location.create(ctx.structure.root);
var entityIdSerialMap_1 = getEntityIdSerialMap(ctx.structure.root);
var labelTable_1 = Array.from(entityIdSerialMap_1.keys());
var valueLabel = function (i) { return labelTable_1[i]; };
var palette_1 = getPalette(entityIdSerialMap_1.size, props, { valueLabel: valueLabel });
legend = palette_1.legend;
color = function (location) {
var serial = undefined;
if (StructureElement.Location.is(location)) {
var entityId = getEntityId(location);
var modelIndex = location.structure.models.indexOf(location.unit.model);
var k = key(entityId, modelIndex);
serial = entityIdSerialMap_1.get(k);
}
else if (Bond.isLocation(location)) {
l_1.unit = location.aUnit;
l_1.element = location.aUnit.elements[location.aIndex];
var entityId = getEntityId(l_1);
var modelIndex = l_1.structure.models.indexOf(l_1.unit.model);
var k = key(entityId, modelIndex);
serial = entityIdSerialMap_1.get(k);
}
return serial === undefined ? DefaultColor : palette_1.color(serial);
};
}
else {
color = function () { return DefaultColor; };
}
return {
factory: EntityIdColorTheme,
granularity: 'group',
color: color,
props: props,
description: Description,
legend: legend
};
}
export var EntityIdColorThemeProvider = {
name: 'entity-id',
label: 'Entity Id',
category: "Chain Property" /* ColorTheme.Category.Chain */,
factory: EntityIdColorTheme,
getParams: getEntityIdColorThemeParams,
defaultValues: PD.getDefaultValues(EntityIdColorThemeParams),
isApplicable: function (ctx) { return !!ctx.structure; }
};