DensityTypeAdaptor.pm 9.2 KB
Newer Older
1 2
=head1 LICENSE

3
Copyright [1999-2013] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
4

5 6 7 8 9 10 11 12 13 14 15 16 17
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut
18 19 20 21 22


=head1 CONTACT

  Please email comments or questions to the public Ensembl
Magali Ruffier's avatar
Magali Ruffier committed
23
  developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
24 25

  Questions may also be sent to the Ensembl help desk at
Magali Ruffier's avatar
Magali Ruffier committed
26
  <http://www.ensembl.org/Help/Contact>.
27 28

=cut
29 30 31 32 33 34 35

=head1 NAME

Bio::EnsEMBL::DBSQL::DensityTypeAdaptor

=head1 SYNOPSIS

36 37
  my $density_type_adaptor =
    $registry->get_adaptor( 'Human', 'Core', 'DensityType' );
38

39 40 41
  my @density_types = @{ $density_type_adaptor->fetch_all() };

  my $dt = $density_type_adaptor->fetch_by_dbID(12);
42

43 44 45
=head1 DESCRIPTION

DensityTypeAdaptor - Performs database interaction for DensityType objects.
46 47 48 49 50 51 52 53 54

=head1 METHODS

=cut

package Bio::EnsEMBL::DBSQL::DensityTypeAdaptor;
use vars qw(@ISA);
use strict;

55 56 57
use Bio::EnsEMBL::DBSQL::BaseAdaptor;

use Bio::EnsEMBL::DensityType;
58 59
use Bio::EnsEMBL::Utils::Exception qw(throw warning);

60 61 62 63 64 65 66 67 68 69 70 71 72
@ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);



=head2 new

  Arg [1]    : see superclass (Bio::EnsEMBL::DBSQL::BaseAdaptor) arguments
  Example    : #use this instead of the constructor directly:
               my $dta = $db_adaptor->get_DensityTypeAdaptor();
  Description: Constructor. Creates a new DensityTypeAdaptor
  Returntype : Bio::EnsEMBL::DBSQL::DensityTypeAdaptor
  Exceptions : none
  Caller     : DBAdaptor
73
  Status     : Stable
74 75 76 77 78 79 80 81 82 83 84 85 86 87

=cut

sub new {
  my $class = shift;

  my $self = $class->SUPER::new(@_);

  $self->{'dbID_cache'} = {};

  return $self;
}


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
# _tables
#  Arg [1]    : none
#  Description: PROTECTED implementation of superclass abstract method.
#               Returns the names, aliases of the tables to use for queries.
#  Returntype : list of listrefs of strings
#  Exceptions : none
#  Caller     : internal
#  Status     : Stable

sub _tables {
  return (['density_type', 'dt']);
}


# _columns
#  Arg [1]    : none
#  Example    : none
#  Description: PROTECTED implementation of superclass abstract method.
#               Returns a list of columns to use for queries.
#  Returntype : list of strings
#  Exceptions : none
#  Caller     : internal
#  Status     : Stable

sub _columns {
  return ('dt.density_type_id', 'dt.analysis_id', 'dt.block_size', 'dt.region_features', 'dt.value_type');
}



118 119 120 121 122

=head2 fetch_all

  Arg [1]    : none
  Example    : my @density_types = @{$density_type_adaptor->fetch_all};
123 124 125 126
  Description: Retrieves every density type in the database.
               NOTE:  In a multi-species database, this method will
               return all the entries, not just the ones associated with
               the current species.
127 128 129
  Returntype : reference to list of Bio::EnsEMBL::DensityType objects
  Exceptions : none
  Caller     : general, new
130
  Status     : Stable
131 132 133 134 135 136 137 138 139

=cut

sub fetch_all {
  my $self = shift;

  my @out;

  my $sth = $self->prepare("SELECT density_type_id, analysis_id, block_size,".
140
                           "       value_type, region_features " .
141 142 143 144
                           "FROM density_type");

  $sth->execute();

145 146
  my($dbID, $analysis_id, $blk_size, $vtype, $region_features );
  $sth->bind_columns(\$dbID, \$analysis_id, \$blk_size, \$vtype, \$region_features );
147 148 149 150 151 152

  my $analysis_adaptor = $self->db->get_AnalysisAdaptor();

  while($sth->fetch()) {
    my $analysis = $analysis_adaptor->fetch_by_dbID($analysis_id);

153

154 155 156 157
    my $dt = Bio::EnsEMBL::DensityType->new(-ADAPTOR => $self,
                                            -DBID    => $dbID,
                                            -ANALYSIS => $analysis,
                                            -BLOCK_SIZE => $blk_size,
158
					    -REGION_FEATURES => $region_features,
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
                                            -VALUE_TYPE => $vtype);

    $self->{'dbID_cache'}->{$dbID} = $dt;

    push @out, $dt;
  }

  return \@out;
}



=head2 fetch_by_dbID

  Arg [1]    : int $dbID
  Example    : my $dt = $density_type_adaptor->fetch_by_dbID($dbID);
  Description: Retrieves a density type object via its internal identifier
  Returntype : Bio::EnsEMBL::DensityType
  Exceptions : throw if dbID argument not defined
  Caller     : general
179
  Status     : Stable
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

=cut

sub fetch_by_dbID {
  my $self = shift;
  my $dbID = shift;

  if(!defined($dbID)) {
    throw("dbID argument must be defined");
  }

  if($self->{'dbID_cache'}->{$dbID}) {
    return $self->{'dbID_cache'}->{$dbID};
  }

  # go back to database and refill caches
  $self->fetch_all();

  return $self->{'dbID_cache'}->{$dbID};
}


=head2 fetch_all_by_logic_name

  Arg [1]    : string $logic_name
  Example    : my @dts = @{$dtype_adaptor->fetch_all('repeat_coverage')};
206 207 208 209
  Description: Retrieves all density types with a given logic name.
               NOTE:  In a multi-species database, this method will
               return all the entries matching the search criteria, not
               just the ones associated with the current species.
210 211 212
  Returntype : reference to list of Bio::EnsEMBL::DensityTypes
  Exceptions : thrown if logic_name argument is not provided
  Caller     : general
213
  Status     : Stable
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

=cut

sub fetch_all_by_logic_name {
  my $self = shift;
  my $logic_name = shift;

  if(!defined($logic_name)) {
    throw("logic_name argument is required.");
  }

  my $analysis_adaptor = $self->db()->get_AnalysisAdaptor();
  my $analysis = $analysis_adaptor->fetch_by_logic_name($logic_name);

  return [] if(!$analysis);

  my $sth = $self->prepare("SELECT density_type_id, block_size,".
231
                           "       value_type, region_features " .
232 233
                           "FROM density_type " .
                           "WHERE analysis_id = ?");
234 235
  $sth->bind_param(1,$analysis->dbID,SQL_INTEGER);
  $sth->execute();
236

237 238
  my($dbID, $blk_size, $vtype, $region_features );
  $sth->bind_columns(\$dbID, \$blk_size, \$vtype, \$region_features);
239 240 241 242

  my @out;

  while($sth->fetch()) {
243

244 245 246 247
    my $dt = Bio::EnsEMBL::DensityType->new(-ADAPTOR => $self,
                                            -DBID    => $dbID,
                                            -ANALYSIS => $analysis,
                                            -BLOCK_SIZE => $blk_size,
248
					    -REGION_FEATURES => $region_features,
249 250 251 252 253 254 255 256 257 258 259
                                            -VALUE_TYPE => $vtype);

    $self->{'dbID_cache'}->{$dbID} = $dt;

    push @out, $dt;
  }

  return \@out;
}


260 261 262 263
=head2 store

  Arg [1]    : list of Bio::EnsEMBL::DensityType @dt
               the density types to store in the database
264
  Example    : $density_type->store(@density_types);
265 266 267 268 269
  Description: Stores a list of density type objects in the database
  Returntype : none
  Exceptions : thrown if @dt is not defined
               or if any elements of @dt are not Bio::EnsEMBL::DensityType 
  Caller     : general
270
  Status     : Stable
271 272

=cut
273 274

sub store {
275 276 277 278 279 280 281
  my ($self,@dt) = @_;

  if( scalar(@dt) == 0 ) {
    throw("Must call store with list of Density Types");
  }

  my $sth = $self->prepare
282
    ("INSERT IGNORE INTO density_type (analysis_id,".
283 284
                                  "block_size, value_type, region_features ) ". 
    "VALUES (?, ?, ?, ?)");
285 286 287 288 289 290 291 292 293

  my $db = $self->db();
  my $analysis_adaptor = $db->get_AnalysisAdaptor();

 FEATURE: foreach my $dt ( @dt ) {
    if( !ref $dt || !$dt->isa("Bio::EnsEMBL::DensityType") ) {
      throw("Density Type must be an Ensembl DensityType, " .
            "not a [".ref($dt)."]");
    }
294

295 296 297
    if($dt->is_stored($db)) {
      next FEATURE;
    }
298 299

    if(!defined($dt->analysis())) {
300 301
      throw("An analysis must be attached to the density type to be stored.");
    }
302

303 304
    #store the analysis if it has not been stored yet
    if(!$dt->analysis->is_stored($db)) {
305
      $analysis_adaptor->store($dt->analysis());
306 307
    }
	
308 309 310 311 312
    my $block_size = $dt->block_size();
    $block_size |= 0;
    my $region_features = $dt->region_features();
    $region_features |= 0;

313 314 315 316 317
    $sth->bind_param(1,$dt->analysis->dbID,SQL_INTEGER);
    $sth->bind_param(2,$block_size,SQL_INTEGER);
    $sth->bind_param(3,$dt->value_type,SQL_VARCHAR);
    $sth->bind_param(4,$region_features, SQL_VARCHAR);
    my $inserted = $sth->execute();
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

    my $dbID;

    # $inserted can be 0E0 which is true but equal to 0
    if(!$inserted || $inserted == 0) {
      # insert failed, presumably because was already stored in database

      my @dts=@{$self->fetch_all_by_logic_name($dt->analysis()->logic_name())};
      my ($stored_dt) = grep {$_->block_size() == $dt->block_size()} @dts;
      if(!$stored_dt) {
        throw("Could not retrieve or store DensityType from database.\n" .
              "Incorrect db permissions or missing density_type table?\n");
      }
      $dbID = $stored_dt->dbID();
    } else {
      $dbID = $sth->{'mysql_insertid'};
    }
335 336

    # next two lines are to set the density type as stored
337
    $dt->dbID($dbID);
338
    $dt->adaptor($self);
339 340

    $self->{'dbID_cache'}->{$dbID} = $dt;
341 342 343
  }
}

344
1;