Debian Codegraph @

csb/bio/io/psipred.py

raw
1 """
2 PSIPRED Parser
3 """
4
5 import csb.core
6
7 from csb.bio.structure import SecondaryStructure, SecStructures, UnknownSecStructureError
8
9
10 class PSIPredParseError(ValueError):
11 pass
12
13
14 class PSIPredResidueInfo(object):
15
16 def __init__(self, rank, residue, sec_structure, helix, strand, coil):
17
18 self.rank = rank
19 self.residue = residue
20 self.sec_structure = sec_structure
21 self.helix = helix
22 self.coil = coil
23 self.strand = strand
24
25
26 class PSIPredParser(object):
27 """
28 Simple PSI-PRED Secondary Structure Parser.
29 """
30
31 def parse(self, psipred_file):
32 """
33 @param psipred_file: source PSI-PRED *.horiz file to parse
34 @type psipred_file: str
35 @rtype: L{SecondaryStructure}
36 """
37
38 ss = []
39 conf = []
40
41 for line in open(psipred_file):
42
43 if line.startswith('Conf:'):
44 conf.extend(line[6:].strip())
45
46 elif line.startswith('Pred:'):
47 ss.append(line[6:].strip())
48
49 ss = ''.join(ss)
50 conf = ''.join(conf)
51
52 if len(ss) != len(conf):
53 raise PSIPredParseError('Invalid PSI-PRED output file')
54
55 if ss:
56 return SecondaryStructure(ss, conf)
57 else:
58 return SecondaryStructure(None)
59
60 def parse_scores(self, scores_file):
61 """
62 @param scores_file: source PSI-PRED *.ss2 file to parse
63 @type scores_file: str
64 @rtype: list of L{PSIPredResidueInfo}
65 """
66 residues = []
67
68 for line in open(scores_file):
69
70 if line.startswith('#') or not line.strip():
71 continue
72 else:
73 line = line.split()
74
75 rank = int(line[0])
76 residue = line[1]
77
78 try:
79 ss = csb.core.Enum.parse(SecStructures, line[2])
80 except csb.core.EnumValueError as e:
81 raise UnknownSecStructureError(str(e))
82
83 coil, helix, strand = map(float, line[3:6])
84
85 residues.append(PSIPredResidueInfo(rank, residue, ss, helix, strand, coil))
86
87 return tuple(residues)
88