File: output.py

package info (click to toggle)
pdb2pqr 2.1.1%2Bdfsg-7%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 47,044 kB
  • sloc: python: 44,152; cpp: 9,847; xml: 9,092; sh: 79; makefile: 55; ansic: 36
file content (510 lines) | stat: -rw-r--r-- 18,967 bytes parent folder | download
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#
# * This library is free software; you can redistribute it and/or
# * modify it under the terms of the GNU Lesser General Public
# * License as published by the Free Software Foundation; either
# * version 2.1 of the License, or (at your option) any later version.
# *
# * This library is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# * Lesser General Public License for more details.
#

#propka3.0, revision 182                                                                      2011-08-09
#-------------------------------------------------------------------------------------------------------
#--                                                                                                   --
#--                                   PROPKA: A PROTEIN PKA PREDICTOR                                 --
#--                                                                                                   --
#--                              VERSION 3.0,  01/01/2011, COPENHAGEN                                 --
#--                              BY MATS H.M. OLSSON AND CHRESTEN R. SONDERGARD                       --
#--                                                                                                   --
#-------------------------------------------------------------------------------------------------------
#
#
#-------------------------------------------------------------------------------------------------------
# References:
#
#   Very Fast Empirical Prediction and Rationalization of Protein pKa Values
#   Hui Li, Andrew D. Robertson and Jan H. Jensen
#   PROTEINS: Structure, Function, and Bioinformatics 61:704-721 (2005)
#
#   Very Fast Prediction and Rationalization of pKa Values for Protein-Ligand Complexes
#   Delphine C. Bas, David M. Rogers and Jan H. Jensen
#   PROTEINS: Structure, Function, and Bioinformatics 73:765-783 (2008)
#
#   PROPKA3: Consistent Treatment of Internal and Surface Residues in Empirical pKa predictions
#   Mats H.M. Olsson, Chresten R. Sondergard, Michal Rostkowski, and Jan H. Jensen
#   Journal of Chemical Theory and Computation, 7, 525-537 (2011)
#-------------------------------------------------------------------------------------------------------
import sys
from . import lib
revision = 182


def printHeader():
    """
    prints the header section
    """
    str  = "%s\n" % ( getPropkaHeader() )
    str += "%s\n" % ( getReferencesHeader() )
    str += "%s\n" % ( getWarningHeader() )

    print(str)


def writePDB(protein, file=None, filename=None, hydrogens=False, options=None):
    """
    Write the residue to the new pdbfile
    """
    
    if file == None:
      # opening file if not given
      if filename == None:
        filename = "%s.pdb" % (protein.name)
      file = open(filename, 'w')
      if options.verbose:
          print(("writing pdbfile %s" % (filename)))
      close_file = True
    else:
      # don't close the file, it was opened in a different place
      close_file = False

    numb = 0
    for chain in protein.chains:
      for residue in chain.residues:
        if residue.resName not in ["N+ ", "C- "]:
          for atom in residue.atoms:
            if hydrogens == False and atom.element == "H":
              """ don't print """
            else:
              numb += 1
              line = atom.makePDBLine(numb=numb)
              line += "\n"
              file.write(line)

    if close_file == True:
      file.close()


def writePQR(protein, label=None, hydrogens=False, options=None):
    """
    Write a quick pqr file for MEAD calculations - quick & dirty for my tests
    """
    import math
    
    filename    = "%s.pqr" % (protein.name)
    solute      = open(filename, 'w')
    filename    = "%s_env.pqr" % (protein.name)
    environment = open(filename, 'w')
    radii = {'H': 1.00,
             'C': 1.70,
             'N': 1.50,
             'O': 1.40,
             'S': 1.85,}

    # setting center and making ogm-file
    residue = protein.getResidue(label=label)
    x = residue.x
    y = residue.y
    z = residue.z
    str  = "%9.3lf%9.3lf%9.3lf     %3d%9.3lf\n" % (x, y, z, 39, 1.000)
    str += "%9.3lf%9.3lf%9.3lf     %3d%9.3lf\n" % (x, y, z, 75, 0.500)
    str += "%9.3lf%9.3lf%9.3lf     %3d%9.3lf\n" % (x, y, z,147, 0.250)
    filename = "%s.ogm" % (protein.name)
    file = open(filename, 'w')
    file.write(str)
    file.close()

    numb = 0
    for chain in protein.chains:
      for residue in chain.residues:
        if residue.resName not in ["N+ ", "C- "]:
          for atom in residue.atoms:
            dX = atom.x - x; dY = atom.y - y; dZ = atom.z - z
            distance = math.sqrt(dX*dX + dY*dY + dZ*dZ)
            if distance > 16.0:
              """ don't print """
            elif hydrogens == False and atom.element == "H":
              """ don't print """
            else:
              numb += 1
              pdbline = atom.makePDBLine(numb=numb, chainID=" ")
              if residue.label == label:
                if   (residue.resName == "ASP" and atom.name in ["OD1", "OD2"]) or (residue.resName == "GLU" and atom.name in ["OE1", "OE2"]):
                  line = "%s %6.2lf%6.2lf\n" % (pdbline[:55], -0.70, radii[atom.element])
                  solute.write(line)
                elif (residue.resName == "ASP" and atom.name in ["CG"]) or (residue.resName == "GLU" and atom.name in ["CD"]):
                  line = "%s %6.2lf%6.2lf\n" % (pdbline[:55],  0.40, radii[atom.element])
                  solute.write(line)
                else:
                  line = "%s %6.2lf%6.2lf\n" % (pdbline[:55],  0.00, radii[atom.element])
                  environment.write(line)
              else:
                line = "%s %6.2lf%6.2lf\n" % (pdbline[:55], 0.00, radii[atom.element])
                environment.write(line)

    solute.close()
    environment.close()


def writePKA(protein, filename=None, reference="neutral", direction="folding", options=None):
    """
    Write the pka-file based on the given protein
    """
    verbose = options.verbose if options is not None else False
    if filename == None:
      filename = "%s.pka" % (protein.name)
    file = open(filename, 'w')
    if verbose == True:
      print(("writing pkafile %s" % (filename)))

    # writing propka header
    str  = "%s\n" % ( getPropkaHeader() )
    str += "%s\n" % ( getReferencesHeader() )
    str += "%s\n" % ( getWarningHeader() )

    # writing pKa determinant section
    str += getDeterminantSection(protein)

    # writing pKa summary section
    str += getSummarySection(protein)
    str += "%s\n" % ( getTheLine() )

    # printing Folding Profile
    str += getFoldingProfileSection(protein, reference=reference, direction=direction, window=[0., 14., 1.0], options=options)

    # printing Protein Charge Profile
    str += getChargeProfileSection(protein)
    str += "\n"

    # now, writing the pka text to file
    file.write(str)

    file.close()


def printTmProfile(protein, reference="neutral", window=[0., 14., 1.], Tm=[0.,0.], Tms=None, ref=None, verbose=False, options=None):
    """
    prints Tm profile
    """
    profile = protein.getTmProfile(reference=reference, grid=[0., 14., 0.1], Tms=Tms, ref=ref, options=options)
    if profile == None:
      str  = "Could not determine Tm-profile\n"
    else:
      str  = " suggested Tm-profile for %s\n" % (protein.name)
      for (pH, Tm) in profile:
        if pH >= window[0] and pH <= window[1] and (pH%window[2] < 0.01 or pH%window[2] > 0.99*window[2]):
          str += "%6.2lf%10.2lf\n" % (pH, Tm)
      if verbose:
          print(str)


def printResult(protein, verbose=False):
    """
    prints all resulting output from determinants and down
    """
    printPKASection(protein)


def printPKASection(protein, verbose=False):
    """
    prints out the pka-section of the result
    """
    # geting the determinants section
    str = getDeterminantSection(protein)
    if verbose:
        print((str[:-1]))

    str = getSummarySection(protein)
    if verbose:
        print(str)


def getDeterminantSection(protein, verbose=False):
    """
    prints out the pka-section of the result
    """
    # getting the same order as in propka2.0
    residue_list = lib.residueList("propka1")
    str  = "%s\n" % ( getDeterminantsHeader() )

    # printing determinants
    for chain in protein.chains:
      for residue_type in residue_list:
        for residue in chain.residues:
          if residue.resName == residue_type:
            str += "%s\n" % ( residue.getDeterminantString() )


    # Add a warning in case of coupled residues
    if protein.coupled_residues:
        str += "%s\n" % ( getTheLine() )
        str += "  Residues that are found to be 'coupled', i.e. titrates together, has been marked by '*' in the above\n"
        str += "  section. Please rerun PropKa with the --display-coupled-residues option for detailed information.\n"

    return  str


def getSummarySection(protein, verbose=False):
    """
    prints out the pka-section of the result
    """
    # getting the same order as in propka2.0
    residue_list = lib.residueList("propka1")
    str  = "%s\n" % ( getSummaryHeader() )
    # printing pKa summary
    for chain in protein.chains:
      for residue_type in residue_list:
        for residue in chain.residues:
          if residue.resName == residue_type:
            str += "%s\n" % ( residue.getSummaryString() )

    return  str


def getFoldingProfileSection(protein, direction="folding", reference="neutral", window=[0., 14., 1.0], verbose=False, options=None):
    """
    returns the protein-folding-profile section
    """
    str  = getTheLine()
    str += "\n"
    str += "Free energy of %9s (kcal/mol) as a function of pH (using %s reference)\n" % (direction, reference)

    profile = protein.getFoldingProfile(reference=reference, direction=direction, grid=[0., 14., 0.1], options=options)
    if profile == None:
      str += "Could not determine folding profile\n"
    else:
      for (pH, dG) in profile:
        if pH >= window[0] and pH <= window[1] and (pH%window[2] < 0.05 or pH%window[2] > 0.95):
          str += "%6.2lf%10.2lf\n" % (pH, dG)
      str += "\n"


    pH, dG = protein.getPHopt()
    if pH == None or dG == None:
      str += "Could not determine pH optimum\n"
    else:
      str += "The pH of optimum stability is %4.1lf for which the free energy is%6.1lf kcal/mol at 298K\n" % (pH, dG)


    dG_min, dG_max = protein.getDG80()
    if dG_min == None or dG_max == None:
      str += "Could not determine pH values where the free energy is within 80 %s of maximum\n" % ("%")
    else:
      str += "The free energy is within 80 %s of maximum at pH %4.1lf to %4.1lf\n" % ("%", dG_min, dG_max)


    pH_min, pH_max = protein.getStabilityRange()
    if pH_min == None or pH_max == None:
      str += "Could not determine where the free energy is positive\n\n"
    else:
      str += "The free energy is positive in the range %4.1lf - %4.1lf\n\n" % (dG_min, dG_max)
     

    return str



def getChargeProfileSection(protein, verbose=False, options=None):
    """
    returns the protein-folding-profile section
    """
    str  = "Protein charge of folded and unfolded state as a function of pH\n"

    profile = protein.getChargeProfile(grid=[0., 14., 1.], options=options)
    if profile == None:
      str += "Could not determine charge profile\n"
    else:
      str += "%6s%10s%8s\n" % ("pH", "unfolded", "folded")
      for (pH, Q_pro, Q_mod) in profile:
        str += "%6.2lf%10.2lf%8.2lf\n" % (pH, Q_mod, Q_pro)


    pI_pro, pI_mod = protein.getPI()
    if pI_pro == None or pI_mod == None:
      str += "Could not determine the pI\n\n"
    else:
      str += "The pI is %5.2lf (folded) and %5.2lf (unfolded)" % (pI_pro, pI_mod)
     

    return str


def writeJackalScapFile(mutationData=None, filename="1xxx_scap.list", options=None):
    """
    writing a scap file for, i.e., generating a mutated protein
    """
    file = open(filename, 'w')

    for chainID, code1, resNumb, code2 in mutationData:
      str = "%s, %d, %s\n" % (chainID, resNumb, code2)
      file.write(str)
    file.close()


def writeScwrlSequenceFile(sequence, filename="x-ray.seq", options=None):
    """
    writing a scwrl sequence file for, e.g.,  generating a mutated protein
    """
    file = open(filename, 'w')

    start = 0
    while len(sequence[start:]) > 60:
      file.write( "%s\n" % (sequence[start:start+60]) )
      start += 60
    file.write( "%s\n" % (sequence[start:]) )

    file.close()


def writeWhatIfFile(mutationData=None, pdbfile=None, newfile=None, filename="whatif.sh", options=None):
    """
    writing a scap file for, i.e., generating a mutated protein
    """
    file = open(filename, 'w')

    debump_numbers = ""
    str  = ""
    str += "#!/bin/sh\n"
    str += "if [ -f %s ]; then\n  rm %s\nfi\n" % (newfile, newfile)
    str += "whatif <<- EOF\n"
    str += "getmol %s\n" % (pdbfile)
    str += "\n"
    for chainID, code1, resNumb, code2 in mutationData:
      str += "%s %d %s\n" % ("mutate", resNumb, code2)
      debump_numbers += " %d" % (resNumb)
    str += "%s %s 0\n" % ("debump", debump_numbers)
    str += "\n"
    str += "soup\n"
    str += "makmol\n"
    str += "\n"
    str += "%s\n" % (newfile)
    str += "all\n"
    str += "0\n"
    str += "\n"
    str += "fullstp\n"
    str += "y\n"
    str += "EOF\n"
    str += "\n"
    str += "for file in ALTERR.LOG DSSPOUT fort.78 PDBFILE PDBFILE.PDB WHATIF.FIG\n"
    str += "do\n"
    str += "  if [ -f $file ]; then\n"
    str += "    rm $file\n"
    str += "  fi\n"
    str += "done\n"
    str += "\n"

    file.write(str)

    file.close()





# --- various header text --- #


def getPropkaHeader():
    """
    Creates the header
    """
    from datetime import date
    today = date.today()
    str  = "propka3.0, revision %s %79s\n" % (revision, today)
    str += "-------------------------------------------------------------------------------------------------------\n"
    str += "--                                                                                                   --\n"
    str += "--                                   PROPKA: A PROTEIN PKA PREDICTOR                                 --\n"
    str += "--                                                                                                   --\n"
    str += "--                                VERSION 1.0,  04/25/2004, IOWA CITY                                --\n"
    str += "--                                             BY HUI LI                                             --\n"
    str += "--                                                                                                   --\n"
    str += "--                               VERSION 2.0,  11/05/2007, IOWA CITY/COPENHAGEN                      --\n"
    str += "--                                BY DELPHINE C. BAS AND DAVID M. ROGERS                             --\n"
    str += "--                                                                                                   --\n"
    str += "--                              VERSION 3.0,  xx/xx/2010, COPENHAGEN                                 --\n"
    str += "--                              BY MATS H.M. OLSSON AND CHRESTEN R. SONDERGARD                       --\n"
    str += "--                                                                                                   --\n"
    str += "-------------------------------------------------------------------------------------------------------\n"
    str += "\n"

    return str


def getReferencesHeader():
    """
    Returns the 'references' part in output file
    """

    str  = ""
    str += "-------------------------------------------------------------------------------------------------------\n"
    str += " References:\n"
    str += "\n"
    str += "   Very Fast Empirical Prediction and Rationalization of Protein pKa Values\n"
    str += "   Hui Li, Andrew D. Robertson and Jan H. Jensen\n"
    str += "   PROTEINS: Structure, Function, and Bioinformatics 61:704-721 (2005)\n"
    str += "   \n"
    str += "   Very Fast Prediction and Rationalization of pKa Values for Protein-Ligand Complexes\n"
    str += "   Delphine C. Bas, David M. Rogers and Jan H. Jensen\n"
    str += "   PROTEINS: Structure, Function, and Bioinformatics 73:765-783 (2008)\n"
    str += "   \n"
    str += "   PROPKA3: Consistent Treatment of Internal and Surface Residues in Empirical pKa predictions\n"
    str += "   Mats H.M. Olsson, Chresten R. Sondergard, Michal Rostkowski, and Jan H. Jensen\n"
    str += "   Journal of Chemical Theory and Computation, to be submitted (2010)\n"
    str += "-------------------------------------------------------------------------------------------------------\n"

    return str


def getWarningHeader():
    """
    Returns the 'warning' part in output file
    """

    str  = ""
    str += "-------------------------------------------------------------------------------------------------------\n"
    str += " WARNING !\n"
    str += "\n"
    str += "   Propka3.0 is not identical to propka2.0 and does not work with ligands\n"
    str += "-------------------------------------------------------------------------------------------------------\n"

    return str


def getDeterminantsHeader():
    """
    Creates the Determinant header
    """
    str  = ""
    str += "---------  -----   ------   ---------------------    --------------    --------------    --------------\n"
    str += "                            DESOLVATION  EFFECTS       SIDECHAIN          BACKBONE        COULOMBIC\n"
    str += " RESIDUE    pKa    BURIED     REGULAR      RE        HYDROGEN BOND     HYDROGEN BOND      INTERACTION\n"
    str += "---------  -----   ------   ---------   ---------    --------------    --------------    --------------\n"

    return str


def getSummaryHeader():
    """
    returns the summary header
    """
    str  = getTheLine()
    str += "\n"
    str += "SUMMARY OF THIS PREDICTION\n"
    str += "     RESIDUE    pKa   pKmodel   ligand atom-type"

    return str


def getTheLine():
    """
    draw the line - Johnny Cash would have been proud - or actually Aerosmith!
    """
    str  = ""
    for i in range(0, 104):
      str += "-"

    return str