Package csb :: Package bio :: Package io :: Module dssp

[frames] | no frames]

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

      1  """
      2  DSSP Parser
      3  """
      4
      5  import csb.core
      6  import csb.io
      7
      8  from csb.bio.structure import SecStructures, UnknownSecStructureError
      9
     10
     11 -class DSSPParseError(ValueError):
     12      pass
     13
     14
     15 -class ResidueAssignmentInfo(object):
     16
     17 -    def __init__(self, residue_id, accession, chain, secondary_structure, phi, psi):
     18
     19          self.residue_id = residue_id
     20          self.accession = accession
     21          self.chain = chain
     22          self.secondary_structure = secondary_structure
     23          self.phi = phi
     24          self.psi = psi
     25
     26
     27 -class DSSPParser(object):
     28      """
     29      Simple DSSP Secondary Structure Parser.
     30      """
     31
     32 -    def parse(self, dssp_file):
     33          """
     34          @param dssp_file: source DSSP file to parse
     35          @type dssp_file: str
     36          @return: a dictionary of L{ResidueAssignmentInfo} objects
     37          @rtype: dict
     38          """
     39
     40          data = {}
     41          start = False
     42          offset = 0                  # assume old DSSP format
     43
     44          for line in open(dssp_file):
     45
     46              if not start:
     47
     48                  if line.startswith('HEADER'):
     49                      accession = line[62:66].strip().lower()
     50
     51                  elif line.startswith('  #  RESIDUE'):
     52                      if len(line) >= 140:
     53                          offset = 4  # the new DSSP format
     54                      start = True
     55              else:
     56                  if line[13] == '!':
     57                      continue
     58
     59                  residue_id = line[6:11].strip()
     60                  chain = line[11]
     61                  try:
     62                      ss = line[16].strip()
     63                      if ss == '':
     64                          ss = SecStructures.Gap
     65                      else:
     66                          ss = csb.core.Enum.parse(SecStructures, ss)
     67                  except csb.core.EnumValueError as e:
     68                      raise UnknownSecStructureError(str(e))
     69                  phi = float(line[104 + offset : 109 + offset])
     70                  psi = float(line[110 + offset : 115 + offset])
     71
     72
     73                  if chain not in data:
     74                      data[chain] = {}
     75
     76                  data[chain][residue_id] = ResidueAssignmentInfo(residue_id, accession, chain, ss, phi, psi)
     77
     78          return data
     79
     80 -class StrideParser(object):
     81      """
     82      Simple STRIDE Secondary Structure Parser.
     83      """
     84
     85 -    def parse(self, stride_file):
     86          """
     87          @param stride_file: source STRIDE file to parse
     88          @type stride_file: str
     89          @return: a dictionary of L{ResidueAssignmentInfo} objects
     90          @rtype: dict
     91          """
     92
     93          data = {}
     94
     95          for line in open(stride_file):
     96              if line.startswith('ASG '):
     97
     98                  fields = line.split()
     99
    100                  residue_id = fields[3]
    101                  chain = fields[2]
    102                  accession = fields[-1].lower()
    103                  try:
    104                      ss = csb.core.Enum.parse(SecStructures, fields[5])
    105                  except csb.core.EnumValueError as e:
    106                      raise UnknownSecStructureError(str(e))
    107                  phi = float(fields[7])
    108                  psi = float(fields[8])
    109
    110                  if chain not in data:
    111                      data[chain] = {}
    112
    113                  data[chain][residue_id] = ResidueAssignmentInfo(residue_id, accession, chain, ss, phi, psi)
    114
    115          return data
    116
    117
    118 -def get(accession, prefix='http://www.pdb.org/pdb/files/'):
    119      """
    120      Download and parse a DSSP entry.
    121
    122      @param accession: accession number of the entry
    123      @type accession: str
    124      @param prefix: download URL prefix
    125      @type prefix: str
    126
    127      @return: see L{DSSPParser.parse}
    128      @rtype: dict
    129      """
    130      dssp = csb.io.TempFile()
    131
    132      browser = csb.io.urllib.urlopen(prefix + accession.lower() + '.dssp')
    133      dssp.write(browser.read().decode('utf-8'))
    134      dssp.flush()
    135
    136      return DSSPParser().parse(dssp.name)
    137