Back to index

python-biopython  1.60
Public Member Functions | Public Attributes
BioSQL.BioSeqDatabase.DBServer Class Reference

List of all members.

Public Member Functions

def __init__
def __repr__
def __getitem__
def __len__
def __contains__
def __iter__
def keys
def values
def items
def iterkeys
def itervalues
def iteritems
def keys
def values
def items
def __delitem__
def remove_database
def new_database
def load_database_sql
def commit
def rollback
def close

Public Attributes

 module
 adaptor
 module_name

Detailed Description

Represents a BioSQL database continaing namespaces (sub-databases).

This acts like a Python dictionary, giving access to each namespace
(defined by a row in the biodatabase table) as a BioSeqDatabase object.

Definition at line 106 of file BioSeqDatabase.py.


Constructor & Destructor Documentation

def BioSQL.BioSeqDatabase.DBServer.__init__ (   self,
  conn,
  module,
  module_name = None 
)

Definition at line 112 of file BioSeqDatabase.py.

00112 
00113     def __init__(self, conn, module, module_name=None):
00114         self.module = module
00115         if module_name is None:
00116             module_name = module.__name__
00117         self.adaptor = Adaptor(conn, DBUtils.get_dbutils(module_name))
00118         self.module_name = module_name
        

Member Function Documentation

def BioSQL.BioSeqDatabase.DBServer.__contains__ (   self,
  value 
)
Check if a namespace (sub-database) in this database.

Definition at line 130 of file BioSeqDatabase.py.

00130 
00131     def __contains__(self, value):
00132         """Check if a namespace (sub-database) in this database."""
00133         sql = "SELECT COUNT(name) FROM biodatabase WHERE name=%s;"
00134         return bool(self.adaptor.execute_and_fetch_col0(sql, (value,))[0])
    
def BioSQL.BioSeqDatabase.DBServer.__delitem__ (   self,
  name 
)
Remove a namespace and all its entries.

Definition at line 183 of file BioSeqDatabase.py.

00183 
00184     def __delitem__(self, name):
00185         """Remove a namespace and all its entries."""
00186         if name not in self:
00187             raise KeyError(name)
00188         self.remove_database(name)

Here is the call graph for this function:

def BioSQL.BioSeqDatabase.DBServer.__getitem__ (   self,
  name 
)

Definition at line 122 of file BioSeqDatabase.py.

00122 
00123     def __getitem__(self, name):
00124         return BioSeqDatabase(self.adaptor, name)

Iterate over namespaces (sub-databases) in the database.

Definition at line 135 of file BioSeqDatabase.py.

00135 
00136     def __iter__(self):
00137         """Iterate over namespaces (sub-databases) in the database."""
00138         #TODO - Iterate over the cursor, much more efficient
00139         return iter(self.adaptor.list_biodatabase_names())        

Number of namespaces (sub-databases) in this database.

Definition at line 125 of file BioSeqDatabase.py.

00125 
00126     def __len__(self):
00127         """Number of namespaces (sub-databases) in this database."""
00128         sql = "SELECT COUNT(name) FROM biodatabase;"
00129         return int(self.adaptor.execute_and_fetch_col0(sql)[0])

Definition at line 119 of file BioSeqDatabase.py.

00119 
00120     def __repr__(self):
00121         return self.__class__.__name__ + "(%r)" % self.adaptor.conn

Close the connection. No further activity possible.

Definition at line 262 of file BioSeqDatabase.py.

00262 
00263     def close(self):
00264         """Close the connection. No further activity possible."""
00265         return self.adaptor.close()

Here is the caller graph for this function:

Commits the current transaction to the database.

Definition at line 254 of file BioSeqDatabase.py.

00254 
00255     def commit(self):
00256         """Commits the current transaction to the database."""
00257         return self.adaptor.commit()

List of (namespace, BioSeqDatabase) for entries in the database.

Definition at line 150 of file BioSeqDatabase.py.

00150 
00151         def items(self):
00152             """List of (namespace, BioSeqDatabase) for entries in the database."""
00153             return [(key, self[key]) for key in self.keys()]
        

Here is the call graph for this function:

Here is the caller graph for this function:

Iterate over (namespace, BioSeqDatabase) in the database.

Definition at line 178 of file BioSeqDatabase.py.

00178 
00179         def items(self):
00180             """Iterate over (namespace, BioSeqDatabase) in the database."""
00181             for key in self:
00182                 yield key, self[key]

Here is the call graph for this function:

Here is the caller graph for this function:

Iterate over (namespace, BioSeqDatabase) in the database.

Definition at line 163 of file BioSeqDatabase.py.

00163 
00164         def iteritems(self):
00165             """Iterate over (namespace, BioSeqDatabase) in the database."""
00166             for key in self:
                yield key, self[key]

Here is the caller graph for this function:

Iterate over namespaces (sub-databases) in the database.

Definition at line 154 of file BioSeqDatabase.py.

00154 
00155         def iterkeys(self):
00156             """Iterate over namespaces (sub-databases) in the database."""
00157             return iter(self)
    
Iterate over BioSeqDatabase objects in the database.

Definition at line 158 of file BioSeqDatabase.py.

00158 
00159         def itervalues(self):
00160             """Iterate over BioSeqDatabase objects in the database."""
00161             for key in self:
00162                 yield self[key]
            

Here is the caller graph for this function:

List of namespaces (sub-databases) in the database.

Definition at line 142 of file BioSeqDatabase.py.

00142 
00143         def keys(self):
00144             """List of namespaces (sub-databases) in the database."""
00145             return self.adaptor.list_biodatabase_names()

Here is the caller graph for this function:

Iterate over namespaces (sub-databases) in the database.

Definition at line 169 of file BioSeqDatabase.py.

00169 
00170         def keys(self):
00171             """Iterate over namespaces (sub-databases) in the database."""
00172             return iter(self)
            

Here is the call graph for this function:

Here is the caller graph for this function:

def BioSQL.BioSeqDatabase.DBServer.load_database_sql (   self,
  sql_file 
)
Load a database schema into the given database.

This is used to create tables, etc when a database is first created.
sql_file should specify the complete path to a file containing
SQL entries for building the tables.

Definition at line 215 of file BioSeqDatabase.py.

00215 
00216     def load_database_sql(self, sql_file):
00217         """Load a database schema into the given database.
00218 
00219         This is used to create tables, etc when a database is first created.
00220         sql_file should specify the complete path to a file containing
00221         SQL entries for building the tables.
00222         """
00223         # Not sophisticated enough for PG schema. Is it needed by MySQL?
00224         # Looks like we need this more complicated way for both. Leaving it
00225         # the default and removing the simple-minded approach.
00226 
00227         # read the file with all comment lines removed
00228         sql_handle = open(sql_file, "rU")
00229         sql = r""
00230         for line in sql_handle:
00231             if line.find("--") == 0: # don't include comment lines
00232                 pass
00233             elif line.find("#") == 0: # ditto for MySQL comments
00234                 pass
00235             elif line.strip(): # only include non-blank lines
00236                 sql += line.strip()
00237                 sql += ' '
00238         
00239         # two ways to load the SQL
00240         # 1. PostgreSQL can load it all at once and actually needs to
00241         # due to FUNCTION defines at the end of the SQL which mess up
00242         # the splitting by semicolons
00243         if self.module_name in ["psycopg2", "pgdb"]:
00244             self.adaptor.cursor.execute(sql)
00245         # 2. MySQL needs the database loading split up into single lines of
00246         # SQL executed one at a time
00247         elif self.module_name in ["MySQLdb", "sqlite3"]:
00248             sql_parts = sql.split(";") # one line per sql command
00249             for sql_line in sql_parts[:-1]: # don't use the last item, it's blank
00250                 self.adaptor.cursor.execute(sql_line)
00251         else:
00252             raise ValueError("Module %s not supported by the loader." %
00253                     (self.module_name))

Here is the call graph for this function:

def BioSQL.BioSeqDatabase.DBServer.new_database (   self,
  db_name,
  authority = None,
  description = None 
)
Add a new database to the server and return it.

Definition at line 206 of file BioSeqDatabase.py.

00206 
00207     def new_database(self, db_name, authority=None, description=None):
00208         """Add a new database to the server and return it.
00209         """
00210         # make the database
00211         sql = r"INSERT INTO biodatabase (name, authority, description)" \
00212               r" VALUES (%s, %s, %s)" 
00213         self.adaptor.execute(sql, (db_name,authority, description))
00214         return BioSeqDatabase(self.adaptor, db_name)

def BioSQL.BioSeqDatabase.DBServer.remove_database (   self,
  db_name 
)
Remove a namespace and all its entries (OBSOLETE).

Try to remove all references to items in a database.

server.remove_database(name)

In keeping with the dictionary interface, you can now do this:

del server[name]

Definition at line 189 of file BioSeqDatabase.py.

00189 
00190     def remove_database(self, db_name):
00191         """Remove a namespace and all its entries (OBSOLETE).
00192         
00193         Try to remove all references to items in a database.
00194         
00195         server.remove_database(name)
00196         
00197         In keeping with the dictionary interface, you can now do this:
00198         
00199         del server[name]
00200         """
00201         import warnings
00202         warnings.warn("This method is obsolete.  In keeping with the dictionary interface, you can now use 'del server[name]' instead", PendingDeprecationWarning)
00203         db_id = self.adaptor.fetch_dbid_by_dbname(db_name)
00204         remover = Loader.DatabaseRemover(self.adaptor, db_id)
00205         remover.remove()

Here is the caller graph for this function:

Rolls backs the current transaction.

Definition at line 258 of file BioSeqDatabase.py.

00258 
00259     def rollback(self):
00260         """Rolls backs the current transaction."""
00261         return self.adaptor.rollback()

List of BioSeqDatabase objects in the database.

Definition at line 146 of file BioSeqDatabase.py.

00146 
00147         def values(self):
00148             """List of BioSeqDatabase objects in the database."""
00149             return [self[key] for key in self.keys()]
    

Here is the call graph for this function:

Here is the caller graph for this function:

Iterate over BioSeqDatabase objects in the database.

Definition at line 173 of file BioSeqDatabase.py.

00173 
00174         def values(self):
00175             """Iterate over BioSeqDatabase objects in the database."""
00176             for key in self:
00177                 yield self[key]
    

Here is the call graph for this function:


Member Data Documentation

Definition at line 116 of file BioSeqDatabase.py.

Definition at line 113 of file BioSeqDatabase.py.

Definition at line 117 of file BioSeqDatabase.py.


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