sequence.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import * as React from 'react'
  8. import { PluginUIComponent } from './base';
  9. import { PluginStateObject as PSO } from '../mol-plugin-state/objects';
  10. import { Sequence } from './sequence/sequence';
  11. import { Structure, StructureElement, StructureProperties as SP, Unit } from '../mol-model/structure';
  12. import { SequenceWrapper } from './sequence/wrapper';
  13. import { PolymerSequenceWrapper } from './sequence/polymer';
  14. import { MarkerAction } from '../mol-util/marker-action';
  15. import { PureSelectControl } from './controls/parameters';
  16. import { ParamDefinition as PD } from '../mol-util/param-definition';
  17. import { HeteroSequenceWrapper } from './sequence/hetero';
  18. import { State, StateSelection } from '../mol-state';
  19. import { ChainSequenceWrapper } from './sequence/chain';
  20. import { ElementSequenceWrapper } from './sequence/element';
  21. import { elementLabel } from '../mol-theme/label';
  22. import { Icon } from './controls/icons';
  23. import { StructureSelectionManager } from '../mol-plugin-state/manager/structure/selection';
  24. const MaxDisplaySequenceLength = 5000
  25. function opKey(l: StructureElement.Location) {
  26. const ids = SP.unit.pdbx_struct_oper_list_ids(l)
  27. const ncs = SP.unit.struct_ncs_oper_id(l)
  28. const hkl = SP.unit.hkl(l)
  29. const spgrOp = SP.unit.spgrOp(l)
  30. return `${ids.sort().join(',')}|${ncs}|${hkl}|${spgrOp}`
  31. }
  32. function splitModelEntityId(modelEntityId: string) {
  33. const [ modelIdx, entityId ] = modelEntityId.split('|')
  34. return [ parseInt(modelIdx), entityId ]
  35. }
  36. function getSequenceWrapper(state: SequenceViewState, structureSelection: StructureSelectionManager): SequenceWrapper.Any | string {
  37. const { structure, modelEntityId, chainGroupId, operatorKey } = state
  38. const l = StructureElement.Location.create(structure)
  39. const [ modelIdx, entityId ] = splitModelEntityId(modelEntityId)
  40. const units: Unit[] = []
  41. for (const unit of structure.units) {
  42. StructureElement.Location.set(l, structure, unit, unit.elements[0])
  43. if (structure.getModelIndex(unit.model) !== modelIdx) continue
  44. if (SP.entity.id(l) !== entityId) continue
  45. if (unit.chainGroupId !== chainGroupId) continue
  46. if (opKey(l) !== operatorKey) continue
  47. units.push(unit)
  48. }
  49. if (units.length > 0) {
  50. const data = { structure, units }
  51. const unit = units[0]
  52. let sw: SequenceWrapper<any>
  53. if (unit.polymerElements.length) {
  54. const l = StructureElement.Location.create(structure, unit, unit.elements[0])
  55. const entitySeq = unit.model.sequence.byEntityKey[SP.entity.key(l)]
  56. // check if entity sequence is available
  57. if (entitySeq && entitySeq.sequence.length <= MaxDisplaySequenceLength) {
  58. sw = new PolymerSequenceWrapper(data)
  59. } else {
  60. if (Unit.isAtomic(unit) || unit.polymerElements.length > MaxDisplaySequenceLength) {
  61. sw = new ChainSequenceWrapper(data)
  62. } else {
  63. sw = new ElementSequenceWrapper(data)
  64. }
  65. }
  66. } else if (Unit.isAtomic(unit)) {
  67. if (unit.residueCount > MaxDisplaySequenceLength) {
  68. sw = new ChainSequenceWrapper(data)
  69. } else {
  70. sw = new HeteroSequenceWrapper(data)
  71. }
  72. } else {
  73. console.warn('should not happen, expecting coarse units to be polymeric')
  74. sw = new ChainSequenceWrapper(data)
  75. }
  76. sw.markResidue(structureSelection.getLoci(structure), MarkerAction.Select)
  77. return sw
  78. } else {
  79. return 'No sequence available'
  80. }
  81. }
  82. function getModelEntityOptions(structure: Structure) {
  83. const options: [string, string][] = []
  84. const l = StructureElement.Location.create(structure)
  85. const seen = new Set<string>()
  86. for (const unit of structure.units) {
  87. StructureElement.Location.set(l, structure, unit, unit.elements[0])
  88. const id = SP.entity.id(l)
  89. const modelIdx = structure.getModelIndex(unit.model)
  90. const key = `${modelIdx}|${id}`
  91. if (seen.has(key)) continue
  92. let description = SP.entity.pdbx_description(l).join(', ')
  93. if (structure.models.length) {
  94. if (structure.representativeModel) { // indicates model trajectory
  95. description += ` (Model ${structure.models[modelIdx].modelNum})`
  96. } else if (description.startsWith('Polymer ')) { // indicates generic entity name
  97. description += ` (${structure.models[modelIdx].entry})`
  98. }
  99. }
  100. const label = `${id}: ${description}`
  101. options.push([ key, label ])
  102. seen.add(key)
  103. }
  104. if (options.length === 0) options.push(['', 'No entities'])
  105. return options
  106. }
  107. function getChainOptions(structure: Structure, modelEntityId: string) {
  108. const options: [number, string][] = []
  109. const l = StructureElement.Location.create(structure)
  110. const seen = new Set<number>()
  111. const [ modelIdx, entityId ] = splitModelEntityId(modelEntityId)
  112. for (const unit of structure.units) {
  113. StructureElement.Location.set(l, structure, unit, unit.elements[0])
  114. if (structure.getModelIndex(unit.model) !== modelIdx) continue
  115. if (SP.entity.id(l) !== entityId) continue
  116. const id = unit.chainGroupId
  117. if (seen.has(id)) continue
  118. // TODO handle special case
  119. // - more than one chain in a unit
  120. let label = elementLabel(l, { granularity: 'chain', hidePrefix: true, htmlStyling: false })
  121. options.push([ id, label ])
  122. seen.add(id)
  123. }
  124. if (options.length === 0) options.push([-1, 'No units'])
  125. return options
  126. }
  127. function getOperatorOptions(structure: Structure, modelEntityId: string, chainGroupId: number) {
  128. const options: [string, string][] = []
  129. const l = StructureElement.Location.create(structure)
  130. const seen = new Set<string>()
  131. const [ modelIdx, entityId ] = splitModelEntityId(modelEntityId)
  132. for (const unit of structure.units) {
  133. StructureElement.Location.set(l, structure, unit, unit.elements[0])
  134. if (structure.getModelIndex(unit.model) !== modelIdx) continue
  135. if (SP.entity.id(l) !== entityId) continue
  136. if (unit.chainGroupId !== chainGroupId) continue
  137. const id = opKey(l)
  138. if (seen.has(id)) continue
  139. const label = unit.conformation.operator.name
  140. options.push([ id, label ])
  141. seen.add(id)
  142. }
  143. if (options.length === 0) options.push(['', 'No operators'])
  144. return options
  145. }
  146. function getStructureOptions(state: State) {
  147. const options: [string, string][] = []
  148. const structures = state.select(StateSelection.Generators.rootsOfType(PSO.Molecule.Structure))
  149. for (const s of structures) {
  150. options.push([s.transform.ref, s.obj!.data.label])
  151. }
  152. if (options.length === 0) options.push(['', 'No structure'])
  153. return options
  154. }
  155. type SequenceViewState = {
  156. structure: Structure,
  157. structureRef: string,
  158. modelEntityId: string,
  159. chainGroupId: number,
  160. operatorKey: string
  161. }
  162. export class SequenceView extends PluginUIComponent<{ }, SequenceViewState> {
  163. state = { structure: Structure.Empty, structureRef: '', modelEntityId: '', chainGroupId: -1, operatorKey: '' }
  164. componentDidMount() {
  165. if (this.plugin.state.data.select(StateSelection.Generators.rootsOfType(PSO.Molecule.Structure)).length > 0) this.setState(this.getInitialState())
  166. this.subscribe(this.plugin.events.state.object.updated, ({ ref, obj }) => {
  167. if (ref === this.state.structureRef && obj && obj.type === PSO.Molecule.Structure.type && obj.data !== this.state.structure) {
  168. this.setState(this.getInitialState())
  169. }
  170. });
  171. this.subscribe(this.plugin.events.state.object.created, ({ obj }) => {
  172. if (obj && obj.type === PSO.Molecule.Structure.type) {
  173. this.setState(this.getInitialState())
  174. }
  175. });
  176. this.subscribe(this.plugin.events.state.object.removed, ({ obj }) => {
  177. if (obj && obj.type === PSO.Molecule.Structure.type && obj.data === this.state.structure) {
  178. this.setState(this.getInitialState())
  179. }
  180. });
  181. }
  182. private getStructure(ref: string) {
  183. const state = this.plugin.state.data;
  184. const cell = state.select(ref)[0];
  185. if (!ref || !cell || !cell.obj) return Structure.Empty;
  186. return (cell.obj as PSO.Molecule.Structure).data;
  187. }
  188. private getSequenceWrapper() {
  189. return getSequenceWrapper(this.state, this.plugin.managers.structure.selection)
  190. }
  191. private getInitialState(): SequenceViewState {
  192. const structureRef = getStructureOptions(this.plugin.state.data)[0][0]
  193. const structure = this.getStructure(structureRef)
  194. let modelEntityId = getModelEntityOptions(structure)[0][0]
  195. let chainGroupId = getChainOptions(structure, modelEntityId)[0][0]
  196. let operatorKey = getOperatorOptions(structure, modelEntityId, chainGroupId)[0][0]
  197. if (this.state.structure && this.state.structure === structure) {
  198. modelEntityId = this.state.modelEntityId
  199. chainGroupId = this.state.chainGroupId
  200. operatorKey = this.state.operatorKey
  201. }
  202. return { structure, structureRef, modelEntityId, chainGroupId, operatorKey }
  203. }
  204. private get params() {
  205. const { structure, modelEntityId, chainGroupId } = this.state
  206. const structureOptions = getStructureOptions(this.plugin.state.data)
  207. const entityOptions = getModelEntityOptions(structure)
  208. const chainOptions = getChainOptions(structure, modelEntityId)
  209. const operatorOptions = getOperatorOptions(structure, modelEntityId, chainGroupId)
  210. return {
  211. structure: PD.Select(structureOptions[0][0], structureOptions, { shortLabel: true }),
  212. entity: PD.Select(entityOptions[0][0], entityOptions, { shortLabel: true }),
  213. chain: PD.Select(chainOptions[0][0], chainOptions, { shortLabel: true, twoColumns: true, label: 'Chain' }),
  214. operator: PD.Select(operatorOptions[0][0], operatorOptions, { shortLabel: true, twoColumns: true })
  215. }
  216. }
  217. private get values(): PD.Values<SequenceView['params']> {
  218. return {
  219. structure: this.state.structureRef,
  220. entity: this.state.modelEntityId,
  221. chain: this.state.chainGroupId,
  222. operator: this.state.operatorKey
  223. }
  224. }
  225. private setParamProps = (p: { param: PD.Base<any>, name: string, value: any }) => {
  226. const state = { ...this.state }
  227. switch (p.name) {
  228. case 'structure':
  229. state.structureRef = p.value
  230. state.structure = this.getStructure(p.value)
  231. state.modelEntityId = getModelEntityOptions(state.structure)[0][0]
  232. state.chainGroupId = getChainOptions(state.structure, state.modelEntityId)[0][0]
  233. state.operatorKey = getOperatorOptions(state.structure, state.modelEntityId, state.chainGroupId)[0][0]
  234. break
  235. case 'entity':
  236. state.modelEntityId = p.value
  237. state.chainGroupId = getChainOptions(state.structure, state.modelEntityId)[0][0]
  238. state.operatorKey = getOperatorOptions(state.structure, state.modelEntityId, state.chainGroupId)[0][0]
  239. break
  240. case 'chain':
  241. state.chainGroupId = p.value
  242. state.operatorKey = getOperatorOptions(state.structure, state.modelEntityId, state.chainGroupId)[0][0]
  243. break
  244. case 'operator':
  245. state.operatorKey = p.value
  246. break
  247. }
  248. this.setState(state)
  249. }
  250. render() {
  251. if (this.getStructure(this.state.structureRef) === Structure.Empty) {
  252. return <div className='msp-sequence'>
  253. <div className='msp-sequence-select'>
  254. <Icon name='help-circle' style={{ cursor: 'help', position: 'absolute', right: 0, top: 0 }}
  255. title='This shows a single sequence. Use the controls to show a different sequence.'/>
  256. <span>Sequence</span><span style={{ fontWeight: 'normal' }}>No structure available</span>
  257. </div>
  258. </div>;
  259. }
  260. const sequenceWrapper = this.getSequenceWrapper()
  261. const params = this.params;
  262. const values = this.values;
  263. return <div className='msp-sequence'>
  264. <div className='msp-sequence-select'>
  265. <Icon name='help-circle' style={{ cursor: 'help', position: 'absolute', right: 0, top: 0 }}
  266. title='This shows a single sequence. Use the controls to show a different sequence.' />
  267. <span>Sequence of</span>
  268. <PureSelectControl title={`[Structure] ${PD.optionLabel(params.structure, values.structure)}`} param={params.structure} name='structure' value={values.structure} onChange={this.setParamProps} />
  269. <PureSelectControl title={`[Entity] ${PD.optionLabel(params.entity, values.entity)}`} param={params.entity} name='entity' value={values.entity} onChange={this.setParamProps} />
  270. <PureSelectControl title={`[Chain] ${PD.optionLabel(params.chain, values.chain)}`} param={params.chain} name='chain' value={values.chain} onChange={this.setParamProps} />
  271. {params.operator.options.length > 1 && <>
  272. <PureSelectControl title={`[Instance] ${PD.optionLabel(params.operator, values.operator)}`} param={params.operator} name='operator' value={values.operator} onChange={this.setParamProps} />
  273. </>}
  274. </div>
  275. {typeof sequenceWrapper === 'string'
  276. ? <div className='msp-sequence-wrapper msp-sequence-wrapper-non-empty'>{sequenceWrapper}</div>
  277. : <Sequence sequenceWrapper={sequenceWrapper} />}
  278. </div>;
  279. }
  280. }