Coverage for biobb_mem/lipyphilic_biobb/lpp_flip_flop.py: 90%

31 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 Lipyphilic FlipFlop class and the command line interface.""" 

4from biobb_common.generic.biobb_object import BiobbObject, launcher 

5import MDAnalysis as mda 

6from biobb_mem.common import ignore_no_box 

7from lipyphilic.analysis.flip_flop import FlipFlop 

8import pandas as pd 

9import numpy as np 

10 

11 

12class LPPFlipFlop(BiobbObject): 

13 """ 

14 | biobb_mem LPPFlipFlop 

15 | Wrapper of the LiPyphilic FlipFlop module for finding flip-flop events in a lipid bilayer. 

16 | LiPyphilic is a Python package for analyzing MD simulations of lipid bilayers. The parameter names and defaults are the same as the ones in the official `Lipyphilic documentation <https://lipyphilic.readthedocs.io/en/stable/reference/lib/flip_flop.html>`_. 

17 

18 Args: 

19 input_top_path (str): Path to the input structure or topology file. File type: input. `Sample file <https://github.com/bioexcel/biobb_mem/raw/main/biobb_mem/test/data/A01JD/A01JD.pdb>`_. Accepted formats: crd (edam:format_3878), gro (edam:format_2033), mdcrd (edam:format_3878), mol2 (edam:format_3816), pdb (edam:format_1476), pdbqt (edam:format_1476), prmtop (edam:format_3881), psf (edam:format_3882), top (edam:format_3881), tpr (edam:format_2333), xml (edam:format_2332), xyz (edam:format_3887). 

20 input_traj_path (str): Path to the input trajectory to be processed. File type: input. `Sample file <https://github.com/bioexcel/biobb_mem/raw/main/biobb_mem/test/data/A01JD/A01JD.xtc>`_. Accepted formats: arc (edam:format_2333), crd (edam:format_3878), dcd (edam:format_3878), ent (edam:format_1476), gro (edam:format_2033), inpcrd (edam:format_3878), mdcrd (edam:format_3878), mol2 (edam:format_3816), nc (edam:format_3650), pdb (edam:format_1476), pdbqt (edam:format_1476), restrt (edam:format_3886), tng (edam:format_3876), trr (edam:format_3910), xtc (edam:format_3875), xyz (edam:format_3887). 

21 input_leaflets_path (str): Path to the input leaflet assignments. File type: input. `Sample file <https://github.com/bioexcel/biobb_mem/raw/main/biobb_mem/test/reference/lipyphilic_biobb/leaflets_data.csv>`_. Accepted formats: csv (edam:format_3752), npy (edam:format_4003). 

22 output_flip_flop_path (str): Path to the output flip-flop data. File type: output. `Sample file <https://github.com/bioexcel/biobb_mem/raw/main/biobb_mem/test/reference/lipyphilic_biobb/flip_flop.csv>`_. Accepted formats: csv (edam:format_3752). 

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

24 * **start** (*int*) - (None) Starting frame for slicing. 

25 * **stop** (*int*) - (None) Ending frame for slicing. 

26 * **steps** (*int*) - (None) Step for slicing. 

27 * **lipid_sel** (*str*) - ("all") Selection string for the lipids in a membrane. The selection should cover **all** residues in the membrane, including cholesterol. 

28 * **frame_cutoff** (*float*) - (1) To be counted as a successful flip-flop, a molecule must reside in its new leaflet for at least ‘frame_cutoff’ consecutive frames. The default is 1, in which case the molecule only needs to move to the opposing leaflet for a single frame for the flip-flop to be successful. 

29 * **ignore_no_box** (*bool*) - (True) 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 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files. 

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

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

33 

34 Examples: 

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

36 

37 from biobb_mem.lipyphilic_biobb.lpp_flip_flop import lpp_flip_flop 

38 prop = { 

39 'lipid_sel': 'name GL1 GL2 ROH', 

40 } 

41 lpp_flip_flop(input_top_path='/path/to/myTopology.tpr', 

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

43 input_leaflets_path='/path/to/leaflets.csv', 

44 output_flip_flop_path='/path/to/flip_flops.csv', 

45 properties=prop) 

46 

47 Info: 

48 * wrapped_software: 

49 * name: LiPyphilic 

50 * version: 0.11.0 

51 * license: GPL-2.0 

52 * ontology: 

53 * name: EDAM 

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

55 

56 """ 

57 

58 def __init__(self, input_top_path: str, input_traj_path: str, 

59 input_leaflets_path: str, output_flip_flop_path: str, 

60 properties: dict | None = None, **kwargs) -> None: 

61 super().__init__(**locals()) 

62 

63 def run(self) -> None: 

64 """Execute the :class:`LPPFlipFlop <lipyphilic_biobb.lpp_flip_flop.LPPFlipFlop>` object.""" 

65 

66 # Load the trajectory 

67 u = mda.Universe(self.sandbox_file("input_top_path"), self.sandbox_file("input_traj_path")) 

68 ignore_no_box(u, self.ignore_no_box, self.out_log, self.global_log) 

69 # Load the leaflets 

70 leaflets_path = self.sandbox_file("input_leaflets_path") 

71 if leaflets_path.endswith('.csv'): 

72 df = pd.read_csv(leaflets_path) 

73 n_frames = len(df['frame'].unique()) 

74 n_residues = len(df['resindex'].unique()) 

75 leaflets = df['leaflet_index'].values.reshape(n_frames, n_residues).T 

76 leaflets = np.asarray(leaflets, dtype=np.int8) 

77 else: # .npy file 

78 leaflets = np.load(leaflets_path).astype(np.int8) 

79 # Create FlipFlop object 

80 flip_flop = FlipFlop( 

81 universe=u, 

82 lipid_sel=self.lipid_sel, 

83 leaflets=leaflets, 

84 frame_cutoff=self.frame_cutoff, 

85 ) 

86 # Run the analysis 

87 flip_flop.run(start=self.start, stop=self.stop, step=self.steps) 

88 

89 # Save the results 

90 resnames = [] 

91 if flip_flop.flip_flops.size > 0: 

92 resnames = u.residues.resnames[flip_flop.flip_flops[:, 0]] 

93 else: 

94 print('No flip-flop events found.') 

95 

96 df = pd.DataFrame({ 

97 'resname': resnames, 

98 'resindex': flip_flop.flip_flops[:, 0], 

99 'start_frame': flip_flop.flip_flops[:, 1], 

100 'end_frame': flip_flop.flip_flops[:, 2], 

101 'end_leaflet': flip_flop.flip_flops[:, 3] 

102 }) 

103 

104 # Save the DataFrame to a CSV file 

105 df.to_csv(self.sandbox_file("output_flip_flop_path"), index=False) 

106 

107 

108lpp_flip_flop = launcher(LPPFlipFlop) 

109 

110if __name__ == '__main__': 

111 LPPFlipFlop.main()