EnsEMBL/Map/DBSQL/MarkerAdaptor.pm untested

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