Coverage for biobb_chemistry/acpype/common.py: 31%

64 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-12-02 09:19 +0000

1""" Common functions for package biobb_chemistry.acpype """ 

2import shlex 

3import shutil 

4from pathlib import Path 

5from biobb_common.generic.biobb_object import BiobbObject 

6from biobb_common.tools import file_utils as fu 

7 

8 

9# acpype options available as properties: property name -> (acpype flag, acpype default) 

10ACPYPE_OPTIONS = { 

11 "charge": ("-n", None), 

12 "charge_method": ("-c", "bcc"), 

13 "multiplicity": ("-m", 1), 

14 "atom_type": ("-a", "gaff2"), 

15 "protein_ff": ("-F", "ff14SB"), 

16 "qprog": ("-q", "sqm"), 

17 "keyword": ("-k", None), 

18 "max_time": ("-s", 10800), 

19 "predindex": ("-r", 4), 

20 "gmx4": ("-z", False), 

21 "merge": ("-g", False), 

22 "sorted": ("-l", False), 

23 "cnstop": ("-t", False), 

24 "chiral": ("-j", False), 

25 "split_molecules": ("-S", False), 

26 "direct": ("-u", False), 

27} 

28 

29# Accepted values of the options with a closed list of values 

30ACPYPE_CHOICES = { 

31 "charge_method": ["gas", "bcc", "abcg2", "user"], 

32 "atom_type": ["gaff", "amber", "gaff2", "amber2"], 

33 "protein_ff": ["ff14SB", "ff99SB"], 

34 "qprog": ["mopac", "sqm", "divcon"], 

35 "predindex": [0, 1, 2, 3, 4, 5], 

36} 

37 

38# Options that acpype 2023.10.27 (the oldest supported version) does not have: 

39# they are only written to the command line when they are not the default value 

40ACPYPE_NEW_OPTIONS = {"protein_ff", "predindex", "split_molecules"} 

41 

42# Options of the blocks that parameterize a molecule (-i input) 

43PARAMS_OPTIONS = ["charge", "charge_method", "multiplicity", "atom_type", "protein_ff", "qprog", "keyword", "max_time", "predindex"] 

44# Options of the GROMACS topologies 

45GMX_OPTIONS = ["gmx4", "merge", "sorted"] 

46# Options of the CNS topologies 

47CNS_OPTIONS = ["cnstop", "chiral"] 

48# Options of the AMBER to GROMACS conversion (-x inpcrd -p prmtop) 

49AMB2GMX_OPTIONS = GMX_OPTIONS + ["split_molecules", "direct"] 

50 

51 

52class AcpypeBase(BiobbObject): 

53 """Base class of the acpype blocks: goes from the properties to the acpype command, run in the 

54 sandbox, and from the files acpype writes to the outputs.""" 

55 

56 #: Input arguments -> acpype flag 

57 acpype_inputs: dict[str, str] = {"input_path": "-i"} 

58 #: Properties of the block that are acpype options, see ACPYPE_OPTIONS 

59 acpype_options: list[str] = [] 

60 #: Output arguments -> file acpype writes, after the basename (ie: "_GMX.gro" for BBB_GMX.gro) 

61 acpype_outputs: dict[str, str] = {} 

62 #: Files included by an output (#include of a GROMACS top, @ of a CNS inp), renamed to the 

63 #: name of the output: {output argument: {file acpype includes, after the basename: output argument}} 

64 acpype_includes: dict[str, dict[str, str]] = {} 

65 #: Folder acpype writes, after the basename 

66 acpype_folder = ".acpype" 

67 

68 def prepare(self) -> None: 

69 """Checks the basename, the charge and the values of the acpype options""" 

70 if not self.basename: 

71 fu.log('No basename provided, default value BBB assigned', self.out_log) 

72 self.basename = str(self.basename or 'BBB') 

73 

74 if "charge" in self.acpype_options: 

75 if self.charge is None or self.charge == '': 

76 fu.log('Charge will be guessed by acpype.', self.out_log) 

77 self.charge = None 

78 elif not isinstance(self.charge, int): 

79 fu.log('Value %s is not compatible as a charge value, default value 0 assigned' % self.charge, self.out_log) 

80 self.charge = 0 

81 

82 classname = type(self).__name__ 

83 for name in self.acpype_options: 

84 value = getattr(self, name) 

85 if name in ACPYPE_CHOICES and value not in ACPYPE_CHOICES[name]: 

86 fu.log(classname + ': Value %s of the %s property is not valid, accepted values: %s' % (value, name, ', '.join(map(str, ACPYPE_CHOICES[name]))), self.out_log) 

87 raise SystemExit(classname + ': Value %s of the %s property is not valid' % (value, name)) 

88 

89 def run(self) -> None: 

90 """Runs acpype in the sandbox and copies the files it writes to the outputs""" 

91 self.cmd = [self.binary_path] 

92 self.cmd += [flag + ' ' + self.stage_io_dict["in"][arg] for arg, flag in self.acpype_inputs.items()] 

93 self.cmd.append('-b ' + self.basename) 

94 for name in self.acpype_options: 

95 flag, default = ACPYPE_OPTIONS[name] 

96 value = getattr(self, name) 

97 if isinstance(default, bool): 

98 if value: 

99 self.cmd.append(flag) 

100 elif value is None or value == '' or (name in ACPYPE_NEW_OPTIONS and value == default): 

101 continue 

102 elif name == "keyword": 

103 self.cmd.append(flag + ' ' + shlex.quote(str(value))) 

104 else: 

105 self.cmd.append(flag + ' ' + str(value)) 

106 

107 self.run_biobb() 

108 

109 # acpype writes its folder where it runs: in the sandbox 

110 folder = Path(self.stage_io_dict["unique_dir"]) / (self.basename + self.acpype_folder) 

111 for arg, suffix in self.acpype_outputs.items(): 

112 src = folder / (self.basename + suffix) 

113 if src.is_file(): 

114 shutil.copy(src, self.sandbox_file(arg)) 

115 fu.log('File %s created' % self.io_dict["out"][arg], self.out_log) 

116 

117 for arg, includes in self.acpype_includes.items(): 

118 output = Path(self.sandbox_file(arg)) 

119 if output.is_file(): 

120 text = output.read_text() 

121 for suffix, included in includes.items(): 

122 text = text.replace(self.basename + suffix, Path(self.sandbox_file(included)).name) 

123 output.write_text(text)