structure.tsx 14 KB

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