| 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::MarkerAdaptor | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | Provides database interaction for the Bio::EnsEMBL::Map::Marker object | ||
| 40 | |||
| 41 | =head1 METHODS | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | package Bio::EnsEMBL::Map::DBSQL::MarkerAdaptor; | ||
| 46 | $Bio::EnsEMBL::Map::DBSQL::MarkerAdaptor::VERSION = '114.0.0'; | ||
| 47 | use strict; | ||
| 48 | |||
| 49 | use vars ('@ISA'); | ||
| 50 | |||
| 51 | use Bio::EnsEMBL::Map::Marker; | ||
| 52 | use Bio::EnsEMBL::Map::MapLocation; | ||
| 53 | use Bio::EnsEMBL::Map::MarkerSynonym; | ||
| 54 | use Bio::EnsEMBL::DBSQL::BaseAdaptor; | ||
| 55 | use Bio::EnsEMBL::Utils::Exception qw(warning); | ||
| 56 | |||
| 57 | @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor); | ||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | =head2 fetch_all | ||
| 63 | |||
| 64 | Arg [1] : none | ||
| 65 | Example : @all_markers = @{$marker_adaptor->fetch_all}; | ||
| 66 | Description: Retrieves all markers from the database | ||
| 67 | Returntype : listref of Bio::EnsEMBL::Map::Markers | ||
| 68 | Exceptions : none | ||
| 69 | Caller : general | ||
| 70 | Status : stable | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub fetch_all { | ||
| 75 | my $self = shift; | ||
| 76 | my $dbID = shift; | ||
| 77 | |||
| 78 | my $sth = $self->prepare("SELECT m.marker_id, m.priority, m.left_primer, | ||
| 79 | m.right_primer, m.type, | ||
| 80 | m.min_primer_dist, m.max_primer_dist, | ||
| 81 | ms.marker_synonym_id, ms.name, ms.source | ||
| 82 | FROM marker m | ||
| 83 | LEFT JOIN marker_synonym ms | ||
| 84 | ON ms.marker_synonym_id = | ||
| 85 | m.display_marker_synonym_id"); | ||
| 86 | |||
| 87 | $sth->execute; | ||
| 88 | |||
| 89 | my( $marker_id, $priority, $left_primer, $right_primer, $type, | ||
| 90 | $min_pdist, $max_pdist, $ms_id, $ms_name, $ms_src); | ||
| 91 | |||
| 92 | $sth->bind_columns(\$marker_id, \$priority, | ||
| 93 | \$left_primer, \$right_primer, \$type, \$min_pdist, \$max_pdist, | ||
| 94 | \$ms_id, \$ms_name, \$ms_src); | ||
| 95 | |||
| 96 | my @out; | ||
| 97 | while($sth->fetch) { | ||
| 98 | #create a display marker synonym for each marker created, if one is defined | ||
| 99 | my $synonym; | ||
| 100 | if($ms_id) { | ||
| 101 | $synonym = Bio::EnsEMBL::Map::MarkerSynonym->new | ||
| 102 | ($ms_id, $ms_src, $ms_name); | ||
| 103 | } | ||
| 104 | |||
| 105 | push @out, Bio::EnsEMBL::Map::Marker->new | ||
| 106 | ($marker_id, $self, $left_primer, $right_primer, $min_pdist, $max_pdist, | ||
| 107 | $priority, $type, $synonym); | ||
| 108 | } | ||
| 109 | |||
| 110 | return \@out; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | =head2 fetch_by_dbID | ||
| 116 | |||
| 117 | Arg [1] : int $dbID | ||
| 118 | The internal identifier of the Marker to retrieve | ||
| 119 | Example : $marker = $marker_adaptor->fetch_by_dbID(123); | ||
| 120 | Description: Retrieves a marker object from the database via its internal | ||
| 121 | identifier. | ||
| 122 | Returntype : Bio::EnsEMBL::Map::Marker | ||
| 123 | Exceptions : thrown if no marker with $dbID is present in the database | ||
| 124 | Caller : general | ||
| 125 | Status : stable | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub fetch_by_dbID { | ||
| 130 | my $self = shift; | ||
| 131 | my $dbID = shift; | ||
| 132 | |||
| 133 | $self->throw('dbID argument is required') unless($dbID); | ||
| 134 | |||
| 135 | my $sth = $self->prepare("SELECT m.marker_id, m.display_marker_synonym_id, | ||
| 136 | m.priority, m.left_primer, m.right_primer, | ||
| 137 | m.type, | ||
| 138 | m.min_primer_dist, m.max_primer_dist, | ||
| 139 | ms.marker_synonym_id, | ||
| 140 | ms.source, ms.name | ||
| 141 | FROM marker m, marker_synonym ms | ||
| 142 | WHERE m.marker_id = ? | ||
| 143 | AND ms.marker_id = m.marker_id"); | ||
| 144 | |||
| 145 | $sth->execute($dbID); | ||
| 146 | |||
| 147 | my( $marker_id, $display_ms_id, $priority, $left_primer, $right_primer, | ||
| 148 | $type, $min_pdist, $max_pdist, $ms_id, $ms_src, $ms_name); | ||
| 149 | |||
| 150 | $sth->bind_columns(\$marker_id, \$display_ms_id, \$priority, | ||
| 151 | \$left_primer, \$right_primer, \$type, | ||
| 152 | \$min_pdist, \$max_pdist, | ||
| 153 | \$ms_id, \$ms_src, \$ms_name); | ||
| 154 | |||
| 155 | my $display_synonym; | ||
| 156 | my @synonyms; | ||
| 157 | while($sth->fetch) { | ||
| 158 | #create a new synonym for each row | ||
| 159 | my $s = new Bio::EnsEMBL::Map::MarkerSynonym->new($ms_id, $ms_src, | ||
| 160 | $ms_name); | ||
| 161 | $display_synonym = $s if($display_ms_id == $ms_id); | ||
| 162 | push @synonyms, $s; | ||
| 163 | } | ||
| 164 | |||
| 165 | $sth->finish; | ||
| 166 | |||
| 167 | unless($marker_id) { | ||
| 168 | $self-> warning("marker with dbID=[$dbID] not present in database"); | ||
| 169 | return undef; | ||
| 170 | } | ||
| 171 | |||
| 172 | #now create the marker | ||
| 173 | return new Bio::EnsEMBL::Map::Marker->new( | ||
| 174 | $marker_id, $self, $left_primer, $right_primer,$min_pdist, $max_pdist, | ||
| 175 | $priority, $type,$display_synonym, \@synonyms); | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | =head2 fetch_all_by_synonym | ||
| 180 | |||
| 181 | Arg [1] : string $synonym | ||
| 182 | An name of this marker | ||
| 183 | Arg [2] : (opional) string $source | ||
| 184 | The source of this name | ||
| 185 | Example : @markers = @{$marker_adaptor->fetch_all_by_synonym($id)}; | ||
| 186 | Description: Retrieves a list of markers with the synonym (alias) $synonym | ||
| 187 | and from source $source. In most cases the list will have a | ||
| 188 | single element, however it is possible that multiple markers | ||
| 189 | with the same synonym exist. | ||
| 190 | Returntype : listref of Bio::EnsEMBL::Map::Markers | ||
| 191 | Exceptions : none | ||
| 192 | Caller : general | ||
| 193 | Status : stable | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | sub fetch_all_by_synonym { | ||
| 198 | my ($self, $synonym, $source) = @_; | ||
| 199 | |||
| 200 | $self->throw("synonym argument is required") unless($synonym); | ||
| 201 | |||
| 202 | my $q = "SELECT marker_id | ||
| 203 | FROM marker_synonym ms | ||
| 204 | WHERE ms.name = ?"; | ||
| 205 | |||
| 206 | my @bind_vals = ($synonym); | ||
| 207 | |||
| 208 | if($source) { | ||
| 209 | $q .= " AND ms.source = ?"; | ||
| 210 | push(@bind_vals, $source); | ||
| 211 | } | ||
| 212 | |||
| 213 | my $sth = $self->prepare($q); | ||
| 214 | $sth->execute(@bind_vals); | ||
| 215 | |||
| 216 | my @out = (); | ||
| 217 | my $marker_id; | ||
| 218 | my %seen; | ||
| 219 | |||
| 220 | #fetch the markers and filter out duplictes | ||
| 221 | while(($marker_id) = $sth->fetchrow_array) { | ||
| 222 | next if $seen{$marker_id}; | ||
| 223 | |||
| 224 | # some synonyms point to markers that don't exist, so only add genuine ones | ||
| 225 | my $marker = $self->fetch_by_dbID($marker_id); | ||
| 226 | push @out, $marker if ($marker); | ||
| 227 | $seen{$marker_id} = 1; | ||
| 228 | } | ||
| 229 | |||
| 230 | $sth->finish; | ||
| 231 | |||
| 232 | return \@out; | ||
| 233 | } | ||
| 234 | |||
| 235 | |||
| 236 | |||
| 237 | =head2 fetch_attributes | ||
| 238 | |||
| 239 | Arg [1] : Bio::EnsEMBL::Map::Marker $marker | ||
| 240 | Example : $marker_adaptor->fetch_attributes($marker); | ||
| 241 | Description: Fetches the marker_synonym and map_location attributes of | ||
| 242 | a marker. This is done so that these attributes can be | ||
| 243 | lazy-loaded on request. | ||
| 244 | Returntype : none | ||
| 245 | Exceptions : none | ||
| 246 | Caller : Bio::EnsEMBL::Map::Marker::marker | ||
| 247 | Status : stable | ||
| 248 | |||
| 249 | =cut | ||
| 250 | |||
| 251 | sub fetch_attributes { | ||
| 252 | my $self = shift; | ||
| 253 | my $marker = shift; | ||
| 254 | |||
| 255 | my $m_id = $marker->dbID; | ||
| 256 | |||
| 257 | $self->throw('Marker argument does not have a dbID') unless($m_id); | ||
| 258 | |||
| 259 | # | ||
| 260 | # First Retrieve synonyms for this marker | ||
| 261 | # | ||
| 262 | $marker->flush_MarkerSynonyms; | ||
| 263 | |||
| 264 | my $sth = $self->prepare("SELECT ms.marker_synonym_id, ms.source, ms.name | ||
| 265 | FROM marker_synonym ms | ||
| 266 | WHERE ms.marker_id = ?"); | ||
| 267 | |||
| 268 | my @syns = (); | ||
| 269 | my ($ms_id, $ms_src, $ms_name); | ||
| 270 | |||
| 271 | $sth->execute($m_id); | ||
| 272 | $sth->bind_columns(\$ms_id, \$ms_src, \$ms_name); | ||
| 273 | |||
| 274 | while($sth->fetch) { | ||
| 275 | push @syns, Bio::EnsEMBL::Map::MarkerSynonym->new($ms_id,$ms_src,$ms_name); | ||
| 276 | } | ||
| 277 | $sth->finish; | ||
| 278 | |||
| 279 | $marker->add_MarkerSynonyms(@syns) if(@syns); | ||
| 280 | |||
| 281 | # | ||
| 282 | # Now retrieve map locations for this marker | ||
| 283 | # | ||
| 284 | $marker->flush_MapLocations; | ||
| 285 | |||
| 286 | $sth = $self->prepare("SELECT mloc.chromosome_name, mloc.position, | ||
| 287 | mloc.lod_score, map.map_name, ms.name | ||
| 288 | FROM marker_map_location mloc, map map, | ||
| 289 | marker_synonym ms | ||
| 290 | WHERE mloc.marker_id = ? | ||
| 291 | AND map.map_id = mloc.map_id | ||
| 292 | AND ms.marker_synonym_id = mloc.marker_synonym_id"); | ||
| 293 | |||
| 294 | my($chr_name, $pos, $lod, $mname, $name); | ||
| 295 | my @mlocs; | ||
| 296 | |||
| 297 | $sth->execute($m_id); | ||
| 298 | |||
| 299 | $sth->bind_columns(\$chr_name, \$pos, \$lod, \$mname, \$name); | ||
| 300 | |||
| 301 | while($sth->fetch) { | ||
| 302 | push(@mlocs, Bio::EnsEMBL::Map::MapLocation->new($name, $mname, | ||
| 303 | $chr_name,$pos,$lod)); | ||
| 304 | } | ||
| 305 | |||
| 306 | $sth->finish; | ||
| 307 | |||
| 308 | $marker->add_MapLocations(@mlocs); | ||
| 309 | } | ||
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | =head2 store | ||
| 314 | |||
| 315 | Arg [1] : Bio::EnsEMBL::Map::Marker | ||
| 316 | Example : $marker_adaptor->store(@markers); | ||
| 317 | Description: Stores a list of markers in this database. | ||
| 318 | The dbID and adaptor of each marker will be set on successful | ||
| 319 | storing. | ||
| 320 | Returntype : 1 on success | ||
| 321 | Exceptions : thrown if not all data needed for storing is populated in the | ||
| 322 | marker | ||
| 323 | Caller : general | ||
| 324 | Status : stable | ||
| 325 | |||
| 326 | =cut | ||
| 327 | |||
| 328 | sub store { | ||
| 329 | my ($self, @markers) = @_; | ||
| 330 | |||
| 331 | MARKER:foreach my $marker( @markers ){ | ||
| 332 | |||
| 333 | if($marker->dbID){ | ||
| 334 | if($self->fetch_by_dbID($marker->dbID)){ | ||
| 335 | next MARKER; | ||
| 336 | } | ||
| 337 | } | ||
| 338 | # | ||
| 339 | # Sanity check | ||
| 340 | # | ||
| 341 | if(!$marker || | ||
| 342 | !ref($marker) || | ||
| 343 | !$marker->isa('Bio::EnsEMBL::Map::Marker')) { | ||
| 344 | $self->throw('Incorrect argument [$marker] to store. Expected ' . | ||
| 345 | 'Bio::EnsEMBL::Map::Marker'); | ||
| 346 | } | ||
| 347 | |||
| 348 | # Don't store if already stored | ||
| 349 | if($marker->is_stored($self->db())) { | ||
| 350 | warning('Marker ['.$marker->dbID.'] is already stored in this DB.'); | ||
| 351 | next; | ||
| 352 | } | ||
| 353 | |||
| 354 | # Get/test the display marker synonym | ||
| 355 | my $display_synonym = $marker->display_MarkerSynonym; | ||
| 356 | if(!$display_synonym || !ref($display_synonym) || | ||
| 357 | !$display_synonym->isa('Bio::EnsEMBL::Map::MarkerSynonym')) { | ||
| 358 | $self->throw('Cannot store Marker without an associated '. | ||
| 359 | 'display_MarkerSynonym'); | ||
| 360 | } | ||
| 361 | |||
| 362 | # Store the Marker itself | ||
| 363 | my $q = qq( | ||
| 364 | INSERT INTO marker ( left_primer, right_primer, | ||
| 365 | min_primer_dist, max_primer_dist, | ||
| 366 | priority, type) | ||
| 367 | VALUES ( ?,?,?,?,?,?) ); | ||
| 368 | |||
| 369 | my $sth = $self->prepare($q); | ||
| 370 | |||
| 371 | $sth->execute( $marker->left_primer || '', | ||
| 372 | $marker->right_primer || '', | ||
| 373 | $marker->min_primer_dist || 0, | ||
| 374 | $marker->max_primer_dist || 0, | ||
| 375 | $marker->priority, | ||
| 376 | $marker->type ); | ||
| 377 | |||
| 378 | my $dbID = $self->last_insert_id('marker_id', undef, 'marker'); | ||
| 379 | $marker->dbID($dbID); | ||
| 380 | $marker->adaptor($self); | ||
| 381 | |||
| 382 | if(!$display_synonym->dbID) { | ||
| 383 | # Store synonym | ||
| 384 | $self->_store_MarkerSynonym($marker,$display_synonym); | ||
| 385 | } | ||
| 386 | my $display_synonym_id = $display_synonym->dbID || | ||
| 387 | $self->throw('display_MarkerSynonym must have dbID to store Marker'); | ||
| 388 | |||
| 389 | # Update the marker with the display synonym | ||
| 390 | my $qup = qq( | ||
| 391 | UPDATE marker | ||
| 392 | SET display_marker_synonym_id = $display_synonym_id | ||
| 393 | WHERE marker_id = ? ); | ||
| 394 | my $sthup = $self->prepare($qup); | ||
| 395 | $sthup->execute($dbID); | ||
| 396 | |||
| 397 | # Loop through all MarkerSynonyms and store if needed | ||
| 398 | foreach my $synonym( @{$marker->get_all_MarkerSynonyms} ){ | ||
| 399 | if(!$synonym->dbID) { | ||
| 400 | $self->_store_MarkerSynonym($marker,$synonym); | ||
| 401 | } | ||
| 402 | } | ||
| 403 | |||
| 404 | # Loop through all MapLocations and store if needed | ||
| 405 | foreach my $loc( @{$marker->get_all_MapLocations} ){ | ||
| 406 | # Dunno how to implement this :( Just bomb out | ||
| 407 | $self->throw( 'Storing of MapLocation objects is not yet implemented' ) | ||
| 408 | } | ||
| 409 | |||
| 410 | } | ||
| 411 | return 1; | ||
| 412 | } | ||
| 413 | |||
| 414 | |||
| 415 | =head2 _store_MarkerSynonym | ||
| 416 | |||
| 417 | Arg [1] : Bio::EnsEMBL::Map::Marker | ||
| 418 | Arg [2] : Bio::EnsEMBL::Map::MarkerSynonym | ||
| 419 | Example : $marker_adaptor->_store_MarkerSynonym($marker,$ms); | ||
| 420 | Description: Stores a marker synonym attached to the marker in the database | ||
| 421 | The dbID of each MarkerSynonym will be set on successful | ||
| 422 | storing. | ||
| 423 | Returntype : dbID of the MarkerSynonym | ||
| 424 | Exceptions : thrown if not all data needed for storing is populated | ||
| 425 | Caller : $self->store | ||
| 426 | Status : stable | ||
| 427 | |||
| 428 | =cut | ||
| 429 | |||
| 430 | sub _store_MarkerSynonym{ | ||
| 431 | my $self = shift; | ||
| 432 | my $marker = shift; | ||
| 433 | my $ms = shift; | ||
| 434 | |||
| 435 | # Sanity check | ||
| 436 | if(!$marker || !ref($marker) || | ||
| 437 | !$marker->isa('Bio::EnsEMBL::Map::Marker')) { | ||
| 438 | $self->throw("Incorrect argument [$marker] to _store_MarkerSynonym." . | ||
| 439 | "Expected Bio::EnsEMBL::Map::Marker"); | ||
| 440 | } | ||
| 441 | if(!$ms || !ref($ms) || | ||
| 442 | !$ms->isa('Bio::EnsEMBL::Map::MarkerSynonym')) { | ||
| 443 | $self->throw("Incorrect argument [$ms] to _store_MarkerSynonym." . | ||
| 444 | "Expected Bio::EnsEMBL::Map::MarkerSynonym"); | ||
| 445 | } | ||
| 446 | |||
| 447 | # Don't store if already stored | ||
| 448 | if($ms->dbID) { | ||
| 449 | warning('MarkerSynonym ['.$ms->dbID.'] is already stored in this DB.'); | ||
| 450 | return; | ||
| 451 | } | ||
| 452 | |||
| 453 | my $marker_id = $marker->dbID || | ||
| 454 | throw( "Marker has no dbID. Cannot store MarkerSynonym" ); | ||
| 455 | |||
| 456 | # Store the synonym | ||
| 457 | my $q = qq( | ||
| 458 | INSERT INTO marker_synonym ( marker_id, source, name ) | ||
| 459 | VALUES ( ?,?,?) ); | ||
| 460 | |||
| 461 | my $sth = $self->prepare($q); | ||
| 462 | |||
| 463 | $sth->execute( $marker_id, | ||
| 464 | $ms->source, | ||
| 465 | $ms->name ); | ||
| 466 | |||
| 467 | my $dbID = $self->last_insert_id('marker_synonym_id', undef, 'marker_synonym'); | ||
| 468 | $ms->dbID($dbID); | ||
| 469 | return $dbID; | ||
| 470 | } | ||
| 471 | |||
| 472 | 1; |