Skip to content

Commit d6cbdc1

Browse files
committed
Further refinements to the per timestep refactor, relocating functions to LevelManager
- Relocating the following functions as they deal more intrinsically with the levels rather than the entropy calculations - Relocated `compute_dihedral_conformations` to `LevelManager` - Relocated `build_covariance_matrices` to `LevelManager` - Relocated `update_force_torque_matrices` to `LevelManager`
1 parent 5498916 commit d6cbdc1

2 files changed

Lines changed: 191 additions & 170 deletions

File tree

CodeEntropy/entropy.py

Lines changed: 21 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,15 @@ def execute(self):
5252
reduced_atom, number_molecules, levels = self._initialize_molecules()
5353

5454
force_matrices, torque_matrices, states_ua, states_res = (
55-
self._build_covariance_matrices(
56-
reduced_atom, number_molecules, levels, start, end, step, number_frames
55+
self._level_manager.build_covariance_matrices(
56+
self,
57+
reduced_atom,
58+
number_molecules,
59+
levels,
60+
start,
61+
end,
62+
step,
63+
number_frames,
5764
)
5865
)
5966

@@ -116,164 +123,6 @@ def _initialize_molecules(self):
116123
number_molecules, levels = self._level_manager.select_levels(reduced_atom)
117124
return reduced_atom, number_molecules, levels
118125

119-
def _build_covariance_matrices(
120-
self, reduced_atom, number_molecules, levels, start, end, step, number_frames
121-
):
122-
"""
123-
Construct force and torque covariance matrices for all molecules and levels.
124-
125-
Parameters:
126-
reduced_atom (Universe): The reduced atom selection.
127-
number_molecules (int): Number of molecules in the system.
128-
levels (list): List of entropy levels per molecule.
129-
start (int): Start frame index.
130-
end (int): End frame index.
131-
step (int): Step size for frame iteration.
132-
number_frames (int): Total number of frames to process.
133-
134-
Returns:
135-
tuple: A tuple containing:
136-
- force_matrices (dict): Force covariance matrices by level.
137-
- torque_matrices (dict): Torque covariance matrices by level.
138-
- states_ua (dict): Conformational states at the united-atom level.
139-
- states_res (list): Conformational states at the residue level.
140-
"""
141-
force_matrices = {
142-
"ua": {},
143-
"res": [None] * number_molecules,
144-
"poly": [None] * number_molecules,
145-
}
146-
torque_matrices = {
147-
"ua": {},
148-
"res": [None] * number_molecules,
149-
"poly": [None] * number_molecules,
150-
}
151-
states_ua = {}
152-
states_res = [None] * number_molecules
153-
154-
for timestep in reduced_atom.trajectory[start:end:step]:
155-
time_index = timestep.frame - start
156-
157-
for mol_id in range(number_molecules):
158-
mol = self._get_molecule_container(reduced_atom, mol_id)
159-
for level in levels[mol_id]:
160-
self._update_force_torque_matrices(
161-
mol,
162-
mol_id,
163-
level,
164-
levels[mol_id],
165-
time_index,
166-
number_frames,
167-
force_matrices,
168-
torque_matrices,
169-
)
170-
171-
return force_matrices, torque_matrices, states_ua, states_res
172-
173-
def _update_force_torque_matrices(
174-
self,
175-
mol,
176-
mol_id,
177-
level,
178-
level_list,
179-
time_index,
180-
num_frames,
181-
force_matrices,
182-
torque_matrices,
183-
):
184-
"""
185-
Update force and torque matrices for a given molecule and entropy level.
186-
187-
Parameters:
188-
mol (AtomGroup): The molecule to process.
189-
mol_id (int): Index of the molecule.
190-
level (str): Current entropy level ("united_atom", "residue", or "polymer").
191-
level_list (list): List of levels for the molecule.
192-
time_index (int): Index of the current frame.
193-
num_frames (int): Total number of frames.
194-
force_matrices (dict): Dictionary of force matrices to update.
195-
torque_matrices (dict): Dictionary of torque matrices to update.
196-
"""
197-
highest = level == level_list[-1]
198-
199-
if level == "united_atom":
200-
for res_id, residue in enumerate(mol.residues):
201-
key = (mol_id, res_id)
202-
res = self._run_manager.new_U_select_atom(
203-
mol, f"index {residue.atoms.indices[0]}:{residue.atoms.indices[-1]}"
204-
)
205-
res.trajectory[time_index]
206-
207-
f_mat, t_mat = self._level_manager.get_matrices(
208-
res,
209-
level,
210-
num_frames,
211-
highest,
212-
force_matrices["ua"].get(key),
213-
torque_matrices["ua"].get(key),
214-
)
215-
force_matrices["ua"][key] = f_mat
216-
torque_matrices["ua"][key] = t_mat
217-
218-
elif level in ["residue", "polymer"]:
219-
mol.trajectory[time_index]
220-
key = "res" if level == "residue" else "poly"
221-
f_mat, t_mat = self._level_manager.get_matrices(
222-
mol,
223-
level,
224-
num_frames,
225-
highest,
226-
force_matrices[key][mol_id],
227-
torque_matrices[key][mol_id],
228-
)
229-
force_matrices[key][mol_id] = f_mat
230-
torque_matrices[key][mol_id] = t_mat
231-
232-
def _compute_dihedral_conformations(
233-
self,
234-
selector,
235-
level,
236-
ce,
237-
number_frames,
238-
bin_width,
239-
start,
240-
end,
241-
step,
242-
):
243-
"""
244-
Compute dihedral conformations for a given selector and entropy level.
245-
246-
Parameters:
247-
selector (AtomGroup): Atom selection to compute dihedrals for.
248-
level (str): Entropy level ("united_atom" or "residue").
249-
ce (ConformationalEntropy): Conformational entropy calculator.
250-
number_frames (int): Number of frames to process.
251-
bin_width (float): Bin width for dihedral angle discretization.
252-
start (int): Start frame index.
253-
end (int): End frame index.
254-
step (int): Step size for frame iteration.
255-
256-
Returns:
257-
tuple: A tuple containing:
258-
- states (list): List of conformation strings per frame.
259-
- dihedrals (list): List of dihedral angle definitions.
260-
"""
261-
dihedrals = self._level_manager.get_dihedrals(selector, level)
262-
num_dihedrals = len(dihedrals)
263-
264-
conformation = np.zeros((num_dihedrals, number_frames))
265-
for i, dihedral in enumerate(dihedrals):
266-
conformation[i] = ce.assign_conformation(
267-
selector, dihedral, number_frames, bin_width, start, end, step
268-
)
269-
270-
states = [
271-
"".join(str(int(conformation[d][f])) for d in range(num_dihedrals))
272-
for f in range(number_frames)
273-
]
274-
275-
return states, dihedrals
276-
277126
def _compute_entropies(
278127
self,
279128
reduced_atom,
@@ -346,15 +195,17 @@ def _compute_entropies(
346195
res_container, "not name H*"
347196
)
348197

349-
states_ua[key], _ = self._compute_dihedral_conformations(
350-
heavy_res,
351-
level,
352-
ce,
353-
number_frames,
354-
bin_width,
355-
start,
356-
end,
357-
step,
198+
states_ua[key] = (
199+
self._level_manager.compute_dihedral_conformations(
200+
heavy_res,
201+
level,
202+
ce,
203+
number_frames,
204+
bin_width,
205+
start,
206+
end,
207+
step,
208+
)
358209
)
359210

360211
self._process_united_atom_entropy(
@@ -372,7 +223,7 @@ def _compute_entropies(
372223
)
373224

374225
elif level == "residue":
375-
states_res, _ = self._compute_dihedral_conformations(
226+
states_res = self._level_manager.compute_dihedral_conformations(
376227
mol, level, ce, number_frames, bin_width, start, end, step
377228
)
378229

0 commit comments

Comments
 (0)