Coverage for biobb_mem/lipyphilic_biobb/common.py: 30%

20 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-24 13:50 +0000

1""" Common functions for package biobb_mem.lipyphilic_biobb """ 

2import numpy as np 

3from MDAnalysis.transformations import translate, set_dimensions 

4from biobb_common.tools import file_utils as fu 

5 

6 

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) 

12 

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)) 

18 

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) 

26 

27 

28def ignore_no_box(u, ignore_no_box, out_log, global_log): 

29 if u.dimensions is None: 

30 if ignore_no_box: 

31 fu.log('Setting box dimensions using the minimum and maximum positions of the atoms.', 

32 out_log, global_log) 

33 set_box(u) 

34 else: 

35 fu.log('The trajectory does not contain box information. ' 

36 'Please set the ignore_no_box property to True to ignore this error.', 

37 out_log, global_log) 

38 raise ValueError("Box dimensions are required but not found in the trajectory.")