TmViewerStandalone.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author Joan Segura <joan.segura@rcsb.org>
  6. * @author Yana Rose <yana.rose@rcsb.org>
  7. * @author Sebastian Bittrich <sebastian.bittrich@rcsb.org>
  8. */
  9. /**
  10. * Copyright (C) 2024, Protein Bioinformatics Research Group, HUN-REN RCNS
  11. *
  12. * Licensed under CC BY-NC 4.0, see LICENSE file for more info.
  13. *
  14. * @author Gabor Tusnady <tusnady.gabor@ttk.hu>
  15. * @author Csongor Gerdan <gerdan.csongor@ttk.hu>
  16. */
  17. import { Viewer, ViewerProps } from '@rcsb/rcsb-molstar/build/src/viewer';
  18. import { TmDetColorThemeProvider, updateSiteColors } from '../../TmFv3DApp/tmdet-extension/tmdet-color-theme';
  19. import { MembraneOrientationPreset } from '../../TmFv3DApp/tmdet-extension/behavior';
  20. import { TmDetLabelProvider } from '../../TmFv3DApp/tmdet-extension/labeling';
  21. import { BuiltInTrajectoryFormat } from 'molstar/lib/mol-plugin-state/formats/trajectory';
  22. import { PresetProps } from '@rcsb/rcsb-molstar/build/src/viewer/helpers/preset';
  23. import { Mat4 } from 'molstar/lib/commonjs/mol-math/linear-algebra';
  24. import { TrajectoryHierarchyPresetProvider } from 'molstar/lib/mol-plugin-state/builder/structure/hierarchy-preset';
  25. import { registerRegionDescriptorData } from '../../TmFv3DApp/UniTmpHelper';
  26. import { TmDetRcsbPreset } from '../TmTrajectoryHierarchyPreset';
  27. import { MolScriptBuilder } from 'molstar/lib/mol-script/language/builder';
  28. import { StateTransforms } from 'molstar/lib/mol-plugin-state/transforms';
  29. import { Color } from 'molstar/lib/mol-util/color';
  30. import { Script } from 'molstar/lib/mol-script/script';
  31. import { ColorNames } from 'molstar/lib/mol-util/color/names';
  32. export class TmViewerStandalone extends Viewer {
  33. constructor(elementOrId: string | HTMLElement, props: Partial<ViewerProps> = {}, withTmDetBehaviour: boolean = true) {
  34. super(elementOrId, props);
  35. if (withTmDetBehaviour) {
  36. this.initBehaviour();
  37. }
  38. }
  39. public async initBehaviour() {
  40. let tree = this.plugin.state.behaviors.build();
  41. this.plugin.builders.structure.representation.registerPreset(MembraneOrientationPreset);
  42. this.plugin.representation.structure.themes.colorThemeRegistry.add(TmDetColorThemeProvider);
  43. this.plugin.managers.lociLabels.addProvider(TmDetLabelProvider);
  44. await this.plugin.runTask(this.plugin.state.behaviors.updateTree(tree, { doNotUpdateCurrent: true, doNotLogTiming: true }));
  45. }
  46. loadStructureFromUrl<P, S>(url: string, format: BuiltInTrajectoryFormat, isBinary: boolean, config?: {
  47. props?: PresetProps & {
  48. dataLabel?: string;
  49. };
  50. matrix?: Mat4;
  51. reprProvider?: TrajectoryHierarchyPresetProvider<P, S>;
  52. params?: P;
  53. }): Promise<any> {
  54. return super.loadStructureFromUrl(url, format, isBinary, config);
  55. }
  56. chainSelection(auth_asym_id: string) {
  57. return MolScriptBuilder.struct.generator.atomGroups({
  58. 'chain-test': MolScriptBuilder.core.rel.eq([MolScriptBuilder.struct.atomProperty.macromolecular.auth_asym_id(), auth_asym_id])
  59. });
  60. }
  61. setChainColor(labelAsymIds: string[], hexRgb: string|false = false) {
  62. this.overPaint(labelAsymIds, hexRgb, false);
  63. }
  64. resetChainColor(labelAsymIds: string[]) {
  65. this.overPaint(labelAsymIds, false, true);
  66. }
  67. /**
  68. * Util function to handle overpaint logic.
  69. * @param labelAsymId
  70. * @param hexRgb default color grey, when it is false
  71. * @param clear if true, it clears the overpaint color
  72. */
  73. protected overPaint(labelAsymIds: string[], hexRgb: string|false = false, clear: boolean = false) {
  74. if (labelAsymIds.length == 0) {
  75. console.warn('No specified chain(s) for overpaintings');
  76. return;
  77. }
  78. const state = this.plugin.state.data;
  79. const selected = state.selectQ(q => q.ofTransformer(StateTransforms.Representation.StructureRepresentation3D));
  80. if (selected.length == 0) {
  81. console.log('Representation not found');
  82. return;
  83. }
  84. const chainList = labelAsymIds.join(' ');
  85. selected.forEach(async (representation) => {
  86. const update = this.plugin.build().to(representation);
  87. update.apply(StateTransforms.Representation.OverpaintStructureRepresentation3DFromScript, {
  88. layers: [
  89. {
  90. script: Script(
  91. `(sel.atom.atom-groups :chain-test (set.has (set ${chainList}) atom.label_asym_id))`,
  92. 'mol-script'
  93. ),
  94. color: hexRgb !== false ? Color.fromHexString(hexRgb) : ColorNames.white,
  95. clear: clear
  96. }
  97. ]
  98. });
  99. await update.commit();
  100. });
  101. }
  102. async loadWithUNITMPMembraneRepresentation(params: any) {
  103. setTimeout(() => { this.plugin.clear(); }, 100); // clear scene after some delay
  104. updateSiteColors(params.side1);
  105. setTimeout(() => { (async () => {
  106. await registerRegionDescriptorData(params.regionDescriptorUrl, params.side1);
  107. // load structure
  108. this.loadStructureFromUrl(params.structureUrl, 'mmcif', false, { reprProvider: TmDetRcsbPreset });
  109. })(); }, 500);
  110. }
  111. }