biotite 0.39.0__cp312-cp312-win_amd64.whl → 0.41.0__cp312-cp312-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of biotite might be problematic. Click here for more details.

Files changed (121) hide show
  1. biotite/__init__.py +3 -3
  2. biotite/application/dssp/app.py +18 -18
  3. biotite/database/pubchem/download.py +23 -23
  4. biotite/database/pubchem/query.py +7 -7
  5. biotite/database/rcsb/download.py +19 -14
  6. biotite/file.py +17 -9
  7. biotite/sequence/align/banded.c +256 -235
  8. biotite/sequence/align/banded.cp312-win_amd64.pyd +0 -0
  9. biotite/sequence/align/cigar.py +60 -15
  10. biotite/sequence/align/kmeralphabet.c +241 -220
  11. biotite/sequence/align/kmeralphabet.cp312-win_amd64.pyd +0 -0
  12. biotite/sequence/align/kmersimilarity.c +213 -194
  13. biotite/sequence/align/kmersimilarity.cp312-win_amd64.pyd +0 -0
  14. biotite/sequence/align/kmertable.cp312-win_amd64.pyd +0 -0
  15. biotite/sequence/align/kmertable.cpp +231 -203
  16. biotite/sequence/align/localgapped.c +256 -235
  17. biotite/sequence/align/localgapped.cp312-win_amd64.pyd +0 -0
  18. biotite/sequence/align/localungapped.c +233 -212
  19. biotite/sequence/align/localungapped.cp312-win_amd64.pyd +0 -0
  20. biotite/sequence/align/multiple.c +253 -232
  21. biotite/sequence/align/multiple.cp312-win_amd64.pyd +0 -0
  22. biotite/sequence/align/pairwise.c +272 -251
  23. biotite/sequence/align/pairwise.cp312-win_amd64.pyd +0 -0
  24. biotite/sequence/align/permutation.c +213 -194
  25. biotite/sequence/align/permutation.cp312-win_amd64.pyd +0 -0
  26. biotite/sequence/align/selector.c +215 -195
  27. biotite/sequence/align/selector.cp312-win_amd64.pyd +0 -0
  28. biotite/sequence/align/tracetable.c +213 -193
  29. biotite/sequence/align/tracetable.cp312-win_amd64.pyd +0 -0
  30. biotite/sequence/annotation.py +2 -2
  31. biotite/sequence/codec.c +233 -212
  32. biotite/sequence/codec.cp312-win_amd64.pyd +0 -0
  33. biotite/sequence/io/fasta/convert.py +27 -24
  34. biotite/sequence/phylo/nj.c +213 -194
  35. biotite/sequence/phylo/nj.cp312-win_amd64.pyd +0 -0
  36. biotite/sequence/phylo/tree.c +225 -200
  37. biotite/sequence/phylo/tree.cp312-win_amd64.pyd +0 -0
  38. biotite/sequence/phylo/upgma.c +213 -194
  39. biotite/sequence/phylo/upgma.cp312-win_amd64.pyd +0 -0
  40. biotite/structure/__init__.py +2 -0
  41. biotite/structure/basepairs.py +7 -12
  42. biotite/structure/bonds.c +1435 -1277
  43. biotite/structure/bonds.cp312-win_amd64.pyd +0 -0
  44. biotite/structure/celllist.c +215 -195
  45. biotite/structure/celllist.cp312-win_amd64.pyd +0 -0
  46. biotite/structure/charges.c +1050 -1099
  47. biotite/structure/charges.cp312-win_amd64.pyd +0 -0
  48. biotite/structure/dotbracket.py +2 -0
  49. biotite/structure/filter.py +30 -37
  50. biotite/structure/info/__init__.py +5 -8
  51. biotite/structure/info/atoms.py +31 -68
  52. biotite/structure/info/bonds.py +47 -101
  53. biotite/structure/info/ccd/README.rst +8 -0
  54. biotite/structure/info/ccd/amino_acids.txt +1663 -0
  55. biotite/structure/info/ccd/carbohydrates.txt +1135 -0
  56. biotite/structure/info/ccd/components.bcif +0 -0
  57. biotite/structure/info/ccd/nucleotides.txt +798 -0
  58. biotite/structure/info/ccd.py +95 -0
  59. biotite/structure/info/groups.py +90 -0
  60. biotite/structure/info/masses.py +21 -20
  61. biotite/structure/info/misc.py +78 -25
  62. biotite/structure/info/standardize.py +17 -12
  63. biotite/structure/integrity.py +19 -70
  64. biotite/structure/io/__init__.py +2 -4
  65. biotite/structure/io/ctab.py +12 -106
  66. biotite/structure/io/general.py +167 -181
  67. biotite/structure/io/gro/file.py +16 -16
  68. biotite/structure/io/mmtf/__init__.py +3 -0
  69. biotite/structure/io/mmtf/convertarray.c +217 -196
  70. biotite/structure/io/mmtf/convertarray.cp312-win_amd64.pyd +0 -0
  71. biotite/structure/io/mmtf/convertfile.c +215 -195
  72. biotite/structure/io/mmtf/convertfile.cp312-win_amd64.pyd +0 -0
  73. biotite/structure/io/mmtf/decode.c +223 -202
  74. biotite/structure/io/mmtf/decode.cp312-win_amd64.pyd +0 -0
  75. biotite/structure/io/mmtf/encode.c +213 -194
  76. biotite/structure/io/mmtf/encode.cp312-win_amd64.pyd +0 -0
  77. biotite/structure/io/mmtf/file.py +34 -26
  78. biotite/structure/io/mol/__init__.py +4 -2
  79. biotite/structure/io/mol/convert.py +71 -7
  80. biotite/structure/io/mol/ctab.py +414 -0
  81. biotite/structure/io/mol/header.py +116 -0
  82. biotite/structure/io/mol/{file.py → mol.py} +69 -82
  83. biotite/structure/io/mol/sdf.py +909 -0
  84. biotite/structure/io/npz/__init__.py +3 -0
  85. biotite/structure/io/npz/file.py +21 -18
  86. biotite/structure/io/pdb/__init__.py +3 -3
  87. biotite/structure/io/pdb/file.py +89 -34
  88. biotite/structure/io/pdb/hybrid36.c +63 -43
  89. biotite/structure/io/pdb/hybrid36.cp312-win_amd64.pyd +0 -0
  90. biotite/structure/io/pdbqt/file.py +32 -32
  91. biotite/structure/io/pdbx/__init__.py +12 -6
  92. biotite/structure/io/pdbx/bcif.py +648 -0
  93. biotite/structure/io/pdbx/cif.py +1032 -0
  94. biotite/structure/io/pdbx/component.py +246 -0
  95. biotite/structure/io/pdbx/convert.py +858 -386
  96. biotite/structure/io/pdbx/encoding.c +112803 -0
  97. biotite/structure/io/pdbx/encoding.cp312-win_amd64.pyd +0 -0
  98. biotite/structure/io/pdbx/legacy.py +267 -0
  99. biotite/structure/molecules.py +151 -151
  100. biotite/structure/repair.py +253 -0
  101. biotite/structure/sasa.c +213 -194
  102. biotite/structure/sasa.cp312-win_amd64.pyd +0 -0
  103. biotite/structure/sequence.py +112 -0
  104. biotite/structure/superimpose.py +618 -116
  105. {biotite-0.39.0.dist-info → biotite-0.41.0.dist-info}/METADATA +3 -3
  106. {biotite-0.39.0.dist-info → biotite-0.41.0.dist-info}/RECORD +109 -103
  107. {biotite-0.39.0.dist-info → biotite-0.41.0.dist-info}/WHEEL +1 -1
  108. biotite/structure/info/amino_acids.json +0 -1556
  109. biotite/structure/info/amino_acids.py +0 -42
  110. biotite/structure/info/carbohydrates.json +0 -1122
  111. biotite/structure/info/carbohydrates.py +0 -39
  112. biotite/structure/info/intra_bonds.msgpack +0 -0
  113. biotite/structure/info/link_types.msgpack +0 -1
  114. biotite/structure/info/nucleotides.json +0 -772
  115. biotite/structure/info/nucleotides.py +0 -39
  116. biotite/structure/info/residue_masses.msgpack +0 -0
  117. biotite/structure/info/residue_names.msgpack +0 -3
  118. biotite/structure/info/residues.msgpack +0 -0
  119. biotite/structure/io/pdbx/file.py +0 -652
  120. {biotite-0.39.0.dist-info → biotite-0.41.0.dist-info}/LICENSE.rst +0 -0
  121. {biotite-0.39.0.dist-info → biotite-0.41.0.dist-info}/top_level.txt +0 -0
@@ -8,31 +8,226 @@ This module provides functions for structure superimposition.
8
8
 
9
9
  __name__ = "biotite.structure"
10
10
  __author__ = "Patrick Kunzmann, Claude J. Rogers"
11
- __all__ = ["superimpose", "superimpose_apply"]
11
+ __all__ = ["superimpose", "superimpose_homologs",
12
+ "superimpose_without_outliers",
13
+ "AffineTransformation", "superimpose_apply"]
14
+
12
15
 
13
16
  import numpy as np
14
17
  from .atoms import coord
15
- from .geometry import centroid
18
+ from .geometry import centroid, distance
19
+ from .filter import filter_amino_acids, filter_nucleotides
20
+ from .sequence import to_sequence
21
+ from ..sequence.alphabet import common_alphabet
22
+ from ..sequence.seqtypes import ProteinSequence
23
+ from ..sequence.align import SubstitutionMatrix, align_optimal, get_codes
24
+
25
+
26
+ class AffineTransformation:
27
+ """
28
+ An affine transformation, consisting of translations and a rotation.
29
+
30
+ Parameters
31
+ ----------
32
+ center_translation : ndarray, shape=(3,) or shape=(m,3), dtype=float
33
+ The translation vector for moving the centroid into the
34
+ origin.
35
+ rotation : ndarray, shape=(3,3) or shape=(m,3,3), dtype=float
36
+ The rotation matrix.
37
+ target_translation : ndarray, shape=(m,3), dtype=float
38
+ The translation vector for moving the structure onto the
39
+ fixed one.
40
+
41
+ Attributes
42
+ ----------
43
+ center_translation, rotation, target_translation : ndarray
44
+ Same as the parameters.
45
+ The dimensions are always expanded to *(m,3)* or *(m,3,3)*,
46
+ respectively.
47
+ """
48
+ def __init__(self, center_translation, rotation, target_translation):
49
+ self.center_translation = _expand_dims(center_translation, 2)
50
+ self.rotation = _expand_dims(rotation, 3)
51
+ self.target_translation = _expand_dims(target_translation, 2)
52
+
53
+
54
+ def apply(self, atoms):
55
+ """
56
+ Apply this transformation on the given structure.
57
+
58
+ Parameters
59
+ ----------
60
+ atoms : AtomArray or AtomArrayStack or ndarray, shape(n,), dtype=float or ndarray, shape(m,n), dtype=float
61
+ The structure to apply the transformation on.
62
+
63
+ Returns
64
+ -------
65
+ transformed : AtomArray or AtomArrayStack or ndarray, shape(n,), dtype=float or ndarray, shape(m,n), dtype=float
66
+ A copy of the `atoms` structure,
67
+ with transformations applied.
68
+ Only coordinates are returned, if coordinates were given in
69
+ `atoms`.
70
+
71
+ Examples
72
+ --------
73
+
74
+ >>> coord = np.arange(15).reshape(5,3)
75
+ >>> print(coord)
76
+ [[ 0 1 2]
77
+ [ 3 4 5]
78
+ [ 6 7 8]
79
+ [ 9 10 11]
80
+ [12 13 14]]
81
+ >>> # Rotates 90 degrees around the z-axis
82
+ >>> transform = AffineTransformation(
83
+ ... center_translation=np.array([0,0,0]),
84
+ ... rotation=np.array([
85
+ ... [0, -1, 0],
86
+ ... [1, 0, 0],
87
+ ... [0, 0, 1]
88
+ ... ]),
89
+ ... target_translation=np.array([0,0,0])
90
+ ... )
91
+ >>> print(transform.apply(coord))
92
+ [[ -1. 0. 2.]
93
+ [ -4. 3. 5.]
94
+ [ -7. 6. 8.]
95
+ [-10. 9. 11.]
96
+ [-13. 12. 14.]]
97
+
98
+ """
99
+ mobile_coord = coord(atoms)
100
+ original_shape = mobile_coord.shape
101
+ mobile_coord = _reshape_to_3d(mobile_coord)
102
+ if mobile_coord.shape[0] != self.rotation.shape[0]:
103
+ raise IndexError(
104
+ f"Number of transformations is {self.rotation.shape[0]}, "
105
+ f"but number of structure models is {mobile_coord.shape[0]}"
106
+ )
107
+
108
+ superimposed_coord = mobile_coord.copy()
109
+ superimposed_coord += self.center_translation[:, np.newaxis, :]
110
+ superimposed_coord = _multi_matmul(self.rotation, superimposed_coord)
111
+ superimposed_coord += self.target_translation[:, np.newaxis, :]
112
+
113
+ superimposed_coord = superimposed_coord.reshape(original_shape)
114
+ if isinstance(atoms, np.ndarray):
115
+ return superimposed_coord
116
+ else:
117
+ superimposed = atoms.copy()
118
+ superimposed.coord = superimposed_coord
119
+ return superimposed
120
+
121
+
122
+ def as_matrix(self):
123
+ """
124
+ Get the translations and rotation as a combined 4x4
125
+ transformation matrix.
126
+
127
+ Multiplying this matrix with coordinates in the form
128
+ *(x, y, z, 1)* will apply the same transformation as
129
+ :meth:`apply()` to coordinates in the form *(x, y, z)*.
130
+
131
+ Returns
132
+ -------
133
+ transformation_matrix : ndarray, shape=(m,4,4), dtype=float
134
+ The transformation matrix.
135
+ *m* is the number of models in the transformation.
136
+
137
+ Examples
138
+ --------
139
+
140
+ >>> coord = np.arange(15).reshape(5,3)
141
+ >>> print(coord)
142
+ [[ 0 1 2]
143
+ [ 3 4 5]
144
+ [ 6 7 8]
145
+ [ 9 10 11]
146
+ [12 13 14]]
147
+ >>> # Rotates 90 degrees around the z-axis
148
+ >>> transform = AffineTransformation(
149
+ ... center_translation=np.array([0,0,0]),
150
+ ... rotation=np.array([
151
+ ... [0, -1, 0],
152
+ ... [1, 0, 0],
153
+ ... [0, 0, 1]
154
+ ... ]),
155
+ ... target_translation=np.array([0,0,0])
156
+ ... )
157
+ >>> print(transform.apply(coord))
158
+ [[ -1. 0. 2.]
159
+ [ -4. 3. 5.]
160
+ [ -7. 6. 8.]
161
+ [-10. 9. 11.]
162
+ [-13. 12. 14.]]
163
+ >>> # Use a 4x4 matrix for transformation as alternative
164
+ >>> coord_4 = np.concatenate([coord, np.ones((len(coord), 1))], axis=-1)
165
+ >>> print(coord_4)
166
+ [[ 0. 1. 2. 1.]
167
+ [ 3. 4. 5. 1.]
168
+ [ 6. 7. 8. 1.]
169
+ [ 9. 10. 11. 1.]
170
+ [12. 13. 14. 1.]]
171
+ >>> print((transform.as_matrix()[0] @ coord_4.T).T)
172
+ [[ -1. 0. 2. 1.]
173
+ [ -4. 3. 5. 1.]
174
+ [ -7. 6. 8. 1.]
175
+ [-10. 9. 11. 1.]
176
+ [-13. 12. 14. 1.]]
177
+
178
+ """
179
+ n_models = self.rotation.shape[0]
180
+ rotation_mat = _3d_identity(n_models, 4)
181
+ rotation_mat[:, :3, :3] = self.rotation
182
+ center_translation_mat = _3d_identity(n_models, 4)
183
+ center_translation_mat[:, :3, 3] = self.center_translation
184
+ target_translation_mat = _3d_identity(n_models, 4)
185
+ target_translation_mat[:, :3, 3] = self.target_translation
186
+ return target_translation_mat @ rotation_mat @ center_translation_mat
187
+
188
+
189
+ def _expand_dims(array, n_dims):
190
+ """
191
+ Expand the dimensions of an `ndarray` to a certain number of
192
+ dimensions.
193
+ """
194
+ while array.ndim < n_dims:
195
+ array = array[np.newaxis, ...]
196
+ return array
197
+
198
+
199
+ def _3d_identity(m, n):
200
+ """
201
+ Create an array of *m* identity matrices of shape *(n, n)*
202
+ """
203
+ matrices = np.zeros((m, n, n), dtype=float)
204
+ indices = np.arange(n)
205
+ matrices[:, indices, indices] = 1
206
+ return matrices
16
207
 
17
208
 
18
209
  def superimpose(fixed, mobile, atom_mask=None):
19
210
  """
20
- Superimpose structures onto a fixed structure.
21
-
22
- The superimposition is performed using the Kabsch algorithm
23
- :footcite:`Kabsch1976, Kabsch1978`, so that the RMSD between the
24
- superimposed and the fixed structure is minimized.
25
-
211
+ Superimpose structures onto each other, minimizing the RMSD between
212
+ them.
213
+ :footcite:`Kabsch1976, Kabsch1978`.
214
+
215
+ More precisely, the `mobile` structure is rotated and translated onto
216
+ the `fixed` structure.
217
+
26
218
  Parameters
27
219
  ----------
28
- fixed : AtomArray, shape(n,) or ndarray, shape(n,), dtype=float
29
- The fixed structure.
220
+ fixed : AtomArray, shape(n,) or AtomArrayStack, shape(m,n) or ndarray, shape(n,), dtype=float or ndarray, shape(m,n), dtype=float
221
+ The fixed structure(s).
30
222
  Alternatively coordinates can be given.
31
223
  mobile: AtomArray, shape(n,) or AtomArrayStack, shape(m,n) or ndarray, shape(n,), dtype=float or ndarray, shape(m,n), dtype=float
32
224
  The structure(s) which is/are superimposed on the `fixed`
33
225
  structure.
34
226
  Each atom at index *i* in `mobile` must correspond the
35
227
  atom at index *i* in `fixed` to obtain correct results.
228
+ Furthermore, if both `fixed` and `mobile` are
229
+ :class:`AtomArrayStack` objects, they must have the same
230
+ number of models.
36
231
  Alternatively coordinates can be given.
37
232
  atom_mask: ndarray, dtype=bool, optional
38
233
  If given, only the atoms covered by this boolean mask will be
@@ -41,62 +236,55 @@ def superimpose(fixed, mobile, atom_mask=None):
41
236
  on the covered atoms instead of all atoms.
42
237
  The returned superimposed structure will contain all atoms
43
238
  of the input structure, regardless of this parameter.
44
-
239
+
45
240
  Returns
46
241
  -------
47
242
  fitted : AtomArray or AtomArrayStack or ndarray, shape(n,), dtype=float or ndarray, shape(m,n), dtype=float
48
243
  A copy of the `mobile` structure(s),
49
- superimposed on the fixed structure.
244
+ superimposed on the fixed structure(s).
50
245
  Only coordinates are returned, if coordinates were given in
51
246
  `mobile`.
52
- transformation : tuple or tuple list
53
- The tuple contains the transformations that were applied on
54
- `mobile`. This can be used in `apply_superimposition()` in order
55
- to transform another AtomArray in the same way.
56
- The first element contains the translation vector for moving the
57
- centroid into the origin.
58
- The second element contains the rotation matrix.
59
- The third element contains the translation vector for moving the
60
- structure onto the fixed.
61
- The three transformations are performed sequentially.
62
-
247
+ transformation : AffineTransformation
248
+ The affine transformation(s) that were applied on `mobile`.
249
+ :meth:`AffineTransformation.apply()` can be used to transform
250
+ another AtomArray in the same way.
251
+
63
252
  See Also
64
253
  --------
65
- superimpose_apply
66
-
254
+ superimpose_without_outliers : Superimposition with outlier removal
255
+ superimpose_homologs : Superimposition of homologous structures
256
+
67
257
  Notes
68
258
  -----
69
- The `transformation` tuple can be used in
70
- :func:`superimpose_apply()` in order to transform another
71
- :class:`AtomArray` in the same way.
72
- This can come in handy, in case you want to superimpose two
259
+ The `transformation` can come in handy, in case you want to
260
+ superimpose two
73
261
  structures with different amount of atoms.
74
262
  Often the two structures need to be filtered in order to obtain the
75
263
  same size and annotation arrays.
76
264
  After superimposition the transformation can be applied on the
77
- original structure using :func:`superimpose_apply()`.
78
-
265
+ original structure using :meth:`AffineTransformation.apply()`.
266
+
79
267
  References
80
268
  ----------
81
-
269
+
82
270
  .. footbibliography::
83
-
271
+
84
272
  Examples
85
273
  --------
86
-
274
+
87
275
  At first two models of a structure are taken and one of them is
88
276
  randomly rotated/translated.
89
277
  Consequently the RMSD is quite large:
90
-
278
+
91
279
  >>> array1 = atom_array_stack[0]
92
280
  >>> array2 = atom_array_stack[1]
93
281
  >>> array2 = translate(array2, [1,2,3])
94
282
  >>> array2 = rotate(array2, [1,2,3])
95
283
  >>> print("{:.3f}".format(rmsd(array1, array2)))
96
284
  11.260
97
-
285
+
98
286
  RMSD decreases after superimposition of only CA atoms:
99
-
287
+
100
288
  >>> array2_fit, transformation = superimpose(
101
289
  ... array1, array2, atom_mask=(array2.atom_name == "CA")
102
290
  ... )
@@ -110,95 +298,300 @@ def superimpose(fixed, mobile, atom_mask=None):
110
298
  >>> print("{:.3f}".format(rmsd(array1, array2_fit)))
111
299
  1.928
112
300
  """
113
-
114
- m_coord = coord(mobile)
115
- f_coord = coord(fixed)
116
- mshape = m_coord.shape
117
- mdim = m_coord.ndim
118
- if f_coord.ndim != 2:
119
- raise ValueError("Expected fixed array to be an AtomArray")
120
- if mdim < 2:
121
- raise ValueError(
122
- "Expected mobile array to be an AtomArray or AtomArrayStack"
123
- )
124
- if mdim == 2:
125
- # normalize inputs. Fixed coords has shape (n, 3)
126
- # and mobile has shape (m, n, 3)
127
- m_coord = m_coord[np.newaxis, ...]
128
-
129
- nmodels = m_coord.shape[0]
130
- if f_coord.shape[0] != m_coord.shape[1]:
131
- raise ValueError(
132
- f"Expected fixed array and mobile array to have the same number "
133
- f"of atoms, but {f_coord.shape[0]} != {m_coord.shape[1]}"
134
- )
301
+ # Bring coordinates into the same dimensionality
302
+ mob_coord = _reshape_to_3d(coord(mobile))
303
+ fix_coord = _reshape_to_3d(coord(fixed))
135
304
 
136
305
  if atom_mask is not None:
137
306
  # Implicitly this creates array copies
138
- mob_filtered = m_coord[..., atom_mask, :]
139
- fix_filtered = f_coord[atom_mask, :]
307
+ mob_filtered = mob_coord[:, atom_mask, :]
308
+ fix_filtered = fix_coord[:, atom_mask, :]
140
309
  else:
141
- mob_filtered = np.copy(m_coord)
142
- fix_filtered = np.copy(f_coord)
143
-
310
+ mob_filtered = np.copy(mob_coord)
311
+ fix_filtered = np.copy(fix_coord)
312
+
144
313
  # Center coordinates at (0,0,0)
145
314
  mob_centroid = centroid(mob_filtered)
146
315
  fix_centroid = centroid(fix_filtered)
147
- mob_filtered -= mob_centroid[..., np.newaxis, :]
148
- fix_filtered -= fix_centroid
149
-
150
- s_coord = m_coord.copy() - mob_centroid[..., np.newaxis, :]
151
- # Perform Kabsch algorithm for every model
152
- transformations = [None] * nmodels
153
- for i in range(nmodels):
154
- rotation = _superimpose(fix_filtered, mob_filtered[i])
155
- s_coord[i] = np.dot(rotation, s_coord[i].T).T
156
- transformations[i] = (-mob_centroid[i], rotation, fix_centroid)
157
- s_coord += fix_centroid
158
-
159
- if isinstance(mobile, np.ndarray):
160
- superimposed = s_coord.reshape(mshape)
161
- else:
162
- superimposed = mobile.copy()
163
- superimposed.coord = s_coord.reshape(mshape)
316
+ mob_centered_filtered = mob_filtered - mob_centroid[:, np.newaxis, :]
317
+ fix_centered_filtered = fix_filtered - fix_centroid[:, np.newaxis, :]
164
318
 
165
- if mdim == 2:
166
- return superimposed, transformations[0]
167
- else:
168
- return superimposed, transformations
319
+ rotation = _get_rotation_matrices(
320
+ fix_centered_filtered, mob_centered_filtered
321
+ )
322
+ transform = AffineTransformation(-mob_centroid, rotation, fix_centroid)
323
+ return transform.apply(mobile), transform
324
+
325
+
326
+ def superimpose_without_outliers(fixed, mobile, min_anchors=3,
327
+ max_iterations=10, quantiles=(0.25, 0.75),
328
+ outlier_threshold=1.5):
329
+ r"""
330
+ Superimpose structures onto a fixed structure, ignoring
331
+ conformational outliers.
332
+
333
+ This method iteratively superimposes the `mobile` structure onto the
334
+ `fixed` structure, removes conformational outliers and superimposes
335
+ the remaining atoms (called *anchors*) again until no outlier
336
+ remains.
337
+
338
+
339
+ Parameters
340
+ ----------
341
+ fixed : AtomArray, shape(n,) or AtomArrayStack, shape(m,n) or ndarray, shape(n,), dtype=float or ndarray, shape(m,n), dtype=float
342
+ The fixed structure(s).
343
+ Alternatively coordinates can be given.
344
+ mobile: AtomArray, shape(n,) or AtomArrayStack, shape(m,n) or ndarray, shape(n,), dtype=float or ndarray, shape(m,n), dtype=float
345
+ The structure(s) which is/are superimposed on the `fixed`
346
+ structure.
347
+ Each atom at index *i* in `mobile` must correspond the
348
+ atom at index *i* in `fixed` to obtain correct results.
349
+ Furthermore, if both `fixed` and `mobile` are
350
+ :class:`AtomArrayStack` objects, they must have the same
351
+ number of models.
352
+ Alternatively coordinates can be given.
353
+ min_anchors : int, optional
354
+ The outlier removal is stopped, if less than `min_anchors`
355
+ anchors would be left.
356
+ max_iterations : int, optional
357
+ The maximum number of iterations for removing conformational
358
+ outliers.
359
+ Setting the value to 1 means that no outlier removal is
360
+ conducted.
361
+ quantiles : tuple (float, float), optional
362
+ The lower and upper quantile for the interpercentile range
363
+ (IPR).
364
+ By default the interquartile range is taken.
365
+ outlier_threshold : float, optional
366
+ The threshold for considering a conformational outlier.
367
+ The threshold is given in units of IPR.
368
+
369
+ Returns
370
+ -------
371
+ fitted : AtomArray or AtomArrayStack
372
+ A copy of the `mobile` structure(s), superimposed on the fixed
373
+ structure.
374
+ Only coordinates are returned, if coordinates were given in
375
+ `mobile`.
376
+ transform : AffineTransformation
377
+ This object contains the affine transformation(s) that were
378
+ applied on `mobile`.
379
+ :meth:`AffineTransformation.apply()` can be used to transform
380
+ another AtomArray in the same way.
381
+ anchor_indices : ndarray, shape(k,), dtype=int
382
+ The indices of the anchor atoms.
383
+ These atoms were used for the superimposition.
384
+
385
+ See Also
386
+ --------
387
+ superimpose : Superimposition without outlier removal
388
+ superimpose_homologs : Superimposition of homologous structures
169
389
 
390
+ Notes
391
+ -----
392
+ This method runs the following algorithm in iterations:
393
+
394
+ 1. Superimpose anchor atoms of `mobile` onto `fixed`.
395
+ 2. Calculate the squared distance :math:`d^2` between the
396
+ superimposed anchors.
397
+ 3. Remove conformational outliers from anchors based on the
398
+ following criterion:
399
+
400
+ .. math:: d^2 > P_\text{upper}(d^2) + \left( P_\text{upper}(d^2) - P_\text{lower}(d^2) \right) \cdot T
401
+
402
+ In prose this means that an anchor is considered an outlier, if
403
+ it is `outlier_threshold` :math:`T` times the interpercentile
404
+ range (IPR) above the upper percentile.
405
+ By default, this is 1.5 times the interquartile range, which is
406
+ the usual threshold to mark outliers in box plots.
170
407
 
171
- def _superimpose(fix_centered, mob_centered):
408
+ In the beginning, all atoms are considered as anchors.
409
+
410
+ Considering all atoms (not only the anchors), this approach does
411
+ **not** minimize the RMSD, in contrast to :func:`superimpose()`.
412
+ The purpose of this function is to ignore outliers to decrease the
413
+ RMSD in the more conserved parts of the structure.
172
414
  """
173
- Perform the Kabsch algorithm using only the coordinates.
415
+ if max_iterations < 1:
416
+ raise ValueError("Maximum number of iterations must be at least 1")
417
+
418
+ # Ensure that the first quantile is smaller than the second one
419
+ quantiles = sorted(quantiles)
420
+
421
+ fixed_coord = coord(fixed)
422
+ mobile_coord = coord(mobile)
423
+ # Before refinement, all anchors are included
424
+ # 'inlier' is the opposite of 'outlier'
425
+ updated_inlier_mask = np.ones(fixed_coord.shape[-2], dtype=bool)
426
+
427
+ for _ in range(max_iterations):
428
+ # Run superimposition
429
+ inlier_mask = updated_inlier_mask
430
+ filtered_fixed_coord = fixed_coord[..., inlier_mask, :]
431
+ filtered_mobile_coord = mobile_coord[..., inlier_mask, :]
432
+ superimposed_coord, transform = superimpose(
433
+ filtered_fixed_coord, filtered_mobile_coord
434
+ )
435
+
436
+ # Find outliers
437
+ sq_dist = distance(filtered_fixed_coord, superimposed_coord) ** 2
438
+ if sq_dist.ndim == 2:
439
+ # If multiple models are superimposed,
440
+ # use the mean squared distance to determine outliers
441
+ sq_dist = np.mean(sq_dist, axis=0)
442
+ lower_quantile, upper_quantile = np.quantile(sq_dist, quantiles)
443
+ ipr = upper_quantile - lower_quantile
444
+ updated_inlier_mask = inlier_mask.copy()
445
+ # Squared distance was only calculated for the existing inliers
446
+ # -> update the mask only for these atoms
447
+ updated_inlier_mask[updated_inlier_mask] = (
448
+ sq_dist <= upper_quantile + outlier_threshold * ipr
449
+ )
450
+ if np.all(updated_inlier_mask):
451
+ # No outliers anymore -> early termination
452
+ break
453
+ if np.count_nonzero(updated_inlier_mask) < min_anchors:
454
+ # Less than min_anchors anchors would be left -> early termination
455
+ break
456
+
457
+ anchor_indices = np.where(inlier_mask)[0]
458
+ return transform.apply(mobile), transform, anchor_indices
459
+
460
+
461
+ def superimpose_homologs(fixed, mobile, substitution_matrix=None,
462
+ gap_penalty=-10, min_anchors=3, **kwargs):
463
+ r"""
464
+ Superimpose one protein or nucleotide chain onto another one,
465
+ considering sequence differences and conformational outliers.
466
+
467
+ The method finds corresponding residues by sequence alignment and
468
+ selects their :math:`C_{\alpha}` or :math:`P` atoms as
469
+ superimposition *anchors*.
470
+ Then iteratively the anchor atoms are superimposed and outliers are
471
+ removed.
472
+
473
+ Parameters
474
+ ----------
475
+ fixed : AtomArray, shape(n,) or AtomArrayStack, shape(m,n)
476
+ The fixed structure(s).
477
+ Must comprise a single chain.
478
+ mobile : AtomArray, shape(n,) or AtomArrayStack, shape(m,n)
479
+ The structure(s) which is/are superimposed on the `fixed`
480
+ structure.
481
+ Must comprise a single chain.
482
+ substitution_matrix : str or SubstitutionMatrix, optional
483
+ The (name of the) substitution matrix used for sequence
484
+ alignment.
485
+ Must fit the chain type.
486
+ By default, ``"BLOSUM62"`` and ``"NUC"`` are used respectively.
487
+ Only aligned residues with a positive score are considered as
488
+ initial anchors.
489
+ gap_penalty : int or tuple of int, optional
490
+ The gap penalty for sequence alignment.
491
+ A single value indicates a linear penalty, while a tuple
492
+ indicates an affine penalty.
493
+ min_anchors : int, optional
494
+ If less than `min_anchors` anchors are found by sequence
495
+ alignment, the method ditches the alignment and matches all
496
+ anchor atoms.
497
+ If the number of anchor atoms is not equal in `fixed` and
498
+ `mobile` in this fallback case, an exception is raised.
499
+ Furthermore, the outlier removal is stopped, if less than
500
+ `min_anchors` anchors would be left.
501
+ **kwargs
502
+ Additional parameters for
503
+ :func:`superimpose_without_outliers()`.
504
+
505
+ Returns
506
+ -------
507
+ fitted : AtomArray or AtomArrayStack
508
+ A copy of the `mobile` structure(s), superimposed on the fixed
509
+ structure(s).
510
+ transform : AffineTransformation
511
+ This object contains the affine transformation(s) that were
512
+ applied on `mobile`.
513
+ :meth:`AffineTransformation.apply()` can be used to transform
514
+ another AtomArray in the same way.
515
+ fixed_anchor_indices, mobile_anchor_indices : ndarray, shape(k,), dtype=int
516
+ The indices of the anchor atoms in the fixed and mobile
517
+ structure, respectively.
518
+ These atoms were used for the superimposition.
519
+
520
+ See Also
521
+ --------
522
+ superimpose : Superimposition without outlier removal
523
+ superimpose_without_outliers : Internally used for outlier removal
524
+
525
+ Notes
526
+ -----
527
+ As this method relies on sequence alignment, it works only for
528
+ proteins/nucleic acids with decent sequence homology.
174
529
  """
175
- # Calculating rotation matrix
176
- y = mob_centered
177
- x = fix_centered
178
- # Calculate covariance matrix
179
- cov = np.dot(x.T, y)
180
- v, s, w = np.linalg.svd(cov)
181
- # Remove possibility of reflected atom coordinates
182
- if np.linalg.det(v) * np.linalg.det(w) < 0:
183
- v[:,-1] *= -1
184
- rotation = np.dot(v,w)
185
- return rotation
530
+ fixed_anchor_indices = _get_backbone_anchor_indices(fixed)
531
+ mobile_anchor_indices = _get_backbone_anchor_indices(mobile)
532
+ if (
533
+ len(fixed_anchor_indices) < min_anchors or
534
+ len(mobile_anchor_indices) < min_anchors
535
+ ):
536
+ raise ValueError(
537
+ "Structures have too few CA atoms for required number of anchors"
538
+ )
539
+
540
+ anchor_indices = _find_matching_anchors(
541
+ fixed[..., fixed_anchor_indices],
542
+ mobile[..., mobile_anchor_indices],
543
+ substitution_matrix,
544
+ gap_penalty,
545
+ )
546
+ if len(anchor_indices) < min_anchors:
547
+ # Fallback: Match all backbone anchors
548
+ if len(fixed_anchor_indices) != len(mobile_anchor_indices):
549
+ raise ValueError(
550
+ "Tried fallback due to low anchor number, "
551
+ "but number of CA atoms does not match"
552
+ )
553
+ fixed_anchor_indices = fixed_anchor_indices
554
+ mobile_anchor_indices = mobile_anchor_indices
555
+ else:
556
+ # The anchor indices point to the CA atoms
557
+ # -> get the corresponding indices for the whole structure
558
+ fixed_anchor_indices = fixed_anchor_indices[anchor_indices[:, 0]]
559
+ mobile_anchor_indices = mobile_anchor_indices[anchor_indices[:, 1]]
560
+
561
+ _, transform, selected_anchor_indices = superimpose_without_outliers(
562
+ fixed[..., fixed_anchor_indices],
563
+ mobile[..., mobile_anchor_indices],
564
+ min_anchors,
565
+ **kwargs
566
+ )
567
+ fixed_anchor_indices = fixed_anchor_indices[selected_anchor_indices]
568
+ mobile_anchor_indices = mobile_anchor_indices[selected_anchor_indices]
569
+
570
+ return (
571
+ transform.apply(mobile),
572
+ transform,
573
+ fixed_anchor_indices,
574
+ mobile_anchor_indices,
575
+ )
186
576
 
187
577
 
188
578
  def superimpose_apply(atoms, transformation):
189
579
  """
190
- Superimpose structures using a given transformation tuple.
191
-
192
- The transformation tuple is obtained by prior superimposition.
193
-
580
+ Superimpose structures using a given :class:`AffineTransformation`.
581
+
582
+ The :class:`AffineTransformation` can be obtained by prior
583
+ superimposition.
584
+
585
+ DEPRECATED: Use :func:`AffineTransformation.apply()` instead.
586
+
194
587
  Parameters
195
588
  ----------
196
589
  atoms : AtomArray or ndarray, shape(n,), dtype=float
197
590
  The structure to apply the transformation on.
198
591
  Alternatively coordinates can be given.
199
- transformation: tuple, size=3
200
- The transformation tuple, obtained by :func:`superimpose()`.
201
-
592
+ transformation: AffineTransformation
593
+ The transformation, obtained by :func:`superimpose()`.
594
+
202
595
  Returns
203
596
  -------
204
597
  fitted : AtomArray or AtomArrayStack
@@ -206,20 +599,129 @@ def superimpose_apply(atoms, transformation):
206
599
  with transformations applied.
207
600
  Only coordinates are returned, if coordinates were given in
208
601
  `atoms`.
209
-
602
+
210
603
  See Also
211
604
  --------
212
605
  superimpose
213
606
  """
214
- trans1, rot, trans2 = transformation
215
- s_coord = coord(atoms).copy()
216
- s_coord += trans1
217
- s_coord = np.dot(rot, s_coord.T).T
218
- s_coord += trans2
607
+ return transformation.apply(atoms)
608
+
219
609
 
220
- if isinstance(atoms, np.ndarray):
221
- return s_coord
610
+ def _reshape_to_3d(coord):
611
+ """
612
+ Reshape the coordinate array to 3D, if it is 2D.
613
+ """
614
+ if coord.ndim < 2:
615
+ raise ValueError(
616
+ "Coordinates must be at least two-dimensional"
617
+ )
618
+ if coord.ndim == 2:
619
+ return coord[np.newaxis, ...]
620
+ elif coord.ndim == 3:
621
+ return coord
222
622
  else:
223
- transformed = atoms.copy()
224
- transformed.coord = s_coord
225
- return transformed
623
+ raise ValueError(
624
+ "Coordinates must be at most three-dimensional"
625
+ )
626
+
627
+
628
+ def _get_rotation_matrices(fixed, mobile):
629
+ """
630
+ Get the rotation matrices to superimpose the given mobile
631
+ coordinates into the given fixed coordinates, minimizing the RMSD.
632
+
633
+ Uses the *Kabsch* algorithm.
634
+ Both sets of coordinates must already be centered at origin.
635
+ """
636
+ # Calculate cross-covariance matrices
637
+ cov = np.sum(fixed[:,:,:,np.newaxis] * mobile[:,:,np.newaxis,:], axis=1)
638
+ v, s, w = np.linalg.svd(cov)
639
+ # Remove possibility of reflected atom coordinates
640
+ reflected_mask = (np.linalg.det(v) * np.linalg.det(w) < 0)
641
+ v[reflected_mask, :, -1] *= -1
642
+ matrices = np.matmul(v, w)
643
+ return matrices
644
+
645
+
646
+ def _multi_matmul(matrices, vectors):
647
+ """
648
+ Calculate the matrix multiplication of m matrices
649
+ with m x n vectors.
650
+ """
651
+ return np.transpose(
652
+ np.matmul(
653
+ matrices,
654
+ np.transpose(vectors, axes=(0, 2, 1))
655
+ ),
656
+ axes=(0, 2, 1)
657
+ )
658
+
659
+
660
+ def _get_backbone_anchor_indices(atoms):
661
+ """
662
+ Select one representative anchor atom for each amino acid and
663
+ nucleotide and return their indices.
664
+ """
665
+ return np.where(
666
+ ((filter_amino_acids(atoms)) & (atoms.atom_name == "CA")) |
667
+ ((filter_nucleotides(atoms)) & (atoms.atom_name == "P"))
668
+ )[0]
669
+
670
+
671
+ def _find_matching_anchors(
672
+ fixed_anchor_atoms,
673
+ mobile_anchors_atoms,
674
+ substitution_matrix,
675
+ gap_penalty,
676
+ ):
677
+ """
678
+ Find corresponding residues using pairwise sequence alignment.
679
+ """
680
+ fixed_seq = _to_sequence(fixed_anchor_atoms)
681
+ mobile_seq = _to_sequence(mobile_anchors_atoms)
682
+ common_alph = common_alphabet([fixed_seq.alphabet, mobile_seq.alphabet])
683
+ if common_alph is None:
684
+ raise ValueError("Cannot superimpose peptides with nucleic acids")
685
+
686
+ if substitution_matrix is None:
687
+ if isinstance(fixed_seq, ProteinSequence):
688
+ substitution_matrix = SubstitutionMatrix.std_protein_matrix()
689
+ else:
690
+ substitution_matrix = SubstitutionMatrix.std_nucleotide_matrix()
691
+ elif isinstance(substitution_matrix, str):
692
+ substitution_matrix = SubstitutionMatrix(
693
+ common_alph, common_alph, substitution_matrix
694
+ )
695
+ score_matrix = substitution_matrix.score_matrix()
696
+
697
+ alignment = align_optimal(
698
+ fixed_seq,
699
+ mobile_seq,
700
+ substitution_matrix,
701
+ gap_penalty,
702
+ terminal_penalty=False,
703
+ max_number=1,
704
+ )[0]
705
+ alignment_codes = get_codes(alignment)
706
+ anchor_mask = (
707
+ # Anchors must be similar amino acids
708
+ (score_matrix[alignment_codes[0], alignment_codes[1]] > 0)
709
+ # Cannot anchor gaps
710
+ & (alignment_codes[0] != -1)
711
+ & (alignment_codes[1] != -1)
712
+ )
713
+ anchors = alignment.trace[anchor_mask]
714
+ return anchors
715
+
716
+
717
+ def _to_sequence(atoms):
718
+ sequences, _ = to_sequence(atoms, allow_hetero=True)
719
+ if len(sequences) == 0:
720
+ raise ValueError(
721
+ "Structure does not contain any amino acids or nucleotides"
722
+ )
723
+ if len(sequences) > 1:
724
+ raise ValueError(
725
+ "Structure contains multiple chains, but only one is allowed"
726
+ )
727
+ return sequences[0]