transformation.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * Copyright (C) 2022, Protein Bioinformatics Research Group, RCNS
  3. *
  4. * Licensed under CC BY-NC 4.0, see LICENSE file for more info.
  5. *
  6. * @author Gabor Tusnady <tusnady.gabor@ttk.hu>
  7. * @author Csongor Gerdan <gerdan.csongor@ttk.hu>
  8. */
  9. import { MolScriptBuilder as MS } from 'molstar/lib/mol-script/language/builder';
  10. import { PluginUIContext } from 'molstar/lib/mol-plugin-ui/context';
  11. import { Mat4, Vec3 } from 'molstar/lib/mol-math/linear-algebra';
  12. import { PDBTMDescriptor, PDBTMTransformationMatrix, PMS } from './types';
  13. import { createStructureRepresentationParams } from 'molstar/lib/mol-plugin-state/helpers/structure-representation-params';
  14. import { StateTransforms } from 'molstar/lib/mol-plugin-state/transforms';
  15. import { StateObjectRef, StateBuilder } from 'molstar/lib/mol-state';
  16. import { Expression } from 'molstar/lib/mol-script/language/expression';
  17. import { MembraneOrientation } from './prop';
  18. import { TmDetColorThemeProvider } from './tmdet-color-theme';
  19. import { DebugUtil } from './debug-utils';
  20. export function applyTransformations(plugin: PluginUIContext, pdbtmDescriptor: PDBTMDescriptor) {
  21. const annotations = pdbtmDescriptor.additional_entry_annotations;
  22. const membraneTransformation = transformationForStateTransform(pdbtmDescriptor.additional_entry_annotations.membrane.transformation_matrix);
  23. if (annotations?.biomatrix?.matrix_list) {
  24. annotations.biomatrix.matrix_list.forEach(function(mx) {
  25. mx.apply_to_chain_list.forEach(function(chainPair) {
  26. let id = chainPair.chain_id;
  27. let newId = chainPair.new_chain_id;
  28. if (annotations.biomatrix.chain_deletes?.includes(newId)) {
  29. DebugUtil.log(`${id} -> ${newId} transformation skipped due to delete rule`);
  30. return;
  31. }
  32. const mtx = transformationForStateTransform(mx.transformation_matrix);
  33. const composedTransformation = Mat4.mul(Mat4(), membraneTransformation, mtx);
  34. chainTransformation(plugin, composedTransformation, id, newId);
  35. // await plugin.runTask(Task.create(`TMDET: Transform '${id}' into '${newId}'`, async () => {
  36. // chainTransformation(plugin, mx.transformation_matrix, id, newId);
  37. // }));
  38. });
  39. });
  40. }
  41. // WARNING: the components cannot be accessed here (created by chainTransformations in the above loop)
  42. // Maybe due to some kind of synchronization behavior.
  43. // plugin.runTask with "await" also cannot synchronize here.
  44. //
  45. // So this function call transforms only already existing components due to a side effect of another issue.
  46. transformWholeModel(plugin, pdbtmDescriptor.additional_entry_annotations.membrane.transformation_matrix);
  47. // await plugin.runTask(Task.create(`TMDET: Apply membrane transformation`, async () => {
  48. // transformWholeModel(plugin, pdbtmDescriptor.additional_entry_annotations.membrane.transformation_matrix);
  49. // }));
  50. }
  51. export function transformWholeModel(plugin: PluginUIContext, membraneMatrix: PDBTMTransformationMatrix) {
  52. //
  53. // membrane transformation
  54. //
  55. const membraneTransformation = transformationForStateTransform(membraneMatrix);
  56. // transform each component
  57. DebugUtil.log("STRUCTURES", getCurrentHierarchy(plugin).structures);
  58. getCurrentHierarchy(plugin).structures[0].components.forEach(component => {
  59. const structure: StateObjectRef<PMS> = component.cell;
  60. const update: StateBuilder.To<any, any> = plugin.build().to(structure);
  61. const label = component.cell.obj!.label.toUpperCase();
  62. DebugUtil.log("memb.transform of", label, component.cell.obj?.id);
  63. DebugUtil.log(`${label} component.cell.obj:`, component.cell.obj);
  64. // DebugUtil.log(`${label} component:`, component);
  65. update
  66. .apply(StateTransforms.Model.TransformStructureConformation, {
  67. "transform": { name: "matrix", params: { data: membraneTransformation, transpose: false } }
  68. });
  69. update.commit();
  70. });
  71. }
  72. /**
  73. * Perform transformation on a chain.
  74. *
  75. * @param plugin UI context
  76. * @param transformationMatrix 4x4 matrix describes transformation and translation
  77. * @param chainId Id of chain to be selected for transformation
  78. */
  79. export function chainTransformation(plugin: PluginUIContext, transformationMatrix: Mat4, chainId: string, newId: string): void {
  80. const query: Expression = getChainExpression(chainId);
  81. // const transformation = transformationForStateTransform(transformationMatrix);
  82. const transformation = transformationMatrix;
  83. const structure: StateObjectRef<PMS> = plugin.managers.structure.hierarchy.current.models[0].structures[0].cell;
  84. const update: StateBuilder.To<any, any> = plugin.build().to(structure);
  85. update
  86. .apply(
  87. StateTransforms.Model.StructureSelectionFromExpression,
  88. { label: newId, expression: query }
  89. )
  90. .apply(StateTransforms.Model.TransformStructureConformation, {
  91. "transform": { name: "matrix", params: { data: transformation, transpose: false } }
  92. })
  93. .apply(
  94. StateTransforms.Representation.StructureRepresentation3D,
  95. createStructureRepresentationParams(plugin, structure.obj?.data, {
  96. type: 'cartoon',
  97. color: TmDetColorThemeProvider.name as any //, colorParams: { pdbtmDescriptor }
  98. })
  99. );
  100. update.commit();
  101. DebugUtil.log(`${chainId}->${newId} DONE`);
  102. }
  103. // function vadd(a: Vec3, b: Vec3): Vec3 {
  104. // return Vec3.add(Vec3.zero(), a, b);
  105. // }
  106. function vneg(u: Vec3): Vec3 {
  107. return Vec3.negate(Vec3.zero(), u);
  108. }
  109. export function createMembraneOrientation(pdbtmDescriptor: PDBTMDescriptor): MembraneOrientation {
  110. const membrane = pdbtmDescriptor.additional_entry_annotations.membrane;
  111. const membraneNormal: Vec3 = Vec3.fromObj(membrane.normal);
  112. const result: MembraneOrientation = {
  113. planePoint1: membraneNormal,
  114. planePoint2: vneg(membraneNormal),
  115. centroid: Vec3.zero(),
  116. normalVector: membraneNormal,
  117. // (NOTE: the TMDET extension calculates and sets it during applying preset)
  118. radius: membrane.radius
  119. };
  120. return result;
  121. }
  122. export function transformationForStateTransform(tmatrix: PDBTMTransformationMatrix): Mat4 {
  123. // matrix expected in column-major order
  124. const mx: Mat4 = Mat4.fromArray(Mat4.zero(),
  125. [
  126. tmatrix.rowx.x, tmatrix.rowy.x, tmatrix.rowz.x, 0,
  127. tmatrix.rowx.y, tmatrix.rowy.y, tmatrix.rowz.y, 0,
  128. tmatrix.rowx.z, tmatrix.rowy.z, tmatrix.rowz.z, 0,
  129. 0, 0, 0, 1
  130. ], 0
  131. );
  132. Mat4.setTranslation(mx, Vec3.create(
  133. tmatrix.rowx.t,
  134. tmatrix.rowy.t,
  135. tmatrix.rowz.t
  136. ));
  137. // TODO: DebugUtil.log('MAT4\n', Mat4.makeTable(mx));
  138. return mx;
  139. }
  140. export function getAtomGroupExpression(chainId: string, auth_array: number[]): Expression {
  141. // TODO DebugUtil.log('auth_array:', auth_array);
  142. const query: Expression =
  143. MS.struct.generator.atomGroups({
  144. 'residue-test': MS.core.set.has([MS.set( ...auth_array ), MS.ammp('auth_seq_id')]),
  145. 'chain-test': MS.core.rel.eq([chainId, MS.ammp('label_asym_id')])
  146. });
  147. return query;
  148. }
  149. export function getChainExpression(chainId: string): Expression {
  150. const query: Expression =
  151. MS.struct.generator.atomGroups({
  152. 'chain-test': MS.core.rel.eq([chainId, MS.ammp('label_asym_id')])
  153. });
  154. return query;
  155. }
  156. export function getCurrentHierarchy(plugin: PluginUIContext) {
  157. return plugin.managers.structure.hierarchy.current;
  158. }