selection.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { MolScriptBuilder as MS } from 'molstar/lib/mol-script/language/builder';
  2. import { StructureSelectionQueries as Q } from 'molstar/lib/mol-plugin-state/helpers/structure-selection-query';
  3. import { StructureRepresentationRegistry } from 'molstar/lib/mol-repr/structure/registry';
  4. import { Expression } from 'molstar/lib/mol-script/language/expression';
  5. import { QueryContext, Structure, StructureElement, StructureSelection } from 'molstar/lib/mol-model/structure';
  6. import { compile } from 'molstar/lib/mol-script/runtime/query/compiler';
  7. export type Range = {
  8. readonly beg: number
  9. readonly end?: number
  10. }
  11. export type Target = {
  12. readonly authSeqId?: number
  13. // readonly authSeqRange?: Range
  14. readonly labelSeqId?: number
  15. readonly labelSeqRange?: Range
  16. readonly labelCompId?: string
  17. // readonly authAsymId?: string
  18. readonly labelAsymId?: string
  19. /**
  20. * Mol*-internal UUID of a model.
  21. */
  22. readonly modelId?: string
  23. /**
  24. * Mol*-internal representation, like 'ASM_2'. Enumerated in the order of appearance in the source file. If
  25. * possible, specify the assemblyId when using this selector.
  26. */
  27. readonly operatorName?: string
  28. /**
  29. * Strucmotif-/BioJava-specific representation, like 'Px42'. This is a single 'pdbx_struct_oper_list.id' value or a
  30. * combination thereof. Specify the assemblyId when using this selector. Order matters, use order as specified in
  31. * the source CIF file.
  32. */
  33. readonly structOperId?: string
  34. /**
  35. * Extend selection to whole chain, by default only the first residue is selected. This is used by the
  36. * oligoInteraction preset in rcsb-sierra, which should focus the whole oligo chain. Not wanted for the
  37. * ligandInteraction preset, which would otherwise focus alternative conformations and symmetry mates.
  38. */
  39. readonly extendToChain?: boolean
  40. }
  41. export type SelectBase = {
  42. readonly modelId: string
  43. readonly labelAsymId: string
  44. readonly operatorName?: string
  45. }
  46. export type SelectSingle = {
  47. readonly labelSeqId: number
  48. } & SelectBase;
  49. export type SelectRange = {
  50. readonly labelSeqRange: Range
  51. } & SelectBase;
  52. export type SelectTarget = SelectSingle | SelectRange;
  53. export type SelectionExpression = {
  54. tag: string
  55. type: StructureRepresentationRegistry.BuiltIn
  56. label: string
  57. expression: Expression
  58. isHidden?: boolean,
  59. color?: number,
  60. alpha?: number
  61. };
  62. /**
  63. * This serves as adapter between the strucmotif-/BioJava-approach to identify transformed chains and the Mol* way.
  64. * Looks for 'structOperId', converts it to an 'operatorName', and removes the original value. This will
  65. * override pre-existing 'operatorName' values.
  66. * @param targets collection to process
  67. * @param structure parent structure
  68. * @param operatorName optional value to which missing operators are set
  69. */
  70. export function normalizeTargets(targets: Target[], structure: Structure, operatorName = undefined): Target[] {
  71. return targets.map(t => {
  72. if (t.structOperId) {
  73. const { structOperId, ...others } = t;
  74. const oper = toOperatorName(structure, structOperId);
  75. return { ...others, operatorName: oper };
  76. }
  77. return t.operatorName ? t : { ...t, operatorName };
  78. });
  79. }
  80. function toOperatorName(structure: Structure, expression: string): string {
  81. function join(opers: any[]) {
  82. // this makes the assumptions that '1' is the identity operator
  83. if (!opers || !opers.length) return '1';
  84. if (opers.length > 1) {
  85. // Mol* operators are right-to-left
  86. return opers[1] + 'x' + opers[0];
  87. }
  88. return opers[0];
  89. }
  90. for (const unit of structure.units) {
  91. const assembly = unit.conformation.operator.assembly;
  92. if (!assembly) continue;
  93. if (expression === join(assembly.operList)) return `ASM_${assembly.operId}`;
  94. }
  95. // TODO better error handling?
  96. throw Error(`Unable to find expression '${expression}'`);
  97. }
  98. /**
  99. * Convert a selection to an array of selection expressions.
  100. * @param labelBase the base label that will appear in the UI (e.g., the entry ID)
  101. * @param selection a selection by Range or a set of Targets
  102. */
  103. export function createSelectionExpressions(labelBase: string, selection?: Target | Target[]): SelectionExpression[] {
  104. if (selection) {
  105. if ('labelAsymId' in selection) {
  106. const target = selection as Target;
  107. const residues: number[] = (target.labelSeqRange) ? toRange(target.labelSeqRange!.beg, target.labelSeqRange!.end) : [];
  108. const test = rangeToTest(target.labelAsymId!, residues);
  109. const label = labelFromProps(labelBase, target.labelAsymId, residues);
  110. return [{
  111. expression: MS.struct.generator.atomGroups(test),
  112. label: `${label}`,
  113. type: 'cartoon',
  114. tag: 'polymer'
  115. }];
  116. } else if (Array.isArray(selection)) {
  117. const expression = targetsToExpression(selection);
  118. return [{
  119. expression: expression,
  120. label: `${labelBase}`,
  121. type: 'ball-and-stick',
  122. tag: 'polymer'
  123. }];
  124. } else {
  125. throw Error('Unable to handle selection: ' + selection);
  126. }
  127. } else {
  128. return [
  129. {
  130. expression: Q.polymer.expression,
  131. label: `${labelBase} - Polymers`,
  132. type: 'cartoon',
  133. tag: 'polymer'
  134. },
  135. {
  136. expression: Q.ligand.expression,
  137. label: `${labelBase} - Ligands`,
  138. type: 'ball-and-stick',
  139. tag: 'ligand'
  140. },
  141. {
  142. expression: Q.ion.expression,
  143. label: `${labelBase} - Ions`,
  144. type: 'ball-and-stick',
  145. tag: 'ion'
  146. },
  147. {
  148. expression: Q.branched.expression,
  149. label: `${labelBase} - Carbohydrates`,
  150. type: 'carbohydrate',
  151. tag: 'branched-snfg-3d'
  152. },
  153. {
  154. expression: Q.lipid.expression,
  155. label: `${labelBase} - Lipids`,
  156. type: 'ball-and-stick',
  157. tag: 'lipid'
  158. },
  159. {
  160. expression: Q.water.expression,
  161. label: `${labelBase} - Waters`,
  162. type: 'ball-and-stick',
  163. tag: 'water'
  164. }
  165. ];
  166. }
  167. }
  168. export const toRange = (start: number, end?: number) => {
  169. if (!end) return [start];
  170. const b = start < end ? start : end;
  171. const e = start < end ? end : start;
  172. return [...Array(e - b + 1)].map((_, i) => b + i);
  173. };
  174. const labelFromProps = (entryId: string, labelAsymId?: string, range?: number[]) => {
  175. return entryId + (labelAsymId ? `.${labelAsymId}` : '') +
  176. (range && range.length > 0 ? `:${range[0]}` : '') +
  177. (range && range.length > 1 ? `-${range[range.length - 1]}` : '');
  178. };
  179. export function rangeToTest(asymId: string, residues: number[], operatorName?: string) {
  180. const chainTests: Expression[] = [MS.core.rel.eq([MS.ammp('label_asym_id'), asymId])];
  181. if (operatorName)
  182. chainTests.push(MS.core.rel.eq([operatorName, MS.acp('operatorName')]));
  183. if (residues.length > 0) {
  184. return {
  185. 'chain-test': MS.core.logic.and(chainTests),
  186. 'residue-test': MS.core.set.has([MS.set(...residues), MS.ammp('label_seq_id')])
  187. };
  188. } else {
  189. return { 'chain-test': MS.core.logic.and(chainTests) };
  190. }
  191. }
  192. export function targetToLoci(target: Target, structure: Structure): StructureElement.Loci {
  193. const expression = targetToExpression(target);
  194. const query = compile<StructureSelection>(expression);
  195. const selection = query(new QueryContext(structure));
  196. return StructureSelection.toLociWithSourceUnits(selection);
  197. }
  198. function targetsToExpression(targets: Target[]): Expression {
  199. const expressions = targets.map(t => targetToExpression(t));
  200. return MS.struct.combinator.merge(expressions);
  201. }
  202. function targetToExpression(target: Target): Expression {
  203. const residueTests: Expression[] = [];
  204. const chainTests: Expression[] = [];
  205. const tests: { 'residue-test': Expression, 'chain-test': Expression } = Object.create(null);
  206. if (target.authSeqId) {
  207. residueTests.push(MS.core.rel.eq([target.authSeqId, MS.ammp('auth_seq_id')]));
  208. } else if (target.labelSeqId) {
  209. residueTests.push(MS.core.rel.eq([target.labelSeqId, MS.ammp('label_seq_id')]));
  210. } else if (target.labelSeqRange) {
  211. residueTests.push(MS.core.rel.inRange([MS.ammp('label_seq_id'), target.labelSeqRange.beg, target.labelSeqRange.end ?? target.labelSeqRange.beg]));
  212. }
  213. if (target.labelCompId) {
  214. residueTests.push(MS.core.rel.eq([target.labelCompId, MS.ammp('label_comp_id')]));
  215. }
  216. if (residueTests.length === 1) {
  217. tests['residue-test'] = residueTests[0];
  218. } else if (residueTests.length > 1) {
  219. tests['residue-test'] = MS.core.logic.and(residueTests);
  220. }
  221. if (target.labelAsymId) {
  222. chainTests.push(MS.core.rel.eq([target.labelAsymId, MS.ammp('label_asym_id')]));
  223. }
  224. if (target.operatorName) {
  225. chainTests.push(MS.core.rel.eq([target.operatorName, MS.acp('operatorName')]));
  226. }
  227. if (chainTests.length === 1) {
  228. tests['chain-test'] = chainTests[0];
  229. } else if (chainTests.length > 1) {
  230. tests['chain-test'] = MS.core.logic.and(chainTests);
  231. }
  232. if (Object.keys(tests).length > 0) {
  233. return MS.struct.modifier.union([
  234. MS.struct.generator.atomGroups(tests)
  235. ]);
  236. } else {
  237. return MS.struct.generator.empty;
  238. }
  239. }