Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 244 lines (183 sloc) 6.258 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
# $Id$
# Adaptor for Multiple BioSQL databases.
# By Juguang Xiao <juguang@tll.org.sg>

=head1 NAME

Bio::DB::BioSQL::MultiDB

=head1 SYNOPSIS

use Bio::DB::BioSQL::MultiDB;

# create the common biosql db adaptors
my $swissprot_db; # Physical databases may be located on different servers
my $embl_db; # or accessible by different users.

# register them by bio-database
my $multiDB = Bio::DB::BioSQL::MultiDB->new(
'swissprot' => $swissprot_db,
'embl' => $embl_db
);

# Each time before you want to create a persistent object for
# Bio::Seq, assign the 'namescape' sub of seq object first, as the
# biodatabase name.
my $seq; # for either store or fetch.
$seq->namespace('swissprot');

# OR you need to assign the default namespace for multiDB
$multiDB->namespace('swissport');

my $pseq = $multiDB->create_persistent($seq);
$pseq->store;


# If you want to fetch a seq, then you have to specify namespace for
# multiDB first
$multiDB->namespace('swissport');
$pseq = $multiDB->get_object_adaptor->find_by_unique_key($seq);

=head1 DESCRIPTION

The scalability issue will arise, when multiple huge bio databases are
loaded in a single database in RDBMS, due to the scalability of the
RDBMS. So one solution to solve it is simply to distribute them into
multiple physical database, while a user expects to manage them by one
logic adaptor.

So here you go, MultiDB aims at such issue to solve. The way to apply
that is pretty simple. You, first, load data from different
biodatabase, such as swissprot or embl, into physical RDBMS databases;
then create a db adaptor for each simple physical biosql db; finally
register these adaptors into MultiDB and use it as that was a normal
dbadaptor.

=head1 CONTACT

Juguang Xiao, juguang@tll.org.sg

=head1 APPENDIX

The rest of the documentation details each of the object
methods. Internal methods are usually preceded with a _


=cut

package Bio::DB::BioSQL::MultiDB;

use strict;
use vars qw(@ISA);

use Bio::Root::Root;

@ISA = qw(Bio::Root::Root);


=head2 new

=cut

sub new{
    my ($class, @args) = @_;
    my %dbs = @args;
    my $self = $class->SUPER::new(@args);

    foreach (keys %dbs){
        $self->_namespace_dbs($_, $dbs{$_});
    }
    return $self;
}

sub _namespace_dbs{
    my ($self, $key, $value) = @_;
    $self->{_namespace_dbs} = {} unless $self->{_namespace_dbs};
    if (exists $self->{_namespace_dbs}->{$key}){
return $self->{_namespace_dbs}->{$key};
}elsif(!defined $value){
# $self->throw("Cannot find \'$key\' as namespace. It may not regiested");
}

    return $self->{_namespace_dbs}->{$key} = $value if $value;

}

=head2 create_persistent

This method offers the same interface as Bio::DB::BioSQL::DBAdaptor,
hence the usage is same as well.

NOTE: You need to assign $obj-E<gt>namespace as the biodatabase name,
such as embl, before you invoke this method.

=cut

sub create_persistent{
    my ($self,$obj,@args) = @_;

    # The object instant creation is copy from Bio::DB::BioSQL::DBAdaptor

    # we need to obtain an instance of the class
    # if it's not already an instance
    if(! ref($obj)) {
        my $class = $obj;
        # load the module first, otherwise new() will fail;
        # this will throw an exception if it fails
        $self->_load_module($class);
        # we wrap this in an eval in order to indicate clearer what failed
        # (if it fails)
        eval {
            $obj = $class->new(@args);
        };
        if($@){
            $self->throw("Failed to instantiate ${obj}: ".$@);
        }
    }

    # The end of coping and here is my code.
    # Try to get namespace
    my $namespace;
    if($obj->isa('Bio::Seq') or $obj->can('namespace')){
        $namespace = $obj->namespace;
    }elsif(defined $self->namespace){
        $namespace = $self->namespace;
    }else{
        $self->throw('The module, '. ref($obj). ', is not supported');
    }

    my $db = $self->_namespace_dbs($namespace);
    return $db->create_persistent($obj);
    
}

sub get_object_adaptor{
    my ($self, $class, $dbc) = @_;
    my ($adp, $adpclass);

    my $namespace;
    if( ref $class){
        if( $class->can('namespace')){
            $namespace = $class->namespace;
        }else{
            $namespace = $self->namespace;
        }
        $class = ref $class;
    }else{
        $namespace = $self->namespace;
    }

    my $db = $self->_namespace_dbs($namespace);
    $adpclass = $db->get_object_adaptor($class, $dbc);

    return $adpclass;
}

sub find_by_unique_key{
    my ($self, $key) = @_;
    
    if($key->can('namespace')){
        if(defined(my $namespace = $key->namespace)){
            my $db = $self->_namespace_dbs($namespace);
            if(defined $db){
                my $adaptor = $db->get_object_adaptor($key);
                return $adaptor->find_by_unique_key($key);
            }
        }
    }

    # namespace is unknown from the key, so search for all registered db(s).
    eval{require Thread;};
    if($@){
# $self->throw("Failed to load Thread:\n$@");
        
        # The non-thread approach
        my @keys;
        foreach(keys %{$self->{_namespace_dbs}} ){
            my $db = $self->_namespace_dbs($_);
            $key->namespace($_);
            my $adaptor = $db->get_object_adaptor($key);
            my $result = $adaptor->find_by_unique_key($key);
push @keys, $result if defined $result;
        }
        return @keys;
    }
    
    my @threads;
    foreach(keys %{$self->{_namespace_dbs}} ){
        my $db = $self->_namespace_dbs($_);
        my $adaptor = $db->get_object_adaptor($key);
        my $t = Thread->new(
            sub{
                return shift->find_by_unique_key(shift);
            },
            $adaptor, $key);
        push @threads, $t;
    }

    my @keys;
    foreach(@threads){
        my $result = $_->join();
        push @keys, $result if defined $result;
    }

    return @keys;
}

sub _find_by_unique_key{
    my ($adaptor, $key) = @_;
    return $adaptor->find_by_unique_key($key);
}

=head2 Get/Set for default namespace

=cut

sub namespace{
    my ($self, $value) = @_;
    return $self->{'_namespace'} = $value if defined $value;
    return $self->{'_namespace'};
}



Something went wrong with that request. Please try again.