Debian Codegraph @

csb/bio/io/dssp.py

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