Coverage for biobb_mem/fatslim/common.py: 83%
36 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 class of the FATSLiM blocks of package biobb_mem."""
2from pathlib import Path, PurePath
3import sys
4import MDAnalysis as mda
5from biobb_common.generic.biobb_object import BiobbObject
6from biobb_mem.common import ignore_no_box
9class FatslimBase(BiobbObject):
10 """Base class of the FATSLiM blocks: the input files of fatslim and the start of its command line."""
12 def fatslim_cmd(self, tmp_dir: Path, command: str, groups: dict[str, str],
13 frame: int | None = None) -> tuple[mda.Universe, list[str]]:
14 """Go from a fatslim command to the universe of the inputs and the start of the command line, with the
15 index (input_ndx_path or the **groups** selections) and the files fatslim reads written in **tmp_dir**.
16 fatslim reads the whole trajectory, or only its **frame** when given."""
17 # MDAnalysis runs in the host, so it reads the original input files
18 top_path = self.io_dict["in"]["input_top_path"]
19 traj_path = self.io_dict["in"].get("input_traj_path")
20 u = mda.Universe(top_path, traj_path) if traj_path else mda.Universe(top_path)
21 box_set = ignore_no_box(u, self.ignore_no_box, self.out_log, self.global_log)
23 if self.io_dict["in"].get("input_ndx_path"):
24 ndx = self.sandbox_file("input_ndx_path")
25 else:
26 ndx = str(tmp_dir / "groups.ndx")
27 with mda.selections.gromacs.SelectionWriter(ndx, mode="w") as ndx_file:
28 for name, selection in groups.items():
29 ndx_file.write(u.select_atoms(selection), name=name)
31 # fatslim reads the topology from a GRO file with the box
32 if frame is not None:
33 u.trajectory[frame]
34 conf = tmp_dir / "conf.gro"
35 u.atoms.write(str(conf))
37 if self.container_path or PurePath(self.binary_path).name != "fatslim":
38 cmd = [self.binary_path]
39 else:
40 # FATSLiM 0.2.2 uses NumPy aliases removed in modern NumPy versions
41 compat = tmp_dir / "fatslim_numpy_compat.py"
42 compat.write_text("import sys\nimport numpy as np\nif not hasattr(np, 'int'):\n np.int = int\n"
43 "from fatslimlib import main\nsys.exit(main(sys.argv[1:]))\n")
44 cmd = [sys.executable, self.staged_path(compat)]
45 cmd += [command, "-n", self.staged_path(ndx), "-c", self.staged_path(conf)]
47 if traj_path and frame is None:
48 traj = self.sandbox_file("input_traj_path")
49 # fatslim reads xtc, trr and gro files with a box; a container sees only the files of its volume
50 if box_set or PurePath(traj).suffix.lower() not in (".xtc", ".trr", ".gro") or (
51 self.container_path and Path(self.staged_path(traj)).is_absolute()):
52 traj = str(tmp_dir / "traj.xtc")
53 with mda.Writer(traj, u.atoms.n_atoms) as writer:
54 for _ in u.trajectory:
55 writer.write(u.atoms)
56 cmd += ["-t", self.staged_path(traj)]
57 return u, cmd