selection.ts 8.5 KB

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