UNPKG

molstar

Version:

A comprehensive macromolecular library.

156 lines 8.1 kB
"use strict"; /** * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.EntitySourceColorThemeProvider = exports.EntitySourceColorTheme = exports.getEntitySourceColorThemeParams = exports.EntitySourceColorThemeParams = void 0; var tslib_1 = require("tslib"); var structure_1 = require("../../mol-model/structure"); var color_1 = require("../../mol-util/color"); var param_definition_1 = require("../../mol-util/param-definition"); var palette_1 = require("../../mol-util/color/palette"); var number_1 = require("../../mol-util/number"); var lists_1 = require("../../mol-util/color/lists"); var mmcif_1 = require("../../mol-model-formats/structure/mmcif"); var DefaultList = 'dark-2'; var DefaultColor = (0, color_1.Color)(0xFAFAFA); var Description = 'Gives ranges of a polymer chain a color based on the entity source it originates from (e.g. gene, plasmid, organism).'; exports.EntitySourceColorThemeParams = (0, tslib_1.__assign)({}, (0, palette_1.getPaletteParams)({ type: 'colors', colorList: DefaultList })); function getEntitySourceColorThemeParams(ctx) { var params = param_definition_1.ParamDefinition.clone(exports.EntitySourceColorThemeParams); if (ctx.structure) { if (getMaps(ctx.structure.root.models).srcKeySerialMap.size > lists_1.ColorLists[DefaultList].list.length) { params.palette.defaultValue.name = 'colors'; params.palette.defaultValue.params = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, params.palette.defaultValue.params), { list: { kind: 'interpolate', colors: (0, lists_1.getColorListFromName)(DefaultList).list } }); } } return params; } exports.getEntitySourceColorThemeParams = getEntitySourceColorThemeParams; function modelEntityKey(modelIndex, entityId) { return modelIndex + "|" + entityId; } function srcKey(modelIndex, entityId, organism, srcId, plasmid, gene) { return modelIndex + "|" + entityId + "|" + organism + "|" + (gene ? gene : (plasmid ? plasmid : srcId)); } function addSrc(seqToSrcByModelEntity, srcKeySerialMap, modelIndex, model, entity_src, scientific_name, plasmid_name, gene_src_gene) { var entity_id = entity_src.entity_id, pdbx_src_id = entity_src.pdbx_src_id, pdbx_beg_seq_num = entity_src.pdbx_beg_seq_num, pdbx_end_seq_num = entity_src.pdbx_end_seq_num; for (var j = 0, jl = entity_src._rowCount; j < jl; ++j) { var entityId = entity_id.value(j); var mK = modelEntityKey(modelIndex, entityId); var seqToSrc = void 0; if (!seqToSrcByModelEntity.has(mK)) { var entityIndex = model.entities.getEntityIndex(entityId); var seq = model.sequence.sequences[entityIndex].sequence; seqToSrc = new Int16Array(seq.length); seqToSrcByModelEntity.set(mK, seqToSrc); } else { seqToSrc = seqToSrcByModelEntity.get(mK); } var plasmid = plasmid_name ? plasmid_name.value(j) : ''; var gene = gene_src_gene ? gene_src_gene.value(j)[0] : ''; var sK = srcKey(modelIndex, entityId, scientific_name.value(j), pdbx_src_id.value(j), plasmid, gene); // may not be given (= 0) indicating src is for the whole seq var beg = pdbx_beg_seq_num.valueKind(j) === 0 /* Present */ ? pdbx_beg_seq_num.value(j) : 1; var end = pdbx_end_seq_num.valueKind(j) === 0 /* Present */ ? pdbx_end_seq_num.value(j) : seqToSrc.length; var srcIndex = void 0; // serial no starting from 1 if (srcKeySerialMap.has(sK)) { srcIndex = srcKeySerialMap.get(sK); } else { srcIndex = srcKeySerialMap.size + 1; srcKeySerialMap.set(sK, srcIndex); } // set src index for (var i = beg, il = end; i <= il; ++i) { seqToSrc[i - 1] = srcIndex; } } } function getMaps(models) { var seqToSrcByModelEntity = new Map(); var srcKeySerialMap = new Map(); // serial no starting from 1 for (var i = 0, il = models.length; i < il; ++i) { var m = models[i]; if (!mmcif_1.MmcifFormat.is(m.sourceData)) continue; var _a = m.sourceData.data.db, entity_src_gen = _a.entity_src_gen, entity_src_nat = _a.entity_src_nat, pdbx_entity_src_syn = _a.pdbx_entity_src_syn; addSrc(seqToSrcByModelEntity, srcKeySerialMap, i, m, entity_src_gen, entity_src_gen.pdbx_gene_src_scientific_name, entity_src_gen.plasmid_name, entity_src_gen.pdbx_gene_src_gene); addSrc(seqToSrcByModelEntity, srcKeySerialMap, i, m, entity_src_nat, entity_src_nat.pdbx_organism_scientific, entity_src_nat.pdbx_plasmid_name); addSrc(seqToSrcByModelEntity, srcKeySerialMap, i, m, pdbx_entity_src_syn, pdbx_entity_src_syn.organism_scientific); } return { seqToSrcByModelEntity: seqToSrcByModelEntity, srcKeySerialMap: srcKeySerialMap }; } function getLabelTable(srcKeySerialMap) { var unnamedCount = 0; return Array.from(srcKeySerialMap.keys()).map(function (v) { var vs = v.split('|'); var organism = vs[2]; var name = (0, number_1.isInteger)(vs[3]) ? "Unnamed " + ++unnamedCount : vs[3]; return "" + name + (organism ? " (" + organism + ")" : ''); }); } function EntitySourceColorTheme(ctx, props) { var color; var legend; if (ctx.structure) { var l_1 = structure_1.StructureElement.Location.create(ctx.structure); var models_1 = ctx.structure.root.models; var _a = getMaps(models_1), seqToSrcByModelEntity_1 = _a.seqToSrcByModelEntity, srcKeySerialMap = _a.srcKeySerialMap; var labelTable_1 = getLabelTable(srcKeySerialMap); var valueLabel = function (i) { return labelTable_1[i]; }; var palette_2 = (0, palette_1.getPalette)(srcKeySerialMap.size, props, { valueLabel: valueLabel }); legend = palette_2.legend; var getSrcColor_1 = function (location) { var modelIndex = models_1.indexOf(location.unit.model); var entityId = structure_1.StructureProperties.entity.id(location); var mK = modelEntityKey(modelIndex, entityId); var seqToSrc = seqToSrcByModelEntity_1.get(mK); if (seqToSrc) { // minus 1 to convert seqId to array index var src = seqToSrc[structure_1.StructureProperties.residue.label_seq_id(location) - 1] - 1; // check for -1 as not all sequence ids have a src given return src === -1 ? DefaultColor : palette_2.color(src); } else { return DefaultColor; } }; color = function (location) { if (structure_1.StructureElement.Location.is(location)) { return getSrcColor_1(location); } else if (structure_1.Bond.isLocation(location)) { l_1.unit = location.aUnit; l_1.element = location.aUnit.elements[location.aIndex]; return getSrcColor_1(l_1); } return DefaultColor; }; } else { color = function () { return DefaultColor; }; } return { factory: EntitySourceColorTheme, granularity: 'group', color: color, props: props, description: Description, legend: legend }; } exports.EntitySourceColorTheme = EntitySourceColorTheme; exports.EntitySourceColorThemeProvider = { name: 'entity-source', label: 'Entity Source', category: "Chain Property" /* Chain */, factory: EntitySourceColorTheme, getParams: getEntitySourceColorThemeParams, defaultValues: param_definition_1.ParamDefinition.getDefaultValues(exports.EntitySourceColorThemeParams), isApplicable: function (ctx) { return !!ctx.structure; } }; //# sourceMappingURL=entity-source.js.map