Coverage for model_workflow/tools/get_box_size.py: 90%
21 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-23 10:54 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-23 10:54 +0000
1# This script is used to find out the box size (x, y and z) in a pdb topology
2# This process is carried by Gromacs
4import os
5from subprocess import run, PIPE, Popen
7from model_workflow.utils.constants import GROMACS_EXECUTABLE
9# Set the box analysis filename
10# This analysis is used here to mine the box size data
11# It is never used further
12box_analysis = 'box.xvg'
14# input_topology_filename - The name string of the input topology file (path)
15# Tested supported formats are .pdb and .tpr
16# input_trajectory_filename - The name string of the input trajectory file (path)
17# Tested supported formats are .trr and .xtc
20def get_box_size(
21 input_topology_filename: str,
22 input_trajectory_filename: str,
23) -> tuple:
25 # Generate the box analysis
26 # WARNING: Do not use the first_frame here instead of the trajectory
27 # In modified topologies the first frame pdb may have lost box size data
28 p = Popen([
29 "echo",
30 "System",
31 ], stdout=PIPE)
32 logs = run([
33 GROMACS_EXECUTABLE,
34 "traj",
35 "-s",
36 input_topology_filename,
37 "-f",
38 input_trajectory_filename,
39 '-ob',
40 box_analysis,
41 '-b',
42 '0',
43 '-e',
44 '0',
45 '-quiet'
46 ], stdin=p.stdout, stdout=PIPE, stderr=PIPE).stdout.decode()
47 p.stdout.close()
49 if not os.path.exists(box_analysis):
50 print(logs)
51 raise SystemExit('ERROR: Something went wrong with Gromacs')
53 # Read the box analysis and get the desired data
54 boxsizex, boxsizey, boxsizez = "", "", ""
55 with open(box_analysis, 'r') as file:
56 for line in file:
57 if line.startswith(("#", "@")) == False:
59 # Simulation box 'x' size
60 boxsizex = float(line.split()[1])
62 # Simulation box 'y' size
63 boxsizey = float(line.split()[2])
65 # Simulation box 'z' size
66 boxsizez = float(line.split()[3])
68 # Display it
69 print('Box size: (' + str(boxsizex) + ',' +
70 str(boxsizey) + ',' + str(boxsizez) + ')')
72 # Remove the box analysis
73 os.remove(box_analysis)
75 # Return gromacs logs
76 return (boxsizex, boxsizey, boxsizez)