molstar
Version:
A comprehensive macromolecular library.
126 lines (125 loc) • 5.57 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.SbNcbrPartialChargesColorThemeProvider = exports.PartialChargesThemeParams = void 0;
exports.getPartialChargesThemeParams = getPartialChargesThemeParams;
exports.PartialChargesColorTheme = PartialChargesColorTheme;
const structure_1 = require("../../../mol-model/structure");
const color_1 = require("../../../mol-util/color");
const param_definition_1 = require("../../../mol-util/param-definition");
const property_1 = require("./property");
const categories_1 = require("../../../mol-theme/color/categories");
const Colors = {
Bond: (0, color_1.Color)(0xffffff),
Error: (0, color_1.Color)(0x00ff00),
MissingCharge: (0, color_1.Color)(0x66ff00),
Negative: (0, color_1.Color)(0xff0000),
Zero: (0, color_1.Color)(0xffffff),
Positive: (0, color_1.Color)(0x0000ff),
getColor: (charge, maxCharge) => {
if (charge === 0)
return Colors.Zero;
if (charge <= -maxCharge)
return Colors.Negative;
if (charge >= maxCharge)
return Colors.Positive;
const t = maxCharge !== 0 ? Math.abs(charge) / maxCharge : 1;
const endColor = charge < 0 ? Colors.Negative : Colors.Positive;
return color_1.Color.interpolate(Colors.Zero, endColor, t);
},
};
exports.PartialChargesThemeParams = {
maxAbsoluteCharge: param_definition_1.ParamDefinition.Numeric(0, { min: 0 }, {
label: 'Charge Range',
}),
absolute: param_definition_1.ParamDefinition.Boolean(false, { isHidden: false, label: 'Use Range' }),
chargeType: param_definition_1.ParamDefinition.Select('residue', [
['atom', 'Atom charges'],
['residue', 'Residue charges'],
], { isHidden: false }),
};
function getPartialChargesThemeParams() {
return param_definition_1.ParamDefinition.clone(exports.PartialChargesThemeParams);
}
function PartialChargesColorTheme(ctx, props) {
var _a, _b;
const model = (_a = ctx.structure) === null || _a === void 0 ? void 0 : _a.models[0];
if (!model) {
throw new Error('No model found');
}
const data = property_1.SbNcbrPartialChargesPropertyProvider.get(model).value;
if (!data) {
throw new Error('No partial charges data found');
}
const { absolute, chargeType } = props;
const { typeIdToAtomIdToCharge, typeIdToResidueToCharge, maxAbsoluteAtomCharges, maxAbsoluteResidueCharges } = data;
const typeId = property_1.SbNcbrPartialChargesPropertyProvider.props(model).typeId;
const atomToCharge = typeIdToAtomIdToCharge.get(typeId);
const residueToCharge = typeIdToResidueToCharge.get(typeId);
let maxCharge = 0;
if (absolute) {
maxCharge = props.maxAbsoluteCharge < 0 ? 0 : props.maxAbsoluteCharge;
}
else if (chargeType === 'atom') {
maxCharge = maxAbsoluteAtomCharges.get(typeId) || 0;
}
else {
maxCharge = maxAbsoluteResidueCharges.get(typeId) || 0;
}
// forces coloring updates
const contextHash = (_b = property_1.SbNcbrPartialChargesPropertyProvider.get(model)) === null || _b === void 0 ? void 0 : _b.version;
const chargeMap = chargeType === 'atom' ? atomToCharge : residueToCharge;
let color;
if (!chargeMap) {
color = (_) => Colors.MissingCharge;
}
else {
color = (location) => {
var _a;
let id = -1;
if (structure_1.StructureElement.Location.is(location)) {
if (structure_1.Unit.isAtomic(location.unit)) {
id = structure_1.StructureProperties.atom.id(location);
}
}
else if (structure_1.Bond.isLocation(location)) {
if (structure_1.Unit.isAtomic(location.aUnit)) {
const l = structure_1.StructureElement.Location.create((_a = ctx.structure) === null || _a === void 0 ? void 0 : _a.root);
l.unit = location.aUnit;
l.element = location.aUnit.elements[location.aIndex];
id = structure_1.StructureProperties.atom.id(l);
}
}
const charge = chargeMap.get(id);
if (charge === undefined) {
console.warn('No charge found for id', id);
return Colors.MissingCharge;
}
return Colors.getColor(charge, maxCharge);
};
}
return {
factory: PartialChargesColorTheme,
granularity: 'group',
color,
props,
description: 'Color atoms and residues based on their partial charge.',
preferSmoothing: false,
contextHash,
};
}
exports.SbNcbrPartialChargesColorThemeProvider = {
label: 'SB NCBR Partial Charges',
name: 'sb-ncbr-partial-charges',
category: categories_1.ColorThemeCategory.Atom,
factory: PartialChargesColorTheme,
getParams: getPartialChargesThemeParams,
defaultValues: param_definition_1.ParamDefinition.getDefaultValues(exports.PartialChargesThemeParams),
isApplicable: (ctx) => !!ctx.structure &&
ctx.structure.models.some((model) => property_1.SbNcbrPartialChargesPropertyProvider.isApplicable(model)),
ensureCustomProperties: {
attach: (ctx, data) => data.structure
? property_1.SbNcbrPartialChargesPropertyProvider.attach(ctx, data.structure.models[0], void 0, true)
: Promise.resolve(),
detach: (data) => data.structure && property_1.SbNcbrPartialChargesPropertyProvider.ref(data.structure.models[0], false),
},
};
;