Skip to content

Commit c3a09ad

Browse files
committed
Interactions extension - more customizability
1 parent 1c75163 commit c3a09ad

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/app/extensions/interactions/index.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { setSubtreeVisibility } from 'molstar/lib/mol-plugin/behavior/static/sta
99
import { PDBeMolstarPlugin } from '../..';
1010
import { QueryParam, queryParamsToMvsComponentExpressions } from '../../helpers';
1111
import { ExtensionCustomState } from '../../plugin-custom-state';
12-
import { AnyColor } from '../../spec';
1312
import { getInteractionApiData, interactionsFromApiData } from './api';
1413

1514

@@ -21,10 +20,17 @@ const getExtensionCustomState = ExtensionCustomState.getter<{ visuals: StateObje
2120
export interface Interaction {
2221
start: QueryParam,
2322
end: QueryParam,
24-
color?: AnyColor,
23+
color?: string,
24+
radius?: number,
25+
dash_length?: number,
2526
tooltip?: string,
2627
}
2728

29+
const DEFAULT_COLOR = 'white';
30+
const DEFAULT_RADIUS = 0.075;
31+
const DEFAULT_DASH_LENGTH = 0.1;
32+
const DEFAULT_OPACITY = 1;
33+
2834
export interface StateObjectHandle {
2935
/** State transform reference */
3036
ref: string,
@@ -34,23 +40,23 @@ export interface StateObjectHandle {
3440
delete: () => Promise<void>,
3541
}
3642

37-
export function loadInteractions_example(viewer: PDBeMolstarPlugin): Promise<StateObjectHandle> {
38-
return loadInteractions(viewer, { interactions: exampleData });
43+
export function loadInteractions_example(viewer: PDBeMolstarPlugin, params?: { opacity?: number, color?: string, radius?: number, dash_length?: number }): Promise<StateObjectHandle> {
44+
return loadInteractions(viewer, { ...params, interactions: exampleData });
3945
}
4046

41-
export async function loadInteractionsFromApi(viewer: PDBeMolstarPlugin, params: { pdbId: string, authAsymId: string, authSeqId: number, structureId?: string }): Promise<StateObjectHandle> {
47+
export async function loadInteractionsFromApi(viewer: PDBeMolstarPlugin, params: { pdbId: string, authAsymId: string, authSeqId: number, structureId?: string, opacity?: number, color?: string, radius?: number, dash_length?: number }): Promise<StateObjectHandle> {
4248
const data = await getInteractionApiData({ ...params, pdbeBaseUrl: viewer.initParams.pdbeUrl });
4349
const interactions = interactionsFromApiData(data, params.pdbId);
44-
return await loadInteractions(viewer, { interactions, structureId: params.structureId });
50+
return await loadInteractions(viewer, { ...params, interactions });
4551
}
4652

4753
/** Show custom atom interactions */
48-
export async function loadInteractions(viewer: PDBeMolstarPlugin, params: { interactions: Interaction[], structureId?: string }): Promise<StateObjectHandle> {
54+
export async function loadInteractions(viewer: PDBeMolstarPlugin, params: { interactions: Interaction[], structureId?: string, opacity?: number, color?: string, radius?: number, dash_length?: number }): Promise<StateObjectHandle> {
4955
const structureId = params.structureId ?? PDBeMolstarPlugin.MAIN_STRUCTURE_ID;
5056
const struct = viewer.getStructure(structureId);
5157
if (!struct) throw new Error(`Did not find structure with ID "${structureId}"`);
5258

53-
const primitivesMvsNode = interactionsToMvsPrimitiveData(params.interactions);
59+
const primitivesMvsNode = interactionsToMvsPrimitiveData(params);
5460

5561
const update = viewer.plugin.build();
5662
const data = update.to(struct.cell).apply(MVSInlinePrimitiveData, { node: primitivesMvsNode as any }, { tags: ['custom-interactions-data'] });
@@ -79,17 +85,20 @@ export async function clearInteractions(viewer: PDBeMolstarPlugin): Promise<void
7985
visuals.length = 0;
8086
}
8187

82-
function interactionsToMvsPrimitiveData(interactions: Interaction[]): MolstarSubtree<'primitives'> {
88+
function interactionsToMvsPrimitiveData(params: { interactions: Interaction[], opacity?: number, color?: string, radius?: number, dash_length?: number }): MolstarSubtree<'primitives'> {
8389
const builder = MVSData.createBuilder();
84-
const primitives = builder.primitives({ opacity: 1, tooltip: 'Custom interactions', color: 'white' });
90+
const primitives = builder.primitives({
91+
opacity: params.opacity ?? DEFAULT_OPACITY,
92+
color: params.color as ColorT ?? DEFAULT_COLOR,
93+
});
8594

86-
for (const interaction of interactions) {
95+
for (const interaction of params.interactions) {
8796
primitives.tube({
8897
start: { expressions: queryParamsToMvsComponentExpressions([interaction.start]) },
8998
end: { expressions: queryParamsToMvsComponentExpressions([interaction.end]) },
90-
radius: 0.075,
91-
dash_length: 0.1,
92-
color: interaction.color as ColorT,
99+
radius: interaction.radius ?? params.radius ?? DEFAULT_RADIUS,
100+
dash_length: interaction.dash_length ?? params.dash_length ?? DEFAULT_DASH_LENGTH,
101+
color: interaction.color as ColorT | undefined,
93102
tooltip: interaction.tooltip,
94103
});
95104
}

0 commit comments

Comments
 (0)