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(object):
20 """Holds information for one node in the SCOP hierarchy. 21 22 Attributes: 23 - sunid - SCOP unique identifiers of this node 24 - parent - Parents sunid 25 - children - Sequence of childrens sunids 26 27 """ 28
29 - def __init__(self, line=None):
30 self.sunid = '' 31 self.parent = '' 32 self.children = [] 33 if line: 34 self._process(line)
35
36 - def _process(self, line):
37 """Parses HIE records. 38 39 Records consist of 3 tab deliminated fields; node's sunid, 40 parent's sunid, and a list of children's sunids. 41 """ 42 # For example :: 43 # 44 # 0 - 46456,48724,51349,53931,56572,56835,56992,57942 45 # 21953 49268 - 46 # 49267 49266 49268,49269 47 line = line.rstrip() # no trailing whitespace 48 columns = line.split('\t') # separate the tab-delineated cols 49 if len(columns) != 3: 50 raise ValueError("I don't understand the format of %s" % line) 51 52 sunid, parent, children = columns 53 54 if sunid == '-': 55 self.sunid = '' 56 else: 57 self.sunid = int(sunid) 58 59 if parent == '-': 60 self.parent = '' 61 else: 62 self.parent = int(parent) 63 64 if children == '-': 65 self.children = () 66 else: 67 children = children.split(',') 68 self.children = [int(x) for x in children]
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 if self.children: 83 s.append(",".join(str(x) for x in self.children)) 84 else: 85 s.append('-') 86 87 return "\t".join(s) + "\n"
88 89
90 -def parse(handle):
91 """Iterates over a HIE file as Hie records for each line. 92 93 Arguments: 94 - handle - file-like object. 95 96 """ 97 for line in handle: 98 if line.startswith('#'): 99 continue 100 yield Record(line)
101