/
chains.py
298 lines (240 loc) · 8.6 KB
/
chains.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# This source code is part of the Biotite package and is distributed
# under the 3-Clause BSD License. Please see 'LICENSE.rst' for further
# information.
"""
This module provides utility for handling data on chain level, rather than
atom level.
"""
__name__ = "biotite.structure"
__author__ = "Patrick Kunzmann"
__all__ = ["get_chain_starts", "apply_chain_wise", "spread_chain_wise",
"get_chain_masks", "get_chain_starts_for", "get_chain_positions",
"chain_iter", "get_chains", "get_chain_count", "chain_iter"]
import numpy as np
from .resutil import *
def get_chain_starts(array, add_exclusive_stop=False):
"""
Get the indices in an atom array, which indicates the beginning of
a new chain.
A new chain starts, when the chain ID changes or when the residue ID
decreases.
Parameters
----------
array : AtomArray or AtomArrayStack
The atom array (stack) to get the chain starts from.
add_exclusive_stop : bool, optional
If true, the exclusive stop of the input atom array, i.e.
``array.array_length()``, is added to the returned array of
start indices as last element.
Returns
-------
starts : ndarray, dtype=int
The start indices of new chains in `array`.
Notes
-----
This method is internally used by all other chain-related
functions.
See also
--------
get_residue_starts
"""
diff = np.diff(array.res_id)
res_id_decrement = diff < 0
# This mask is 'true' at indices where the value changes
chain_id_changes = (array.chain_id[1:] != array.chain_id[:-1])
# Convert mask to indices
# Add 1, to shift the indices from the end of a chain
# to the start of a new chain
chain_starts = np.where(res_id_decrement | chain_id_changes)[0] + 1
# The first chain is not included yet -> Insert '[0]'
if add_exclusive_stop:
return np.concatenate(([0], chain_starts, [array.array_length()]))
else:
return np.concatenate(([0], chain_starts))
def apply_chain_wise(array, data, function, axis=None):
"""
Apply a function to intervals of data, where each interval
corresponds to one chain.
The function takes an atom array (stack) and an data array
(`ndarray`) of the same length. The function iterates through the
chain IDs of the atom array (stack) and identifies intervals of
the same ID. Then the data is
partitioned into the same intervals, and each interval (also an
:class:`ndarray`) is put as parameter into `function`. Each return value is
stored as element in the resulting :class:`ndarray`, therefore each element
corresponds to one chain.
Parameters
----------
array : AtomArray or AtomArrayStack
The atom array (stack) to determine the chains from.
data : ndarray
The data, whose intervals are the parameter for `function`. Must
have same length as `array`.
function : function
The `function` must have either the form *f(data)* or
*f(data, axis)* in case `axis` is given. Every `function` call
must return a value with the same shape and data type.
axis : int, optional
This value is given to the `axis` parameter of `function`.
Returns
-------
processed_data : ndarray
Chain-wise evaluation of `data` by `function`. The size of the
first dimension of this array is equal to the amount of
chains.
See also
--------
apply_residue_wise
"""
starts = get_chain_starts(array, add_exclusive_stop=True)
return apply_segment_wise(starts, data, function, axis)
def spread_chain_wise(array, input_data):
"""
Expand chain-wise data to atom-wise data.
Each value in the chain-wise input is assigned to all atoms of
this chain:
``output_data[i] = input_data[j]``,
*i* is incremented from atom to atom,
*j* is incremented every chain change.
Parameters
----------
array : AtomArray or AtomArrayStack
The atom array (stack) to determine the chains from.
input_data : ndarray
The data to be spread. The length of axis=0 must be equal to
the amount of different chain IDs in `array`.
Returns
-------
output_data : ndarray
Chain-wise spread `input_data`. Length is the same as
`array_length()` of `array`.
See also
--------
spread_residue_wise
"""
starts = get_chain_starts(array, add_exclusive_stop=True)
return spread_segment_wise(starts, input_data)
def get_chain_masks(array, indices):
"""
Get boolean masks indicating the chains to which the given atom
indices belong.
Parameters
----------
array : AtomArray, shape=(n,) or AtomArrayStack, shape=(m,n)
The atom array (stack) to determine the chains from.
indices : ndarray, dtype=int, shape=(k,)
These indices indicate the atoms to get the corresponding
chains for.
Negative indices are not allowed.
Returns
-------
chains_masks : ndarray, dtype=bool, shape=(k,n)
Multiple boolean masks, one for each given index in `indices`.
Each array masks the atoms that belong to the same chain as
the atom at the given index.
See also
--------
get_residue_masks
"""
starts = get_chain_starts(array, add_exclusive_stop=True)
return get_segment_masks(starts, indices)
def get_chain_starts_for(array, indices):
"""
For each given atom index, get the index that points to the
start of the chain that atom belongs to.
Parameters
----------
array : AtomArray or AtomArrayStack
The atom array (stack) to determine the chains from.
indices : ndarray, dtype=int, shape=(k,)
These indices point to the atoms to get the corresponding
chain starts for.
Negative indices are not allowed.
Returns
-------
start_indices : ndarray, dtype=int, shape=(k,)
The indices that point to the chain starts for the input
`indices`.
See also
--------
get_residue_starts_for
"""
starts = get_chain_starts(array, add_exclusive_stop=True)
return get_segment_starts_for(starts, indices)
def get_chain_positions(array, indices):
"""
For each given atom index, obtain the position of the chain
corresponding to this index in the input `array`.
For example, the position of the first chain in the atom array is
``0``, the the position of the second chain is ``1``, etc.
Parameters
----------
array : AtomArray or AtomArrayStack
The atom array (stack) to determine the chains from.
indices : ndarray, dtype=int, shape=(k,)
These indices point to the atoms to get the corresponding
chain positions for.
Negative indices are not allowed.
Returns
-------
start_indices : ndarray, dtype=int, shape=(k,)
The indices that point to the position of the chains.
See also
--------
get_residue_positions
"""
starts = get_chain_starts(array, add_exclusive_stop=True)
return get_segment_positions(starts, indices)
def get_chains(array):
"""
Get the chain IDs of an atom array (stack).
The chains are listed in the same order they occur in the array
(stack).
Parameters
----------
array : AtomArray or AtomArrayStack
The atom array (stack), where the chains are determined.
Returns
-------
ids : ndarray, dtype=str
List of chain IDs.
See also
--------
get_residues
"""
return array.chain_id[get_chain_starts(array)]
def get_chain_count(array):
"""
Get the amount of chains in an atom array (stack).
The count is determined from the `chain_id` annotation.
Each time the chain ID changes, the count is incremented.
Parameters
----------
array : AtomArray or AtomArrayStack
The atom array (stack), where the chains are counted.
Returns
-------
count : int
Amount of chains.
See also
--------
get_residue_count
"""
return len(get_chain_starts(array))
def chain_iter(array):
"""
Iterate over all chains in an atom array (stack).
Parameters
----------
array : AtomArray or AtomArrayStack
The atom array (stack) to iterate over.
Yields
------
chain : AtomArray or AtomArrayStack
A single chain of the input `array`.
See also
--------
residue_iter
"""
starts = get_chain_starts(array, add_exclusive_stop=True)
return segment_iter(array, starts)