| 1 | =head1 LICENSE | ||
| 2 | |||
| 3 | See the NOTICE file distributed with this work for additional information | ||
| 4 | regarding copyright ownership. | ||
| 5 | |||
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 7 | you may not use this file except in compliance with the License. | ||
| 8 | You may obtain a copy of the License at | ||
| 9 | |||
| 10 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 11 | |||
| 12 | Unless required by applicable law or agreed to in writing, software | ||
| 13 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 15 | See the License for the specific language governing permissions and | ||
| 16 | limitations under the License. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | |||
| 21 | =head1 CONTACT | ||
| 22 | |||
| 23 | Please email comments or questions to the public Ensembl | ||
| 24 | developers list at <http://lists.ensembl.org/mailman/listinfo/dev>. | ||
| 25 | |||
| 26 | Questions may also be sent to the Ensembl help desk at | ||
| 27 | <http://www.ensembl.org/Help/Contact>. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | Bio::EnsEMBL::Map::DBSQL::DitagAdaptor | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | my $ditagadaptor = $db->get_DitagAdaptor(); | ||
| 38 | my @ditags = @{ $ditagadaptor->fetch_by_type("ZZ11") }; | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | Provides database interaction for the Bio::EnsEMBL::Map::Ditag object | ||
| 43 | |||
| 44 | =head1 METHODS | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | package Bio::EnsEMBL::Map::DBSQL::DitagAdaptor; | ||
| 49 | $Bio::EnsEMBL::Map::DBSQL::DitagAdaptor::VERSION = '114.0.0'; | ||
| 50 | use strict; | ||
| 51 | use vars ('@ISA'); | ||
| 52 | |||
| 53 | use Bio::EnsEMBL::Map::Ditag; | ||
| 54 | use Bio::EnsEMBL::DBSQL::BaseAdaptor; | ||
| 55 | use Bio::EnsEMBL::Utils::Exception qw( throw warning ); | ||
| 56 | |||
| 57 | @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor); | ||
| 58 | |||
| 59 | |||
| 60 | =head2 fetch_all_by_name | ||
| 61 | |||
| 62 | Arg [1] : ditag name | ||
| 63 | Example : $tag = $ditag_adaptor->fetch_by_name("U3"); | ||
| 64 | Description: Retrieves ditags from the database using the name | ||
| 65 | Returntype : listref of Bio::EnsEMBL::Map::Ditag | ||
| 66 | Caller : general | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub fetch_all_by_name { | ||
| 71 | my ($self, $tagname) = @_; | ||
| 72 | |||
| 73 | if(!$tagname){ | ||
| 74 | throw "must be called with a name of a ditag."; | ||
| 75 | } | ||
| 76 | my $sth = $self->prepare("SELECT d.ditag_id, d.name, d.type, d.tag_count, d.sequence | ||
| 77 | FROM ditag d | ||
| 78 | WHERE d.name = ?"); | ||
| 79 | $sth->execute($tagname); | ||
| 80 | my $result = $self->_fetch($sth); | ||
| 81 | |||
| 82 | return $result; | ||
| 83 | |||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | =head2 fetch_by_dbID | ||
| 88 | |||
| 89 | Arg [1] : ditag type | ||
| 90 | Example : @all_tags = @{$ditag_adaptor->fetch_by_dbID(1003)}; | ||
| 91 | Description: Retrieve ditags with a certain id from the database | ||
| 92 | Returntype : Bio::EnsEMBL::Map::Ditag | ||
| 93 | Caller : general | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub fetch_by_dbID { | ||
| 98 | my ($self, $tagid) = @_; | ||
| 99 | |||
| 100 | if(!$tagid){ | ||
| 101 | throw "must be called with the type of a ditag."; | ||
| 102 | } | ||
| 103 | my $sth = $self->prepare("SELECT d.ditag_id, d.name, d.type, d.tag_count, d.sequence | ||
| 104 | FROM ditag d | ||
| 105 | WHERE d.ditag_id = ?"); | ||
| 106 | $sth->execute($tagid); | ||
| 107 | my $result = $self->_fetch($sth); | ||
| 108 | |||
| 109 | return $result->[0]; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | =head2 fetch_all_by_type | ||
| 114 | |||
| 115 | Arg [1] : ditag type | ||
| 116 | Example : @all_tags = @{$ditag_adaptor->fetch_by_type("SME005")}; | ||
| 117 | Description: Retrieves all ditags of a certain type from the database | ||
| 118 | Returntype : listref of Bio::EnsEMBL::Map::Ditag | ||
| 119 | Caller : general | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub fetch_all_by_type { | ||
| 124 | my ($self, $tagtype) = @_; | ||
| 125 | |||
| 126 | if(!$tagtype){ | ||
| 127 | throw "must be called with the type of a ditag."; | ||
| 128 | } | ||
| 129 | my $sth = $self->prepare("SELECT d.ditag_id, d.name, d.type, d.tag_count, d.sequence | ||
| 130 | FROM ditag d | ||
| 131 | WHERE d.type = ?"); | ||
| 132 | $sth->execute($tagtype); | ||
| 133 | my $result = $self->_fetch($sth); | ||
| 134 | |||
| 135 | return $result; | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | =head2 fetch_by_name_and_type | ||
| 140 | |||
| 141 | Arg [1] : ditag name | ||
| 142 | Arg [2] : ditag type | ||
| 143 | Example : $tag = $ditag_adaptor->fetch_by_name_and_type("U3", "SME005"); | ||
| 144 | Description: Retrieves ditags from the database using name/type combination | ||
| 145 | which should be non-ambiguous | ||
| 146 | Returntype : Bio::EnsEMBL::Map::Ditag | ||
| 147 | Caller : general | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | sub fetch_by_name_and_type { | ||
| 152 | my ($self, $tagname, $tagtype) = @_; | ||
| 153 | |||
| 154 | if(!$tagname or !$tagtype){ | ||
| 155 | throw "must be called with a name and type of a ditag."; | ||
| 156 | } | ||
| 157 | my $sth = $self->prepare("SELECT d.ditag_id, d.name, d.type, d.tag_count, d.sequence | ||
| 158 | FROM ditag d | ||
| 159 | WHERE d.name = ? and d.type = ?"); | ||
| 160 | $sth->execute($tagname, $tagtype); | ||
| 161 | my $result = $self->_fetch($sth); | ||
| 162 | |||
| 163 | return $result->[0]; | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | =head2 fetch_all | ||
| 168 | |||
| 169 | Args : none | ||
| 170 | Example : @all_tags = @{$ditag_adaptor->fetch_all}; | ||
| 171 | Description: Retrieves all ditags from the database | ||
| 172 | Returntype : listref of Bio::EnsEMBL::Map::Ditag | ||
| 173 | Caller : general | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | sub fetch_all { | ||
| 178 | my ($self) = @_; | ||
| 179 | |||
| 180 | my $sth = $self->prepare("SELECT d.ditag_id, d.name, d.type, d.tag_count, d.sequence | ||
| 181 | FROM ditag d"); | ||
| 182 | $sth->execute; | ||
| 183 | my $result = $self->_fetch($sth); | ||
| 184 | |||
| 185 | return $result; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | =head2 fetch_all_with_limit | ||
| 190 | |||
| 191 | Arg [1] : ditag type | ||
| 192 | Arg [2] : row limit | ||
| 193 | Arg [3] : row offset | ||
| 194 | Description: fetch_by_type with row limit and offset | ||
| 195 | Returntype : listref of Bio::EnsEMBL::Map::Ditag | ||
| 196 | Caller : general | ||
| 197 | |||
| 198 | =cut | ||
| 199 | |||
| 200 | sub fetch_all_with_limit { | ||
| 201 | my ($self, $tagtype, $limit, $offset) = @_; | ||
| 202 | |||
| 203 | my @ditags = (); | ||
| 204 | my $sql = "SELECT d.ditag_id, d.name, d.type, d.tag_count, d.sequence ". | ||
| 205 | "FROM ditag d ". | ||
| 206 | "WHERE d.type = ? LIMIT ? OFFSET ?;"; | ||
| 207 | my $sth = $self->db->dbc->prepare($sql); | ||
| 208 | $sth->execute($tagtype, $limit, $offset); | ||
| 209 | my $result = $self->_fetch($sth); | ||
| 210 | |||
| 211 | return $result; | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | =head2 _fetch | ||
| 216 | |||
| 217 | Arg [1] : statement handler object | ||
| 218 | Description: generic sql-fetch function for the ditag fetch methods | ||
| 219 | Returntype : listref of Bio::EnsEMBL::Map::Ditag | ||
| 220 | Caller : private | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | sub _fetch { | ||
| 225 | my ($self, $sth) = @_; | ||
| 226 | |||
| 227 | my($tag_id, $name, $type, $count, $sequence); | ||
| 228 | my @tags; | ||
| 229 | |||
| 230 | $sth->bind_columns(\$tag_id, \$name, \$type, \$count, \$sequence); | ||
| 231 | while($sth->fetch) { | ||
| 232 | push @tags, Bio::EnsEMBL::Map::Ditag->new ( | ||
| 233 | -dbID => $tag_id, | ||
| 234 | -name => $name, | ||
| 235 | -type => $type, | ||
| 236 | -tag_count => $count, | ||
| 237 | -sequence => $sequence, | ||
| 238 | -adaptor => $self, | ||
| 239 | ); | ||
| 240 | } | ||
| 241 | |||
| 242 | return \@tags; | ||
| 243 | } | ||
| 244 | |||
| 245 | |||
| 246 | =head2 store | ||
| 247 | |||
| 248 | Arg [1] : Bio::EnsEMBL::Map::Ditag | ||
| 249 | Example : $ditag_adaptor->store(\@ditags); | ||
| 250 | Description: Stores a single ditag or | ||
| 251 | a list of ditags in this database. | ||
| 252 | Returntype : none | ||
| 253 | Caller : general | ||
| 254 | |||
| 255 | =cut | ||
| 256 | |||
| 257 | sub store { | ||
| 258 | my ($self, $ditags) = @_; | ||
| 259 | |||
| 260 | if(ref $ditags eq 'ARRAY'){ | ||
| 261 | if(scalar(@$ditags) == 0){ | ||
| 262 | throw("Must call store with ditag or list ref of ditags"); | ||
| 263 | } | ||
| 264 | } | ||
| 265 | elsif($ditags){ | ||
| 266 | my @ditags; | ||
| 267 | push @ditags, $ditags; | ||
| 268 | $ditags = \@ditags; | ||
| 269 | } | ||
| 270 | else{ | ||
| 271 | throw("Must call store with ditag or list ref of ditags not ".$ditags); | ||
| 272 | } | ||
| 273 | |||
| 274 | my $db = $self->db() or throw "Couldn t get database connection."; | ||
| 275 | |||
| 276 | TAG: | ||
| 277 | foreach my $ditag (@$ditags) { | ||
| 278 | |||
| 279 | if ( !ref $ditag || !$ditag->isa("Bio::EnsEMBL::Map::Ditag") ) { | ||
| 280 | throw( "Object must be an Ensembl Ditag, " . "not a [" . ref($ditag) . "]" ); | ||
| 281 | } | ||
| 282 | |||
| 283 | if ( $ditag->is_stored($db) ) { | ||
| 284 | warning( "Ditag [" . $ditag->dbID . "] is already stored in this database." ); | ||
| 285 | next TAG; | ||
| 286 | } | ||
| 287 | |||
| 288 | #check if tag with same name/type already exists | ||
| 289 | my $sth = $self->prepare( "SELECT COUNT(*) FROM ditag | ||
| 290 | WHERE name = ? AND type = ?" ); | ||
| 291 | $sth->execute($ditag->name, $ditag->type); | ||
| 292 | if($sth->fetchrow() > 0){ | ||
| 293 | warning( "Ditag with name/type ".$ditag->name." / ".$ditag->type. | ||
| 294 | " is already stored in this database.\n". | ||
| 295 | "Use update_ditag() instead."); | ||
| 296 | next TAG; | ||
| 297 | } | ||
| 298 | |||
| 299 | if ( $ditag->dbID ) { | ||
| 300 | my $sth = $self->prepare( "INSERT INTO ditag( ditag_id , name, type, tag_count, sequence ) ". | ||
| 301 | "VALUES( ?,?,?,?,? )" ); | ||
| 302 | $sth->bind_param(1,$ditag->dbID,SQL_INTEGER); | ||
| 303 | $sth->bind_param(2,$ditag->name,SQL_VARCHAR); | ||
| 304 | $sth->bind_param(3,$ditag->type,SQL_VARCHAR); | ||
| 305 | $sth->bind_param(4,$ditag->tag_count,SQL_VARCHAR); | ||
| 306 | $sth->bind_param(5,$ditag->sequence,SQL_VARCHAR); | ||
| 307 | $sth->execute(); | ||
| 308 | } else { | ||
| 309 | my $sth = $self->prepare( "INSERT INTO ditag( name, type, tag_count, sequence ) ". | ||
| 310 | "VALUES( ?,?,?,? )" ); | ||
| 311 | $sth->bind_param(1,$ditag->name,SQL_VARCHAR); | ||
| 312 | $sth->bind_param(2,$ditag->type,SQL_VARCHAR); | ||
| 313 | $sth->bind_param(3,$ditag->tag_count,SQL_VARCHAR); | ||
| 314 | $sth->bind_param(4,$ditag->sequence,SQL_VARCHAR); | ||
| 315 | $sth->execute(); | ||
| 316 | my $dbID = $self->last_insert_id('ditag_id', undef, 'ditag'); | ||
| 317 | $ditag->dbID($dbID); | ||
| 318 | $ditag->adaptor($self); | ||
| 319 | } | ||
| 320 | } | ||
| 321 | |||
| 322 | return 1; | ||
| 323 | } | ||
| 324 | |||
| 325 | |||
| 326 | =head2 print_creation | ||
| 327 | |||
| 328 | Arg [1] : ditag probe name | ||
| 329 | Arg [2] : ditag type | ||
| 330 | Arg [3] : ditag count | ||
| 331 | Arg [4] : ditag sequence | ||
| 332 | Arg [5] : (optional) ditag dbID | ||
| 333 | Description: convenience method to produce SQL insert statements | ||
| 334 | to speed up the creation of new ditags | ||
| 335 | Returntype : string | ||
| 336 | |||
| 337 | =cut | ||
| 338 | |||
| 339 | sub print_creation { | ||
| 340 | my ($self, $probe_name, $type, $count, $sequence, $dbid) = @_; | ||
| 341 | my $string; | ||
| 342 | if($dbid){ | ||
| 343 | $string = "INSERT INTO ditag( ditag_id, name, type, tag_count, sequence ) ". | ||
| 344 | "VALUES($dbid, '".$probe_name."', '".$type."', ".$count."'".$sequence."');\n"; | ||
| 345 | } | ||
| 346 | else { | ||
| 347 | $string = "INSERT INTO ditag( name, type, tag_count, sequence ) ". | ||
| 348 | "VALUES('".$probe_name."', '".$type."', ".$count.", '".$sequence."');\n"; | ||
| 349 | } | ||
| 350 | |||
| 351 | return $string; | ||
| 352 | } | ||
| 353 | |||
| 354 | |||
| 355 | =head2 update_ditag | ||
| 356 | |||
| 357 | Arg [1] : ditag to update | ||
| 358 | Description: update an existing ditag with new values | ||
| 359 | Returntype : true on success | ||
| 360 | |||
| 361 | =cut | ||
| 362 | |||
| 363 | sub update_ditag { | ||
| 364 | my ($self, $ditag) = @_; | ||
| 365 | |||
| 366 | my $sth = $self->prepare( "UPDATE ditag SET name=?, type=?, tag_count=?, sequence=? where ditag_id=?;" ); | ||
| 367 | my $result =$sth->execute( | ||
| 368 | $ditag->name, | ||
| 369 | $ditag->type, | ||
| 370 | $ditag->tag_count, | ||
| 371 | $ditag->sequence, | ||
| 372 | $ditag->dbID, | ||
| 373 | ); | ||
| 374 | |||
| 375 | return $result; | ||
| 376 | } | ||
| 377 | |||
| 378 | |||
| 379 | =head2 list_dbIDs | ||
| 380 | |||
| 381 | Args : None | ||
| 382 | Example : my @feature_ids = @{$da->list_dbIDs()}; | ||
| 383 | Description: Gets an array of internal IDs for all Ditag objects in | ||
| 384 | the current database. | ||
| 385 | Returntype : List of ints | ||
| 386 | Exceptions : None | ||
| 387 | |||
| 388 | =cut | ||
| 389 | |||
| 390 | sub list_dbIDs { | ||
| 391 | my ($self, $ordered) = @_; | ||
| 392 | |||
| 393 | return $self->_list_dbIDs('ditag'); | ||
| 394 | } | ||
| 395 | |||
| 396 | 1; |