Package Bio :: Package SCOP :: Module Hie
[hide private]
[frames] | no frames]

Source Code for Module Bio.SCOP.Hie

  1  # Copyright 2001 by Gavin E. Crooks.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  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   
21 -class Record:
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 """
30 - def __init__(self):
31 self.sunid = '' 32 self.parent = '' 33 self.children = []
34
35 - def __str__(self):
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
57 -class Iterator:
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
75 - def next(self):
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 # Not a comment line 81 if self._parser is not None : 82 return self._parser.parse(line) 83 return line
84
85 - def __iter__(self):
86 return iter(self.next, None)
87 88
89 -class Parser:
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 #For example :: 96 # 97 #0 - 46456,48724,51349,53931,56572,56835,56992,57942 98 #21953 49268 - 99 #49267 49266 49268,49269
100 - def parse(self, entry):
101 """Returns a Hie Record """ 102 entry = entry.rstrip() # no trailing whitespace 103 columns = entry.split('\t') # separate the tab-delineated cols 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