transformation.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 { Mat4, Quat, Vec3 } from 'molstar/lib/mol-math/linear-algebra';
  11. import { PDBTMDescriptor, PDBTMTransformationMatrix, PMS } from './types';
  12. import { createStructureRepresentationParams } from 'molstar/lib/mol-plugin-state/helpers/structure-representation-params';
  13. import { StateTransforms } from 'molstar/lib/mol-plugin-state/transforms';
  14. import { StateObjectRef, StateBuilder } from 'molstar/lib/mol-state';
  15. import { Expression } from 'molstar/lib/mol-script/language/expression';
  16. import { MembraneOrientation } from './prop';
  17. import { TmDetColorThemeProvider } from './tmdet-color-theme';
  18. import { DebugUtil } from './debug-utils';
  19. import { PluginContext } from 'molstar/lib/mol-plugin/context';
  20. export function applyTransformations(plugin: PluginContext, 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: PluginContext, 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: PluginContext, 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 vneg(u: Vec3): Vec3 {
  104. return Vec3.negate(Vec3.zero(), u);
  105. }
  106. export function createMembraneOrientation(pdbtmDescriptor: PDBTMDescriptor): MembraneOrientation {
  107. const membrane = pdbtmDescriptor.additional_entry_annotations.membrane;
  108. let membraneNormal: Vec3 = Vec3.fromObj(membrane.normal);
  109. const result: MembraneOrientation = {
  110. planePoint1: membraneNormal,
  111. planePoint2: vneg(membraneNormal),
  112. centroid: Vec3.zero(),
  113. normalVector: membraneNormal,
  114. radius: membrane.radius
  115. };
  116. return result;
  117. }
  118. export function transformationForStateTransform(tmatrix: PDBTMTransformationMatrix): Mat4 {
  119. // matrix expected in column-major order
  120. const mx: Mat4 = Mat4.fromArray(Mat4.zero(),
  121. [
  122. tmatrix.rowx.x, tmatrix.rowy.x, tmatrix.rowz.x, 0,
  123. tmatrix.rowx.y, tmatrix.rowy.y, tmatrix.rowz.y, 0,
  124. tmatrix.rowx.z, tmatrix.rowy.z, tmatrix.rowz.z, 0,
  125. 0, 0, 0, 1
  126. ], 0
  127. );
  128. Mat4.setTranslation(mx, Vec3.create(
  129. tmatrix.rowx.t,
  130. tmatrix.rowy.t,
  131. tmatrix.rowz.t
  132. ));
  133. // TODO: DebugUtil.log('MAT4\n', Mat4.makeTable(mx));
  134. return mx;
  135. }
  136. export function getAtomGroupExpression(chainId: string, auth_array: number[]): Expression {
  137. // TODO DebugUtil.log('auth_array:', auth_array);
  138. const query: Expression =
  139. MS.struct.generator.atomGroups({
  140. 'residue-test': MS.core.set.has([MS.set( ...auth_array ), MS.ammp('auth_seq_id')]),
  141. 'chain-test': MS.core.rel.eq([chainId, MS.ammp('label_asym_id')])
  142. });
  143. return query;
  144. }
  145. export function getChainExpression(chainId: string): Expression {
  146. const query: Expression =
  147. MS.struct.generator.atomGroups({
  148. 'chain-test': MS.core.rel.eq([chainId, MS.ammp('label_asym_id')])
  149. });
  150. return query;
  151. }
  152. export function getCurrentHierarchy(plugin: PluginContext) {
  153. return plugin.managers.structure.hierarchy.current;
  154. }