structure.tsx 14 KB

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