BioSQL.BioSeqDatabase module

Connect with a BioSQL database and load Biopython like objects from it.

This provides interfaces for loading biological objects from a relational database, and is compatible with the BioSQL standards.

BioSQL.BioSeqDatabase.open_database(driver='MySQLdb', **kwargs)

Load an existing BioSQL-style database.

This function is the easiest way to retrieve a connection to a database, doing something like:

from BioSQL import BioSeqDatabase
server = BioSeqDatabase.open_database(user="root", db="minidb")
Arguments:
  • driver - The name of the database driver to use for connecting. The driver should implement the python DB API. By default, the MySQLdb driver is used.

  • user -the username to connect to the database with.

  • password, passwd - the password to connect with

  • host - the hostname of the database

  • database or db - the name of the database

class BioSQL.BioSeqDatabase.DBServer(conn, module, module_name=None)

Bases: object

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.

__init__(self, conn, module, module_name=None)

Create a DBServer object.

Arguments:
  • conn - A database connection object

  • module - The module used to create the database connection

  • module_name - Optionally, the name of the module. Default: module.__name__

Normally you would not want to create a DBServer object yourself. Instead use the open_database function, which returns an instance of DBServer.

__repr__(self)

Return a short description of the class name and database connection.

__getitem__(self, name)

Return a BioSeqDatabase object.

Arguments:
  • name - The name of the BioSeqDatabase

__len__(self)

Return number of namespaces (sub-databases) in this database.

__contains__(self, value)

Check if a namespace (sub-database) in this database.

__iter__(self)

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

keys(self)

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

values(self)

Iterate over BioSeqDatabase objects in the database.

items(self)

Iterate over (namespace, BioSeqDatabase) in the database.

__delitem__(self, name)

Remove a namespace and all its entries.

remove_database(self, db_name)

Remove a namespace and all its entries (OBSOLETE).

Examples

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]
new_database(self, db_name, authority=None, description=None)

Add a new database to the server and return it.

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.

commit(self)

Commit the current transaction to the database.

rollback(self)

Roll-back the current transaction.

close(self)

Close the connection. No further activity possible.

class BioSQL.BioSeqDatabase.Adaptor(conn, dbutils, wrap_cursor=False)

Bases: object

High level wrapper for a database connection and cursor.

Most database calls in BioSQL are done indirectly though this adaptor class. This provides helper methods for fetching data and executing sql.

__init__(self, conn, dbutils, wrap_cursor=False)

Create an Adaptor object.

Arguments:
  • conn - A database connection

  • dbutils - A BioSQL.DBUtils object

  • wrap_cursor - Optional, whether to wrap the cursor object

last_id(self, table)

Return the last row id for the selected table.

autocommit(self, y=True)

Set the autocommit mode. True values enable; False value disable.

commit(self)

Commit the current transaction.

rollback(self)

Roll-back the current transaction.

close(self)

Close the connection. No further activity possible.

fetch_dbid_by_dbname(self, dbname)

Return the internal id for the sub-database using its name.

fetch_seqid_by_display_id(self, dbid, name)

Return the internal id for a sequence using its display id.

Arguments:
  • dbid - the internal id for the sub-database

  • name - the name of the sequence. Corresponds to the name column of the bioentry table of the SQL schema

fetch_seqid_by_accession(self, dbid, name)

Return the internal id for a sequence using its accession.

Arguments:
  • dbid - the internal id for the sub-database

  • name - the accession of the sequence. Corresponds to the accession column of the bioentry table of the SQL schema

fetch_seqids_by_accession(self, dbid, name)

Return a list internal ids using an accession.

Arguments:
  • dbid - the internal id for the sub-database

  • name - the accession of the sequence. Corresponds to the accession column of the bioentry table of the SQL schema

fetch_seqid_by_version(self, dbid, name)

Return the internal id for a sequence using its accession and version.

Arguments:
  • dbid - the internal id for the sub-database

  • name - the accession of the sequence containing a version number. Must correspond to <accession>.<version>

fetch_seqid_by_identifier(self, dbid, identifier)

Return the internal id for a sequence using its identifier.

Arguments:
  • dbid - the internal id for the sub-database

  • identifier - the identifier of the sequence. Corresponds to the identifier column of the bioentry table in the SQL schema.

list_biodatabase_names(self)

Return a list of all of the sub-databases.

list_bioentry_ids(self, dbid)

Return a list of internal ids for all of the sequences in a sub-databae.

Arguments:
  • dbid - The internal id for a sub-database

list_bioentry_display_ids(self, dbid)

Return a list of all sequence names in a sub-databae.

Arguments:
  • dbid - The internal id for a sub-database

list_any_ids(self, sql, args)

Return ids given a SQL statement to select for them.

This assumes that the given SQL does a SELECT statement that returns a list of items. This parses them out of the 2D list they come as and just returns them in a list.

execute_one(self, sql, args=None)

Execute sql that returns 1 record, and return the record.

execute(self, sql, args=None)

Just execute an sql command.

executemany(self, sql, args)

Execute many sql commands.

get_subseq_as_string(self, seqid, start, end)

Return a substring of a sequence.

Arguments:
  • seqid - The internal id for the sequence

  • start - The start position of the sequence; 0-indexed

  • end - The end position of the sequence

execute_and_fetch_col0(self, sql, args=None)

Return a list of values from the first column in the row.

execute_and_fetchall(self, sql, args=None)

Return a list of tuples of all rows.

class BioSQL.BioSeqDatabase.MysqlConnectorAdaptor(conn, dbutils, wrap_cursor=False)

Bases: BioSQL.BioSeqDatabase.Adaptor

A BioSQL Adaptor class with fixes for the MySQL interface.

BioSQL was failing due to returns of bytearray objects from the mysql-connector-python database connector. This adaptor class scrubs returns of bytearrays and of byte strings converting them to string objects instead. This adaptor class was made in response to backwards incompatible changes added to mysql-connector-python in release 2.0.0 of the package.

execute_one(self, sql, args=None)

Execute sql that returns 1 record, and return the record.

execute_and_fetch_col0(self, sql, args=None)

Return a list of values from the first column in the row.

execute_and_fetchall(self, sql, args=None)

Return a list of tuples of all rows.

class BioSQL.BioSeqDatabase.BioSeqDatabase(adaptor, name)

Bases: object

Represents a namespace (sub-database) within the BioSQL database.

i.e. One row in the biodatabase table, and all all rows in the bioentry table associated with it.

__init__(self, adaptor, name)

Create a BioDatabase object.

Arguments:
  • adaptor - A BioSQL.Adaptor object

  • name - The name of the sub-database (namespace)

__repr__(self)

Return a short summary of the BioSeqDatabase.

get_Seq_by_id(self, name)

Get a DBSeqRecord object by its name.

Example: seq_rec = db.get_Seq_by_id(‘ROA1_HUMAN’)

The name of this method is misleading since it returns a DBSeqRecord rather than a DBSeq ojbect, and presumably was to mirror BioPerl.

get_Seq_by_acc(self, name)

Get a DBSeqRecord object by accession number.

Example: seq_rec = db.get_Seq_by_acc(‘X77802’)

The name of this method is misleading since it returns a DBSeqRecord rather than a DBSeq ojbect, and presumably was to mirror BioPerl.

get_Seq_by_ver(self, name)

Get a DBSeqRecord object by version number.

Example: seq_rec = db.get_Seq_by_ver(‘X77802.1’)

The name of this method is misleading since it returns a DBSeqRecord rather than a DBSeq ojbect, and presumably was to mirror BioPerl.

get_Seqs_by_acc(self, name)

Get a list of DBSeqRecord objects by accession number.

Example: seq_recs = db.get_Seq_by_acc(‘X77802’)

The name of this method is misleading since it returns a list of DBSeqRecord objects rather than a list of DBSeq ojbects, and presumably was to mirror BioPerl.

get_all_primary_ids(self)

All the primary_ids of the sequences in the database (OBSOLETE).

These maybe ids (display style) or accession numbers or something else completely different - they are not meaningful outside of this database implementation.

Please use .keys() instead of .get_all_primary_ids()

__getitem__(self, key)

Return a DBSeqRecord for one of the sequences in the sub-database.

Arguments:
  • key - The internal id for the sequence

__delitem__(self, key)

Remove an entry and all its annotation.

__len__(self)

Return number of records in this namespace (sub database).

__contains__(self, value)

Check if a primary (internal) id is this namespace (sub database).

__iter__(self)

Iterate over ids (which may not be meaningful outside this database).

keys(self)

Iterate over ids (which may not be meaningful outside this database).

values(self)

Iterate over DBSeqRecord objects in the namespace (sub database).

items(self)

Iterate over (id, DBSeqRecord) for the namespace (sub database).

lookup(self, **kwargs)

Return a DBSeqRecord using an acceptable identifier.

Arguments:
  • kwargs - A single key-value pair where the key is one of primary_id, gi, display_id, name, accession, version

get_Seq_by_primary_id(self, seqid)

Get a DBSeqRecord by the primary (internal) id (OBSOLETE).

Rather than db.get_Seq_by_primary_id(my_id) use db[my_id]

The name of this method is misleading since it returns a DBSeqRecord rather than a DBSeq ojbect, and presumably was to mirror BioPerl.

load(self, record_iterator, fetch_NCBI_taxonomy=False)

Load a set of SeqRecords into the BioSQL database.

record_iterator is either a list of SeqRecord objects, or an Iterator object that returns SeqRecord objects (such as the output from the Bio.SeqIO.parse() function), which will be used to populate the database.

fetch_NCBI_taxonomy is boolean flag allowing or preventing connection to the taxonomic database on the NCBI server (via Bio.Entrez) to fetch a detailed taxonomy for each SeqRecord.

Example:

from Bio import SeqIO
count = db.load(SeqIO.parse(open(filename), format))

Returns the number of records loaded.