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 24 - sunid - SCOP unique identifiers of this node 25 - parent - Parents sunid 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 = [int(x) for x in children]
68
69 - def __str__(self):
70 s = [] 71 s.append(str(self.sunid)) 72 73 if self.parent: 74 s.append(str(self.parent)) 75 else: 76 if self.sunid != 0: 77 s.append('0') 78 else: 79 s.append('-') 80 81 if self.children: 82 s.append(",".join(str(x) for x in self.children)) 83 else: 84 s.append('-') 85 86 return "\t".join(s) + "\n"
87 88
89 -def parse(handle):
90 """Iterates over a HIE file as Hie records for each line. 91 92 Arguments: 93 94 - handle - file-like object. 95 """ 96 for line in handle: 97 if line.startswith('#'): 98 continue 99 yield Record(line)
100