Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

import argparse 

import os 

import config 

import log 

from formats import read_pdb_line 

 

 

def drange(dmin, dmax, step): 

"""Decimal Range 

 

Requires: 

dmin (float or int) is the range minimum value 

dmax (float or int) is the range maximum value 

step (float or int) is the range step value 

 

Ensures: 

lrange (list) 

 

Example: 

drange(5, 7.5, 0.5) 

-> [5.0, 5.5, 6.0, 6.5, 7.0, 7.5] 

 

""" 

lrange = [] 

dmin, dmax = float(dmin), float(dmax) 

step = float(step) 

inv_step = step ** -1 

dmin_int = int(int(dmin) * inv_step) 

dmax_int = int(int(dmax) * inv_step) + 1 

for i in range(dmin_int, dmax_int): 

lrange.append(i / inv_step) 

return lrange 

 

 

def setParameter(param, value): 

if param in config.input_conversion.keys(): 

param = config.input_conversion[param] 

config.params[param] = value 

 

 

def getParameter(param): 

if param in config.input_conversion.keys(): 

param = config.input_conversion[param] 

return config.params[param] 

 

 

def inputParametersFilter(settings): 

""" 

Check if input parameters are valid 

 

Mandatory Parameters: 

- Dielectric Constant 

- Ionic Strength 

- Temperature 

- Percentege of grid filling 

- pH range 

- sites_A 

 

""" 

config.pid = os.getpid() 

config.script_dir = os.path.dirname(__file__) 

param_names = settings.keys() 

# MANDATORY # 

mandatory_params = ('structure', 'epsin', 'ionicstr', 

'pbc_dimensions', 'temp', 'grid_fill', 

'ncpus', 'pH', 'sites_A') 

for param in mandatory_params: 

if param not in param_names: 

log.requiredParameterError(param) 

if type(settings[param]) in (float, int): 

settings[param] = str(settings[param]) 

if len(settings[param]) == 0: 

log.requiredParameterError(param) 

 

# Check parameter conditions: parameter is integer 

integer_params = ('gsize', 'seed', 'ncpus') 

for param in integer_params: 

if param in param_names: 

try: 

param_value = int(settings[param]) 

except: 

log.inputVariableError(param, 

'an integer.', '') 

setParameter(param, param_value) 

 

# Check parameter conditions: parameter is float 

float_params = ('scaleP', 'scaleM', 'ionicstr', 'convergence', 'nlit', 

'nonit', 'relfac', 'relpar', 'pHstep', 'epssol', 

'temp', 'epsin', 'slice', 'cutoff') 

for param in float_params: 

if param in param_names: 

try: 

param_value = float(settings[param]) 

except: 

log.inputVariableError(param, 

'a float.', '') 

setParameter(param, param_value) 

 

# Check parameter conditions: parameter > 0 

# These parameters have already been checked for type int or float 

great_params = ('scaleP', 'scaleM', 'convergence', 'pHstep', 'gsize', 

'ncpus', 'temp', 'grid_fill', 'pH_step') 

for param in great_params: 

if param in param_names: 

try: 

param_value = getParameter(param) 

assert param_value > 0 

except: 

log.inputVariableError(param, 

'greater than zero.', '') 

setParameter(param, param_value) 

 

# Check parameters conditions: parameter is boolean 

bool_params = ('pbx', 'pby', 'clean_pdb') 

for param in bool_params: 

if param in param_names: 

param_value = settings[param] 

if param_value == 'yes': 

param_value = True 

elif param_value == 'no': 

param_value = False 

else: 

log.inputVariableError(param, 

'either "yes" or "no".', '') 

 

setParameter(param, param_value) 

 

# Check particular parameter conditions 

if 'bndcon' in param_names and \ 

settings['bndcon'] not in ('1', '2', '3', '4'): 

 

log.inputVariableError('bndcon', 

'1 (zero), 2(dipolar),' 

' 3(focusing) or 4 (coulombic).', '') 

setParameter('bndcon', settings['bndcon']) 

 

if 'precision' in param_names and \ 

settings['precision'] not in ('single', 'double'): 

 

log.inputVariableError('precision', 

'either "single" or "double".', '') 

setParameter('precision', settings['precision']) 

 

if 'ffID' in param_names and \ 

settings['ffID'] not in ('G54A7'): # for now only GROMOS FF 

log.inputVariableError('ffID', 

'equal to "G54A7".', '') 

setParameter('ffID', settings['ffID']) 

 

file_path = os.path.join(config.script_dir, config.params['ffID']) 

config.f_crg = '{0}/DataBaseT.crg'.format(file_path) 

config.f_siz = '{0}/DataBaseT.siz'.format(file_path) 

 

if settings['pbc_dimensions'] not in ('0', '2'): 

log.inputVariableError('pbc_dimensions', 

'either "0" or "2".', '') 

else: 

param_value = int(settings['pbc_dimensions']) 

setParameter('pbc_dimensions', param_value) 

 

# Needs to accept both a single value and a range 

pH_parts = settings['pH'].split(',') 

if len(pH_parts) > 1: 

try: 

pHmin = float(pH_parts[0]) 

pHmax = float(pH_parts[1]) 

setParameter('pHmin', float(pH_parts[0])) 

setParameter('pHmax', float(pH_parts[1])) 

except: 

log.inputVariableError('pH', 

'a float.', '') 

else: 

try: 

setParameter('pHmin', float(pH_parts[0])) 

setParameter('pHmax', float(pH_parts[0])) 

except: 

log.inputVariableError('pH', 

'a float.', '') 

setParameter('pH', [param_value]) 

if pHmin >= pHmax: 

log.inputVariableError('pHmax', 

'a float greater than pHmin.', '') 

 

 

# Declare IO Files 

# Input .pdb File 

config.f_in = settings['structure'] 

f_in_parts = settings['structure'].split('.') 

if len(f_in_parts) <= 1: 

log.inputVariableError('structure', 

'a string containing a file extension.', 

'Ex: structure.pdb or structure.gro') 

extension = f_in_parts[1].lower() 

if extension not in ('gro', 'pdb'): 

log.inputVariableError('structure', 

'a string containing a valid file extension.', 

'Ex: structure.pdb or structure.gro') 

 

config.f_in_extension = extension 

 

# Output pKs File 

if 'output' in param_names: 

config.f_out = settings['output'] 

else: 

outputname = settings['structure'].split('.')[0] 

config.f_out = outputname 

 

# Output Titration File 

if 'titration_output' in param_names: 

config.f_prot_out = settings['titration_output'] 

 

# Output log File 

if 'logfile' in param_names: 

config.f_log = settings['logfile'] # default: "LOG" 

 

# Check coherence between variables 

if getParameter('pbc_dim') == 2 and \ 

getParameter('relfac') != 0.2 and 'relfac' not in settings: 

setParameter('relfac', 0.2) 

 

if 'lipid_definition' in settings: 

for i in settings['lipid_definition']: 

resname = settings['lipid_definition'][i] 

config.lipids[i] = resname 

if resname in config.lipid_residues: 

resname_i = config.lipid_residues.index(resname) 

del config.lipid_residues[resname_i] 

return 

 

 

def readSettings(filename): 

"""Reads the settings file. 

 

This file should have the following format: 

- commented lines should being with a '#' 

- every parameter should be declared as such: name = value 

 

All parameter values are interpreted as strings, however, a type 

check is later performed for each declared input value. 

 

All parameter names not recognizable are reported as a warning. 

""" 

parameters = {} 

parameters['lipid_definition'] = {} 

with open(filename) as f: 

nline = 0 

for line in f: 

nline += 1 

if len(line.strip()) > 0 and line[0] != '#': 

parts = line.split('=') 

param_name = parts[0].strip() 

param_value = '='.join(parts[1:]).strip() 

if 'lipid_definition' in param_name: 

parts = param_value.split(':') 

old_name = parts[0] 

new_name = parts[1] 

parameters['lipid_definition'][old_name] = new_name 

elif len(parts) != 2 or \ 

not len(param_name) > 0 or \ 

not len(param_value) > 0: 

raise IOError('Incorrect format in line {0} of file {1}: ' 

'\n{1}#{0}: {2}'.format(nline, filename, line)) 

else: 

parameters[param_name] = param_value 

 

# Search for all titrable sites in different chains 

sites = {} 

for param_name in parameters: 

if 'site' in param_name: 

chain = param_name.split('_')[1] 

chain_sites = parameters[param_name].split(', ') 

if chain_sites != ['all']: 

sites[chain] = chain_sites 

config.sites = sites 

 

return parameters 

 

 

def checkParsedInput(): 

"""Gets the CLI arguments and interprets them""" 

 

parser = argparse.ArgumentParser( 

formatter_class=argparse.RawDescriptionHelpFormatter, description=""" 

Object-Oriented Script to Calculate the pKint of each site \ 

as well as the pairwise energies 

Requires: 

DelPhi2Py module installation 

Nanoshaper and cppSolver are optional libraries 

 

Objects: 

DelPhiParams stores the DelPhi input parameters like a .prm file 

 

TitratingMolecule is the molecule which has more than one Site 

 

Site 

 

Tautomer 

 

Example: 

python pypka.py test.pdb test.dat -o pKas.out --debug 

 

""") 

 

# Mandatory Arguments 

parser.add_argument('settings', help=' settings file name', 

default="settings.dat", action='store') 

 

# Optional Arguments 

parser.add_argument('--debug', help='activation of the debug mode ' 

'to print extra information', action='store_true') 

 

args = parser.parse_args() 

 

# Apply some criteria to input arguments 

if not os.path.isfile(args.settings): 

raise IOError('File {0} does not exist.'.format(args.settings)) 

 

# Read Settings File 

config.f_dat = args.settings 

config.debug = args.debug 

parameters = readSettings(args.settings) 

 

return parameters