Back to index

python-biopython  1.60
Public Member Functions | Public Attributes | Private Attributes
Bio.SCOP.Scop Class Reference

List of all members.

Public Member Functions

def __init__
def getRoot
def getDomainBySid
def getNodeBySunid
def getDomains
def write_hie
def write_des
def write_cla
def getDomainFromSQL
def getAscendentFromSQL
def getDescendentsFromSQL
def write_hie_sql
def write_cla_sql
def write_des_sql

Public Attributes

 db_handle
 root

Private Attributes

 _sidDict
 _sunidDict
 _domains

Detailed Description

The entire SCOP hierarchy.

root -- The root node of the hierarchy 

Definition at line 155 of file __init__.py.


Constructor & Destructor Documentation

def Bio.SCOP.Scop.__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.

Definition at line 161 of file __init__.py.

00161 
00162                  dir_path=None, db_handle=None, version=None):
00163         """Build the SCOP hierarchy from the SCOP parsable files, or a sql backend.
00164 
00165         If no file handles are given, then a Scop object with a single
00166         empty root node is returned.
00167 
00168         If a directory and version are given (with dir_path=.., version=...) or
00169         file handles for each file, the whole scop tree will be built in memory.
00170 
00171         If a MySQLdb database handle is given, the tree will be built as needed,
00172         minimising construction times.  To build the SQL database to the methods
00173         write_xxx_sql to create the tables.
00174         
00175         """
00176         self._sidDict = {}
00177         self._sunidDict = {}
00178 
00179         if cla_handle==des_handle==hie_handle==dir_path==db_handle==None: return 
00180 
00181         if dir_path is None and db_handle is None:
00182             if cla_handle == None or des_handle==None or hie_handle==None:
00183                 raise RuntimeError("Need CLA, DES and HIE files to build SCOP")
00184 
00185         sunidDict = {}
00186 
00187         self.db_handle = db_handle
00188         try:
00189             
00190             if db_handle:
00191                 # do nothing if we have a db handle, we'll do it all on the fly
00192                 pass
00193             
00194             else:
00195                 # open SCOP parseable files 
00196                 if dir_path:
00197                     if not version:
00198                         raise RuntimeError("Need SCOP version to find parsable files in directory")
00199                     if cla_handle or des_handle or hie_handle:
00200                         raise RuntimeError("Cannot specify SCOP directory and specific files")
00201                 
00202                     cla_handle = _open_scop_file( dir_path, version, 'cla')
00203                     des_handle = _open_scop_file( dir_path, version, 'des')
00204                     hie_handle = _open_scop_file( dir_path, version, 'hie')
00205                 
00206                 root = Node()
00207                 domains = []
00208                 root.sunid=0
00209                 root.type='ro'
00210                 sunidDict[root.sunid] = root
00211                 self.root = root
00212                 root.description = 'SCOP Root'
00213 
00214                 # Build the rest of the nodes using the DES file
00215                 records = Des.parse(des_handle)
00216                 for record in records:
00217                     if record.nodetype =='px':
00218                         n = Domain()
00219                         n.sid = record.name
00220                         domains.append(n)
00221                     else : 
00222                         n = Node()
00223                     n.sunid = record.sunid
00224                     n.type = record.nodetype
00225                     n.sccs = record.sccs
00226                     n.description = record.description
00227                     
00228                     sunidDict[n.sunid] = n
00229  
00230                 # Glue all of the Nodes together using the HIE file
00231                 records = Hie.parse(hie_handle)
00232                 for record in records:
00233                     if record.sunid not in sunidDict:
00234                         print record.sunid
00235                         
00236                     n = sunidDict[record.sunid]
00237     
00238                     if record.parent != '' : # Not root node
00239     
00240                         if record.parent not in sunidDict:
00241                             raise ValueError("Incomplete data?")
00242                                        
00243                         n.parent = sunidDict[record.parent]
00244                 
00245                     for c in record.children:
00246                         if c not in sunidDict:
00247                             raise ValueError("Incomplete data?")
00248                         n.children.append(sunidDict[c])
00249 
00250                         
00251                 # Fill in the gaps with information from the CLA file
00252                 sidDict = {}
00253                 records = Cla.parse(cla_handle)
00254                 for record in records:
00255                     n = sunidDict[record.sunid]
00256                     assert n.sccs == record.sccs
00257                     assert n.sid == record.sid
00258                     n.residues = record.residues
00259                     sidDict[n.sid] = n
00260 
00261                 # Clean up
00262                 self._sunidDict = sunidDict
00263                 self._sidDict = sidDict
00264                 self._domains = tuple(domains)
00265 
00266         finally:
00267             if dir_path:
00268                 # If we opened the files, we close the files
00269                 if cla_handle : cla_handle.close()
00270                 if des_handle : des_handle.close()
00271                 if hie_handle : hie_handle.close()
00272                                     


Member Function Documentation

def Bio.SCOP.Scop.getAscendentFromSQL (   self,
  node,
  type 
)
Get ascendents using SQL backend

Definition at line 389 of file __init__.py.

00389 
00390     def getAscendentFromSQL(self, node, type):
00391         """Get ascendents using SQL backend"""
00392         if nodeCodeOrder.index(type) >= nodeCodeOrder.index(node.type): return None
00393 
00394         cur = self.db_handle.cursor()
00395         cur.execute("SELECT "+type+" from cla WHERE "+node.type+"=%s", (node.sunid))
00396         result = cur.fetchone()
00397         if result is not None:
00398             return self.getNodeBySunid(result[0])
00399         else:
00400             return None
00401 
                            

Here is the call graph for this function:

def Bio.SCOP.Scop.getDescendentsFromSQL (   self,
  node,
  type 
)
Get descendents of a node using the database backend.  This avoids
repeated iteration of SQL calls and is therefore much quicker than
repeatedly calling node.getChildren().

Definition at line 402 of file __init__.py.

00402 
00403     def getDescendentsFromSQL(self, node, type):
00404         """Get descendents of a node using the database backend.  This avoids
00405         repeated iteration of SQL calls and is therefore much quicker than
00406         repeatedly calling node.getChildren().
00407         """
00408         if nodeCodeOrder.index(type) <= nodeCodeOrder.index(node.type): return []
00409 
00410         des_list = []
00411         
00412 
00413         # SQL cla table knows nothing about 'ro'
00414         if node.type == 'ro':
00415             for c in node.getChildren():
00416                 for d in self.getDescendentsFromSQL(c,type):
00417                     des_list.append(d)
00418             return des_list
00419         
00420         cur = self.db_handle.cursor()
00421         
00422         
00423         if type != 'px':
00424             cur.execute("SELECT DISTINCT des.sunid,des.type,des.sccs,description FROM \
00425             cla,des WHERE cla."+node.type+"=%s AND cla."+type+"=des.sunid", (node.sunid))
00426             data = cur.fetchall()
00427             for d in data:
00428                 if int(d[0]) not in self._sunidDict:
00429                     n = Node(scop=self)
00430                     [n.sunid,n.type,n.sccs,n.description] = d
00431                     n.sunid=int(n.sunid)
00432                     self._sunidDict[n.sunid] = n
00433 
00434                     cur.execute("SELECT parent FROM hie WHERE child=%s", n.sunid)
00435                     n.parent = cur.fetchone()[0]
00436                                     
00437                     cur.execute("SELECT child FROM hie WHERE parent=%s", n.sunid)
00438                     children = []
00439                     for c in cur.fetchall():
00440                         children.append(c[0])
00441                     n.children = children
00442                     
00443                     
00444                 des_list.append( self._sunidDict[int(d[0])] )
00445 
00446         else:
00447             cur.execute("SELECT cla.sunid,sid,pdbid,residues,cla.sccs,type,description,sp\
00448              FROM cla,des where cla.sunid=des.sunid and cla."+node.type+"=%s",
00449                         node.sunid)
00450 
00451             data = cur.fetchall()
00452             for d in data:
00453                 if int(d[0]) not in self._sunidDict:
00454                     n = Domain(scop=self)
00455                     #[n.sunid, n.sid, n.pdbid, n.residues, n.sccs, n.type,
00456                     #n.description,n.parent] = data
00457                     [n.sunid,n.sid, pdbid,n.residues,n.sccs,n.type,n.description,
00458                      n.parent] = d[0:8]
00459                     n.residues = Residues(n.residues)
00460                     n.residues.pdbid = pdbid
00461                     n.sunid = int(n.sunid)
00462                     self._sunidDict[n.sunid] = n
00463                     self._sidDict[n.sid] = n
00464                     
00465                     
00466                 des_list.append( self._sunidDict[int(d[0])] )
00467 
00468         return des_list
00469         
00470                 
00471         

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.SCOP.Scop.getDomainBySid (   self,
  sid 
)
Return a domain from its sid

Definition at line 277 of file __init__.py.

00277 
00278     def getDomainBySid(self, sid):
00279         """Return a domain from its sid"""
00280         if sid in self._sidDict:
00281             return self._sidDict[sid]
00282         if self.db_handle:
00283             self.getDomainFromSQL(sid=sid)
00284             if sid in self._sidDict:
00285                 return self._sidDict[sid]
00286         else:
00287             return None
00288 

Here is the call graph for this function:

def Bio.SCOP.Scop.getDomainFromSQL (   self,
  sunid = None,
  sid = None 
)
Load a node from the SQL backend using sunid or sid

Definition at line 338 of file __init__.py.

00338 
00339     def getDomainFromSQL(self, sunid=None, sid=None):
00340         """Load a node from the SQL backend using sunid or sid"""
00341         if sunid==sid==None: return None
00342 
00343         cur = self.db_handle.cursor()
00344         
00345         if sid:
00346             cur.execute("SELECT sunid FROM cla WHERE sid=%s", sid)
00347             res = cur.fetchone()
00348             if res is None:
00349                 return None
00350             sunid = res[0]
00351 
00352         cur.execute("SELECT * FROM des WHERE sunid=%s", sunid)
00353         data = cur.fetchone()
00354 
00355         if data is not None:
00356             n = None
00357             
00358             #determine if Node or Domain
00359             if data[1] != "px":
00360                 n = Node(scop=self)
00361 
00362                 cur.execute("SELECT child FROM hie WHERE parent=%s", sunid)
00363                 children = []
00364                 for c in cur.fetchall():
00365                     children.append(c[0])
00366                 n.children = children
00367                 
00368                 
00369             else:
00370                 n = Domain(scop=self)
00371                 cur.execute("select sid, residues, pdbid from cla where sunid=%s",
00372                                sunid)
00373                 
00374                 [n.sid,n.residues,pdbid] = cur.fetchone()
00375                 n.residues = Residues(n.residues)
00376                 n.residues.pdbid=pdbid
00377                 self._sidDict[n.sid] = n
00378                 
00379             [n.sunid,n.type,n.sccs,n.description] = data
00380 
00381             if data[1] != 'ro':
00382                 cur.execute("SELECT parent FROM hie WHERE child=%s", sunid)
00383                 n.parent = cur.fetchone()[0]
00384 
00385             n.sunid = int(n.sunid)
00386 
00387             self._sunidDict[n.sunid] = n
00388         

Here is the caller graph for this function:

def Bio.SCOP.Scop.getDomains (   self)
Returns an ordered tuple of all SCOP Domains

Definition at line 301 of file __init__.py.

00301 
00302     def getDomains(self):
00303         """Returns an ordered tuple of all SCOP Domains"""
00304         if self.db_handle:
00305             return self.getRoot().getDescendents('px')
00306         else:
00307             return self._domains
00308 
00309 

Here is the call graph for this function:

def Bio.SCOP.Scop.getNodeBySunid (   self,
  sunid 
)
Return a node from its sunid

Definition at line 289 of file __init__.py.

00289 
00290     def getNodeBySunid(self, sunid):
00291         """Return a node from its sunid"""
00292         if sunid in self._sunidDict:
00293             return self._sunidDict[sunid]
00294         if self.db_handle:
00295             self.getDomainFromSQL(sunid=sunid)
00296             if sunid in self._sunidDict:
00297                 return self._sunidDict[sunid]
00298         else:
00299             return None
00300 
    

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.SCOP.Scop.getRoot (   self)

Definition at line 273 of file __init__.py.

00273 
00274     def getRoot(self):
00275         return self.getNodeBySunid(0)
00276 

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.SCOP.Scop.write_cla (   self,
  handle 
)
Build a CLA SCOP parsable file from this object

Definition at line 329 of file __init__.py.

00329 
00330     def write_cla(self, handle):
00331         """Build a CLA SCOP parsable file from this object"""                
00332         nodes = self._sidDict.values()
00333         # We order nodes to ease comparison with original file
00334         nodes.sort(key = lambda n: n.sunid)
00335         for n in nodes:
00336             handle.write(str(n.toClaRecord()))
00337 

def Bio.SCOP.Scop.write_cla_sql (   self,
  handle 
)
Write CLA data to SQL database

Definition at line 485 of file __init__.py.

00485 
00486     def write_cla_sql(self, handle):
00487         """Write CLA data to SQL database"""
00488         cur = handle.cursor()
00489 
00490         cur.execute("DROP TABLE IF EXISTS cla")
00491         cur.execute("CREATE TABLE cla (sunid INT, sid CHAR(8), pdbid CHAR(4),\
00492         residues VARCHAR(50), sccs CHAR(10), cl INT, cf INT, sf INT, fa INT,\
00493         dm INT, sp INT, px INT, PRIMARY KEY (sunid), INDEX (SID) )")
00494 
00495         for n in self._sidDict.itervalues():
00496             c = n.toClaRecord()
00497             cur.execute( "INSERT INTO cla VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
00498                          (n.sunid, n.sid, c.residues.pdbid, c.residues, n.sccs,
00499                          n.getAscendent('cl').sunid, n.getAscendent('cf').sunid,
00500                          n.getAscendent('sf').sunid, n.getAscendent('fa').sunid,
00501                          n.getAscendent('dm').sunid, n.getAscendent('sp').sunid,
00502                          n.sunid ))                         
00503         

def Bio.SCOP.Scop.write_des (   self,
  handle 
)
Build a DES SCOP parsable file from this object

Definition at line 319 of file __init__.py.

00319 
00320     def write_des(self, handle):
00321         """Build a DES SCOP parsable file from this object""" 
00322         nodes = self._sunidDict.values()
00323         # Origional SCOP file is not ordered?
00324         nodes.sort(key = lambda n: n.sunid)
00325         for n in nodes:
00326             if n != self.root:
00327                 handle.write(str(n.toDesRecord()))
00328 

def Bio.SCOP.Scop.write_des_sql (   self,
  handle 
)
Write DES data to SQL database

Definition at line 504 of file __init__.py.

00504 
00505     def write_des_sql(self, handle):
00506         """Write DES data to SQL database"""
00507         cur = handle.cursor()
00508 
00509         cur.execute("DROP TABLE IF EXISTS des")
00510         cur.execute("CREATE TABLE des (sunid INT, type CHAR(2), sccs CHAR(10),\
00511         description VARCHAR(255),\
00512         PRIMARY KEY (sunid) )")
00513         
00514         for n in self._sunidDict.itervalues():
00515             cur.execute( "INSERT INTO des VALUES (%s,%s,%s,%s)",
00516                          ( n.sunid, n.type, n.sccs, n.description ) )                        
00517         

def Bio.SCOP.Scop.write_hie (   self,
  handle 
)
Build an HIE SCOP parsable file from this object

Definition at line 310 of file __init__.py.

00310 
00311     def write_hie(self, handle):
00312         """Build an HIE SCOP parsable file from this object"""
00313         nodes = self._sunidDict.values()
00314         # We order nodes to ease comparison with original file
00315         nodes.sort(key = lambda n: n.sunid)
00316         for n in nodes:
00317             handle.write(str(n.toHieRecord()))
00318 

def Bio.SCOP.Scop.write_hie_sql (   self,
  handle 
)
Write HIE data to SQL database

Definition at line 472 of file __init__.py.

00472 
00473     def write_hie_sql(self, handle):
00474         """Write HIE data to SQL database"""
00475         cur = handle.cursor()
00476 
00477         cur.execute("DROP TABLE IF EXISTS hie")
00478         cur.execute("CREATE TABLE hie (parent INT, child INT, PRIMARY KEY (child),\
00479         INDEX (parent) )")
00480 
00481         for p in self._sunidDict.itervalues():
00482             for c in p.children:
00483                 cur.execute("INSERT INTO hie VALUES (%s,%s)" % (p.sunid, c.sunid))
00484 


Member Data Documentation

Definition at line 263 of file __init__.py.

Definition at line 175 of file __init__.py.

Definition at line 176 of file __init__.py.

Definition at line 186 of file __init__.py.

Definition at line 210 of file __init__.py.


The documentation for this class was generated from the following file: