Coverage for biobb_mem/fatslim/fatslim_apl.py: 90%

30 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-11-30 14:15 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the FATSLiM Area per Lipid class and the command line interface.""" 

4from pathlib import Path 

5import csv 

6from biobb_common.generic.biobb_object import launcher 

7from biobb_common.tools import file_utils as fu 

8from biobb_mem.fatslim.common import FatslimBase 

9 

10 

11class FatslimAPL(FatslimBase): 

12 """ 

13 | biobb_mem FatslimAPL 

14 | Wrapper of the `FATSLiM area per lipid <https://pythonhosted.org/fatslim/documentation/apl.html>`_ module for area per lipid calculation. 

15 | FATSLiM is designed to provide efficient and robust analysis of physical parameters from MD trajectories, with a focus on processing large trajectory files quickly. 

16 

17 Args: 

18 input_top_path (str): Path to the input topology file. File type: input. `Sample file <https://github.com/bioexcel/biobb_mem/raw/main/biobb_mem/test/data/A01JD/A01JD.pdb>`_. Accepted formats: tpr (edam:format_2333), gro (edam:format_2033), g96 (edam:format_2033), pdb (edam:format_1476), brk (edam:format_2033), ent (edam:format_1476). 

19 input_traj_path (str) (Optional): Path to the GROMACS trajectory file. File type: input. `Sample file <https://github.com/bioexcel/biobb_mem/raw/main/biobb_mem/test/data/A01JD/A01JD.xtc>`_. Accepted formats: xtc (edam:format_3875), trr (edam:format_3910), cpt (edam:format_2333), gro (edam:format_2033), g96 (edam:format_2033), pdb (edam:format_1476), tng (edam:format_3876). 

20 input_ndx_path (str) (Optional): Path to the input index NDX file for lipid headgroups and the interacting group. File type: input. `Sample file <https://github.com/bioexcel/biobb_mem/raw/main/biobb_mem/test/data/A01JD/headgroups.ndx>`_. Accepted formats: ndx (edam:format_2033). 

21 output_csv_path (str): Path to the output CSV file with the area of each lipid in each analysed frame. File type: output. `Sample file <https://github.com/bioexcel/biobb_mem/raw/main/biobb_mem/test/reference/fatslim/apl.csv>`_. Accepted formats: csv (edam:format_3752). 

22 properties (dic - Python dictionary object containing the tool parameters, not input/output files): 

23 * **lipid_selection** (*str*) - ("not protein and element P") Headgroups MDAnalysis `selection <https://docs.mdanalysis.org/stable/documentation_pages/selections.html>`_. 

24 * **protein_selection** (*str*) - ("protein and not element H") Protein selection interacting with the membrane. 

25 * **cutoff** (*float*) - (3) This option allows user to specify the cutoff distance (in nm) to be used when performing the neighbor search needed by the APL calculation algorithm 

26 * **limit** (*float*) - (10) This option allows user to specify the upper limit (in nm2) for a valid area per lipid value. 

27 * **begin_frame** (*int*) - (-1) Index of the first trajectory frame analysed (-1: the first frame). 

28 * **end_frame** (*int*) - (-1) Index of the trajectory frame where the analysis stops, not analysed (-1: up to the last frame). 

29 * **ignore_no_box** (*bool*) - (False) Ignore the absence of box information in the trajectory. If the trajectory does not contain box information, the box will be set to the minimum and maximum positions of the atoms in the trajectory. 

30 * **binary_path** (*str*) - ("fatslim") Path to the fatslim executable binary. 

31 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files. 

32 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist. 

33 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

34 * **container_path** (*str*) - (None) Container path definition. 

35 * **container_image** (*str*) - ('afandiadib/ambertools:serial') Container image definition. 

36 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition. 

37 * **container_user_id** (*str*) - (None) Container user_id definition. 

38 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container. 

39 

40 Examples: 

41 This is a use example of how to use the building block from Python:: 

42 

43 from biobb_mem.fatslim.fatslim_apl import fatslim_apl 

44 prop = { 

45 'lipid_selection': '(resname DPPC and name P8)', 

46 'cutoff': 3 

47 } 

48 fatslim_apl(input_top_path='/path/to/myTopology.tpr', 

49 input_traj_path='/path/to/myTrajectory.xtc', 

50 output_csv_path='/path/to/apl.csv', 

51 properties=prop) 

52 

53 Info: 

54 * wrapped_software: 

55 * name: FATSLiM 

56 * version: 0.2.2 

57 * license: GNU 

58 * ontology: 

59 * name: EDAM 

60 * schema: http://edamontology.org/EDAM.owl 

61 

62 """ 

63 

64 def __init__(self, input_top_path: str, output_csv_path: str, input_traj_path: str | None = None, input_ndx_path: str | None = None, properties: dict | None = None, **kwargs) -> None: 

65 super().__init__(**locals()) 

66 

67 def run(self) -> None: 

68 """Execute the :class:`FatslimAPL <fatslim.fatslim_apl.FatslimAPL>` object.""" 

69 tmp_dir = Path(self.create_tmp_dir()) 

70 _, cmd = self.fatslim_cmd(tmp_dir, "apl", {"headgroups": self.lipid_selection, "protein": self.protein_selection}) 

71 self.cmd = cmd + [ 

72 "--begin-frame", str(self.begin_frame), 

73 "--end-frame", str(self.end_frame), 

74 "--export-apl-raw", self.staged_path(tmp_dir / "apl.csv"), 

75 "--apl-cutoff", str(self.cutoff), 

76 "--apl-limit", str(self.limit) 

77 ] 

78 self.run_biobb() 

79 

80 # fatslim writes one CSV per analysed frame: apl_frame_00000.csv for the first one 

81 frame_csvs = sorted(tmp_dir.glob("apl_frame_*.csv")) 

82 if not frame_csvs: 

83 fu.log("No area per lipid file: FATSLiM analysed no frame or failed.", self.out_log, self.global_log) 

84 return 

85 first_frame = max(0, self.begin_frame) 

86 with open(self.sandbox_file("output_csv_path"), "w", newline="") as output_csv: 

87 writer = csv.writer(output_csv) 

88 for i, frame_csv in enumerate(frame_csvs): 

89 with open(frame_csv, newline="") as f: 

90 header, *rows = csv.reader(f) 

91 if i == 0: 

92 writer.writerow(["frame", *header]) 

93 frame = first_frame + int(frame_csv.stem.rsplit("_", 1)[1]) 

94 writer.writerows([frame, *row] for row in rows) 

95 

96 

97fatslim_apl = launcher(FatslimAPL) 

98 

99if __name__ == '__main__': 

100 FatslimAPL.main()