selection.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. };
  61. /**
  62. * This serves as adapter between the strucmotif-/BioJava-approach to identify transformed chains and the Mol* way.
  63. * Looks for 'structOperId', converts it to an 'operatorName', and removes the original value. This will
  64. * override pre-existing 'operatorName' values.
  65. * @param targets collection to process
  66. * @param structure parent structure
  67. * @param operatorName optional value to which missing operators are set
  68. */
  69. export function normalizeTargets(targets: Target[], structure: Structure, operatorName = undefined): Target[] {
  70. return targets.map(t => {
  71. if (t.structOperId) {
  72. const { structOperId, ...others } = t;
  73. const oper = toOperatorName(structure, structOperId);
  74. return { ...others, operatorName: oper };
  75. }
  76. return t.operatorName ? t : { ...t, operatorName };
  77. });
  78. }
  79. function toOperatorName(structure: Structure, expression: string): string {
  80. function join(opers: any[]) {
  81. // this makes the assumptions that '1' is the identity operator
  82. if (!opers || !opers.length) return '1';
  83. if (opers.length > 1) {
  84. // Mol* operators are right-to-left
  85. return opers[1] + 'x' + opers[0];
  86. }
  87. return opers[0];
  88. }
  89. for (const unit of structure.units) {
  90. const assembly = unit.conformation.operator.assembly;
  91. if (!assembly) continue;
  92. if (expression === join(assembly.operList)) return `ASM_${assembly.operId}`;
  93. }
  94. // TODO better error handling?
  95. throw Error(`Unable to find expression '${expression}'`);
  96. }
  97. /**
  98. * Convert a selection to an array of selection expressions.
  99. * @param labelBase the base label that will appear in the UI (e.g., the entry ID)
  100. * @param selection a selection by Range or a set of Targets
  101. */
  102. export function createSelectionExpressions(labelBase: string, selection?: Target | Target[]): SelectionExpression[] {
  103. if (selection) {
  104. if ('labelAsymId' in selection) {
  105. const target = selection as Target;
  106. const residues: number[] = (target.labelSeqRange) ? toRange(target.labelSeqRange!.beg, target.labelSeqRange!.end) : [];
  107. const test = rangeToTest(target.labelAsymId!, residues);
  108. const label = labelFromProps(labelBase, target.labelAsymId, residues);
  109. return [{
  110. expression: MS.struct.generator.atomGroups(test),
  111. label: `${label}`,
  112. type: 'cartoon',
  113. tag: 'polymer'
  114. }];
  115. } else if (Array.isArray(selection)) {
  116. const expression = targetsToExpression(selection);
  117. return [{
  118. expression: expression,
  119. label: `${labelBase}`,
  120. type: 'ball-and-stick',
  121. tag: 'polymer'
  122. }];
  123. } else {
  124. throw Error('Unable to handle selection: ' + selection);
  125. }
  126. } else {
  127. return [
  128. {
  129. expression: Q.polymer.expression,
  130. label: `${labelBase} - Polymers`,
  131. type: 'cartoon',
  132. tag: 'polymer'
  133. },
  134. {
  135. expression: Q.ligand.expression,
  136. label: `${labelBase} - Ligands`,
  137. type: 'ball-and-stick',
  138. tag: 'ligand'
  139. },
  140. {
  141. expression: Q.ion.expression,
  142. label: `${labelBase} - Ions`,
  143. type: 'ball-and-stick',
  144. tag: 'ion'
  145. },
  146. {
  147. expression: Q.branched.expression,
  148. label: `${labelBase} - Carbohydrates`,
  149. type: 'carbohydrate',
  150. tag: 'branched-snfg-3d'
  151. },
  152. {
  153. expression: Q.lipid.expression,
  154. label: `${labelBase} - Lipids`,
  155. type: 'ball-and-stick',
  156. tag: 'lipid'
  157. },
  158. {
  159. expression: Q.water.expression,
  160. label: `${labelBase} - Waters`,
  161. type: 'ball-and-stick',
  162. tag: 'water'
  163. }
  164. ];
  165. }
  166. }
  167. export const toRange = (start: number, end?: number) => {
  168. if (!end) return [start];
  169. const b = start < end ? start : end;
  170. const e = start < end ? end : start;
  171. return [...Array(e - b + 1)].map((_, i) => b + i);
  172. };
  173. const labelFromProps = (entryId: string, labelAsymId?: string, range?: number[]) => {
  174. return entryId + (labelAsymId ? `.${labelAsymId}` : '') +
  175. (range && range.length > 0 ? `:${range[0]}` : '') +
  176. (range && range.length > 1 ? `-${range[range.length - 1]}` : '');
  177. };
  178. export function rangeToTest(asymId: string, residues: number[], operatorName?: string) {
  179. const chainTests: Expression[] = [MS.core.rel.eq([MS.ammp('label_asym_id'), asymId])];
  180. if(operatorName)
  181. chainTests.push(MS.core.rel.eq([operatorName, MS.acp('operatorName')]));
  182. if (residues.length > 0) {
  183. return {
  184. 'chain-test': MS.core.logic.and(chainTests),
  185. 'residue-test': MS.core.set.has([MS.set(...residues), MS.ammp('label_seq_id')])
  186. };
  187. } else {
  188. return { 'chain-test': MS.core.logic.and(chainTests) };
  189. }
  190. }
  191. export function targetToLoci(target: Target, structure: Structure): StructureElement.Loci {
  192. const expression = targetToExpression(target);
  193. const query = compile<StructureSelection>(expression);
  194. const selection = query(new QueryContext(structure));
  195. return StructureSelection.toLociWithSourceUnits(selection);
  196. }
  197. function targetsToExpression(targets: Target[]): Expression {
  198. const expressions = targets.map(t => targetToExpression(t));
  199. return MS.struct.combinator.merge(expressions);
  200. }
  201. function targetToExpression(target: Target): Expression {
  202. const residueTests: Expression[] = [];
  203. const chainTests: Expression[] = [];
  204. const tests: { 'residue-test': Expression, 'chain-test': Expression } = Object.create(null);
  205. if (target.authSeqId) {
  206. residueTests.push(MS.core.rel.eq([target.authSeqId, MS.ammp('auth_seq_id')]));
  207. } else if (target.labelSeqId) {
  208. residueTests.push(MS.core.rel.eq([target.labelSeqId, MS.ammp('label_seq_id')]));
  209. } else if (target.labelSeqRange) {
  210. residueTests.push(MS.core.rel.inRange([MS.ammp('label_seq_id'), target.labelSeqRange.beg, target.labelSeqRange.end ?? target.labelSeqRange.beg]));
  211. }
  212. if (target.labelCompId) {
  213. residueTests.push(MS.core.rel.eq([target.labelCompId, MS.ammp('label_comp_id')]));
  214. }
  215. if (residueTests.length === 1) {
  216. tests['residue-test'] = residueTests[0];
  217. } else if (residueTests.length > 1) {
  218. tests['residue-test'] = MS.core.logic.and(residueTests);
  219. }
  220. if (target.labelAsymId) {
  221. chainTests.push(MS.core.rel.eq([target.labelAsymId, MS.ammp('label_asym_id')]));
  222. }
  223. if (target.operatorName) {
  224. chainTests.push(MS.core.rel.eq([target.operatorName, MS.acp('operatorName')]));
  225. }
  226. if (chainTests.length === 1) {
  227. tests['chain-test'] = chainTests[0];
  228. } else if (chainTests.length > 1) {
  229. tests['chain-test'] = MS.core.logic.and(chainTests);
  230. }
  231. if (Object.keys(tests).length > 0) {
  232. return MS.struct.modifier.union([
  233. MS.struct.generator.atomGroups(tests)
  234. ]);
  235. } else {
  236. return MS.struct.generator.empty;
  237. }
  238. }