UNPKG

molstar

Version:

A comprehensive macromolecular library.

106 lines (105 loc) 4.25 kB
/** * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ import { StructureElement, Bond } from '../../mol-model/structure'; import { ColorScale, Color } from '../../mol-util/color'; import { ParamDefinition as PD } from '../../mol-util/param-definition'; var DefaultColor = Color(0xCCCCCC); var Description = 'Gives every polymer residue a color based on its `seq_id` value.'; export var SequenceIdColorThemeParams = { list: PD.ColorList('turbo', { presetKind: 'scale' }), }; export function getSequenceIdColorThemeParams(ctx) { return SequenceIdColorThemeParams; // TODO return copy } function getSeqId(unit, element) { var model = unit.model; switch (unit.kind) { case 0 /* Unit.Kind.Atomic */: var residueIndex = model.atomicHierarchy.residueAtomSegments.index[element]; return model.atomicHierarchy.residues.label_seq_id.value(residueIndex); case 1 /* Unit.Kind.Spheres */: return Math.round((model.coarseHierarchy.spheres.seq_id_begin.value(element) + model.coarseHierarchy.spheres.seq_id_end.value(element)) / 2); case 2 /* Unit.Kind.Gaussians */: return Math.round((model.coarseHierarchy.gaussians.seq_id_begin.value(element) + model.coarseHierarchy.gaussians.seq_id_end.value(element)) / 2); } } function getSequenceLength(unit, element) { var model = unit.model; var entityId = ''; switch (unit.kind) { case 0 /* Unit.Kind.Atomic */: var chainIndex = model.atomicHierarchy.chainAtomSegments.index[element]; entityId = model.atomicHierarchy.chains.label_entity_id.value(chainIndex); break; case 1 /* Unit.Kind.Spheres */: entityId = model.coarseHierarchy.spheres.entity_id.value(element); break; case 2 /* Unit.Kind.Gaussians */: entityId = model.coarseHierarchy.gaussians.entity_id.value(element); break; } if (entityId === '') return 0; var entityIndex = model.entities.getEntityIndex(entityId); if (entityIndex === -1) return 0; var entity = model.sequence.byEntityKey[entityIndex]; if (entity === undefined) return 0; return entity.sequence.length; } export function SequenceIdColorTheme(ctx, props) { var scale = ColorScale.create({ listOrName: props.list.colors, minLabel: 'Start', maxLabel: 'End', }); var color = function (location) { if (StructureElement.Location.is(location)) { var unit = location.unit, element = location.element; var seq_id = getSeqId(unit, element); if (seq_id > 0) { var seqLen = getSequenceLength(unit, element); if (seqLen) { scale.setDomain(0, seqLen - 1); return scale.color(seq_id); } } } else if (Bond.isLocation(location)) { var aUnit = location.aUnit, aIndex = location.aIndex; var seq_id = getSeqId(aUnit, aUnit.elements[aIndex]); if (seq_id > 0) { var seqLen = getSequenceLength(aUnit, aUnit.elements[aIndex]); if (seqLen) { scale.setDomain(0, seqLen - 1); return scale.color(seq_id); } } } return DefaultColor; }; return { factory: SequenceIdColorTheme, granularity: 'group', preferSmoothing: true, color: color, props: props, description: Description, legend: scale ? scale.legend : undefined }; } export var SequenceIdColorThemeProvider = { name: 'sequence-id', label: 'Sequence Id', category: "Residue Property" /* ColorTheme.Category.Residue */, factory: SequenceIdColorTheme, getParams: getSequenceIdColorThemeParams, defaultValues: PD.getDefaultValues(SequenceIdColorThemeParams), isApplicable: function (ctx) { return !!ctx.structure; } };