diff --git a/src/mol-geo/geometry/color-data.ts b/src/mol-geo/geometry/color-data.ts index 457c6a14db1e9fdcae82fa889966685f43852537..cc66f80c9edd161b5a518d38acab4be1f9faedc1 100644 --- a/src/mol-geo/geometry/color-data.ts +++ b/src/mol-geo/geometry/color-data.ts @@ -37,16 +37,17 @@ export interface ColorProps { } export function getColorThemeProps(props: ColorProps): ColorThemeProps { - return { - name: props.colorTheme, - domain: props.colorDomain, - value: props.colorValue, - structure: props.structure, - color: props.colorFunction, - granularity: props.colorGranularity, - description: props.colorDescription, - legend: props.colorLegend + const p: ColorThemeProps = { + name: props.colorTheme } + if (props.colorDomain !== undefined) p.domain = props.colorDomain + if (props.colorValue !== undefined) p.value = props.colorValue + if (props.structure !== undefined) p.structure = props.structure + if (props.colorFunction !== undefined) p.color = props.colorFunction + if (props.colorGranularity !== undefined) p.granularity = props.colorGranularity + if (props.colorDescription !== undefined) p.description = props.colorDescription + if (props.colorLegend !== undefined) p.legend = props.colorLegend + return p } export function createColors(ctx: RuntimeContext, locationIt: LocationIterator, props: ColorProps, colorData?: ColorData): Promise<ColorData> { diff --git a/src/mol-geo/representation/structure/index.ts b/src/mol-geo/representation/structure/index.ts index a1822294dcc23c42808cb0fedc553cf43d92360e..d58caef4549e1bb1c98dfff2a1aff0616ee0cd56 100644 --- a/src/mol-geo/representation/structure/index.ts +++ b/src/mol-geo/representation/structure/index.ts @@ -20,7 +20,7 @@ export interface StructureRepresentation<P extends RepresentationProps = {}> ext export const StructureParams = { ...Geometry.Params, - colorTheme: SelectParam<ColorThemeName>('Color Theme', '', 'unit-index', ColorThemeOptions), + colorTheme: SelectParam<ColorThemeName>('Color Theme', '', 'polymer-index', ColorThemeOptions), sizeTheme: SelectParam<SizeThemeName>('Size Theme', '', 'physical', SizeThemeOptions), } export const DefaultStructureProps = paramDefaultValues(StructureParams) diff --git a/src/mol-theme/color.ts b/src/mol-theme/color.ts index 001b497b62913a702ed8e78df347a929006d922f..c11848731cdae4a30529c7d2e62a9ed272b8cc26 100644 --- a/src/mol-theme/color.ts +++ b/src/mol-theme/color.ts @@ -22,6 +22,7 @@ import { ResidueNameColorTheme } from './color/residue-name'; import { SequenceIdColorTheme } from './color/sequence-id'; import { SecondaryStructureColorTheme } from './color/secondary-structure'; import { MoleculeTypeColorTheme } from './color/molecule-type'; +import { PolymerIndexColorTheme } from './color/polymer-index'; export type LocationColor = (location: Location, isSecondary: boolean) => Color @@ -59,6 +60,7 @@ export function ColorTheme(props: ColorThemeProps): ColorTheme { case 'element-index': return ElementIndexColorTheme(props) case 'element-symbol': return ElementSymbolColorTheme(props) case 'molecule-type': return MoleculeTypeColorTheme(props) + case 'polymer-index': return PolymerIndexColorTheme(props) case 'residue-name': return ResidueNameColorTheme(props) case 'secondary-structure': return SecondaryStructureColorTheme(props) case 'sequence-id': return SequenceIdColorTheme(props) @@ -87,6 +89,7 @@ export const ColorThemeInfo = { 'element-index': {}, 'element-symbol': {}, 'molecule-type': {}, + 'polymer-index': {}, 'residue-name': {}, 'secondary-structure': {}, 'sequence-id': {}, diff --git a/src/mol-theme/color/polymer-index.ts b/src/mol-theme/color/polymer-index.ts new file mode 100644 index 0000000000000000000000000000000000000000..891b17300f72f94ab19e1c1883e19c303f9d0659 --- /dev/null +++ b/src/mol-theme/color/polymer-index.ts @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. + * + * @author Alexander Rose <alexander.rose@weirdbyte.de> + */ + +import { ColorScale, Color } from 'mol-util/color'; +import { Location } from 'mol-model/location'; +import { StructureElement, Link } from 'mol-model/structure'; +import { ColorTheme, ColorThemeProps, LocationColor } from '../color'; + +const DefaultColor = Color(0xCCCCCC) +const Description = 'Gives every polymer a unique color based on the position (index) of the polymer in the list of polymers in the structure.' + +export function PolymerIndexColorTheme(props: ColorThemeProps): ColorTheme { + let color: LocationColor + let scale: ColorScale | undefined = undefined + + if (props.structure) { + const { units } = props.structure + let polymerCount = 0 + for (let i = 0, il = units.length; i <il; ++i) { + if (units[i].polymerElements.length > 0) ++polymerCount + } + scale = ColorScale.create({ domain: [ 0, polymerCount - 1 ] }) + const unitIdColor = new Map<number, Color>() + for (let i = 0, j = 0, il = units.length; i <il; ++i) { + if (units[i].polymerElements.length > 0) { + unitIdColor.set(units[i].id, scale.color(j)) + ++j + } + } + + color = (location: Location): Color => { + let color: Color | undefined + if (StructureElement.isLocation(location)) { + color = unitIdColor.get(location.unit.id) + } else if (Link.isLocation(location)) { + color = unitIdColor.get(location.aUnit.id) + } + return color !== undefined ? color : DefaultColor + } + } else { + color = () => DefaultColor + } + + return { + granularity: 'instance', + color, + description: Description, + legend: scale ? scale.legend : undefined + } +} \ No newline at end of file diff --git a/src/mol-theme/color/unit-index.ts b/src/mol-theme/color/unit-index.ts index 92d4f53bbe552f2017b873dd7778ee0d6621e3e2..3e4fed18b986e43f85fa9c076d9d765695f73494 100644 --- a/src/mol-theme/color/unit-index.ts +++ b/src/mol-theme/color/unit-index.ts @@ -21,7 +21,7 @@ export function UnitIndexColorTheme(props: ColorThemeProps): ColorTheme { scale = ColorScale.create({ domain: [ 0, units.length - 1 ] }) const unitIdColor = new Map<number, Color>() for (let i = 0, il = units.length; i <il; ++i) { - unitIdColor.set(units[i].id, scale.color(units[i].id)) + unitIdColor.set(units[i].id, scale.color(i)) } color = (location: Location): Color => {