/usr/share/pdb2pqr/extensions/resinter.py is in pdb2pqr 2.1.1+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | """
Resinter extension
Print interaction energy between each residue pair in the protein.
"""
__date__ = "21 October 2011"
__authors__ = "Kyle Monson and Emile Hogan"
import extensions
from src.hydrogens import Optimize
#itertools FTW!
from itertools import product, permutations, izip, count
from collections import defaultdict
from src.hydrogens import hydrogenRoutines
_titrationSets = (('ARG','AR0'),
('ASP', 'ASH'),
('CYS', 'CYX'),
('GLU', 'GLH'),
('HSD', 'HSE', 'HSP'),
('HID', 'HIE', 'HIP'),
('LYN', 'LYS'),
('TYR', 'TYM'),
('NEUTRAL-CTERM', 'CTERM'),
('NEUTRAL-NTERM', 'NTERM'))
_titrationSetsMap = defaultdict(tuple)
for tsSet in _titrationSets:
for ts in tsSet:
_titrationSetsMap[ts] = tsSet
#loose ends.
_titrationSetsMap['HIS'] = _titrationSetsMap['HSD']
_titrationSetsMap['CYM'] = _titrationSetsMap['CYS']
_pairEnergyResults = {}
#If the residue pair energy for a specific pair changes less than this ignore it.
PAIR_ENERGY_EPSILON = 1.0e-14
def addExtensionOptions(extensionGroup):
"""
Add options.
"""
extensionGroup.add_option('--residue_combinations',
dest='residue_combinations',
action='store_true',
default=False,
help=
'''Remap residues to different titration states and rerun resinter appending output.
Consider only the minimum number of whole protein titration combinations needed to
test each possible pairing of residue titration states. Normally used with
--noopt. If a protein titration state combination results in a pair of residue being
re-tested in the same individual titration states a warning will be generated if the
re-tested result is different. This warning should not be possible if used with --noopt.''')
extensionGroup.add_option('--all_residue_combinations',
dest='all_residue_combinations',
action='store_true',
default=False,
help=
'''Remap residues to ALL possible titration state combinations and rerun resinter appending output.
Results with --noopt should be the same as --residue_combinations. Runs considerably slower than
--residue_combinations and generates the same type of warnings.
Use without --noopt to discover how hydrogen optimization affects residue
interaction energies via the warnings in the output.''')
def usage():
"""
Returns usage text for resinter.
"""
txt = 'Print interaction energy between each residue pair in the protein to {output-path}.resinter.'
return txt
def _combinations(sublist, remainder):
"""
combinations function helper
sublist - first list in list of lists for combinations
remainder - list of remaining lists
"""
if remainder and sublist:
for item in sublist:
for result in _combinations(remainder[0], remainder[1:]):
yield [item] + result
return
elif sublist:
for item in sublist:
yield [item]
return
elif remainder:
for result in _combinations(remainder[0], remainder[1:]):
yield [None] + result
return
yield [None]
def combinations(initialList):
"""
Wrapper for main combinations function.
Iterates over each possible combination of single items
from each sub-list. For example:
combinations([[1,2],[3,4]] -> [1,3], [1,4], [2,3], [2,4] in that order.
initialList - list of lists to derive combinations from.
"""
if not initialList:
return
for result in _combinations(initialList[0], initialList[1:]):
yield result
def pairwiseCombinations(initialList):
"""
Creates the minimum set of combinations that will make available
every possible pair available.
"""
r = [len(x) for x in initialList]
m = min(r)
M = max(r)
n = len(initialList)
R = set()
for i in range(m):
t = [initialList[x][i] for x in range(n)]
R.add(tuple(t))
for i in range(m,M):
t = [initialList[x][min(r[x]-1,i)] for x in range(n)]
R.add(tuple(t))
for i in range(m):
for j in range(n):
for k in range(i+1,r[j]):
prejth = [initialList[x][i] for x in range(j)]
jth = initialList[j][k]
postjth = [initialList[x][i] for x in range(j+1, n)]
t = prejth + [jth] + postjth
R.add(tuple(t))
for i in range(m,M):
for j in range(n):
if r[i] < i:
continue
for k in range(i+1,r[j]):
prejth = [initialList[x][min(r[x]-1,i)] for x in range(j)]
jth = initialList[j][k]
postjth = [initialList[x][min(r[x]-1,i)] for x in range(j+1, n)]
t = prejth + [jth] + postjth
R.add(tuple(t))
return R
def get_residue_interaction_energy(residue1, residue2):
"""
Returns to total energy of every atom pair between the two residues.
Uses Optimize.getPairEnergy and it's donor/accepter model
to determine energy.
residue1 - "donor" residue
residue2 - "acceptor" residue
THE RESULTS OF THIS FUNCTION ARE NOT SYMMETRIC. Swapping
residue1 and residue2 will not always produce the same result.
"""
energy = 0.0
for pair in product(residue1.getAtoms(), residue2.getAtoms()):
energy += Optimize.getPairEnergy(pair[0], pair[1])
return energy
def save_residue_interaction_energies(residues, output):
"""
Writes out the residue interaction energy for each possible
residue pair in the protein.
"""
residuepairs = permutations(residues, 2)
for pair in residuepairs:
energy = get_residue_interaction_energy(pair[0], pair[1])
pairText = str(pair[0]) + ' ' + str(pair[1])
if pairText in _pairEnergyResults:
oldEnergy = _pairEnergyResults[pairText]
energyDiff = oldEnergy - energy
if abs(energyDiff) > PAIR_ENERGY_EPSILON:
txt = '#%s re-tested' % pairText
txt += ' with a difference of %s' % repr(energyDiff)
if (energy != 0):
txt += ' and a reference of %s' % repr(energyDiff/energy)
else:
txt += ' and the previous energy was 0'
txt += '\n'
output.write(txt)
continue
_pairEnergyResults[pairText] = energy
def get_residue_titration_sets(residues):
"""
Returns all possible titration states for each residue as a list of lists.
"""
result = []
for residue in residues:
result.append(_titrationSetsMap.get(residue.name, (residue.name,)))
return result
def process_residue_set(residueSet, routines, output, clean = False,
neutraln = False,
neutralc = False,
ligand = None,
assign_only = False,
chain = False,
debump = True,
opt = True):
routines.write(str(residueSet)+'\n')
routines.removeHydrogens()
for newResidueName, oldResidue, index in izip(residueSet, routines.protein.getResidues(), count()):
if newResidueName is None:
continue
chain = routines.protein.chainmap[oldResidue.chainID]
chainIndex = chain.residues.index(oldResidue)
residueAtoms = oldResidue.atoms
#Create the replacement residue
newResidue = routines.protein.createResidue(residueAtoms, newResidueName)
#Make sure our names are cleaned up for output.
newResidue.renameResidue(newResidueName)
#Drop it in
routines.protein.residues[index] = newResidue
chain.residues[chainIndex] = newResidue
#Run the meaty bits of PDB2PQR
routines.setTermini(neutraln, neutralc)
routines.updateBonds()
if not clean and not assign_only:
routines.updateSSbridges()
if debump:
routines.debumpProtein()
routines.addHydrogens()
hydRoutines = hydrogenRoutines(routines)
if debump:
routines.debumpProtein()
if opt:
hydRoutines.setOptimizeableHydrogens()
hydRoutines.initializeFullOptimization()
hydRoutines.optimizeHydrogens()
else:
hydRoutines.initializeWaterOptimization()
hydRoutines.optimizeHydrogens()
# Special for GLH/ASH, since both conformations were added
hydRoutines.cleanup()
save_residue_interaction_energies(routines.protein.getResidues(), output)
def write_all_residue_interaction_energies_combinations(routines, output, options, all_residue_combinations=False):
"""
For every titration state combination of residue output the
interaction energy for all possible residue pairs.
"""
residueNamesList = get_residue_titration_sets(routines.protein.getResidues())
routines.write("Testing the following combinations\n")
namelist = [r.name for r in routines.protein.getResidues()]
combinationsData = izip(namelist, residueNamesList)
for thing in combinationsData:
routines.write(str(thing)+'\n')
if all_residue_combinations:
combinationGenerator = combinations(residueNamesList)
else:
combinationGenerator = pairwiseCombinations(residueNamesList)
count = 0
for residueSet in combinationGenerator:
count += 1
process_residue_set(residueSet, routines, output,
clean = options.clean,
neutraln = options.neutraln,
neutralc = options.neutralc,
ligand = options.ligand,
assign_only = options.assign_only,
chain = options.chain,
debump = options.debump,
opt = options.opt)
for resultKey in sorted(_pairEnergyResults.iterkeys()):
output.write(resultKey + ' ' + str(_pairEnergyResults[resultKey]) + '\n')
routines.write(str(count)+' residue combinations tried\n')
def create_resinter_output(routines, outfile, options,
residue_combinations=False,
all_residue_combinations=False):
"""
Output the interaction energy between each possible residue pair.
"""
routines.write("Printing residue interaction energies...\n")
output = extensions.extOutputHelper(routines, outfile)
if residue_combinations or all_residue_combinations:
write_all_residue_interaction_energies_combinations(routines, output, options,
all_residue_combinations=all_residue_combinations)
else:
save_residue_interaction_energies(routines.protein.getResidues(), output)
def run_extension(routines, outroot, options):
outname = outroot + ".resinter"
with open(outname, "w") as outfile:
create_resinter_output(routines, outfile, options,
residue_combinations=options.residue_combinations,
all_residue_combinations=options.all_residue_combinations)
|