Coverage for biobb_mem/common.py: 32%
22 statements
« prev ^ index » next coverage.py v7.18.0, created at 2026-11-30 14:15 +0000
« prev ^ index » next coverage.py v7.18.0, created at 2026-11-30 14:15 +0000
1"""Common functions of package biobb_mem."""
2import numpy as np
3from MDAnalysis.transformations import translate, set_dimensions
4from biobb_common.tools import file_utils as fu
7def set_box(u):
8 """Set the box dimensions of the universe based on the positions of the atoms."""
9 # Initialize min and max positions with extreme values
10 min_pos = np.full(3, np.inf)
11 max_pos = np.full(3, -np.inf)
13 # Iterate over all frames to find the overall min and max positions
14 for ts in u.trajectory:
15 positions = u.atoms.positions
16 min_pos = np.minimum(min_pos, positions.min(axis=0))
17 max_pos = np.maximum(max_pos, positions.max(axis=0))
19 # Calculate the dimensions of the box
20 box_dimensions = max_pos - min_pos
21 transformations = [
22 set_dimensions([*box_dimensions, 90, 90, 90]),
23 translate(np.array([0.0, 0.0, -min_pos[2]])) # Shift the system so that the minimum z is at 0
24 ]
25 u.trajectory.add_transformations(*transformations)
28def ignore_no_box(u, ignore_no_box, out_log, global_log) -> bool:
29 """Go from a universe without box to the box of :func:`set_box` with **ignore_no_box**, or to a ValueError.
30 True when the box was set."""
31 if u.dimensions is None:
32 if ignore_no_box:
33 fu.log('Setting box dimensions using the minimum and maximum positions of the atoms.',
34 out_log, global_log)
35 set_box(u)
36 return True
37 else:
38 fu.log('The trajectory does not contain box information. '
39 'Please set the ignore_no_box property to True to ignore this error.',
40 out_log, global_log)
41 raise ValueError("Box dimensions are required but not found in the trajectory.")
42 return False