def toClaRecord(self): """Return a Cla.Record""" rec = Cla.Record() rec.sid = self.sid rec.residues = self.residues rec.sccs = self.sccs rec.sunid = self.sunid n = self while n.sunid != 0: #Not root node rec.hierarchy.append((n.type, str(n.sunid))) n = n.getParent() rec.hierarchy.reverse() return rec
def toClaRecord(self): """Return a Cla.Record""" rec = Cla.Record() rec.sid = self.sid rec.residues = self.residues rec.sccs = self.sccs rec.sunid = self.sunid n = self while n.sunid != 0: #Not root node rec.hierarchy[n.type] = str(n.sunid) n = n.getParent() # Order does not matter in the hierarchy field. For more info, see # http://scop.mrc-lmb.cam.ac.uk/scop/release-notes.html #rec.hierarchy.reverse() return rec
def __init__(self, cla_handle=None, des_handle=None, hie_handle=None, dir_path=None, db_handle=None, version=None): """Build the SCOP hierarchy from the SCOP parsable files, or a sql backend. If no file handles are given, then a Scop object with a single empty root node is returned. If a directory and version are given (with dir_path=.., version=...) or file handles for each file, the whole scop tree will be built in memory. If a MySQLdb database handle is given, the tree will be built as needed, minimising construction times. To build the SQL database to the methods write_xxx_sql to create the tables. """ self._sidDict = {} self._sunidDict = {} if cla_handle == des_handle == hie_handle == dir_path == db_handle == None: return if dir_path is None and db_handle is None: if cla_handle == None or des_handle == None or hie_handle == None: raise RuntimeError("Need CLA, DES and HIE files to build SCOP") sunidDict = {} self.db_handle = db_handle try: if db_handle: # do nothing if we have a db handle, we'll do it all on the fly pass else: # open SCOP parseable files if dir_path: if not version: raise RuntimeError( "Need SCOP version to find parsable files in directory" ) if cla_handle or des_handle or hie_handle: raise RuntimeError( "Cannot specify SCOP directory and specific files") cla_handle = _open_scop_file(dir_path, version, 'cla') des_handle = _open_scop_file(dir_path, version, 'des') hie_handle = _open_scop_file(dir_path, version, 'hie') root = Node() domains = [] root.sunid = 0 root.type = 'ro' sunidDict[root.sunid] = root self.root = root root.description = 'SCOP Root' # Build the rest of the nodes using the DES file records = Des.parse(des_handle) for record in records: if record.nodetype == 'px': n = Domain() n.sid = record.name domains.append(n) else: n = Node() n.sunid = record.sunid n.type = record.nodetype n.sccs = record.sccs n.description = record.description sunidDict[n.sunid] = n # Glue all of the Nodes together using the HIE file records = Hie.parse(hie_handle) for record in records: if record.sunid not in sunidDict: print record.sunid n = sunidDict[record.sunid] if record.parent != '': # Not root node if record.parent not in sunidDict: raise ValueError("Incomplete data?") n.parent = sunidDict[record.parent] for c in record.children: if c not in sunidDict: raise ValueError("Incomplete data?") n.children.append(sunidDict[c]) # Fill in the gaps with information from the CLA file sidDict = {} records = Cla.parse(cla_handle) for record in records: n = sunidDict[record.sunid] assert n.sccs == record.sccs assert n.sid == record.sid n.residues = record.residues sidDict[n.sid] = n # Clean up self._sunidDict = sunidDict self._sidDict = sidDict self._domains = tuple(domains) finally: if dir_path: # If we opened the files, we close the files if cla_handle: cla_handle.close() if des_handle: des_handle.close() if hie_handle: hie_handle.close()
def __init__(self, cla_handle=None, des_handle=None, hie_handle=None, dir_path=None, db_handle=None, version=None): """Build the SCOP hierarchy from the SCOP parsable files, or a sql backend. If no file handles are given, then a Scop object with a single empty root node is returned. If a directory and version are given (with dir_path=.., version=...) or file handles for each file, the whole scop tree will be built in memory. If a MySQLdb database handle is given, the tree will be built as needed, minimising construction times. To build the SQL database to the methods write_xxx_sql to create the tables. """ self._sidDict = {} self._sunidDict = {} if cla_handle==des_handle==hie_handle==dir_path==db_handle==None: return if dir_path is None and db_handle is None: if cla_handle == None or des_handle==None or hie_handle==None: raise RuntimeError("Need CLA, DES and HIE files to build SCOP") sunidDict = {} self.db_handle = db_handle try: if db_handle: # do nothing if we have a db handle, we'll do it all on the fly pass else: # open SCOP parseable files if dir_path: if not version: raise RuntimeError("Need SCOP version to find parsable files in directory") if cla_handle or des_handle or hie_handle: raise RuntimeError("Cannot specify SCOP directory and specific files") cla_handle = _open_scop_file( dir_path, version, 'cla') des_handle = _open_scop_file( dir_path, version, 'des') hie_handle = _open_scop_file( dir_path, version, 'hie') root = Node() domains = [] root.sunid=0 root.type='ro' sunidDict[root.sunid] = root self.root = root root.description = 'SCOP Root' # Build the rest of the nodes using the DES file records = Des.parse(des_handle) for record in records: if record.nodetype =='px': n = Domain() n.sid = record.name domains.append(n) else : n = Node() n.sunid = record.sunid n.type = record.nodetype n.sccs = record.sccs n.description = record.description sunidDict[n.sunid] = n # Glue all of the Nodes together using the HIE file records = Hie.parse(hie_handle) for record in records: if record.sunid not in sunidDict: print record.sunid n = sunidDict[record.sunid] if record.parent != '' : # Not root node if record.parent not in sunidDict: raise ValueError("Incomplete data?") n.parent = sunidDict[record.parent] for c in record.children: if c not in sunidDict: raise ValueError("Incomplete data?") n.children.append(sunidDict[c]) # Fill in the gaps with information from the CLA file sidDict = {} records = Cla.parse(cla_handle) for record in records: n = sunidDict[record.sunid] assert n.sccs == record.sccs assert n.sid == record.sid n.residues = record.residues sidDict[n.sid] = n # Clean up self._sunidDict = sunidDict self._sidDict = sidDict self._domains = tuple(domains) finally: if dir_path: # If we opened the files, we close the files if cla_handle : cla_handle.close() if des_handle : des_handle.close() if hie_handle : hie_handle.close()