structure.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 } from '../helpers';
  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 { PluginCommands } from 'molstar/lib/mol-plugin/command';
  12. import { StateObject, StateBuilder, StateTree, StateSelection } from 'molstar/lib/mol-state';
  13. import { PluginStateObject as PSO } from 'molstar/lib/mol-plugin/state/objects';
  14. import { StateTransforms } from 'molstar/lib/mol-plugin/state/transforms';
  15. import { Vec3 } from 'molstar/lib/mol-math/linear-algebra';
  16. import { Model } from 'molstar/lib/mol-model/structure';
  17. import { PluginContext } from 'molstar/lib/mol-plugin/context';
  18. interface StructureControlsState extends CollapsableState {
  19. trajectoryRef: string
  20. }
  21. export class StructureControlsHelper {
  22. applyState(tree: StateBuilder) {
  23. return PluginCommands.State.Update.dispatch(this.plugin, { state: this.plugin.state.dataState, tree });
  24. }
  25. async preset() {
  26. await this.plugin.helpers.structureRepresentation.preset()
  27. }
  28. async setAssembly(id: string) {
  29. const state = this.plugin.state.dataState;
  30. const tree = state.build();
  31. if (id === 'unitcell') {
  32. const props = { ijkMin: Vec3.create(0, 0, 0), ijkMax: Vec3.create(0, 0, 0) }
  33. tree.delete(StateElements.Assembly)
  34. .to(StateElements.Model).apply(
  35. StateTransforms.Model.StructureSymmetryFromModel,
  36. props, { ref: StateElements.Assembly, tags: [ 'unitcell' ] }
  37. )
  38. } else if (id === 'supercell') {
  39. const props = { ijkMin: Vec3.create(-1, -1, -1), ijkMax: Vec3.create(1, 1, 1) }
  40. tree.delete(StateElements.Assembly)
  41. .to(StateElements.Model).apply(
  42. StateTransforms.Model.StructureSymmetryFromModel,
  43. props, { ref: StateElements.Assembly, tags: [ 'supercell' ] }
  44. )
  45. } else {
  46. tree.delete(StateElements.Assembly)
  47. .to(StateElements.Model).apply(
  48. StateTransforms.Model.StructureAssemblyFromModel,
  49. { id }, { ref: StateElements.Assembly }
  50. )
  51. }
  52. await this.applyState(tree)
  53. await this.preset()
  54. }
  55. async setModel(modelIndex: number) {
  56. const state = this.plugin.state.dataState;
  57. const tree = state.build();
  58. if (modelIndex === -1) {
  59. tree.delete(StateElements.Model)
  60. .to(StateElements.Trajectory).apply(
  61. StateTransforms.Model.StructureFromTrajectory,
  62. {}, { ref: StateElements.Assembly }
  63. )
  64. await this.applyState(tree);
  65. await this.preset()
  66. } else {
  67. if (state.tree.transforms.has(StateElements.Model)) {
  68. tree.to(StateElements.Model).update(
  69. StateTransforms.Model.ModelFromTrajectory,
  70. props => ({ ...props, modelIndex })
  71. );
  72. await this.applyState(tree);
  73. } else {
  74. tree.delete(StateElements.Assembly)
  75. .to(StateElements.Trajectory).apply(
  76. StateTransforms.Model.ModelFromTrajectory,
  77. { modelIndex }, { ref: StateElements.Model }
  78. )
  79. .apply(
  80. StateTransforms.Model.StructureAssemblyFromModel,
  81. { id: 'deposited' }, { ref: StateElements.Assembly }
  82. );
  83. await this.applyState(tree);
  84. await this.preset()
  85. }
  86. }
  87. }
  88. constructor(private plugin: PluginContext) {
  89. }
  90. }
  91. export class StructureControls<P, S extends StructureControlsState> extends CollapsableControls<P, S> {
  92. structureControlsHelper: StructureControlsHelper
  93. constructor(props: P, context?: any) {
  94. super(props, context);
  95. this.structureControlsHelper = new StructureControlsHelper(this.plugin)
  96. }
  97. async setColorTheme(theme: { [k: string]: string }) {
  98. const { themeCtx } = this.plugin.structureRepresentation
  99. const state = this.plugin.state.dataState;
  100. const tree = state.build();
  101. const assembly = this.getAssembly()
  102. const dataCtx = { structure: assembly && assembly.data }
  103. Object.keys(theme).forEach(k => {
  104. const repr = this.getRepresentation(k)
  105. if (repr && repr.params) {
  106. const values = PD.getDefaultValues(themeCtx.colorThemeRegistry.get(name).getParams(dataCtx))
  107. tree.to(repr.transform.ref).update(
  108. StateTransforms.Representation.StructureRepresentation3D,
  109. props => ({ ...props, colorTheme: { name: theme[k], params: values }})
  110. )
  111. }
  112. })
  113. await this.structureControlsHelper.applyState(tree)
  114. }
  115. onChange = async (p: { param: PD.Base<any>, name: string, value: any }) => {
  116. // console.log('onChange', p.name, p.value)
  117. if (p.name === 'assembly') {
  118. this.structureControlsHelper.setAssembly(p.value)
  119. } else if (p.name === 'model') {
  120. this.structureControlsHelper.setModel(p.value)
  121. } else if (p.name === 'colorThemes') {
  122. this.setColorTheme(p.value)
  123. }
  124. }
  125. getRepresentation(type: string) {
  126. return this.plugin.helpers.structureRepresentation.getRepresentation(StateElements.Assembly, type)
  127. }
  128. getParams = () => {
  129. const { themeCtx, registry } = this.plugin.structureRepresentation
  130. const trajectory = this.getTrajectory()
  131. const model = this.getModel()
  132. const assembly = this.getAssembly()
  133. const modelOptions: [number, string][] = []
  134. const assemblyOptions: [string, string][] = [['deposited', 'deposited']]
  135. if (trajectory) {
  136. if (trajectory.data.length > 1) modelOptions.push([-1, `All`])
  137. for (let i = 0, il = trajectory.data.length; i < il; ++i) {
  138. modelOptions.push([i, `${i + 1}`])
  139. }
  140. if (trajectory.data.length === 1 && modelHasSymmetry(trajectory.data[0])) {
  141. assemblyOptions.push(['unitcell', 'unitcell'], ['supercell', 'supercell'])
  142. }
  143. }
  144. let modelValue = 0
  145. if (model) {
  146. if (trajectory) modelValue = trajectory.data.indexOf(model.data)
  147. const { assemblies } = model.data.symmetry
  148. for (let i = 0, il = assemblies.length; i < il; ++i) {
  149. const a = assemblies[i]
  150. assemblyOptions.push([a.id, `${a.id}: ${a.details}`])
  151. }
  152. } else if (assembly) {
  153. // assembly from trajectory, no model
  154. modelValue = -1
  155. }
  156. let assemblyValue = 'deposited'
  157. let colorTypes = themeCtx.colorThemeRegistry.types
  158. let types = registry.types
  159. if (assembly) {
  160. assemblyValue = assembly.data.units[0].conformation.operator.assembly.id
  161. colorTypes = themeCtx.colorThemeRegistry.getApplicableTypes({ structure: assembly.data })
  162. types = registry.getApplicableTypes(assembly.data)
  163. }
  164. const colorThemes: { [k: string]: PD.Any } = {}
  165. for (let i = 0, il = types.length; i < il; ++i) {
  166. const type = types[i][0]
  167. const r = this.getRepresentation(type)
  168. if (r) {
  169. const n = r.params ? r.params.values.colorTheme.name : registry.get(type).defaultColorTheme
  170. const p = themeCtx.colorThemeRegistry.get(n)
  171. const d = { structure: assembly && assembly.data }
  172. const ct = p.factory(d, PD.getDefaultValues(p.getParams(d)))
  173. colorThemes[type] = PD.Select(n, colorTypes, { description: ct.description, legend: ct.legend })
  174. }
  175. }
  176. return {
  177. assembly: PD.Select(assemblyValue, assemblyOptions, {
  178. isHidden: assemblyOptions.length === 1,
  179. description: 'Show a specific biological or crystallographic assembly'
  180. }),
  181. model: PD.Select(modelValue, modelOptions, {
  182. isHidden: modelOptions.length === 1,
  183. description: 'Show a specific model or the full ensamble of models'
  184. }),
  185. // symmetry: PD.Select('todo', [['todo', 'todo']]), // TODO
  186. colorThemes: PD.Group(colorThemes, { isExpanded: true }),
  187. }
  188. }
  189. get values () {
  190. const trajectory = this.getTrajectory()
  191. const model = this.getModel()
  192. const assembly = this.getAssembly()
  193. const { registry } = this.plugin.structureRepresentation
  194. const types = assembly ? registry.getApplicableTypes(assembly.data) : registry.types
  195. const colorThemes: { [k: string]: string } = {}
  196. for (let i = 0, il = types.length; i < il; ++i) {
  197. const type = types[i][0]
  198. const r = this.getRepresentation(type)
  199. colorThemes[type] = r && r.params ? r.params.values.colorTheme.name : registry.get(type).defaultColorTheme
  200. }
  201. let modelValue = 0
  202. if (trajectory) {
  203. modelValue = model ? trajectory.data.indexOf(model.data) : -1
  204. }
  205. let assemblyValue = 'deposited'
  206. if (assembly) {
  207. const tags = (assembly as StateObject).tags
  208. if (tags && tags.includes('unitcell')) {
  209. assemblyValue = 'unitcell'
  210. } else if (tags && tags.includes('supercell')) {
  211. assemblyValue = 'supercell'
  212. } else {
  213. assemblyValue = assembly.data.units[0].conformation.operator.assembly.id || 'deposited'
  214. }
  215. }
  216. return {
  217. assembly: assemblyValue,
  218. model: modelValue,
  219. symmetry: 'todo',
  220. colorThemes,
  221. }
  222. }
  223. private findTrajectoryRef() {
  224. const trajectories = this.plugin.state.dataState.select(StateSelection.Generators.rootsOfType(PSO.Molecule.Trajectory))
  225. return trajectories.length > 0 ? trajectories[0].transform.ref : ''
  226. }
  227. componentDidMount() {
  228. this.setState({ trajectoryRef: this.findTrajectoryRef() })
  229. this.subscribe(this.plugin.events.state.object.updated, ({ ref, state }) => {
  230. if (!this.getTrajectory()) {
  231. this.setState({ trajectoryRef: this.findTrajectoryRef() })
  232. } else if (StateTree.subtreeHasRef(state.tree, this.state.trajectoryRef, ref)) {
  233. this.forceUpdate()
  234. }
  235. })
  236. this.subscribe(this.plugin.events.state.object.created, ({ ref, state }) => {
  237. if (!this.getTrajectory()) {
  238. this.setState({ trajectoryRef: this.findTrajectoryRef() })
  239. } else if (StateTree.subtreeHasRef(state.tree, this.state.trajectoryRef, ref)) {
  240. this.forceUpdate()
  241. }
  242. })
  243. this.subscribe(this.plugin.events.state.object.removed, ({ ref, state }) => {
  244. if (!this.getTrajectory()) {
  245. this.setState({ trajectoryRef: this.findTrajectoryRef() })
  246. } else if (StateTree.subtreeHasRef(state.tree, this.state.trajectoryRef, ref)) {
  247. this.forceUpdate()
  248. }
  249. })
  250. }
  251. private getObj<T extends StateObject>(ref: string): T | undefined {
  252. if (!ref) return undefined
  253. const state = this.plugin.state.dataState
  254. const cell = state.select(ref)[0]
  255. if (!cell || !cell.obj) return undefined
  256. return (cell.obj as T)
  257. }
  258. private getTrajectory() {
  259. return this.getObj<PSO.Molecule.Trajectory>(this.state.trajectoryRef)
  260. }
  261. private getModel() {
  262. if (!this.state.trajectoryRef) return
  263. const models = this.plugin.state.dataState.select(StateSelection.Generators.rootsOfType(PSO.Molecule.Model, this.state.trajectoryRef))
  264. return models.length > 0 ? models[0].obj : undefined
  265. }
  266. private getAssembly() {
  267. if (!this.state.trajectoryRef || !this.plugin.state.dataState.transforms.has(this.state.trajectoryRef)) return
  268. const assemblies = this.plugin.state.dataState.select(StateSelection.Generators.rootsOfType(PSO.Molecule.Structure, this.state.trajectoryRef))
  269. return assemblies.length > 0 ? assemblies[0].obj : undefined
  270. }
  271. defaultState() {
  272. return {
  273. isCollapsed: false,
  274. header: 'Structure Settings',
  275. trajectoryRef: '',
  276. } as S
  277. }
  278. renderControls() {
  279. if (!this.plugin.canvas3d) return null
  280. if (!this.getTrajectory() || !this.getAssembly()) return null
  281. return <div>
  282. <ParameterControls params={this.getParams()} values={this.values} onChange={this.onChange} />
  283. </div>
  284. }
  285. }
  286. function modelHasSymmetry(model: Model) {
  287. const mmcif = model.sourceData.data
  288. return (
  289. mmcif.symmetry._rowCount === 1 && mmcif.cell._rowCount === 1 && !(
  290. mmcif.symmetry.Int_Tables_number.value(0) === 1 &&
  291. mmcif.cell.angle_alpha.value(0) === 90 &&
  292. mmcif.cell.angle_beta.value(0) === 90 &&
  293. mmcif.cell.angle_gamma.value(0) === 90 &&
  294. mmcif.cell.length_a.value(0) === 1 &&
  295. mmcif.cell.length_b.value(0) === 1 &&
  296. mmcif.cell.length_c.value(0) === 1
  297. )
  298. )
  299. }