MsaRowTitleCheckboxComponent.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2021 RCSB PDB and contributors, licensed under MIT, See LICENSE file for more info.
  3. * @author Joan Segura Mora <joan.segura@rcsb.org>
  4. */
  5. import * as React from "react";
  6. import {TagDelimiter} from "@rcsb/rcsb-api-tools/build/RcsbUtils/TagDelimiter";
  7. import {Subscription} from "rxjs";
  8. import {RcsbFvStateInterface} from "../../../../../RcsbFvState/RcsbFvStateInterface";
  9. import {rcsbRequestCtxManager} from "@rcsb/rcsb-saguaro-app/lib/RcsbRequest/RcsbRequestContextManager";
  10. interface MsaRowTitleCheckboxInterface {
  11. disabled:boolean;
  12. entryId: string;
  13. tag: "aligned"|"polymer"|"non-polymer";
  14. stateManager:RcsbFvStateInterface
  15. }
  16. type MsaRowTitleCheckboxType = MsaRowTitleCheckboxInterface & {entityId:string} | MsaRowTitleCheckboxInterface & {instanceId:string;}
  17. interface MsaRowTitleCheckboxState {
  18. checked:boolean;
  19. opacity: 0|1;
  20. }
  21. export class MsaRowTitleCheckboxComponent extends React.Component <MsaRowTitleCheckboxType,MsaRowTitleCheckboxState> {
  22. readonly state: MsaRowTitleCheckboxState = {
  23. checked: this.props.tag == "aligned",
  24. opacity: (this.props.disabled) && this.props.tag != "aligned" ? 0 : 1
  25. };
  26. private subscription: Subscription;
  27. constructor(props: MsaRowTitleCheckboxType) {
  28. super(props);
  29. }
  30. public render():JSX.Element {
  31. return (
  32. <div
  33. style={this.style()}
  34. onClick={()=>{this.click()}}
  35. title={this.title()}
  36. />);
  37. }
  38. public async componentDidMount() {
  39. this.subscribe();
  40. this.requestInfo();
  41. this.setState({opacity: await this.opacity()})
  42. }
  43. public componentWillUnmount() {
  44. this.subscription.unsubscribe();
  45. }
  46. public async componentDidUpdate(prevProps: Readonly<MsaRowTitleCheckboxType>, prevState: Readonly<MsaRowTitleCheckboxState>, snapshot?: any) {
  47. if(!this.props.disabled && prevProps.disabled)
  48. this.requestInfo();
  49. }
  50. private subscribe(): void{
  51. this.subscription = this.props.stateManager.subscribe<
  52. "representation-change"|"missing-component"|"component-info",
  53. {label:string;isHidden:boolean;} & {tag:MsaRowTitleCheckboxInterface["tag"];isHidden:boolean;pdb:{entryId:string;entityId:string;}|{entryId:string;instanceId:string;};} & {isComponent: boolean; isVisible: boolean;}
  54. >((o)=>{
  55. if(o.type == "representation-change" && o.view == "3d-view" && o.data)
  56. this.structureViewerRepresentationChange(o.data as any);
  57. if(o.type == "component-info" && o.view == "3d-view" && o.data)
  58. this.componentInfo(o.data);
  59. })
  60. }
  61. private structureViewerRepresentationChange(d:{label:string;isHidden:boolean;}): void {
  62. const row: string[] = d.label.split(TagDelimiter.entity);
  63. const suffix: string = row.pop()!;
  64. const entryId: string = row.join(TagDelimiter.entity);
  65. const entityId: string = suffix.substring(0,1);
  66. if( this.compId() == `${entryId}${TagDelimiter.entity}${entityId}` ){
  67. //TODO this is a one to many relationship
  68. /*if( d.label.includes("polymer") && this.props.tag == "polymer" && d.isHidden == this.state.checked){
  69. this.setState({checked:!this.state.checked});
  70. }
  71. if( !d.label.includes("polymer") && this.props.tag == "non-polymer" && d.isHidden == this.state.checked){
  72. this.setState({checked:!this.state.checked});
  73. }*/
  74. }
  75. }
  76. private click(): void {
  77. if(this.props.disabled)
  78. return;
  79. this.setState({checked:!this.state.checked},()=>{
  80. this.props.stateManager.next<"representation-change",{tag:MsaRowTitleCheckboxInterface["tag"];isHidden:boolean;pdb:{entryId:string;entityId:string;}|{entryId:string;instanceId:string;};}>({
  81. view:"1d-view",
  82. type: "representation-change",
  83. data:{
  84. pdb: "entityId" in this.props ? {
  85. entryId: this.props.entryId,
  86. entityId: this.props.entityId,
  87. } : {
  88. entryId: this.props.entryId,
  89. instanceId: this.props.instanceId
  90. },
  91. isHidden:!this.state.checked,
  92. tag:this.props.tag
  93. }
  94. });
  95. });
  96. }
  97. private style(): React.CSSProperties {
  98. const color = {
  99. "checked":"rgb(51, 122, 183)",
  100. "unchecked":"rgba(51,122,183,0.44)",
  101. "disabled":"#ddd"
  102. }
  103. return {
  104. width:7,
  105. height:7,
  106. opacity: this.state.opacity,
  107. backgroundColor: this.props.disabled ? color.disabled : color[ this.state.checked ? "checked" : "unchecked"],
  108. border: 1,
  109. borderStyle: "solid",
  110. borderColor: this.props.disabled ? color.disabled : color.checked,
  111. display:"inline-block",
  112. marginLeft:4,
  113. cursor: this.props.disabled ? undefined : "pointer"
  114. };
  115. }
  116. private title(): string | undefined{
  117. if(this.props.disabled)
  118. return undefined;
  119. switch (this.props.tag){
  120. case "aligned":
  121. return `${this.state.checked ? "Hide" : "Show"} Aligned Polymer Chain`;
  122. case "polymer":
  123. return `${this.state.checked ? "Hide" : "Show"} Other Polymer Chains`;
  124. case "non-polymer":
  125. return `${this.state.checked ? "Hide" : "Show"} Non-polymer Chains`;
  126. }
  127. }
  128. private compId(): string {
  129. if("entityId" in this.props)
  130. return `${this.props.entryId}${TagDelimiter.entity}${this.props.entityId}`;
  131. else
  132. return `${this.props.entryId}${TagDelimiter.instance}${this.props.instanceId}`;
  133. }
  134. private getRcsbId(pdb:{entryId:string;entityId:string;}|{entryId:string;instanceId:string;}): string {
  135. if("entityId" in pdb)
  136. return `${pdb.entryId}${TagDelimiter.entity}${pdb.entityId}`;
  137. else
  138. return `${pdb.entryId}${TagDelimiter.instance}${pdb.instanceId}`;
  139. }
  140. private async componentInfo(data: {
  141. tag:MsaRowTitleCheckboxInterface["tag"];
  142. isComponent:boolean;
  143. isVisible:boolean;
  144. pdb: {entryId:string;entityId:string;} | {entryId:string;instanceId:string;};
  145. }): Promise<void> {
  146. if(this.compId() == this.getRcsbId(data.pdb) && this.props.tag == data.tag)
  147. this.setState({checked: data.isVisible, opacity: data.isComponent ? 1 : await this.opacity()})
  148. }
  149. private requestInfo(): void {
  150. this.props.stateManager.next<"component-info", {pdb:{entryId:string;entityId:string;}|{entryId:string;instanceId:string;};tag:MsaRowTitleCheckboxInterface["tag"];}>({
  151. type: "component-info",
  152. view: "1d-view",
  153. data: {
  154. pdb: "entityId" in this.props ? {
  155. entryId: this.props.entryId,
  156. entityId: this.props.entityId,
  157. } : {
  158. entryId: this.props.entryId,
  159. instanceId: this.props.instanceId
  160. },
  161. tag: this.props.tag
  162. }
  163. })
  164. }
  165. private async opacity(): Promise<0 | 1> {
  166. if(this.props.tag == "aligned")
  167. return 1;
  168. return await this.componentOpacity(this.props.tag);
  169. }
  170. private async componentOpacity(componentType: "polymer" | "non-polymer"): Promise<0|1> {
  171. if(!TagDelimiter.isRcsbId(this.props.entryId))
  172. return 0;
  173. switch (componentType) {
  174. case "polymer":
  175. return await this.polymerTest() ? 1 : 0;
  176. case "non-polymer":
  177. return await this.nonPolymerTest() ? 1 : 0;
  178. }
  179. }
  180. private async polymerTest(): Promise<boolean> {
  181. const entryId = this.props.entryId;
  182. const entryInfo = (await rcsbRequestCtxManager.getEntryProperties(entryId))[0];
  183. const polymerChains = Array.from(entryInfo.entityToInstance.values()).flat();
  184. const assemblyChains = Array.from(entryInfo.instanceToOperator?.get(`${this.props.entryId}-1`)?.keys() ?? []).filter( chId => polymerChains.includes(chId));
  185. if(assemblyChains && assemblyChains.length > 1)
  186. return true;
  187. if(entryInfo && (entryInfo.instanceToOperator?.get(`${this.props.entryId}-1`)?.get( (entryInfo.entityToInstance.get(this.compId()) ?? [""])[0] )?.length ?? 0) > 1)
  188. return true;
  189. return false;
  190. }
  191. private async nonPolymerTest(): Promise<boolean> {
  192. const entryId = this.props.entryId;
  193. const entryInfo = (await rcsbRequestCtxManager.getEntryProperties(entryId))[0];
  194. if(entryInfo && Array.from(entryInfo.entityToPrd.values()).filter(v=>v!="").length > 0)
  195. return true;
  196. if(entryInfo && entryInfo.nonPolymerEntityToInstance && entryInfo.nonPolymerEntityToInstance.size > 0)
  197. return true;
  198. return false;
  199. }
  200. }