Package csb :: Package bio :: Package io :: Module whatif |
[frames] | no frames]

# Source Code for Module csb.bio.io.whatif

     1  """
     2  Simple WhatIf/WhatCheck Summary parser
     3  """
     4
     5  import re
     6  import os
     7  import shutil
     8
     9  from csb.io import Shell
    10  from csb.io import TempFolder
    11
    12 -class WhatCheckParser(object):
    13      """
    14      Simple WhatIf/WhatCheck Summary parser
    15      """
    16
    17 -    def __init__(self, binary='DO_WHATCHECK.COM'):
    18          self.binary = binary
    19
    20 -    def parse_summary(self, fn):
    21          """
    22          @param fn: whatif pdbout.txt file to parse
    23          @type fn: str
    24
    25          @return: A dict containing some of the WhatCheck results
    26          @rtype: a dict
    27          """
    28          f_handler = open(os.path.expanduser(fn))
    29          text = f_handler.read()
    30
    31          info = dict()
    32          re_ramachandran = re.compile('Ramachandran\s*Z-score\s*:\s*([0-9.Ee-]+)')
    33          re_1st = re.compile('1st\s*generation\s*packing\s*quality\s*:\s*([0-9.Ee-]+)')
    34          re_2nd = re.compile('2nd\s*generation\s*packing\s*quality\s*:\s*([0-9.Ee-]+)')
    35          re_backbone = re.compile('Backbone\s*conformation\s*Z-score\s*:\s*([0-9.Ee-]+)')
    36          re_rotamer = re.compile('chi-1\S*chi-2\s*rotamer\s*normality\s*:\s*([0-9.Ee-]+)')
    37
    38
    39          info['rama_z_score'] = float(re_ramachandran.search(text).groups(0)[0])
    40          info['bb_z_score'] = float(re_backbone.search(text).groups(0)[0])
    41          info['1st_packing_z_score'] = float(re_1st.search(text).groups(0)[0])
    42          info['2nd_packing_z_score'] = float(re_2nd.search(text).groups(0)[0])
    43          info['rotamer_score'] = float(re_rotamer.search(text).groups(0)[0])
    44
    45          f_handler.close()
    46          return info
    47
    48      parse = parse_summary
    49
    50
    51 -    def run(self, pdb_file):
    52          """
    53          Runs WhatCheck for the given pdbfile and parses the output.
    54          Will fail if the WhatCheck binary is not in the path.
    55
    56          @param pdb_file: file to parse
    57          @return: dict of parsed values
    58          """
    59          wd = os.getcwd()
    60          base = os.path.basename(pdb_file)
    61
    62          with TempFolder() as tmp:
    63              shutil.copy(os.path.expanduser(pdb_file), tmp.name)
    64              os.chdir(tmp.name)
    65              Shell.run('{0} {1}'.format(self.binary,
    66                                         os.path.join(tmp.name, base)))
    67              out = self.parse_summary(os.path.join(tmp.name, 'pdbout.txt'))
    68              os.chdir(wd)
    69
    70          return out
    71