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 -class Record:
20 """Holds information for one node in the SCOP hierarchy. 21 22 sunid -- SCOP unique identifiers of this node 23 24 parent -- Parents sunid 25 26 children -- Sequence of childrens sunids 27 """
28 - def __init__(self, line=None):
29 self.sunid = '' 30 self.parent = '' 31 self.children = [] 32 if line: 33 self._process(line)
34
35 - def _process(self, line):
36 """Parses HIE records. 37 38 Records consist of 3 tab deliminated fields; node's sunid, 39 parent's sunid, and a list of children's sunids. 40 """ 41 #For example :: 42 # 43 #0 - 46456,48724,51349,53931,56572,56835,56992,57942 44 #21953 49268 - 45 #49267 49266 49268,49269 46 line = line.rstrip() # no trailing whitespace 47 columns = line.split('\t') # separate the tab-delineated cols 48 if len(columns) != 3: 49 raise ValueError("I don't understand the format of %s" % line) 50 51 sunid, parent, children = columns 52 53 if sunid =='-': 54 self.sunid = '' 55 else: 56 self.sunid = int(sunid) 57 58 if parent=='-': 59 self.parent = '' 60 else: 61 self.parent = int(parent) 62 63 if children=='-': 64 self.children = () 65 else: 66 children = children.split(',') 67 self.children = map(int, children)
68 69
70 - def __str__(self):
71 s = [] 72 s.append(str(self.sunid)) 73 74 if self.parent: 75 s.append(str(self.parent)) 76 else: 77 if self.sunid != 0: 78 s.append('0') 79 else: 80 s.append('-') 81 82 83 if self.children : 84 child_str = map(str, self.children) 85 s.append(",".join(child_str)) 86 else: 87 s.append('-') 88 89 return "\t".join(s) + "\n"
90 91
92 -def parse(handle):
93 """Iterates over a HIE file, returning a Hie record for each line 94 in the file. 95 96 Arguments: 97 98 handle -- file-like object. 99 """ 100 for line in handle: 101 yield Record(line)
102 103
104 -class Iterator:
105 """Iterates over a HIE file. 106 """
107 - def __init__(self, handle, parser=None):
108 """Create an object that iterates over a HIE file. 109 110 handle -- file-like object. 111 112 parser -- an optional Parser object to change the results into 113 another form. If set to None, then the raw contents 114 of the file will be returned. 115 116 """ 117 import warnings 118 warnings.warn("Bio.SCOP.Hie.Iterator is deprecated. Please use Bio.SCOP.Hie.parse() instead.", DeprecationWarning) 119 from types import FileType, InstanceType 120 if type(handle) is not FileType and type(handle) is not InstanceType: 121 raise TypeError("I expected a file handle or file-like object") 122 self._handle = handle 123 self._parser = parser
124
125 - def next(self):
126 """Retrieve the next HIE record.""" 127 while 1: 128 line = self._handle.readline() 129 if not line: return None 130 if line[0] !='#': break # Not a comment line 131 if self._parser is not None : 132 return self._parser.parse(line) 133 return line
134
135 - def __iter__(self):
136 return iter(self.next, None)
137 138
139 -class Parser:
140 - def __init__(self):
141 import warnings 142 warnings.warn("""Bio.SCOP.Hie.Parser is deprecated. 143 Instead of 144 145 parser = Hie.Parser() 146 record = parser.parse(entry) 147 148 please use 149 150 record = Hie.Record(entry) 151 """, DeprecationWarning)
152
153 - def parse(self, entry):
154 """Returns a Hie Record """ 155 return Record(entry)
156