-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
375 lines (304 loc) · 9.78 KB
/
utils.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
from typing import Dict, List, Tuple, Union
import numpy as np
import pandas as pd
from Bio.PDB.Atom import Atom
from Bio.PDB.Chain import Chain as BioChain
from Bio.PDB.Residue import Residue
from Bio.PDB.Polypeptide import is_aa, is_nucleic
from src.Point import Point
residue_names = [
"ALA",
"ARG",
"ASN",
"ASP",
"CYS",
"GLU",
"GLN",
"GLY",
"HIS",
"HYP",
"ILE",
"LEU",
"LYS",
"MET",
"PHE",
"PRO",
"SER",
"THR",
"TRP",
"TYR",
"VAL",
]
type_1_residues = ["GLY", "ALA", "SER", "THR", "CYS", "VAL", "LEU", "PRO", "ASP", "ASN"]
type_2_residues = ["ILE", "MET", "PHE", "TYR", "TRP", "GLU", "GLN", "HIS", "LYS", "ARG"]
backbone_atoms = ["N", "CA", "C", "O"]
element_masses = {
"C": 12.0107,
"N": 14.0067,
"O": 15.9994,
"S": 32.065,
"P": 30.973762,
"H": 1.00794,
"F": 18.9984032,
"CL": 35.453,
"BR": 79.904,
"I": 126.90447,
}
residue_info = {
"type_1": type_1_residues,
"type_2": type_2_residues,
"backbone_atoms": backbone_atoms,
}
nucleotide_names = ["DA", "DT", "DG", "DC"]
phosphate_atoms = ["OP1", "OP2", "P", "O5'"]
sugar_atoms = ["C1'", "C2'", "C3'", "C4'", "C5'", "O3'", "O4'"]
class AtomHelper:
@staticmethod
def get_atom_type(residue_type: str, atom: Atom) -> str:
"""
Returns the type of the atom. The atom can be a backbone, sidechain,
sugar or phosphate atom.
Parameters
----------
residue_type : str
The type of the residue to which the atom belongs.
atom : Atom
The atom to be processed.
Returns
-------
str
The type of the atom.
"""
atom_name = atom.get_name()
if residue_type in ["type_1", "type_2"]:
if atom_name in residue_info["backbone_atoms"]:
return "backbone"
else:
return "sidechain"
elif residue_type == "ssdna":
if atom_name in sugar_atoms:
return "sugar"
elif atom_name in phosphate_atoms:
return "phosphate"
else:
return "base"
else:
raise ValueError("Residue type is not valid.")
@staticmethod
def element_to_mass(element: str) -> float:
"""
Returns the mass of the element.
Parameters
----------
element : str
The element to be processed.
Returns
-------
float
The mass of the element.
"""
return element_masses[element]
class ChainTypeHelper:
@staticmethod
def get_chain_type(chain: BioChain) -> str:
"""
Returns the type of the chain. The chain can be a protein, ssdna.
Parameters
----------
chain : BioChain
The chain to be processed.
Returns
-------
str
The type of the chain.
"""
for residue in chain:
if residue.get_resname() in residue_names:
return "protein"
elif residue.get_resname() in nucleotide_names:
return "ssdna"
return "unknown"
class UnitTypeHelper:
@staticmethod
def get_unit_type(unit: Residue) -> str:
"""
Returns the type of the unit. The unit can be a type_1 residue, type_2
residue or ssdna.
Parameters
----------
unit : Residue
The unit to be processed.
Returns
-------
str
The type of the unit.
"""
if is_aa(unit):
if unit.get_resname() in residue_info["type_1"]:
return "type_1"
elif unit.get_resname() in residue_info["type_2"]:
return "type_2"
elif is_nucleic(unit):
return "ssdna"
else:
print(unit.get_resname())
raise ValueError("Unit is not a protein or ssdna residue.")
class NucleotideAtomHelper:
@staticmethod
def get_nucleotide_atom_type(atom) -> str:
"""
Returns the type of the atom. The atom can be a sugar, base or
phosphate atom.
Parameters
----------
atom : Atom
The atom to be processed.
Returns
-------
str
The type of the atom.
"""
return AtomHelper.get_atom_type("ssdna", atom)
class COMHelper:
@staticmethod
def _center_of_mass(points: List[Point], element_names: List[str]) -> Point:
"""
Calculates the center of mass of a set of points.
Args:
points (List[Point]): List of Point objects.
element_names (List[str]): List of element names.
Returns:
Point: Center of mass of the set of points.
"""
masses = [AtomHelper.element_to_mass(element) for element in element_names]
coordinates = np.average(
[point.coordinates for point in points], axis=0, weights=masses
)
return Point(coordinates)
@staticmethod
def get_geometric_center(atoms: List[Atom]) -> Union[Point, None]:
"""
Calculates the geometric center of a set of atoms.
Args:
atoms (List[Atom]): List of Atom objects.
Returns:
Point: Geometric center of the set of atoms or None
if the list of atoms is empty.
"""
if len(atoms) == 0:
return None
points = [Point(atom.get_coord()) for atom in atoms]
return COMHelper._center_of_mass(points, ["C"] * len(atoms))
@staticmethod
def center_of_mass(atoms: List[Atom]) -> Union[Point, None]:
"""
Calculates the center of mass of a set of atoms.
Args:
atoms (List[Atom]): List of Atom objects.
Returns:
Point: Center of mass of the set of atoms or None
if the list of atoms is empty.
"""
if len(atoms) == 0:
return None
element_names = [atom.element for atom in atoms]
points = [Point(atom.get_coord()) for atom in atoms]
return COMHelper._center_of_mass(points, element_names)
@staticmethod
def get_sidechain_com(residue: Residue) -> Union[np.ndarray, None]:
"""
Returns the center of mass of the sidechain of the residue.
Parameters
----------
residue : Residue
The residue to be processed.
Returns
-------
Union[np.ndarray, None]
The center of mass of the sidechain of the residue.
Can be None if the residue does not have a sidechain.
"""
sidechain_atoms = [
atom
for atom in residue.get_atoms()
if atom.get_name() not in residue_info["backbone_atoms"]
]
return (
COMHelper.center_of_mass(sidechain_atoms)
if len(sidechain_atoms) > 0
else None
)
@staticmethod
def process_protein_residue(
protein: Residue,
) -> Tuple[np.ndarray, Union[np.ndarray, None]]:
"""
Returns the center of mass of the residue and the center of mass
of the sidechain of the residue.
Parameters
----------
protein : Residue
The residue to be processed.
Returns
-------
Tuple[np.ndarray, Union[np.ndarray, None]]
The center of mass of the residue and the center of mass of the
sidechain of the residue.
"""
ca_coords = protein["CA"].get_coord()
ca_coords = Point(ca_coords)
sidechain_com = COMHelper.get_sidechain_com(protein)
return ca_coords, sidechain_com
@staticmethod
def get_nucleotide_coms(
nucleotide: Residue,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""
Returns the center of mass of the sugar, base and phosphate of the
nucleotide.
Parameters
----------
nucleotide : Residue
The nucleotide to be processed.
Returns
-------
Tuple[np.ndarray, np.ndarray, np.ndarray]
The center of mass of the sugar, base and phosphate of the
nucleotide.
"""
sugar_atoms = []
base_atoms = []
phosphate_atoms = []
for atom in nucleotide.get_atoms():
atom_type = NucleotideAtomHelper.get_nucleotide_atom_type(atom)
if atom_type == "sugar":
sugar_atoms.append(atom)
elif atom_type == "base":
base_atoms.append(atom)
elif atom_type == "phosphate":
phosphate_atoms.append(atom)
sugar_com = COMHelper.center_of_mass(sugar_atoms)
base_com = COMHelper.center_of_mass(base_atoms)
phosphate_com = COMHelper.center_of_mass(phosphate_atoms)
return sugar_com, base_com, phosphate_com
def combine_dicts(dict_list: List[Dict]) -> Dict:
# Initialize a new dictionary
combined_dict: Dict = {}
for d in dict_list:
for key, value in d.items():
# If the key is not yet in the new dict, add it
if key not in combined_dict:
combined_dict[key] = value
else:
# If the value is a numpy array, use numpy's concatenate
if isinstance(value, np.ndarray):
combined_dict[key] = np.concatenate((combined_dict[key], value))
# If the value is a pandas DataFrame, use pandas' concat
elif isinstance(value, pd.DataFrame):
combined_dict[key] = pd.concat(
[combined_dict[key], value]
).reset_index(drop=True)
return combined_dict
def print_key_shapes(dictionary: Dict) -> None:
for key, value in dictionary.items():
print(f"{key}: {value.shape}")