sequence.tsx 14 KB

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