sequence.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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, HelpOutlineSvg } from './controls/icons';
  23. import { StructureSelectionManager } from '../mol-plugin-state/manager/structure/selection';
  24. import { arrayEqual } from '../mol-util/array';
  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: { structure: Structure, modelEntityId: string, chainGroupId: number, operatorKey: string }, 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, polymersOnly = false) {
  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. if (polymersOnly && SP.entity.type(l) !== 'polymer') continue;
  96. let description = SP.entity.pdbx_description(l).join(', ');
  97. if (structure.models.length) {
  98. if (structure.representativeModel) { // indicates model trajectory
  99. description += ` (Model ${structure.models[modelIdx].modelNum})`;
  100. } else if (description.startsWith('Polymer ')) { // indicates generic entity name
  101. description += ` (${structure.models[modelIdx].entry})`;
  102. }
  103. }
  104. const label = `${id}: ${description}`;
  105. options.push([key, label]);
  106. seen.add(key);
  107. }
  108. if (options.length === 0) options.push(['', 'No entities']);
  109. return options;
  110. }
  111. function getChainOptions(structure: Structure, modelEntityId: string) {
  112. const options: [number, string][] = [];
  113. const l = StructureElement.Location.create(structure);
  114. const seen = new Set<number>();
  115. const [modelIdx, entityId] = splitModelEntityId(modelEntityId);
  116. for (const unit of structure.units) {
  117. StructureElement.Location.set(l, structure, unit, unit.elements[0]);
  118. if (structure.getModelIndex(unit.model) !== modelIdx) continue;
  119. if (SP.entity.id(l) !== entityId) continue;
  120. const id = unit.chainGroupId;
  121. if (seen.has(id)) continue;
  122. // TODO handle special case
  123. // - more than one chain in a unit
  124. let label = elementLabel(l, { granularity: 'chain', hidePrefix: true, htmlStyling: false });
  125. options.push([id, label]);
  126. seen.add(id);
  127. }
  128. if (options.length === 0) options.push([-1, 'No units']);
  129. return options;
  130. }
  131. function getOperatorOptions(structure: Structure, modelEntityId: string, chainGroupId: number) {
  132. const options: [string, string][] = [];
  133. const l = StructureElement.Location.create(structure);
  134. const seen = new Set<string>();
  135. const [modelIdx, entityId] = splitModelEntityId(modelEntityId);
  136. for (const unit of structure.units) {
  137. StructureElement.Location.set(l, structure, unit, unit.elements[0]);
  138. if (structure.getModelIndex(unit.model) !== modelIdx) continue;
  139. if (SP.entity.id(l) !== entityId) continue;
  140. if (unit.chainGroupId !== chainGroupId) continue;
  141. const id = opKey(l);
  142. if (seen.has(id)) continue;
  143. const label = unit.conformation.operator.name;
  144. options.push([id, label]);
  145. seen.add(id);
  146. }
  147. if (options.length === 0) options.push(['', 'No operators']);
  148. return options;
  149. }
  150. function getStructureOptions(state: State) {
  151. const options: [string, string][] = [];
  152. const all: Structure[] = [];
  153. const structures = state.select(StateSelection.Generators.rootsOfType(PSO.Molecule.Structure));
  154. for (const s of structures) {
  155. if (!s.obj?.data) continue;
  156. all.push(s.obj.data);
  157. options.push([s.transform.ref, s.obj!.data.label]);
  158. }
  159. if (options.length === 0) options.push(['', 'No structure']);
  160. return { options, all };
  161. }
  162. export type SequenceViewMode = 'single' | 'polymers' | 'all'
  163. const SequenceViewModeParam = PD.Select<SequenceViewMode>('single', [['single', 'Chain'], ['polymers', 'Polymers'], ['all', 'Everything']]);
  164. type SequenceViewState = {
  165. structureOptions: { options: [string, string][], all: Structure[] },
  166. structure: Structure,
  167. structureRef: string,
  168. modelEntityId: string,
  169. chainGroupId: number,
  170. operatorKey: string,
  171. mode: SequenceViewMode
  172. }
  173. export class SequenceView extends PluginUIComponent<{ defaultMode?: SequenceViewMode }, SequenceViewState> {
  174. state: SequenceViewState = { structureOptions: { options: [], all: [] }, structure: Structure.Empty, structureRef: '', modelEntityId: '', chainGroupId: -1, operatorKey: '', mode: 'single' }
  175. componentDidMount() {
  176. if (this.plugin.state.data.select(StateSelection.Generators.rootsOfType(PSO.Molecule.Structure)).length > 0) this.setState(this.getInitialState());
  177. this.subscribe(this.plugin.state.events.object.updated, ({ ref, obj }) => {
  178. if (ref === this.state.structureRef && obj && obj.type === PSO.Molecule.Structure.type && obj.data !== this.state.structure) {
  179. this.sync();
  180. }
  181. });
  182. this.subscribe(this.plugin.state.events.object.created, ({ obj }) => {
  183. if (obj && obj.type === PSO.Molecule.Structure.type) {
  184. this.sync();
  185. }
  186. });
  187. this.subscribe(this.plugin.state.events.object.removed, ({ obj }) => {
  188. if (obj && obj.type === PSO.Molecule.Structure.type && obj.data === this.state.structure) {
  189. this.sync();
  190. }
  191. });
  192. }
  193. private sync() {
  194. const structureOptions = getStructureOptions(this.plugin.state.data);
  195. if (arrayEqual(structureOptions.all, this.state.structureOptions.all)) return;
  196. this.setState(this.getInitialState());
  197. }
  198. private getStructure(ref: string) {
  199. const state = this.plugin.state.data;
  200. const cell = state.select(ref)[0];
  201. if (!ref || !cell || !cell.obj) return Structure.Empty;
  202. return (cell.obj as PSO.Molecule.Structure).data;
  203. }
  204. private getSequenceWrapper(params: SequenceView['params']) {
  205. return {
  206. wrapper: getSequenceWrapper(this.state, this.plugin.managers.structure.selection),
  207. label: `${PD.optionLabel(params.chain, this.state.chainGroupId)} | ${PD.optionLabel(params.entity, this.state.modelEntityId)}`
  208. };
  209. }
  210. private getSequenceWrappers(params: SequenceView['params']) {
  211. if (this.state.mode === 'single') return [this.getSequenceWrapper(params)];
  212. const structure = this.getStructure(this.state.structureRef);
  213. const wrappers: { wrapper: (string | SequenceWrapper.Any), label: string }[] = [];
  214. for (const [modelEntityId, eLabel] of getModelEntityOptions(structure, this.state.mode === 'polymers')) {
  215. for (const [chainGroupId, cLabel] of getChainOptions(structure, modelEntityId)) {
  216. for (const [operatorKey] of getOperatorOptions(structure, modelEntityId, chainGroupId)) {
  217. wrappers.push({
  218. wrapper: getSequenceWrapper({
  219. structure,
  220. modelEntityId,
  221. chainGroupId,
  222. operatorKey
  223. }, this.plugin.managers.structure.selection),
  224. label: `${cLabel} | ${eLabel}`
  225. });
  226. }
  227. }
  228. }
  229. return wrappers;
  230. }
  231. private getInitialState(): SequenceViewState {
  232. const structureOptions = getStructureOptions(this.plugin.state.data);
  233. const structureRef = structureOptions.options[0][0];
  234. const structure = this.getStructure(structureRef);
  235. let modelEntityId = getModelEntityOptions(structure)[0][0];
  236. let chainGroupId = getChainOptions(structure, modelEntityId)[0][0];
  237. let operatorKey = getOperatorOptions(structure, modelEntityId, chainGroupId)[0][0];
  238. if (this.state.structure && this.state.structure === structure) {
  239. modelEntityId = this.state.modelEntityId;
  240. chainGroupId = this.state.chainGroupId;
  241. operatorKey = this.state.operatorKey;
  242. }
  243. return { structureOptions, structure, structureRef, modelEntityId, chainGroupId, operatorKey, mode: this.props.defaultMode ?? 'single' };
  244. }
  245. private get params() {
  246. const { structureOptions, structure, modelEntityId, chainGroupId } = this.state;
  247. const entityOptions = getModelEntityOptions(structure);
  248. const chainOptions = getChainOptions(structure, modelEntityId);
  249. const operatorOptions = getOperatorOptions(structure, modelEntityId, chainGroupId);
  250. return {
  251. structure: PD.Select(structureOptions.options[0][0], structureOptions.options, { shortLabel: true }),
  252. entity: PD.Select(entityOptions[0][0], entityOptions, { shortLabel: true }),
  253. chain: PD.Select(chainOptions[0][0], chainOptions, { shortLabel: true, twoColumns: true, label: 'Chain' }),
  254. operator: PD.Select(operatorOptions[0][0], operatorOptions, { shortLabel: true, twoColumns: true }),
  255. mode: SequenceViewModeParam
  256. };
  257. }
  258. private get values(): PD.Values<SequenceView['params']> {
  259. return {
  260. structure: this.state.structureRef,
  261. entity: this.state.modelEntityId,
  262. chain: this.state.chainGroupId,
  263. operator: this.state.operatorKey,
  264. mode: this.state.mode
  265. };
  266. }
  267. private setParamProps = (p: { param: PD.Base<any>, name: string, value: any }) => {
  268. const state = { ...this.state };
  269. switch (p.name) {
  270. case 'mode':
  271. state.mode = p.value;
  272. if (this.state.mode === state.mode) return;
  273. if (state.mode === 'all' || state.mode === 'polymers') {
  274. break;
  275. }
  276. case 'structure':
  277. if (p.name === 'structure') state.structureRef = p.value;
  278. state.structure = this.getStructure(state.structureRef);
  279. state.modelEntityId = getModelEntityOptions(state.structure)[0][0];
  280. state.chainGroupId = getChainOptions(state.structure, state.modelEntityId)[0][0];
  281. state.operatorKey = getOperatorOptions(state.structure, state.modelEntityId, state.chainGroupId)[0][0];
  282. break;
  283. case 'entity':
  284. state.modelEntityId = p.value;
  285. state.chainGroupId = getChainOptions(state.structure, state.modelEntityId)[0][0];
  286. state.operatorKey = getOperatorOptions(state.structure, state.modelEntityId, state.chainGroupId)[0][0];
  287. break;
  288. case 'chain':
  289. state.chainGroupId = p.value;
  290. state.operatorKey = getOperatorOptions(state.structure, state.modelEntityId, state.chainGroupId)[0][0];
  291. break;
  292. case 'operator':
  293. state.operatorKey = p.value;
  294. break;
  295. }
  296. this.setState(state);
  297. }
  298. render() {
  299. if (this.getStructure(this.state.structureRef) === Structure.Empty) {
  300. return <div className='msp-sequence'>
  301. <div className='msp-sequence-select'>
  302. <Icon svg={HelpOutlineSvg} style={{ cursor: 'help', position: 'absolute', right: 0, top: 0 }}
  303. title='Shows a sequence of one or more chains. Use the controls to alter selection.' />
  304. <span>Sequence</span><span style={{ fontWeight: 'normal' }}>No structure available</span>
  305. </div>
  306. </div>;
  307. }
  308. const params = this.params;
  309. const values = this.values;
  310. const sequenceWrappers = this.getSequenceWrappers(params);
  311. return <div className='msp-sequence'>
  312. <div className='msp-sequence-select'>
  313. <Icon svg={HelpOutlineSvg} style={{ cursor: 'help', position: 'absolute', right: 0, top: 0 }}
  314. title='This shows a single sequence. Use the controls to show a different sequence.' />
  315. <span>Sequence of</span>
  316. <PureSelectControl title={`[Structure] ${PD.optionLabel(params.structure, values.structure)}`} param={params.structure} name='structure' value={values.structure} onChange={this.setParamProps} />
  317. <PureSelectControl title={`[Mode]`} param={SequenceViewModeParam} name='mode' value={values.mode} onChange={this.setParamProps} />
  318. {values.mode === 'single' && <PureSelectControl title={`[Entity] ${PD.optionLabel(params.entity, values.entity)}`} param={params.entity} name='entity' value={values.entity} onChange={this.setParamProps} />}
  319. {values.mode === 'single' && <PureSelectControl title={`[Chain] ${PD.optionLabel(params.chain, values.chain)}`} param={params.chain} name='chain' value={values.chain} onChange={this.setParamProps} />}
  320. {params.operator.options.length > 1 && <>
  321. <PureSelectControl title={`[Instance] ${PD.optionLabel(params.operator, values.operator)}`} param={params.operator} name='operator' value={values.operator} onChange={this.setParamProps} />
  322. </>}
  323. </div>
  324. <NonEmptySequenceWrapper>
  325. {sequenceWrappers.map((s, i) => {
  326. const elem = typeof s.wrapper === 'string'
  327. ? <div key={i} className='msp-sequence-wrapper'>{s.wrapper}</div>
  328. : <Sequence key={i} sequenceWrapper={s.wrapper} />;
  329. if (values.mode === 'single') return elem;
  330. return <React.Fragment key={i}>
  331. <div className='msp-sequence-chain-label'>{s.label}</div>
  332. {elem}
  333. </React.Fragment>;
  334. })}
  335. </NonEmptySequenceWrapper>
  336. </div>;
  337. }
  338. }
  339. function NonEmptySequenceWrapper({ children }: { children: React.ReactNode }) {
  340. return <div className='msp-sequence-wrapper-non-empty'>
  341. {children}
  342. </div>;
  343. }