This file is indexed.

/usr/share/pdb2pqr/extensions/newresinter.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
344
345
346
347
348
349
350
351
352
353
"""
    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 src.hydrogens import hydrogenRoutines

#Here are the Ri -> [Ri0, Ri1] maps:
#ARG -> [{AR0}, {ARG}]
#ASP -> [{ASP}, {ASH}]
#GLU -> [{GLU}, {GLH}]
#CYS -> [{CYM}, {CYS}]
#LYS -> [{LYN}, {LYS}]
#TYR -> [{TYM}, {TYR}]
#CTERM -> [{CTERM}, {NEUTRAL-CTERM}]
#NTERM -> [{NEUTRAL-NTERM}, {NTERM}]
#HIS -> [{HSD, HSE}, {HIS}]
#HIP -> [{HID, HIE}, {HIP}]
#HSP -> [{HSD, HSE}, {HSP}]

_titrationSets = ((('AR0',), 'ARG'),
                  (('ASH',), 'ASP'),
                  (('CYX',), 'CYS'),
                  (('GLU',), 'GLH'),
                  (('HSD', 'HSE'), 'HSP'),
                  (('HID', 'HIE'), 'HIP'),
                  (('LYN',), 'LYS'),
                  (('TYM',), 'TYR'),
                  (('CTERM',), 'NEUTRAL-CTERM'),
                  (('NEUTRAL-NTERM',), 'NTERM'))

_titrationSetsMap = {}

for tsSet in _titrationSets:
    for ts in tsSet[0]:
        _titrationSetsMap[ts] = tsSet
        
    _titrationSetsMap[tsSet[1]] = tsSet
    
#loose ends.
_titrationSetsMap['HIS'] = _titrationSetsMap['HSD']
_titrationSetsMap['CYM'] = _titrationSetsMap['CYS']
        
def usage():
    """
    Returns usage text for newresinter.
    """
    txt = 'Print interaction energy between each residue pair in the protein to {output-path}.newresinter.'
    return txt

def run_extension(routines, outroot, options):
    outname = outroot + ".newresinter"
    with open(outname, "w") as outfile:
        processor = ResInter(routines, outfile, options)
        processor.generate_all()
        processor.write_resinter_output()

class ResInter(object):
    def __init__(self, routines, outfile, options):
        self.pairEnergyResults = {}
        self.combinationCount = 0
        self.totalCombinations = 0
        self.options = options
        self.output = extensions.extOutputHelper(routines, outfile)
        self.routines = routines

    def save_interation_energy(self, first, second):
        energy = get_residue_interaction_energy(first, second)
        pairText = str(first) + ' ' + str(second)
        if pairText in self.pairEnergyResults:            
            txt = '#%s re-tested!!! LOLWAT?\n' % pairText
            self.output.write(txt)             
        else:
            self.pairEnergyResults[pairText] = energy
                    

    def save_all_residue_interaction_energies(self):
        """
        Writes out the residue interaction energy for each possible
        residue pair in the protein.
        """
        residuepairs = permutations(self.routines.protein.getResidues(), 2)
        
        for pair in residuepairs:
            self.save_interation_energy(pair[0], pair[1])
            
    def save_one_with_all_interaction_energies(self, i):
        """
        Writes out the residue interaction energy for each possible
        residue pair in the protein.
        """
        residues = list(self.routines.protein.getResidues())
        target = residues[i]
        del residues[i]        
        
        for residue in residues:
            self.save_interation_energy(target, residue)
            self.save_interation_energy(residue, target)
            
    def save_pair_interaction_energies(self, i, j):
        """
        Writes out the residue interaction energy for each possible
        residue pair in the protein.
        """
        residues = list(self.routines.protein.getResidues())

        self.save_interation_energy(residues[i], residues[j])
        self.save_interation_energy(residues[j], residues[i])
            
    def create_all_protonated(self):
        residueSet = get_residue_titration_set_protonated(self.routines.protein.getResidues())
        self.process_residue_set(residueSet, 
                                clean = self.options.clean,
                                neutraln = self.options.neutraln,
                                neutralc = self.options.neutralc,
                                ligand = self.options.ligand,
                                assign_only = self.options.assign_only,
                                chain = self.options.chain,
                                debump = self.options.debump,
                                opt = self.options.opt)
        
        self.save_all_residue_interaction_energies()
        
    def create_all_single_unprotonated(self):
        combinations = residue_set_single_unprotonated_combinations(self.routines.protein.getResidues())
        for residueSet, i in combinations:
            self.process_residue_set(residueSet, 
                                     clean = self.options.clean,
                                     neutraln = self.options.neutraln,
                                     neutralc = self.options.neutralc,
                                     ligand = self.options.ligand,
                                     assign_only = self.options.assign_only,
                                     chain = self.options.chain,
                                     debump = self.options.debump,
                                     opt = self.options.opt)
        
            self.save_one_with_all_interaction_energies(i)
            
    def create_all_pair_unprotonated(self):
        combinations = residue_set_pair_unprotonated_combinations(self.routines.protein.getResidues())
        for residueSet, i, j in combinations:
            self.process_residue_set(residueSet, 
                                     clean = self.options.clean,
                                     neutraln = self.options.neutraln,
                                     neutralc = self.options.neutralc,
                                     ligand = self.options.ligand,
                                     assign_only = self.options.assign_only,
                                     chain = self.options.chain,
                                     debump = self.options.debump,
                                     opt = self.options.opt)
        
            self.save_pair_interaction_energies(i, j)
            
    def count_combinations(self):
        n = 0 # total iterable residues
        k = 0 # total iterable residues with two possible choices.
        
        allProtonated = get_residue_titration_set_protonated(self.routines.protein.getResidues())
        
        for name in allProtonated:
            if name in _titrationSetsMap:
                n += 1
                
                if len(_titrationSetsMap[name][0]) == 2:
                    k += 1
        
        self.totalCombinations = (((n+k)**2)+(n-k)+2)/2
            
    def generate_all(self):
        """
        For every titration state combination of residue output the 
        interaction energy for all possible residue pairs. 
        """
        self.routines.write("Printing residue interaction energies...\n")
        
        self.count_combinations()
        
        #Phase 1: Everything protonated
        self.create_all_protonated()
        
        #Phase 2: Single unprotonated paired with everything else.
        self.create_all_single_unprotonated()
        
        #Phase 2: Pair unprotonated paired with each other.
        self.create_all_pair_unprotonated()

    def write_resinter_output(self):
        """
        Output the interaction energy between each possible residue pair.
        """
        for resultKey in sorted(self.pairEnergyResults.iterkeys()):
            self.output.write(resultKey + ' ' + str(self.pairEnergyResults[resultKey]) + '\n')
        
        self.routines.write(str(self.combinationCount)+' residue combinations tried\n')
                
    def process_residue_set(self, residueSet,  
                            clean = False,
                            neutraln = False,
                            neutralc = False,
                            ligand = None,
                            assign_only = False,
                            chain = False,
                            debump = True,
                            opt = True):
        
        self.combinationCount += 1
        
        txt = "Running combination {0} of {1}\n".format(self.combinationCount, self.totalCombinations)
        self.routines.write(txt)
        
        self.routines.write(str(residueSet)+'\n')
        
        self.routines.removeHydrogens()
        
        for newResidueName, oldResidue, index in izip(residueSet, self.routines.protein.getResidues(), count()):
            if newResidueName is None:
                continue
            
            chain = self.routines.protein.chainmap[oldResidue.chainID]
            chainIndex = chain.residues.index(oldResidue)
            residueAtoms = oldResidue.atoms
            
            #Create the replacement residue
            newResidue = self.routines.protein.createResidue(residueAtoms, newResidueName)
            
            #Make sure our names are cleaned up for output.
            newResidue.renameResidue(newResidueName)
            
            #Drop it in
            self.routines.protein.residues[index] = newResidue
            chain.residues[chainIndex] = newResidue
        
        #Run the meaty bits of PDB2PQR  
        self.routines.setTermini(neutraln, neutralc)
        self.routines.updateBonds()
        
        if not clean and not assign_only:
            self.routines.updateSSbridges()
    
            if debump:
                self.routines.debumpProtein()
                
            self.routines.addHydrogens()
    
            hydRoutines = hydrogenRoutines(self.routines)
    
            if debump:
                self.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()
                    
        
def get_residue_titration_set_protonated(residues):
    """
    Returns residue set when everything is protonated.
    """
    result = []
    for residue in residues:
        residueTest = _titrationSetsMap.get(residue.name)
        if residueTest:
            residueTest = residueTest[1]
        else:
            residueTest = residue.name
        result.append(residueTest)
        
    return result

def residue_set_single_unprotonated_combinations(residues):
    """
    Yields pair (residue set, residue index) for 
    every "single unprotonated" combination.
    residue set - set for process_residue_set
    residue index - index of residue that was left unprotonated
    """    
    protonatedNames = get_residue_titration_set_protonated(residues)
    
    for name, i in izip(protonatedNames, count()):
        if not name in _titrationSetsMap:
            continue
        
        tStateSet = _titrationSetsMap[name][0]
        
        for tState in tStateSet:
            result = list(protonatedNames)
            result[i] = tState
            yield result, i
            
def residue_set_pair_unprotonated_combinations(residues):
    """
    Yields pair (residue set, 1rst residue index, 2nd residue index) for 
    every "single unprotonated" combination.
    residue set - set for process_residue_set
    1rst residue index - index of 1rst residue that was left unprotonated
    2nd residue index - index of 2nd residue that was left unprotonated
    """    
    protonatedNames = get_residue_titration_set_protonated(residues)
    
    for i in xrange(0,len(protonatedNames)):
        firstName = protonatedNames[i]
        if not firstName in _titrationSetsMap:
            continue
        firstStateSet = _titrationSetsMap[firstName][0]
        for j in xrange(0,i):
            secondName = protonatedNames[j]
            if not secondName in _titrationSetsMap:
                continue            
            
            secondStateSet = _titrationSetsMap[secondName][0]
            
            for firstState in firstStateSet:
                for secondState in secondStateSet:
                    result = list(protonatedNames)
                    result[i] = firstState
                    result[j] = secondState
                    yield result, i, j


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