structure.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import * as React from 'react';
  7. import { CollapsableControls, CollapsableState } from 'molstar/lib/mol-plugin/ui/base';
  8. import { StateElements, AssemblyNames, StructureViewerState } from '../types';
  9. import { ParameterControls } from 'molstar/lib/mol-plugin/ui/controls/parameters';
  10. import { ParamDefinition as PD } from 'molstar/lib/mol-util/param-definition';
  11. import { StateObject, StateTree, StateSelection } from 'molstar/lib/mol-state';
  12. import { PluginStateObject as PSO } from 'molstar/lib/mol-plugin/state/objects';
  13. import { StateTransforms } from 'molstar/lib/mol-plugin/state/transforms';
  14. import { Model } from 'molstar/lib/mol-model/structure';
  15. import { stringToWords } from 'molstar/lib/mol-util/string';
  16. interface StructureControlsState extends CollapsableState {
  17. trajectoryRef: string
  18. isDisabled: boolean
  19. }
  20. export class StructureControls<P, S extends StructureControlsState> extends CollapsableControls<P, S> {
  21. constructor(props: P, context?: any) {
  22. super(props, context);
  23. }
  24. get structureView () {
  25. return (this.plugin.customState as StructureViewerState).structureView
  26. }
  27. async setColorTheme(theme: { [k: string]: string }) {
  28. const { themeCtx } = this.plugin.structureRepresentation
  29. const state = this.plugin.state.dataState;
  30. const tree = state.build();
  31. const assembly = this.getAssembly()
  32. const dataCtx = { structure: assembly && assembly.data }
  33. Object.keys(theme).forEach(k => {
  34. const repr = this.getRepresentation(k)
  35. if (repr && repr.params) {
  36. const values = PD.getDefaultValues(themeCtx.colorThemeRegistry.get(name).getParams(dataCtx))
  37. tree.to(repr.transform.ref).update(
  38. StateTransforms.Representation.StructureRepresentation3D,
  39. props => ({ ...props, colorTheme: { name: theme[k], params: values }})
  40. )
  41. }
  42. })
  43. await this.structureView.applyState(tree)
  44. }
  45. onChange = async (p: { param: PD.Base<any>, name: string, value: any }) => {
  46. // console.log('onChange', p.name, p.value)
  47. if (p.name === 'assembly') {
  48. this.structureView.setAssembly(p.value)
  49. } else if (p.name === 'model') {
  50. this.structureView.setModel(p.value)
  51. } else if (p.name === 'colorThemes') {
  52. this.setColorTheme(p.value)
  53. }
  54. }
  55. getRepresentation(type: string) {
  56. return this.plugin.helpers.structureRepresentation.getRepresentation(StateElements.Assembly, type)
  57. }
  58. getParams = () => {
  59. const { themeCtx, registry } = this.plugin.structureRepresentation
  60. const trajectory = this.getTrajectory()
  61. const model = this.getModel()
  62. const assembly = this.getAssembly()
  63. const modelOptions: [number, string][] = []
  64. const assemblyOptions: [string, string][] = []
  65. if (model && modelFromCrystallography(model.data)) {
  66. assemblyOptions.push([AssemblyNames.Deposited, 'Asymmetric Unit'])
  67. } else {
  68. assemblyOptions.push([AssemblyNames.Deposited, 'Deposited'])
  69. }
  70. if (trajectory) {
  71. if (trajectory.data.length > 1) modelOptions.push([-1, `All`])
  72. for (let i = 0, il = trajectory.data.length; i < il; ++i) {
  73. modelOptions.push([i, `${i + 1}`])
  74. }
  75. if (trajectory.data.length === 1 && modelHasSymmetry(trajectory.data[0])) {
  76. assemblyOptions.push(
  77. [AssemblyNames.Unitcell, 'Unitcell'],
  78. [AssemblyNames.Supercell, 'Supercell'],
  79. [AssemblyNames.CrystalContacts, 'Crystal Contacts']
  80. )
  81. }
  82. }
  83. let modelValue = 0
  84. if (model) {
  85. if (trajectory) modelValue = trajectory.data.indexOf(model.data)
  86. const { assemblies } = model.data.symmetry
  87. for (let i = 0, il = assemblies.length; i < il; ++i) {
  88. const a = assemblies[i]
  89. assemblyOptions.push([a.id, `${a.id}: ${stringToWords(a.details)}`])
  90. }
  91. } else if (assembly) {
  92. // assembly from trajectory, no model
  93. modelValue = -1
  94. }
  95. let assemblyValue: string = AssemblyNames.Deposited
  96. let colorTypes = themeCtx.colorThemeRegistry.types
  97. let types = registry.types
  98. if (assembly) {
  99. assemblyValue = assembly.data.units[0].conformation.operator.assembly.id
  100. colorTypes = themeCtx.colorThemeRegistry.getApplicableTypes({ structure: assembly.data })
  101. types = registry.getApplicableTypes(assembly.data)
  102. }
  103. const colorThemes: { [k: string]: PD.Any } = {}
  104. for (let i = 0, il = types.length; i < il; ++i) {
  105. const type = types[i][0]
  106. const r = this.getRepresentation(type)
  107. if (r) {
  108. const n = r.params ? r.params.values.colorTheme.name : registry.get(type).defaultColorTheme
  109. const p = themeCtx.colorThemeRegistry.get(n)
  110. const d = { structure: assembly && assembly.data }
  111. const ct = p.factory(d, PD.getDefaultValues(p.getParams(d)))
  112. colorThemes[type] = PD.Select(n, colorTypes, { description: ct.description, legend: ct.legend })
  113. }
  114. }
  115. return {
  116. assembly: PD.Select(assemblyValue, assemblyOptions, {
  117. isHidden: assemblyOptions.length === 1,
  118. description: 'Show a specific biological, crystallographic or artificial assembly'
  119. }),
  120. model: PD.Select(modelValue, modelOptions, {
  121. isHidden: modelOptions.length === 1,
  122. description: 'Show a specific model or the full ensamble of models'
  123. }),
  124. // symmetry: PD.Select('todo', [['todo', 'todo']]), // TODO
  125. colorThemes: PD.Group(colorThemes, { isExpanded: true }),
  126. }
  127. }
  128. get values () {
  129. const trajectory = this.getTrajectory()
  130. const model = this.getModel()
  131. const assembly = this.getAssembly()
  132. const { registry } = this.plugin.structureRepresentation
  133. const types = assembly ? registry.getApplicableTypes(assembly.data) : registry.types
  134. const colorThemes: { [k: string]: string } = {}
  135. for (let i = 0, il = types.length; i < il; ++i) {
  136. const type = types[i][0]
  137. const r = this.getRepresentation(type)
  138. colorThemes[type] = r && r.params ? r.params.values.colorTheme.name : registry.get(type).defaultColorTheme
  139. }
  140. let modelValue = 0
  141. if (trajectory) {
  142. modelValue = model ? trajectory.data.indexOf(model.data) : -1
  143. }
  144. let assemblyValue: string = AssemblyNames.Deposited
  145. if (assembly) {
  146. const tags = (assembly as StateObject).tags
  147. if (tags && tags.includes('unitcell')) {
  148. assemblyValue = AssemblyNames.Unitcell
  149. } else if (tags && tags.includes('supercell')) {
  150. assemblyValue = AssemblyNames.Supercell
  151. } else if (tags && tags.includes('crystal-contacts')) {
  152. assemblyValue = AssemblyNames.CrystalContacts
  153. } else {
  154. assemblyValue = assembly.data.units[0].conformation.operator.assembly.id || AssemblyNames.Deposited
  155. }
  156. }
  157. return {
  158. assembly: assemblyValue,
  159. model: modelValue,
  160. // symmetry: 'todo', // TODO
  161. colorThemes,
  162. }
  163. }
  164. private findTrajectoryRef() {
  165. const trajectories = this.plugin.state.dataState.select(StateSelection.Generators.rootsOfType(PSO.Molecule.Trajectory))
  166. return trajectories.length > 0 ? trajectories[0].transform.ref : ''
  167. }
  168. componentDidMount() {
  169. this.setState({ trajectoryRef: this.findTrajectoryRef() })
  170. this.subscribe(this.plugin.events.state.object.updated, ({ ref, state }) => {
  171. if (!this.getTrajectory()) {
  172. this.setState({ trajectoryRef: this.findTrajectoryRef() })
  173. } else if (StateTree.subtreeHasRef(state.tree, this.state.trajectoryRef, ref)) {
  174. this.forceUpdate()
  175. }
  176. })
  177. this.subscribe(this.plugin.events.state.object.created, ({ ref, state }) => {
  178. if (!this.getTrajectory()) {
  179. this.setState({ trajectoryRef: this.findTrajectoryRef() })
  180. } else if (StateTree.subtreeHasRef(state.tree, this.state.trajectoryRef, ref)) {
  181. this.forceUpdate()
  182. }
  183. })
  184. this.subscribe(this.plugin.events.state.object.removed, ({ ref, state }) => {
  185. if (!this.getTrajectory()) {
  186. this.setState({ trajectoryRef: this.findTrajectoryRef() })
  187. } else if (StateTree.subtreeHasRef(state.tree, this.state.trajectoryRef, ref)) {
  188. this.forceUpdate()
  189. }
  190. })
  191. this.subscribe(this.plugin.state.dataState.events.isUpdating, v => this.setState({ isDisabled: v }))
  192. }
  193. private getObj<T extends StateObject>(ref: string): T | undefined {
  194. if (!ref) return undefined
  195. const state = this.plugin.state.dataState
  196. const cell = state.select(ref)[0]
  197. if (!cell || !cell.obj) return undefined
  198. return (cell.obj as T)
  199. }
  200. private getTrajectory() {
  201. return this.getObj<PSO.Molecule.Trajectory>(this.state.trajectoryRef)
  202. }
  203. private getModel() {
  204. if (!this.state.trajectoryRef) return
  205. const models = this.plugin.state.dataState.select(StateSelection.Generators.rootsOfType(PSO.Molecule.Model, this.state.trajectoryRef))
  206. return models.length > 0 ? models[0].obj : undefined
  207. }
  208. private getAssembly() {
  209. if (!this.state.trajectoryRef || !this.plugin.state.dataState.transforms.has(this.state.trajectoryRef)) return
  210. const assemblies = this.plugin.state.dataState.select(StateSelection.Generators.rootsOfType(PSO.Molecule.Structure, this.state.trajectoryRef))
  211. return assemblies.length > 0 ? assemblies[0].obj : undefined
  212. }
  213. defaultState() {
  214. return {
  215. isCollapsed: false,
  216. header: 'Structure Settings',
  217. trajectoryRef: '',
  218. isDisabled: false
  219. } as S
  220. }
  221. renderControls() {
  222. if (!this.plugin.canvas3d) return null
  223. if (!this.getTrajectory() || !this.getAssembly()) return null
  224. return <div>
  225. <ParameterControls params={this.getParams()} values={this.values} onChange={this.onChange} isDisabled={this.state.isDisabled} />
  226. </div>
  227. }
  228. }
  229. function modelHasSymmetry(model: Model) {
  230. const mmcif = model.sourceData.data
  231. return (
  232. mmcif.symmetry._rowCount === 1 && mmcif.cell._rowCount === 1 && !(
  233. mmcif.symmetry.Int_Tables_number.value(0) === 1 &&
  234. mmcif.cell.angle_alpha.value(0) === 90 &&
  235. mmcif.cell.angle_beta.value(0) === 90 &&
  236. mmcif.cell.angle_gamma.value(0) === 90 &&
  237. mmcif.cell.length_a.value(0) === 1 &&
  238. mmcif.cell.length_b.value(0) === 1 &&
  239. mmcif.cell.length_c.value(0) === 1
  240. )
  241. )
  242. }
  243. function modelFromCrystallography(model: Model) {
  244. const mmcif = model.sourceData.data
  245. for (let i = 0; i < mmcif.exptl.method.rowCount; i++) {
  246. const v = mmcif.exptl.method.value(i).toUpperCase()
  247. if (v.indexOf('DIFFRACTION') >= 0) return true
  248. }
  249. return false
  250. }