AssemblyView.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. import {RcsbFvDOMConstants} from "../../../RcsbFvConstants/RcsbFvConstants";
  2. import * as React from "react";
  3. import {
  4. buildInstanceSequenceFv,
  5. buildMultipleInstanceSequenceFv,
  6. getRcsbFv,
  7. setBoardConfig,
  8. unmount
  9. } from "@rcsb/rcsb-saguaro-app";
  10. import {AbstractView, AbstractViewInterface} from "../AbstractView";
  11. import {InstanceSequenceOnchangeInterface} from "@rcsb/rcsb-saguaro-app/build/dist/RcsbFvWeb/RcsbFvBuilder/RcsbFvInstanceBuilder";
  12. import {RcsbFvTrackDataElementInterface} from "@rcsb/rcsb-saguaro";
  13. import {ChainSelectionInterface} from "../../../RcsbFvSelection/RcsbFvSelection";
  14. import {SaguaroPluginModelMapType} from "../../../RcsbFvStructure/StructurePlugins/SaguaroPluginInterface";
  15. import {SelectionInterface} from "@rcsb/rcsb-saguaro/build/RcsbBoard/RcsbSelection";
  16. import {OptionPropsInterface} from "@rcsb/rcsb-saguaro-app/build/dist/RcsbFvWeb/WebTools/SelectButton";
  17. import {OptionProps} from "react-select/src/components/Option";
  18. import {components} from 'react-select';
  19. import {ChainDisplay} from "./ChainDisplay";
  20. import {
  21. StructureSelectionQueries as Q,
  22. StructureSelectionQuery
  23. } from 'molstar/lib/mol-plugin-state/helpers/structure-selection-query';
  24. import {StructureRepresentationRegistry} from "molstar/lib/mol-repr/structure/registry";
  25. import {Expression} from "molstar/lib/mol-script/language/expression";
  26. export interface AssemblyViewInterface {
  27. entryId: string;
  28. }
  29. export class AssemblyView extends AbstractView<AssemblyViewInterface & AbstractViewInterface, AssemblyViewInterface & AbstractViewInterface>{
  30. private currentLabelAsymId: string;
  31. private currentEntryId: string;
  32. private currentModelId: string;
  33. private currentModelNumber: string;
  34. private createComponentThreshold: number = 3;
  35. private innerSelectionFlag: boolean = false;
  36. private currentSelectedComponentId: string;
  37. private currentModelMap:SaguaroPluginModelMapType;
  38. //private readonly componentSet = new Map<string, {current: Set<string>, previous: Set<string>}>();
  39. constructor(props: AssemblyViewInterface & AbstractViewInterface) {
  40. super({
  41. ...props
  42. });
  43. }
  44. protected additionalContent(): JSX.Element {
  45. return (
  46. <div style={{marginTop:10}}>
  47. <div>
  48. <div id={RcsbFvDOMConstants.SELECT_INSTANCE_PFV_ID} style={{display:"inline-block"}}/>
  49. <div style={{display:"inline-block", marginLeft:25}}>
  50. <a href={"/docs/sequence-viewers/protein-feature-view"} target={"_blank"}>Help</a>
  51. </div>
  52. </div>
  53. <div style={{position:"absolute", top:5, right:5}} >
  54. <a style={{textDecoration:"none", color:"#337ab7", cursor:"pointer", marginRight:15}} target={"_blank"} href={"/docs/sequence-viewers/3d-protein-feature-view"}>
  55. Help
  56. </a>
  57. <a style={{textDecoration:"none", color: "#337ab7", cursor:"pointer"}} onClick={()=>{this.props.unmount(true)}}>
  58. Back
  59. </a>
  60. </div>
  61. </div>
  62. );
  63. }
  64. componentDidMount (): void {
  65. super.componentDidMount();
  66. const width: number | undefined = document.getElementById(this.componentDivId)?.getBoundingClientRect().width;
  67. if(width == null)
  68. return;
  69. const trackWidth: number = width - 190 - 55;
  70. setBoardConfig({
  71. trackWidth: trackWidth,
  72. elementClickCallBack:(e:RcsbFvTrackDataElementInterface)=>{
  73. this.props.plugin.clearFocus();
  74. if(this.currentSelectedComponentId != null)
  75. this.props.plugin.removeComponent(this.currentSelectedComponentId);
  76. if(e == null)
  77. return;
  78. const x = e.begin;
  79. const y = e.end ?? e.begin;
  80. if(e.isEmpty){
  81. this.props.plugin.cameraFocus(this.currentModelId, this.currentLabelAsymId, [x,y]);
  82. this.currentSelectedComponentId = this.currentLabelAsymId +":"+ ((x === y) ? x.toString() : x.toString()+","+y.toString());
  83. setTimeout(()=>{
  84. this.props.plugin.createComponent(
  85. this.currentSelectedComponentId,
  86. this.currentModelId,
  87. [{asymId: this.currentLabelAsymId, position: x}, {asymId: this.currentLabelAsymId, position: y}],
  88. 'ball-and-stick'
  89. ).then(()=>{
  90. if(x === y)
  91. setTimeout(()=>{
  92. this.props.plugin.setFocus(this.currentModelId, this.currentLabelAsymId, x, y);
  93. },200);
  94. });
  95. },100);
  96. }else{
  97. this.props.plugin.cameraFocus(this.currentModelId, this.currentLabelAsymId, x, y);
  98. if((y-x)<this.createComponentThreshold){
  99. this.currentSelectedComponentId = this.currentLabelAsymId +":"+ (x === y ? x.toString() : x.toString()+"-"+y.toString());
  100. setTimeout(()=>{
  101. this.props.plugin.createComponent(
  102. this.currentSelectedComponentId,
  103. this.currentModelId,
  104. processGaps(this.currentModelId, this.currentLabelAsymId, e),
  105. 'ball-and-stick'
  106. ).then(()=>{
  107. if(x === y)
  108. setTimeout(()=>{
  109. this.props.plugin.setFocus(this.currentModelId, this.currentLabelAsymId, x, y);
  110. },200);
  111. });
  112. },100);
  113. }
  114. }
  115. },
  116. selectionChangeCallBack:(selection: Array<SelectionInterface>)=>{
  117. if(this.innerSelectionFlag)
  118. return;
  119. this.props.plugin.clearSelection('select', {modelId: this.currentModelId, labelAsymId: this.currentLabelAsymId});
  120. this.props.selection.clearSelection('select', this.currentLabelAsymId);
  121. if(selection == null || selection.length === 0) {
  122. this.resetPluginView();
  123. return;
  124. }
  125. this.select(selection);
  126. },
  127. highlightHoverPosition:true,
  128. highlightHoverElement:true,
  129. highlightHoverCallback:(selection: RcsbFvTrackDataElementInterface[])=>{
  130. this.props.plugin.clearSelection('hover');
  131. if(selection != null && selection.length > 0) {
  132. if(selection[0].isEmpty){
  133. const selectionList = [{modelId: this.currentModelId, asymId: this.currentLabelAsymId, position: selection[0].begin}];
  134. if(selection[0].end != null) selectionList.push({modelId: this.currentModelId, asymId: this.currentLabelAsymId, position: selection[0].end})
  135. this.props.plugin.select(
  136. selectionList,
  137. 'hover',
  138. 'add'
  139. );
  140. }else {
  141. this.props.plugin.select(processMultipleGaps(this.currentModelId, this.currentLabelAsymId, selection), 'hover', 'set');
  142. }
  143. }
  144. },
  145. });
  146. }
  147. componentWillUnmount() {
  148. super.componentWillUnmount();
  149. unmount(this.pfvDivId);
  150. }
  151. protected structureSelectionCallback(): void{
  152. this.pluginSelectCallback('select');
  153. }
  154. protected structureHoverCallback(): void{
  155. this.pluginSelectCallback('hover');
  156. }
  157. protected representationChangeCallback(): void{
  158. //TODO
  159. }
  160. private pluginSelectCallback(mode:'select'|'hover'): void{
  161. if(getRcsbFv(this.pfvDivId) == null)
  162. return;
  163. this.innerSelectionFlag = true;
  164. if(mode === 'select' && this.currentSelectedComponentId != null){
  165. this.props.plugin.removeComponent(this.currentSelectedComponentId);
  166. }
  167. const allSel: Array<ChainSelectionInterface> | undefined = this.props.selection.getSelection(mode);
  168. if(allSel == null || allSel.length ===0) {
  169. getRcsbFv(this.pfvDivId).clearSelection(mode);
  170. if(mode === 'select')
  171. this.resetPluginView();
  172. }else if(mode === 'select' && this.props.selection.getLastSelection('select')?.labelAsymId != null && this.props.selection.getLastSelection('select')?.labelAsymId != this.currentLabelAsymId){
  173. const authId: string | undefined = this.currentModelMap
  174. .get(this.currentModelId)?.chains
  175. .filter(ch=>(ch.label===this.props.selection.getLastSelection('select')?.labelAsymId))[0]?.auth;
  176. this.modelChangeCallback(this.currentModelMap, authId);
  177. }else{
  178. const sel: ChainSelectionInterface | undefined = this.props.selection.getSelectionWithCondition(this.currentModelId, this.currentLabelAsymId, mode);
  179. if (sel == null) {
  180. getRcsbFv(this.pfvDivId).clearSelection(mode);
  181. if(mode === 'select')
  182. this.resetPluginView();
  183. } else {
  184. getRcsbFv(this.pfvDivId).setSelection({elements: sel.regions, mode: mode});
  185. }
  186. }
  187. this.innerSelectionFlag = false;
  188. }
  189. protected async modelChangeCallback(modelMap:SaguaroPluginModelMapType, defaultAuthId?: string): Promise<void> {
  190. this.currentModelMap = modelMap;
  191. this.props.plugin.clearFocus();
  192. const onChangeCallback: Map<string, (x: InstanceSequenceOnchangeInterface)=>void> = new Map<string, (x: InstanceSequenceOnchangeInterface) => {}>();
  193. const filterInstances: Map<string, Set<string>> = new Map<string, Set<string>>();
  194. modelMap.forEach((v,k)=>{
  195. onChangeCallback.set(v.entryId,(x)=>{
  196. this.currentEntryId = v.entryId;
  197. this.currentLabelAsymId = x.asymId;
  198. this.currentModelId = k;
  199. setTimeout(()=>{
  200. this.props.selection.setLastSelection('select', null);
  201. this.structureSelectionCallback();
  202. },1000);
  203. });
  204. filterInstances.set(v.entryId,new Set<string>(v.chains.map(d=>d.label)));
  205. });
  206. unmount(this.pfvDivId);
  207. const entryId: string = Array.from(modelMap.values()).map(d=>d.entryId)[0];
  208. if(entryId != null)
  209. buildInstanceSequenceFv(
  210. this.pfvDivId,
  211. RcsbFvDOMConstants.SELECT_INSTANCE_PFV_ID,
  212. entryId, {
  213. defaultValue: defaultAuthId,
  214. onChangeCallback: onChangeCallback.get(entryId),
  215. filterInstances: filterInstances.get(entryId),
  216. selectButtonOptionProps:(props:OptionProps<OptionPropsInterface>)=>(components.Option && <div style={{display:'flex'}}>
  217. <ChainDisplay plugin={this.props.plugin} label={props.data.label}/><components.Option {...props}/>
  218. </div>)
  219. }
  220. );
  221. if(!defaultAuthId)
  222. await this.createComponents(modelMap);
  223. }
  224. protected updateDimensions(): void{
  225. const width: number = window.document.getElementById(this.componentDivId)?.getBoundingClientRect().width ?? 0;
  226. const trackWidth: number = width - 190 - 55;
  227. getRcsbFv(this.pfvDivId).updateBoardConfig({boardConfigData:{trackWidth:trackWidth}}).then(()=>{
  228. this.structureSelectionCallback();
  229. });
  230. }
  231. private select(selection: Array<SelectionInterface>): void{
  232. selection.forEach(d=>{
  233. const e: RcsbFvTrackDataElementInterface = d.rcsbFvTrackDataElement;
  234. const x = e.begin;
  235. const y = e.end ?? e.begin;
  236. if(e.isEmpty){
  237. this.props.plugin.select(
  238. [{modelId: this.currentModelId, asymId: this.currentLabelAsymId, position: x},{modelId: this.currentModelId, asymId: this.currentLabelAsymId, position: y}], 'select',
  239. 'add'
  240. );
  241. this.props.selection.addSelectionFromRegion(this.currentModelId, this.currentLabelAsymId, {begin:x, end:y, isEmpty: true, source: 'sequence'}, 'select');
  242. }else{
  243. this.props.plugin.select(processGaps(this.currentModelId, this.currentLabelAsymId, e), 'select', 'add');
  244. this.props.selection.addSelectionFromRegion(this.currentModelId, this.currentLabelAsymId, {begin:x, end:y, source: 'sequence'}, 'select');
  245. }
  246. });
  247. }
  248. private resetPluginView(): void {
  249. this.props.plugin.clearFocus();
  250. this.props.plugin.resetCamera();
  251. }
  252. private async createComponents(modelMap:SaguaroPluginModelMapType): Promise<void> {
  253. await this.props.plugin.displayComponent("Water", false);
  254. await this.props.plugin.colorComponent("Polymer", 'chain-id');
  255. const chains: Array<{modelId: string; auth: string; label: string;}> = new Array<{modelId: string; auth: string; label: string;}>();
  256. modelMap.forEach((entry, modelId)=>{
  257. entry.chains.forEach(ch=>{
  258. if(ch.type === "polymer") {
  259. chains.push({modelId: modelId, auth: ch.auth, label: ch.label});
  260. }
  261. });
  262. });
  263. this.props.plugin.removeComponent();
  264. this.props.plugin.clearFocus();
  265. for(const ch of chains) {
  266. const label: string = ch.auth === ch.label ? ch.label : `${ch.label} [auth ${ch.auth}]`;
  267. await this.props.plugin.createComponent(label, ch.modelId, ch.label, 'cartoon');
  268. await this.props.plugin.colorComponent(label, 'chain-id');
  269. }
  270. /*this.props.plugin.pluginCall((plugin)=>{
  271. const createComponent = (label: string, tag: string, expression: Expression, representationType: StructureRepresentationRegistry.BuiltIn) => {
  272. return plugin.managers.structure.component.add({
  273. selection: StructureSelectionQuery(tag, expression),
  274. options: { checkExisting: false, label: label },
  275. representation: representationType,
  276. });
  277. }
  278. const recursive = (componentList: {label: string; tag: string; expression: Expression; representationType: StructureRepresentationRegistry.BuiltIn;}[])=>{
  279. if(componentList.length>0){
  280. const component = componentList.shift()!;
  281. createComponent(component.label, component.tag, component.expression, component.representationType).then(()=>{
  282. recursive(componentList);
  283. });
  284. }
  285. };
  286. recursive([{
  287. label: 'Ligands',
  288. tag: 'ligand',
  289. expression: Q.ligand.expression,
  290. representationType: 'ball-and-stick'
  291. },{
  292. label: 'Carbohydrates',
  293. tag: 'carbohydrate',
  294. expression: Q.branched.expression,
  295. representationType: 'carbohydrate'
  296. },{
  297. label: 'Ions',
  298. tag: 'ion',
  299. expression: Q.ion.expression,
  300. representationType: 'ball-and-stick'
  301. },{
  302. label: 'Lipids',
  303. tag: 'lipid',
  304. expression: Q.lipid.expression,
  305. representationType: 'ball-and-stick'
  306. }]);
  307. });*/
  308. await this.props.plugin.removeComponent("Polymer");
  309. }
  310. /*private removeComponents(labelAsymId?:string){
  311. if(labelAsymId != null){
  312. this.componentSet.get(labelAsymId)?.current.forEach(componentId=>{
  313. this.props.plugin.removeComponent(componentId);
  314. });
  315. }else{
  316. Array.from(this.componentSet.keys()).forEach(labelAsymId=>{
  317. this.componentSet.get(labelAsymId)?.current.forEach(componentId=>{
  318. this.props.plugin.removeComponent(componentId);
  319. });
  320. });
  321. }
  322. }
  323. private removeObsoleteComponents(): void{
  324. this.componentSet.get(this.currentLabelAsymId)?.previous.forEach(componentId=>{
  325. if(!this.componentSet.get(this.currentLabelAsymId)?.current.has(componentId)) {
  326. this.props.plugin.removeComponent(componentId);
  327. }
  328. });
  329. }
  330. private resetComponentKeys(): void {
  331. if(!this.componentSet.has(this.currentLabelAsymId))
  332. this.componentSet.set(this.currentLabelAsymId, {current: new Set<string>(), previous: new Set<string>()});
  333. this.componentSet.get(this.currentLabelAsymId)?.previous.clear();
  334. this.componentSet.get(this.currentLabelAsymId)?.current.forEach(e=>{
  335. this.componentSet.get(this.currentLabelAsymId)?.previous.add(e);
  336. });
  337. this.componentSet.get(this.currentLabelAsymId)?.current.clear();
  338. }*/
  339. }
  340. function processGaps(modelId: string, asymId: string, e: RcsbFvTrackDataElementInterface): Array<{modelId: string; asymId: string; begin: number; end: number;}>{
  341. const regions: Array<{modelId: string; asymId: string; begin: number; end: number;}> = new Array<{modelId: string; asymId: string; begin: number; end: number}>();
  342. let lastIndex: number = e.begin;
  343. e.gaps?.forEach((g)=>{
  344. regions.push({
  345. modelId: modelId,
  346. asymId: asymId,
  347. begin: lastIndex,
  348. end: g.begin
  349. });
  350. lastIndex = g.end;
  351. });
  352. regions.push({
  353. modelId: modelId,
  354. asymId: asymId,
  355. begin: lastIndex,
  356. end: e.end ?? e.begin
  357. });
  358. return regions;
  359. }
  360. function processMultipleGaps(modelId: string, asymId: string, list: Array<RcsbFvTrackDataElementInterface>): Array<{modelId: string; asymId: string; begin: number; end: number;}>{
  361. let regions: Array<{modelId: string; asymId: string; begin: number; end: number;}> = new Array<{modelId: string; asymId: string; begin: number; end: number}>();
  362. list.forEach(e=>{
  363. regions = regions.concat(processGaps(modelId, asymId, e));
  364. });
  365. return regions;
  366. }