1
2
3
4
5
6
7 """ Handle the SCOP HIErarchy files, which describe the SCOP hierarchy in
8 terms of SCOP unique identifiers (sunid).
9
10 The file format is described in the scop
11 "release notes.":http://scop.berkeley.edu/release-notes-1.55.html
12 The latest HIE file can be found
13 "elsewhere at SCOP.":http://scop.mrc-lmb.cam.ac.uk/scop/parse/
14
15 "Release 1.55":http://scop.berkeley.edu/parse/dir.hie.scop.txt_1.55 (July 2001)
16 """
17
18
19 from types import *
20
22 """Holds information for one node in the SCOP hierarchy.
23
24 sunid -- SCOP unique identifiers of this node
25
26 parent -- Parents sunid
27
28 children -- Sequence of childrens sunids
29 """
31 self.sunid = ''
32 self.parent = ''
33 self.children = []
34
36 s = []
37 s.append(str(self.sunid))
38
39 if self.parent:
40 s.append(str(self.parent))
41 else:
42 if self.sunid != 0:
43 s.append('0')
44 else:
45 s.append('-')
46
47
48 if self.children :
49 child_str = map(str, self.children)
50 s.append(",".join(child_str))
51 else:
52 s.append('-')
53
54 return "\t".join(s) + "\n"
55
56
58 """Iterates over a HIE file.
59 """
60 - def __init__(self, handle, parser=None):
61 """Create an object that iterates over a HIE file.
62
63 handle -- file-like object.
64
65 parser -- an optional Parser object to change the results into
66 another form. If set to None, then the raw contents
67 of the file will be returned.
68
69 """
70 if type(handle) is not FileType and type(handle) is not InstanceType:
71 raise TypeError, "I expected a file handle or file-like object"
72 self._handle = handle
73 self._parser = parser
74
76 """Retrieve the next HIE record."""
77 while 1:
78 line = self._handle.readline()
79 if not line: return None
80 if line[0] !='#': break
81 if self._parser is not None :
82 return self._parser.parse(line)
83 return line
84
86 return iter(self.next, None)
87
88
90 """Parses HIE records.
91
92 Records consist of 3 tab deliminated fields; node's sunid,
93 parent's sunid, and a list of children's sunids.
94 """
95
96
97
98
99
101 """Returns a Hie Record """
102 entry = entry.rstrip()
103 columns = entry.split('\t')
104 if len(columns) != 3:
105 raise SyntaxError, "I don't understand the format of %s" % entry
106
107 rec = Record()
108 rec.sunid, rec.parent, children = columns
109
110 if rec.sunid =='-' : rec.sunid = ''
111 if rec.parent =='-' : rec.parent = ''
112 else : rec.parent = int( rec.parent )
113
114 if children =='-' :
115 rec.children = ()
116 else :
117 rec.children = children.split(',')
118 rec.children = map ( int, rec.children )
119
120 rec.sunid = int(rec.sunid)
121
122 return rec
123