Skip to content

Commit ac1f2ac

Browse files
committed
updating counting flexible dihedrals and tests
1 parent 131a68d commit ac1f2ac

6 files changed

Lines changed: 171 additions & 236 deletions

File tree

‎CodeEntropy/entropy/workflow.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ def _build_reduced_universe(self) -> Any:
239239
MDAnalysis Universe (reduced according to user selections).
240240
"""
241241
selection = self._args.selection_string
242-
start = self._args.start
243-
end = self._args.end
244-
step = self._args.step
242+
start = self._args.start or 0
243+
end = len(self._universe.trajectory) if self._args.end == -1 else self._args.end
244+
step = self._args.step or 1
245245
if selection == "all":
246246
reduced_atoms = self._universe
247247
else:

‎CodeEntropy/levels/dihedrals.py‎

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ def build_conformational_states(
7979
"""
8080
number_groups = len(groups)
8181
states_ua: dict[UAKey, list[str]] = {}
82+
# states_res: list[list[str]] = [[]]
8283
states_res: list[list[str]] = [[] for _ in range(number_groups)]
8384
flexible_ua: dict[UAKey, int] = {}
84-
flexible_res: list[int] = [0] * number_groups
85+
flexible_res: list[int] = []
8586

8687
task: TaskID | None = None
8788
if progress is not None:
@@ -132,6 +133,11 @@ def build_conformational_states(
132133
if progress is not None and task is not None:
133134
progress.advance(task)
134135

136+
logger.debug(f"States UA: {states_ua}")
137+
logger.debug(f"Number of flexible dihedrals UA: {flexible_ua}")
138+
logger.debug(f"States Res: {states_res}")
139+
logger.debug(f"Number of flexible dihedrals Res: {flexible_res}")
140+
135141
return states_ua, states_res, flexible_ua, flexible_res
136142

137143
def _select_heavy_residue(self, mol: Any, res_id: int) -> Any:
@@ -217,10 +223,11 @@ def _identify_peaks(
217223
number_frames = len(rep_mol.trajectory)
218224
num_residues = len(rep_mol.residues)
219225

226+
num_dihedrals_ua: list[Any] = [0 for _ in range(num_residues)]
227+
phi_ua = {}
228+
phi_res: dict[list, list[float]] = {}
220229
peaks_ua: list[list[Any]] = [[] for _ in range(num_residues)]
221230
peaks_res: list[Any] = []
222-
phi_ua: list[list[Any]] = [[] for _ in range(num_residues)]
223-
phi_res: list[Any] = []
224231

225232
for molecule in molecules:
226233
mol = self._universe_operations.extract_fragment(data_container, molecule)
@@ -230,19 +237,21 @@ def _identify_peaks(
230237
for res_id in range(num_residues):
231238
heavy_res = self._select_heavy_residue(mol, res_id)
232239
dihedrals = self._get_dihedrals(heavy_res, level)
233-
num_dihedrals_ua = len(dihedrals)
234-
if num_dihedrals_ua == 0:
240+
num_dihedrals_ua[res_id] = len(dihedrals)
241+
if num_dihedrals_ua[res_id] == 0:
235242
# No dihedrals, no peaks
236243
phi_ua[res_id] = []
237244

238245
else:
246+
if res_id not in phi_ua:
247+
phi_ua[res_id] = {}
239248
dihedral_results = Dihedral(dihedrals).run()
240-
phis = self._process_dihedral_phi(
249+
phi_ua[res_id] = self._process_dihedral_phi(
241250
dihedral_results,
242-
num_dihedrals_ua,
251+
num_dihedrals_ua[res_id],
243252
number_frames,
253+
phi_ua[res_id],
244254
)
245-
phi_ua[res_id].append(phis)
246255

247256
elif level == "residue":
248257
dihedrals = self._get_dihedrals(mol, level)
@@ -253,24 +262,33 @@ def _identify_peaks(
253262

254263
else:
255264
dihedral_results = Dihedral(dihedrals).run()
256-
phis = self._process_dihedral_phi(
265+
phi_res = self._process_dihedral_phi(
257266
dihedral_results,
258267
num_dihedrals_res,
259268
number_frames,
269+
phi_res,
260270
)
261-
phi_res.append(phis)
271+
272+
logger.debug(f"phi_ua {phi_ua}")
273+
logger.debug(f"phi_res {phi_res}")
262274

263275
for level in level_list:
264276
if level == "united_atom":
265277
for res_id in range(num_residues):
266-
peaks_ua[res_id] = self._process_histogram(
267-
num_dihedrals_ua, phi_ua[res_id], bin_width
268-
)
278+
if phi_ua[res_id] is None:
279+
peaks_ua[res_id] = []
280+
else:
281+
peaks_ua[res_id] = self._process_histogram(
282+
num_dihedrals_ua[res_id], phi_ua[res_id], bin_width
283+
)
269284

270285
elif level == "residue":
271-
peaks_res = self._process_histogram(
272-
num_dihedrals_res, phi_res, bin_width
273-
)
286+
if phi_res is None:
287+
peaks_res = []
288+
else:
289+
peaks_res = self._process_histogram(
290+
num_dihedrals_res, phi_res, bin_width
291+
)
274292

275293
return peaks_ua, peaks_res
276294

@@ -279,9 +297,10 @@ def _process_dihedral_phi(
279297
dihedral_results,
280298
num_dihedrals,
281299
number_frames,
300+
phi_values,
282301
):
283302
"""
284-
Find peaks from array of dihedral angle values.
303+
Find array of dihedral angle values.
285304
286305
Args:
287306
dihedral_results: the result of MDAnalysis Dihedrals.run.
@@ -290,7 +309,6 @@ def _process_dihedral_phi(
290309
Returns:
291310
peaks
292311
"""
293-
phi_values = [[] for _ in range(num_dihedrals)]
294312
for dihedral_index in range(num_dihedrals):
295313
phi: list[float] = []
296314

@@ -300,7 +318,10 @@ def _process_dihedral_phi(
300318
value += 360
301319
phi.append(float(value))
302320

303-
phi_values[dihedral_index].append(phi)
321+
if dihedral_index not in phi_values:
322+
phi_values[dihedral_index] = phi
323+
else:
324+
phi_values[dihedral_index].extend(phi)
304325

305326
return phi_values
306327

@@ -325,6 +346,9 @@ def _process_histogram(
325346
phi = phi_values[dihedral_index]
326347
number_bins = int(360 / bin_width)
327348
popul, bin_edges = np.histogram(a=phi, bins=number_bins, range=(0, 360))
349+
350+
logger.debug(f"Histogram: {popul}")
351+
328352
bin_value = [
329353
0.5 * (bin_edges[i] + bin_edges[i + 1]) for i in range(0, len(popul))
330354
]
@@ -401,6 +425,8 @@ def _assign_states(
401425
number_frames = len(rep_mol.trajectory)
402426
num_residues = len(rep_mol.residues)
403427

428+
state_res = []
429+
flex_res = 0
404430
for molecule in molecules:
405431
mol = self._universe_operations.extract_fragment(data_container, molecule)
406432

@@ -417,44 +443,38 @@ def _assign_states(
417443
flexible_ua[key] = 0
418444
else:
419445
dihedral_results = Dihedral(dihedrals).run()
420-
states, flexible_ua[key] = self._process_conformations(
446+
states, flexible = self._process_conformations(
421447
peaks_ua[res_id],
422448
dihedral_results,
423449
num_dihedrals,
424450
number_frames,
425451
)
426452
if key not in states_ua:
427453
states_ua[key] = states
454+
flexible_ua[key] = flexible
428455
else:
429456
states_ua[key].extend(states)
430-
431-
logger.debug(f"States UA {key}: {states_ua[key]}")
432-
logger.debug(
433-
f"Number of flexible dihedrals UA {key}: {flexible_ua[key]}"
434-
)
457+
flexible_ua[key] = max(flexible_ua[key], flexible)
435458

436459
if level == "residue":
437460
dihedrals = self._get_dihedrals(mol, level)
438461
num_dihedrals = len(dihedrals)
439462
if num_dihedrals == 0:
440463
# No dihedrals, no conformations
441-
states_res[group_id] = []
442-
flexible_res[group_id] = 0
464+
state_res = []
443465
else:
444466
dihedral_results = Dihedral(dihedrals).run()
445-
states, flexible_res[group_id] = self._process_conformations(
467+
states, flexible = self._process_conformations(
446468
peaks_res,
447469
dihedral_results,
448470
num_dihedrals,
449471
number_frames,
450-
molecule,
451472
)
452-
states_res[group_id].extend(states)
473+
state_res.extend(states)
474+
flex_res = max(flex_res, flexible)
453475

454-
logger.debug(f"States Res {group_id}: {states_res[group_id]}")
455-
logger.debug(
456-
f"Number of flexible dihedrals Res {group_id}: {flexible_res[group_id]}"
457-
)
476+
states_res.append(state_res)
477+
flexible_res.append(flex_res)
458478

459479
def _process_conformations(
460480
self, peaks, dihedral_results, num_dihedrals, number_frames
@@ -475,8 +495,8 @@ def _process_conformations(
475495
conformation: list[Any] = []
476496

477497
# Check for flexible dihedrals
478-
if len(peaks[dihedral_index]) > 1:
479-
num_flexible += 1
498+
# if len(peaks[dihedral_index]) > 1:
499+
# num_flexible += 1
480500

481501
# Get conformations
482502
for timestep in range(number_frames):
@@ -492,6 +512,10 @@ def _process_conformations(
492512
distances = [abs(value - peak) for peak in peaks[dihedral_index]]
493513
conformation.append(np.argmin(distances))
494514

515+
unique = np.unique(conformation)
516+
if len(unique) > 1:
517+
num_flexible += 1
518+
495519
conformations.append(conformation)
496520

497521
# Concatenate all the dihedrals in the molecule into the state

‎tests/unit/CodeEntropy/entropy/test_workflow.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def test_build_reduced_universe_non_all_selects_and_writes_universe():
235235

236236
assert out is reduced2
237237
uops.select_atoms.assert_called_once_with(universe, "protein")
238-
uops.select_frames.assert_called_once_with(reduced, 0, -1, 1)
238+
uops.select_frames.assert_called_once_with(reduced, 0, 3, 1)
239239

240240

241241
def test_compute_water_entropy_updates_selection_string_and_calls_internal_method():
24 KB
Binary file not shown.

0 commit comments

Comments
 (0)