AssemblyModelSate.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {ChainInfo, OperatorInfo, SaguaroPluginModelMapType} from "../RcsbFvStructure/StructureViewerInterface";
  2. interface AssemblyModelStateInterface {
  3. modelId: string;
  4. entryId:string;
  5. assemblyId:string;
  6. labelAsymId:string;
  7. operator: OperatorInfo
  8. }
  9. export class AssemblyModelSate {
  10. private modelMap:SaguaroPluginModelMapType;
  11. private state: Partial<AssemblyModelStateInterface> = {};
  12. constructor(modelMap?:SaguaroPluginModelMapType) {
  13. if(modelMap)
  14. this.modelMap = modelMap;
  15. }
  16. public setMap(modelMap:SaguaroPluginModelMapType): void{
  17. this.modelMap = modelMap;
  18. this.setFirstModel();
  19. }
  20. public getMap(): SaguaroPluginModelMapType{
  21. return this.modelMap;
  22. }
  23. public set(state: Partial<AssemblyModelStateInterface>): void{
  24. this.state = {...this.state,...state};
  25. }
  26. public get(key: keyof AssemblyModelStateInterface): string|OperatorInfo|undefined {
  27. return this.state[key];
  28. }
  29. public getString(key: keyof Omit<AssemblyModelStateInterface,"operator">): string {
  30. if(!this.state[key])
  31. throw `${key} is undefined`;
  32. return this.state[key]!;
  33. }
  34. public getOperator(): OperatorInfo | undefined {
  35. return this.state.operator;
  36. }
  37. public forEach(f: (v:{entryId: string; assemblyId: string, chains:Array<ChainInfo>;},k:string)=>void): void{
  38. this.modelMap.forEach((v,k)=>f(v,k));
  39. }
  40. public entries(): IterableIterator<[string,{entryId: string; assemblyId: string; chains:Array<ChainInfo>;}]>{
  41. return this.modelMap.entries();
  42. }
  43. public setOperator(asymId?:string, opName?:string) {
  44. const currentChainInfo: ChainInfo|undefined = this.getChainInfo(asymId??this.state.labelAsymId);
  45. this.state.operator = opName ? currentChainInfo?.operators.filter(op=>(op.name === opName))[0] : currentChainInfo?.operators[0];
  46. }
  47. public getChainInfo(asymId?:string): ChainInfo | undefined{
  48. if(!this.state.modelId)
  49. throw "modelId not define";
  50. if(asymId)
  51. return this.modelMap.get(this.state.modelId)?.chains.find(ch=>ch.label===asymId);
  52. else
  53. return this.modelMap.get(this.state.modelId)?.chains.find(ch=>ch.label===this.state.labelAsymId);
  54. }
  55. private setFirstModel(): void{
  56. this.state.modelId = Array.from(this.modelMap.keys())[0];
  57. this.state.entryId = this.modelMap.get(this.state.modelId)!.entryId;
  58. this.state.assemblyId = this.modelMap.get(this.state.modelId)!.assemblyId;
  59. this.state.operator = undefined;
  60. }
  61. }