# Copyright 2002 by Andrew Dalke.  All rights reserved.
# Revisions 2007-2008 by Peter Cock.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.
#
# Note that BioSQL (including the database schema and scripts) is
# available and licensed separately.  Please consult www.biosql.org

"""Load biopython objects into a BioSQL database for persistent storage.

This code makes it possible to store biopython objects in a relational
database and then retrieve them back. You shouldn't use any of the
classes in this module directly. Rather, call the load() method on
a database object.
"""
# standard modules
from time import gmtime, strftime

# biopython
from Bio import Alphabet
from Bio.SeqUtils.CheckSum import crc64


class DatabaseLoader:
    """Load a database with biopython objects.
    """
    def __init__(self, adaptor, dbid):
        """Initialize with connection information for the database.

        XXX Figure out what I need to load a database and document it.
        """
        self.adaptor = adaptor
        self.dbid = dbid
    
    def load_seqrecord(self, record):
        """Load a Biopython SeqRecord into the database.
        """
        bioentry_id = self._load_bioentry_table(record)
        self._load_bioentry_date(record, bioentry_id)
        self._load_biosequence(record, bioentry_id)
        self._load_comment(record, bioentry_id)
        self._load_dbxrefs(record, bioentry_id)
        references = record.annotations.get('references', ())
        for reference, rank in zip(references, range(len(references))):
            self._load_reference(reference, rank, bioentry_id)
        self._load_annotations(record, bioentry_id)
        for seq_feature_num in range(len(record.features)):
            seq_feature = record.features[seq_feature_num]
            self._load_seqfeature(seq_feature, seq_feature_num, bioentry_id)

    def _get_ontology_id(self, name, definition=None):
        """Returns the identifier for the named ontology.

        This looks through the onotology table for a the given entry name.
        If it is not found, a row is added for this ontology (using the
        definition if supplied).  In either case, the id corresponding to
        the provided name is returned, so that you can reference it in
        another table.
        """
        oids = self.adaptor.execute_and_fetch_col0(
            "SELECT ontology_id FROM ontology WHERE name = %s",
            (name,))
        if oids:
            return oids[0]
        self.adaptor.execute(
            "INSERT INTO ontology(name, definition) VALUES (%s, %s)",
            (name, definition))
        return self.adaptor.last_id("ontology")

    
    def _get_term_id(self,
                     name,
                     ontology_id=None,
                     definition=None,
                     identifier=None):
        """Get the id that corresponds to a term.

        This looks through the term table for a the given term. If it
        is not found, a new id corresponding to this term is created.
        In either case, the id corresponding to that term is returned, so
        that you can reference it in another table.

        The ontology_id should be used to disambiguate the term.
        """

        # try to get the term id
        sql = r"SELECT term_id FROM term " \
              r"WHERE name = %s"
        fields = [name]
        if ontology_id:
            sql += ' AND ontology_id = %s'
            fields.append(ontology_id)
        id_results = self.adaptor.execute_and_fetchall(sql, fields)
        # something is wrong
        if len(id_results) > 1:
            raise ValueError("Multiple term ids for %s: %r" % 
                             (name, id_results))
        elif len(id_results) == 1:
            return id_results[0][0]
        else:
            sql = r"INSERT INTO term (name, definition," \
                  r" identifier, ontology_id)" \
                  r" VALUES (%s, %s, %s, %s)"
            self.adaptor.execute(sql, (name, definition,
                                       identifier, ontology_id))
            return self.adaptor.last_id("term")

    def _add_dbxref(self, dbname, accession, version):
       """Insert a dbxref and return its id."""
       
       self.adaptor.execute(
           "INSERT INTO dbxref(dbname, accession, version)" \
           " VALUES (%s, %s, %s)", (dbname, accession, version))
       return self.adaptor.last_id("dbxref")
           
    def _get_taxon_id(self, record):
        """Get the taxon id for this record.

        record - a SeqRecord object

        This searches the taxon/taxon_name tables using the
        NCBI taxon ID, scientific name and common name to find
        the matching taxon table entry's id.
        
        If the species isn't in the taxon table, and we have at
        least the NCBI taxon ID, scientific name or common name,
        a minimal stub entry is created in the table.

        If this information is not in the record's annotation,
        then None is returned.

        See also the BioSQL script load_ncbi_taxonomy.pl which
        will populate and update the taxon/taxon_name tables
        with the latest information from the NCBI.
        """
        
        # To find the NCBI taxid, first check for a top level annotation
        ncbi_taxon_id = None
        if "ncbi_taxid" in record.annotations :
            #Could be a list of IDs.
            if isinstance(record.annotations["ncbi_taxid"],list) :
                if len(record.annotations["ncbi_taxid"])==1 :
                    ncbi_taxon_id = record.annotations["ncbi_taxid"][0]
            else :
                ncbi_taxon_id = record.annotations["ncbi_taxid"]
        if not ncbi_taxon_id:
            # Secondly, look for a source feature
            for f in record.features:
                if f.type == 'source':
                    quals = getattr(f, 'qualifiers', {})
                    if "db_xref" in quals:
                        for db_xref in f.qualifiers["db_xref"]:
                            if db_xref.startswith("taxon:"):
                                ncbi_taxon_id = int(db_xref[6:])
                                break
                if ncbi_taxon_id: break
        if ncbi_taxon_id:
            taxa = self.adaptor.execute_and_fetch_col0(
                "SELECT taxon_id FROM taxon WHERE ncbi_taxon_id = %s",
                (ncbi_taxon_id,))
            if taxa:
                #Good, we have mapped the NCBI taxid to a taxon table entry
                return taxa[0]
            #Bad, this NCBI taxon id is not in the database!

        
        # Either we didn't find the NCBI taxid in the record, or it
        # isn't in the database yet. Next, we'll try and find a match
        # based on the species name (stored in GenBank files as the
        # the organism and/or the source).

        try :
            scientific_name = record.annotations["organism"][:255]
        except KeyError :
            scientific_name = None
        try :
            common_name = record.annotations["source"][:255]
        except KeyError :
            common_name = None
        # Note: The maximum length for taxon names in the schema is 255.
        # Cropping it now should help in getting a match when searching,
        # and avoids an error if we try and add these to the database.

        if ncbi_taxon_id is None \
        and not common_name and not scientific_name :
            # Nothing to go on... and there is no point adding
            # a new entry to the database.  We'll just leave this
            # sequence's taxon as a NULL in the database.
            return None

        if scientific_name:
            taxa = self.adaptor.execute_and_fetch_col0(
                "SELECT taxon_id FROM taxon_name" \
                " WHERE name_class = 'scientific name' AND name = %s",
                (scientific_name,))
            if taxa:
                #Good, mapped the scientific name to a taxon table entry
                return taxa[0]

        # Last chance...
        if common_name:
            taxa = self.adaptor.execute_and_fetch_col0(
                "SELECT DISTINCT taxon_id FROM taxon_name" \
                " WHERE name = %s",
                (common_name,))
            #Its natural that several distinct taxa will have the same common
            #name - in which case we can't resolve the taxon uniquely.
            if len(taxa) > 1:
                raise ValueError("Taxa: %d species have name %r" % (
                    len(taxa),
                    common_name))
            if taxa:
                #Good, mapped the common name to a taxon table entry
                return taxa[0]

        # At this point, as far as we can tell, this species isn't
        # in the taxon table already.  So we'll have to add it.

        """
        # Possible simplification of the scary code below; pending
        # discussion on the BioSQL mailing list...
        #
        # We will record the bare minimum; as long as the NCBI taxon
        # id is present, then (re)running load_ncbi_taxonomy.pl should
        # fill in the taxonomomy lineage.
        #
        # I am NOT going to try and record the lineage, even if it
        # is in the record annotation as a list of names, as we won't
        # know the NCBI taxon IDs for these parent nodes.
        self.adaptor.execute(
            "INSERT INTO taxon(parent_taxon_id, ncbi_taxon_id, node_rank,"\
            " left_value, right_value)" \
            " VALUES (%s, %s, %s, %s, %s)", (None,
                                             ncbi_taxon_id,
                                             "species",
                                             None,
                                             None))
        taxon_id = self.adaptor.last_id("taxon")
        if scientific_name:
            self.adaptor.execute(
                "INSERT INTO taxon_name(taxon_id, name, name_class)" \
                "VALUES (%s, %s, 'scientific name')", (
                taxon_id, scientific_name))
        if common_name:
            self.adaptor.execute(
                "INSERT INTO taxon_name(taxon_id, name, name_class)" \
                "VALUES (%s, %s, 'common name')", (
                taxon_id, common_name))
        return taxon_id
        """

        # OK, let's try inserting the species.
        # Chances are we don't have enough information ...
        # Furthermore, it won't be in the hierarchy.

        lineage = []
        for c in record.annotations.get("taxonomy", []):
            lineage.append([None, None, c])
        if lineage:
            lineage[-1][1] = "genus"
        lineage.append([None, "species", record.annotations["organism"]])
        # XXX do we have them?
        if "subspecies" in record.annotations:
            lineage.append([None, "subspecies",
                            record.annotations["subspecies"]])
        if "variant" in record.annotations:
            lineage.append([None, "varietas",
                            record.annotations["variant"]])
        lineage[-1][0] = ncbi_taxon_id
        
        left_value = self.adaptor.execute_one(
            "SELECT MAX(left_value) FROM taxon")[0]
        if not left_value:
            left_value = 0
        left_value += 1
        
        # XXX -- Brad: Fixing this for now in an ugly way because
        # I am getting overlaps for right_values. I need to dig into this
        # more to actually understand how it works. I'm not sure it is
        # actually working right anyhow.
        right_start_value = self.adaptor.execute_one(
            "SELECT MAX(right_value) FROM taxon")[0]
        if not right_start_value:
            right_start_value = 0
        right_value = right_start_value + 2 * len(lineage) - 1

        parent_taxon_id = None
        for taxon in lineage:
            self.adaptor.execute(
                "INSERT INTO taxon(parent_taxon_id, ncbi_taxon_id, node_rank,"\
                " left_value, right_value)" \
                " VALUES (%s, %s, %s, %s, %s)", (parent_taxon_id,
                                                 taxon[0],
                                                 taxon[1],
                                                 left_value,
                                                 right_value))
            taxon_id = self.adaptor.last_id("taxon")
            self.adaptor.execute(
                "INSERT INTO taxon_name(taxon_id, name, name_class)" \
                "VALUES (%s, %s, 'scientific name')", (taxon_id, taxon[2][:255]))
            #Note the name field is limited to 255, some SwissProt files
            #have a multi-species name which can be longer.  So truncate this.
            left_value += 1
            right_value -= 1
            parent_taxon_id = taxon_id
        if common_name:
            self.adaptor.execute(
                "INSERT INTO taxon_name(taxon_id, name, name_class)" \
                "VALUES (%s, %s, 'common name')", (
                taxon_id, common_name))

        return taxon_id

    def _load_bioentry_table(self, record):
        """Fill the bioentry table with sequence information.

        record - SeqRecord object to add to the database.
        """
        # get the pertinent info and insert it
        
        if record.id.find('.') >= 0: # try to get a version from the id
            accession, version = record.id.split('.')
            version = int(version)
        else: # otherwise just use a version of 0
            accession = record.id
            version = 0

        #Find the taxon id (this is not just the NCBI Taxon ID)
        #NOTE - If the species isn't defined in the taxon table,
        #a new minimal entry is created.
        taxon_id = self._get_taxon_id(record)

        identifier = record.annotations.get('gi')
        description = getattr(record, 'description', None)
        division = record.annotations.get("data_file_division", "UNK")
        
        sql = """
        INSERT INTO bioentry (
         biodatabase_id,
         taxon_id,
         name,
         accession,
         identifier,
         division,
         description,
         version)
        VALUES (
         %s,
         %s,
         %s,
         %s,
         %s,
         %s,
         %s,
         %s)"""
        self.adaptor.execute(sql, (self.dbid,
                                   taxon_id,
                                   record.name, 
                                   accession,
                                   identifier,
                                   division,
                                   description,
                                   version))
        # now retrieve the id for the bioentry
        bioentry_id = self.adaptor.last_id('bioentry')

        return bioentry_id

    def _load_bioentry_date(self, record, bioentry_id):
        """Add the effective date of the entry into the database.

        record - a SeqRecord object with an annotated date
        bioentry_id - corresponding database identifier
        """
        # dates are GenBank style, like:
        # 14-SEP-2000
        date = record.annotations.get("date",
                                      strftime("%d-%b-%Y", gmtime()).upper())
        annotation_tags_id = self._get_ontology_id("Annotation Tags")
        date_id = self._get_term_id("date_changed", annotation_tags_id)
        sql = r"INSERT INTO bioentry_qualifier_value" \
              r" (bioentry_id, term_id, value, rank)" \
              r" VALUES (%s, %s, %s, 1)" 
        self.adaptor.execute(sql, (bioentry_id, date_id, date))

    def _load_biosequence(self, record, bioentry_id):
        """Record a SeqRecord's sequence and alphabet in the database.

        record - a SeqRecord object with a seq property
        bioentry_id - corresponding database identifier
        """
        # determine the string representation of the alphabet
        if isinstance(record.seq.alphabet, Alphabet.DNAAlphabet):
            alphabet = "dna"
        elif isinstance(record.seq.alphabet, Alphabet.RNAAlphabet):
            alphabet = "rna"
        elif isinstance(record.seq.alphabet, Alphabet.ProteinAlphabet):
            alphabet = "protein"
        else:
            alphabet = "unknown"

        sql = r"INSERT INTO biosequence (bioentry_id, version, " \
              r"length, seq, alphabet) " \
              r"VALUES (%s, 0, %s, %s, %s)"
        self.adaptor.execute(sql, (bioentry_id,
                                   len(record.seq.data),
                                   record.seq.data,
                                   alphabet))

    def _load_comment(self, record, bioentry_id):
        """Record a SeqRecord's annotated comment in the database.

        record - a SeqRecord object with an annotated comment
        bioentry_id - corresponding database identifier
        """
        # Assume annotations['comment'] is not a list
        comment = record.annotations.get('comment')
        if not comment:
            return
        comment = comment.replace('\n', ' ')

        sql = "INSERT INTO comment (bioentry_id, comment_text, rank)" \
              " VALUES (%s, %s, %s)"
        self.adaptor.execute(sql, (bioentry_id, comment, 1))
        
    def _load_annotations(self, record, bioentry_id) :
        """Record a SeqRecord's misc annotations in the database.

        The annotation strings are recorded in the bioentry_qualifier_value
        table, except for special cases like the reference, comment and
        taxonomy which are handled with their own tables.

        record - a SeqRecord object with an annotations dictionary
        bioentry_id - corresponding database identifier
        """
        mono_sql = "INSERT INTO bioentry_qualifier_value" \
                   "(bioentry_id, term_id, value)" \
                   " VALUES (%s, %s, %s)"
        many_sql = "INSERT INTO bioentry_qualifier_value" \
                   "(bioentry_id, term_id, value, rank)" \
                   " VALUES (%s, %s, %s, %s)"
        tag_ontology_id = self._get_ontology_id('Annotation Tags')
        for key, value in record.annotations.iteritems() :
            if key in ["references", "comment", "ncbi_taxid"] :
                #Handled separately
                continue
            term_id = self._get_term_id(key, ontology_id=tag_ontology_id)
            if isinstance(value, list) :
                rank = 0
                for entry in value :
                    if isinstance(entry, str) or isinstance(entry, int):
                        #Easy case
                        rank += 1
                        self.adaptor.execute(many_sql, \
                                     (bioentry_id, term_id, str(entry), rank))
                    else :
                        pass
                        #print "Ignoring annotation '%s' sub-entry of type '%s'" \
                        #      % (key, str(type(entry)))
            elif isinstance(value, str) or isinstance(value, int):
                #Have a simple single entry, leave rank as the DB default
                self.adaptor.execute(mono_sql, \
                                     (bioentry_id, term_id, str(value)))
            else :
                pass
                #print "Ignoring annotation '%s' entry of type '%s'" \
                #      % (key, type(value))


    def _load_reference(self, reference, rank, bioentry_id):
        """Record a SeqRecord's annotated references in the database.

        record - a SeqRecord object with annotated references
        bioentry_id - corresponding database identifier
        """

        refs = None
        if reference.medline_id:
            refs = self.adaptor.execute_and_fetch_col0(
                "SELECT reference_id" \
                "  FROM reference JOIN dbxref USING (dbxref_id)" \
                " WHERE dbname = 'MEDLINE' AND accession = %s",
                (reference.medline_id,))
        if not refs and reference.pubmed_id:
            refs = self.adaptor.execute_and_fetch_col0(
                "SELECT reference_id" \
                "  FROM reference JOIN dbxref USING (dbxref_id)" \
                " WHERE dbname = 'PUBMED' AND accession = %s",
                (reference.pubmed_id,))
        if not refs:
            s = []
            for f in reference.authors, reference.title, reference.journal:
                s.append(f or "<undef>")
            crc = crc64("".join(s))
            refs = self.adaptor.execute_and_fetch_col0(
                "SELECT reference_id FROM reference" \
                  r" WHERE crc = %s", (crc,))
        if not refs:
            if reference.medline_id:
                dbxref_id = self._add_dbxref("MEDLINE",
                                             reference.medline_id, 0)
            elif reference.pubmed_id:
                dbxref_id = self._add_dbxref("PUBMED",
                                             reference.pubmed_id, 0)
            else:
                dbxref_id = None
            authors = reference.authors or None
            title =  reference.title or None
            #The location/journal field cannot be Null, so default
            #to an empty string rather than None:
            journal = reference.journal or ""
            self.adaptor.execute(
                "INSERT INTO reference (dbxref_id, location," \
                " title, authors, crc)" \
                " VALUES (%s, %s, %s, %s, %s)",
                (dbxref_id, journal, title,
                 authors, crc))
            reference_id = self.adaptor.last_id("reference")
        else:
            reference_id = refs[0]

        if reference.location:
            start = 1 + int(str(reference.location[0].start))
            end = int(str(reference.location[0].end))
        else:
            start = None
            end = None
        
        sql = "INSERT INTO bioentry_reference (bioentry_id, reference_id," \
              " start_pos, end_pos, rank)" \
              " VALUES (%s, %s, %s, %s, %s)"
        self.adaptor.execute(sql, (bioentry_id, reference_id,
                                   start, end, rank + 1))
        
    def _load_seqfeature(self, feature, feature_rank, bioentry_id):
        """Load a biopython SeqFeature into the database.
        """
        seqfeature_id = self._load_seqfeature_basic(feature.type, feature_rank,
                                                    bioentry_id)
        self._load_seqfeature_locations(feature, seqfeature_id)
        self._load_seqfeature_qualifiers(feature.qualifiers, seqfeature_id)

    def _load_seqfeature_basic(self, feature_type, feature_rank, bioentry_id):
        """Load the first tables of a seqfeature and returns the id.

        This loads the "key" of the seqfeature (ie. CDS, gene) and
        the basic seqfeature table itself.
        """
        ontology_id = self._get_ontology_id('SeqFeature Keys')
        seqfeature_key_id = self._get_term_id(feature_type,
                                              ontology_id = ontology_id)
        
        # XXX source is always EMBL/GenBank/SwissProt here; it should depend on
        # the record (how?)
        source_cat_id = self._get_ontology_id('SeqFeature Sources')
        source_term_id = self._get_term_id('EMBL/GenBank/SwissProt',
                                      ontology_id = source_cat_id)
        
        sql = r"INSERT INTO seqfeature (bioentry_id, type_term_id, " \
              r"source_term_id, rank) VALUES (%s, %s, %s, %s)"
        self.adaptor.execute(sql, (bioentry_id, seqfeature_key_id,
                                   source_term_id, feature_rank + 1))
        seqfeature_id = self.adaptor.last_id('seqfeature')

        return seqfeature_id

    def _load_seqfeature_locations(self, feature, seqfeature_id):
        """Load all of the locations for a SeqFeature into tables.

        This adds the locations related to the SeqFeature into the
        seqfeature_location table. Fuzzies are not handled right now.
        For a simple location, ie (1..2), we have a single table row
        with seq_start = 1, seq_end = 2, location_rank = 1.

        For split locations, ie (1..2, 3..4, 5..6) we would have three
        row tables with:
            start = 1, end = 2, rank = 1
            start = 3, end = 4, rank = 2
            start = 5, end = 6, rank = 3
        """
        # two cases, a simple location or a split location
        if not feature.sub_features:    # simple location
            self._insert_seqfeature_location(feature, 1, seqfeature_id)
        else: # split location
            for rank, cur_feature in enumerate(feature.sub_features):
                self._insert_seqfeature_location(cur_feature,
                                                 rank + 1,
                                                 seqfeature_id)

    def _insert_seqfeature_location(self, feature, rank, seqfeature_id):
        """Add a location of a SeqFeature to the seqfeature_location table.
        """
        sql = r"INSERT INTO location (seqfeature_id, " \
              r"start_pos, end_pos, strand, rank) " \
              r"VALUES (%s, %s, %s, %s, %s)"

        # convert biopython locations to the 1-based location system
        # used in bioSQL
        # XXX This could also handle fuzzies
        start = feature.location.nofuzzy_start + 1
        end = feature.location.nofuzzy_end
        # Biopython uses None when we don't know strand information but
        # BioSQL requires something (non null) and sets this as zero
        # So we'll use the strand or 0 if Biopython spits out None
        strand = feature.strand or 0

        self.adaptor.execute(sql, (seqfeature_id, start, end, strand, rank))

    def _load_seqfeature_qualifiers(self, qualifiers, seqfeature_id):
        """Insert the (key, value) pair qualifiers relating to a feature.

        Qualifiers should be a dictionary of the form:
            {key : [value1, value2]}
        """
        tag_ontology_id = self._get_ontology_id('Annotation Tags')
        for qualifier_key in qualifiers.keys():
            # Treat db_xref qualifiers differently to sequence annotation
            # qualifiers by populating the seqfeature_dbxref and dbxref
            # tables.  Other qualifiers go into the seqfeature_qualifier_value
            # and (if new) term tables.
            if qualifier_key != 'db_xref':
                qualifier_key_id = self._get_term_id(qualifier_key,
                                                  ontology_id=tag_ontology_id)
                # now add all of the values to their table
                entries = qualifiers[qualifier_key]
                if not isinstance(entries, list) :
                    # Could be a plain string, or an int or a float.
                    # However, we exect a list of strings here.
                    entries = [entries]
                for qual_value_rank in range(len(entries)):
                    qualifier_value = entries[qual_value_rank]
                    sql = r"INSERT INTO seqfeature_qualifier_value "\
                          r" (seqfeature_id, term_id, rank, value) VALUES"\
                          r" (%s, %s, %s, %s)"
                    self.adaptor.execute(sql, (seqfeature_id,
                                               qualifier_key_id,
                                               qual_value_rank + 1,
                                               qualifier_value))
            else:
                # The dbxref_id qualifier/value sets go into the dbxref table
                # as dbname, accession, version tuples, with dbxref.dbxref_id
                # being automatically assigned, and into the seqfeature_dbxref
                # table as seqfeature_id, dbxref_id, and rank tuples
                self._load_seqfeature_dbxref(qualifiers[qualifier_key],
                                             seqfeature_id)


    def _load_seqfeature_dbxref(self, dbxrefs, seqfeature_id):
        """Add the database crossreferences of a SeqFeature to the database.

            o dbxrefs           List, dbxref data from the source file in the
                                format <database>:<accession>

            o seqfeature_id     Int, the identifier for the seqfeature in the
                                seqfeature table

            Insert dbxref qualifier data for a seqfeature into the
            seqfeature_dbxref and, if required, dbxref tables.
            The dbxref_id qualifier/value sets go into the dbxref table
            as dbname, accession, version tuples, with dbxref.dbxref_id
            being automatically assigned, and into the seqfeature_dbxref
            table as seqfeature_id, dbxref_id, and rank tuples
        """
        # NOTE - In older versions of Biopython, we would map the GenBank
        # db_xref "name", for example "GI" to "GeneIndex", and give a warning
        # for any unknown terms.  This was a long term maintainance problem,
        # and differed from BioPerl and BioJava's implementation.  See bug 2405
        for rank, value in enumerate(dbxrefs):
            # Split the DB:accession format string at colons.  We have to
            # account for multiple-line and multiple-accession entries
            try:
                dbxref_data = value.replace(' ','').replace('\n','').split(':')
                db = dbxref_data[0]
                accessions = dbxref_data[1:]
            except:
                raise ValueError("Parsing of db_xref failed: '%s'" % value)
            # Loop over all the grabbed accessions, and attempt to fill the
            # table
            for accession in accessions:
                # Get the dbxref_id value for the dbxref data
                dbxref_id = self._get_dbxref_id(db, accession)
                # Insert the seqfeature_dbxref data
                self._get_seqfeature_dbxref(seqfeature_id, dbxref_id, rank+1)
        
    def _get_dbxref_id(self, db, accession):
        """ _get_dbxref_id(self, db, accession) -> Int

            o db          String, the name of the external database containing
                          the accession number

            o accession   String, the accession of the dbxref data

            Finds and returns the dbxref_id for the passed data.  The method
            attempts to find an existing record first, and inserts the data
            if there is no record.
        """
        # Check for an existing record
        sql = r'SELECT dbxref_id FROM dbxref WHERE dbname = %s ' \
              r'AND accession = %s'
        dbxref_id = self.adaptor.execute_and_fetch_col0(sql, (db, accession))
        # If there was a record, return the dbxref_id, else create the
        # record and return the created dbxref_id
        if dbxref_id:
            return dbxref_id[0]
        return self._add_dbxref(db, accession, 0)

    def _get_seqfeature_dbxref(self, seqfeature_id, dbxref_id, rank):
        """ Check for a pre-existing seqfeature_dbxref entry with the passed
            seqfeature_id and dbxref_id.  If one does not exist, insert new
            data

        """
        # Check for an existing record
        sql = r"SELECT seqfeature_id, dbxref_id FROM seqfeature_dbxref " \
              r"WHERE seqfeature_id = '%s' AND dbxref_id = '%s'"
        result = self.adaptor.execute_and_fetch_col0(sql, (seqfeature_id,
                                                           dbxref_id))
        # If there was a record, return without executing anything, else create
        # the record and return
        if result:
            return result
        return self._add_seqfeature_dbxref(seqfeature_id, dbxref_id, rank)

    def _add_seqfeature_dbxref(self, seqfeature_id, dbxref_id, rank):
        """ Insert a seqfeature_dbxref row and return the seqfeature_id and
            dbxref_id
        """
        sql = r'INSERT INTO seqfeature_dbxref ' \
              '(seqfeature_id, dbxref_id, rank) VALUES' \
              r'(%s, %s, %s)'
        self.adaptor.execute(sql, (seqfeature_id, dbxref_id, rank))
        return (seqfeature_id, dbxref_id)

    def _load_dbxrefs(self, record, bioentry_id) :
        """Load any sequence level cross references into the database.

        See table bioentry_dbxref"""
        for rank, value in enumerate(record.dbxrefs):
            # Split the DB:accession string at first colon.
            # We have to cope with things like:
            # "MGD:MGI:892" (db="MGD", accession="MGI:892")
            # "GO:GO:123" (db="GO", accession="GO:123")
            #
            # Annoyingly I have seen the NCBI use both the style
            # "GO:GO:123" and "GO:123" in different vintages.
            assert value.count("\n")==0
            try:
                db, accession = value.split(':',1)
                db = db.strip()
                accession = accession.strip()
            except:
                raise ValueError("Parsing of dbxrefs list failed: '%s'" % value)
            # Get the dbxref_id value for the dbxref data
            dbxref_id = self._get_dbxref_id(db, accession)
            # Insert the bioentry_dbxref  data
            self._get_bioentry_dbxref(bioentry_id, dbxref_id, rank+1)

    def _get_bioentry_dbxref(self, bioentry_id, dbxref_id, rank):
        """ Check for a pre-existing bioentry_dbxref entry with the passed
            seqfeature_id and dbxref_id.  If one does not exist, insert new
            data

        """
        # Check for an existing record
        sql = r"SELECT bioentry_id, dbxref_id FROM bioentry_dbxref " \
              r"WHERE bioentry_id = '%s' AND dbxref_id = '%s'"
        result = self.adaptor.execute_and_fetch_col0(sql, (bioentry_id,
                                                           dbxref_id))
        # If there was a record, return without executing anything, else create
        # the record and return
        if result:
            return result
        return self._add_bioentry_dbxref(bioentry_id, dbxref_id, rank)

    def _add_bioentry_dbxref(self, bioentry_id, dbxref_id, rank):
        """ Insert a bioentry_dbxref row and return the seqfeature_id and
            dbxref_id
        """
        sql = r'INSERT INTO bioentry_dbxref ' \
              '(bioentry_id,dbxref_id,rank) VALUES ' \
              '(%s, %s, %s)'
        self.adaptor.execute(sql, (bioentry_id, dbxref_id, rank))
        return (bioentry_id, dbxref_id)
            
class DatabaseRemover:
    """Complement the Loader functionality by fully removing a database.

    This probably isn't really useful for normal purposes, since you
    can just do a:
        DROP DATABASE db_name
    and then recreate the database. But, it's really useful for testing
    purposes.

    YB: now use the cascaded deletions
    """
    def __init__(self, adaptor, dbid):
        """Initialize with a database id and adaptor connection.
        """
        self.adaptor = adaptor
        self.dbid = dbid

    def remove(self):
        """Remove everything related to the given database id.
        """
        sql = r"DELETE FROM bioentry WHERE biodatabase_id = %s"
        self.adaptor.execute(sql, (self.dbid,))
        sql = r"DELETE FROM biodatabase WHERE biodatabase_id = %s"
        self.adaptor.execute(sql, (self.dbid,))

        
