EnsEMBL/DBSQL/UnmappedObjectAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 163 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 15 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 245 CC 18 SCAR 35.4
CC
18
Cov
0%
store
110.0
fetch_all_by_analysis
20.0
fetch_by_identifier
20.0
CRAP 34.6
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::DBSQL::UnmappedObjectAdaptor
34  
35 =head1 SYNOPSIS
36  
37 my $uoa = $database_adaptor->get_UnmappedObjectAdaptor();
38  
39 my $missed = @{ $uoa->fetch_all_by_type('xref') };
40  
41 =head1 DESCRIPTION
42  
43 Unmapped ObjectAdaptor - An adaptor responsible for the creation,
44 editing, retrieval of Unmapped Objects. These being the Objects that
45 where not mapped in a specific process i.e. xref, cDNA, Markers.
46  
47 =head1 METHODS
48  
49 =cut
50  
51 package Bio::EnsEMBL::DBSQL::UnmappedObjectAdaptor;
52 $Bio::EnsEMBL::DBSQL::UnmappedObjectAdaptor::VERSION = '114.0.0_50'; # TRIAL
53 $Bio::EnsEMBL::DBSQL::UnmappedObjectAdaptor::VERSION = '114.0.050';
54 use vars qw(@ISA);
55 use strict;
56  
57  
58 use POSIX;
59 use Bio::EnsEMBL::Utils::Cache;
60 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
61 use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
62 use Bio::EnsEMBL::UnmappedObject;
63 use Bio::EnsEMBL::Analysis;
64 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor);
65  
66  
67 =head2 new
68  
69 Arg [1] : list of args @args
70 Superclass constructor arguments
71 Example : none
72 Description: Constructor which just initializes internal cache structures
73 Returntype : Bio::EnsEMBL::DBSQL::UnmappedObjectAdaptor
74 Exceptions : none
75 Caller : implementing subclass constructors
76 Status : At Risk
77  
78 =cut
79  
80 our %desc_to_id;
81 sub new {
82 my $proto = shift;
83  
84 my $class = ref($proto) || $proto;
85 my $self = $class->SUPER::new(@_);
86  
87 my $sth =
88 $self->prepare("
89 SELECT
90 unmapped_reason_id,
91 full_description
92 FROM
93 unmapped_reason
94 ");
95  
96 $sth->execute();
97  
98 my ( $id, $desc );
99 $sth->bind_columns( \( $id, $desc ) );
100  
101 while ( $sth->fetch() ) {
102 $desc_to_id{$desc} = $id;
103 }
104  
105 $sth->finish();
106  
107  
108 return $self;
109 }
110  
111  
112 # _tables
113 # Arg [1] : none
114 # Description: PROTECTED implementation of superclass abstract method
115 # returns the names, aliases of the tables to use for queries
116 # Returntype : list of listrefs of strings
117 # Exceptions : none
118 # Caller : internal
119 # Status : At Risk
120 sub _tables {
121 my $self = shift;
122  
123 return (['unmapped_object', 'uo'],
124 ['unmapped_reason', 'ur']);
125 }
126  
127  
128 # _columns
129 # Arg [1] : none
130 # Example : none
131 # Description: PROTECTED implementation of superclass abstract method
132 # returns a list of columns to use for queries
133 # Returntype : list of strings
134 # Exceptions : none
135 # Caller : internal
136 # Status : At Risk
137  
138 sub _columns {
139 my $self = shift;
140  
141 return qw(uo.unmapped_object_id uo.type uo.analysis_id uo.external_db_id
142 uo.identifier uo.unmapped_reason_id uo.query_score uo.target_score
143 uo.ensembl_id uo.ensembl_object_type
144 ur.summary_description ur.full_description);
145 }
146  
147 sub _left_join {
148 return ( [
149 'unmapped_object', "uo.unmapped_reason_id = ur.unmapped_reason_id"
150 ] );
151 }
152  
153 =head2 list_dbIDs
154  
155 Arg [1] : none
156 Example : @unmapped_object_ids = @{$unmapped_object_adaptor->list_dbIDs()};
157 Description: Gets an array of internal ids for all unmapped_objects in the current db
158 Returntype : list of ints
159 Exceptions : none
160 Caller : ?
161 Status : Stable
162  
163 =cut
164  
165 sub list_dbIDs {
166 my ($self) = @_;
167  
168 return $self->_list_dbIDs("unmapped_object");
169 }
170  
171 =head2 list_unmapped_reasons
172  
173 Arg [1] : none
174 Example : @unmapped_object_reason+ids =
175 @{$unmapped_object_adaptor->list_unmapped_reasons()};
176 Description: Gets an array of internal ids for all unmapped_objects in the current db
177 Returntype : list of ints
178 Exceptions : none
179 Caller : ?
180 Status : Stable
181  
182 =cut
183  
184 sub list_unmapped_reasons {
185 my ($self) = @_;
186  
187 return $self->_list_dbIDs("unmapped_reason");
188 }
189  
190  
191 # _objs_from_sth
192  
193 # Arg [1] : StatementHandle $sth
194 # Example : none
195 # Description: PROTECTED implementation of abstract superclass method.
196 # responsible for the creation of UnmappedObjects
197 # Returntype : listref of Bio::EnsEMBL::UnmappedObjects
198 # Exceptions : none
199 # Caller : internal
200 # Status : At Risk
201  
202 sub _objs_from_sth {
203 my ($self, $sth) = @_;
204  
205 my($unmapped_object_id, $type, $analysis_id, $external_db_id, $identifier,
206 $unmapped_reason_id, $query_score, $target_score, $ensembl_id,
207 $ensembl_object_type, $summary, $full_desc);
208  
209 $sth->bind_columns(\$unmapped_object_id,\$type, \$analysis_id,
210 \$external_db_id, \$identifier, \$unmapped_reason_id,
211 \$query_score, \$target_score, \$ensembl_id,
212 \$ensembl_object_type, \$summary, \$full_desc);
213  
214 my $analysis_adaptor = $self->db->get_AnalysisAdaptor();
215  
216 my @features;
217 while($sth->fetch()) {
218 my $analysis = $analysis_adaptor->fetch_by_dbID($analysis_id);
219
220 #print "$identifier\n";
221  
222 push( @features,
223 $self->_create_feature(
224 'Bio::EnsEMBL::UnmappedObject', {
225 -unmapped_object_id => $unmapped_object_id,
226 -unmapped_reason_id => $unmapped_reason_id,
227 -type => $type,
228 -analysis => $analysis,
229 -external_db_id => $external_db_id,
230 -identifier => $identifier,
231 -query_score => $query_score,
232 -target_score => $target_score,
233 -ensembl_id => $ensembl_id,
234 -ensembl_object_type => $ensembl_object_type,
235 -summary => $summary,
236 -full_desc => $full_desc,
237 -adaptor => $self
238 } ) );
239  
240 }
241 return \@features;
242 }
243  
244  
245  
246 =head2 store
247  
248 Arg [1] : list of Bio::EnsEMBL::UnmappedObjects @uo
249 the unmapped objects to store in the database
250 Example : $ou_adaptor->store(@uo);
251 Description: Stores a list of unmapped objects in the database
252 Returntype : none
253 Exceptions : thrown if no Analysis, or no list of objects to store.
254 Caller : general
255 Status : Stable
256  
257 =cut
258  
259 sub store{
260 my ($self,@uos) = @_;
261  
262 if( scalar(@uos) == 0 ) {
263 throw("Must call store with list of UnmappedObjects");
264 }
265  
266  
267 my $db = $self->db();
268 my $analysis_adaptor = $db->get_AnalysisAdaptor();
269  
270 my $sth_reason = $self->prepare
271 ("INSERT INTO unmapped_reason (summary_description, full_description)".
272 " VALUES (?,?)");
273  
274 my $sth_unmapped_object = $self->prepare
275 ("INSERT INTO unmapped_object (type, analysis_id, external_db_id,
276 identifier, unmapped_reason_id, query_score, target_score,
277 ensembl_id, ensembl_object_type)".
278 " VALUES (?,?,?,?,?,?,?,?,?)");
279  
280 my $sth_fetch_reason =
281 $self->prepare(
282 "SELECT
283 unmapped_reason_id
284 FROM
285 unmapped_reason
286 WHERE
287 full_description = ?
288 " );
289  
290 FEATURE: foreach my $uo ( @uos ) {
291  
292 if( !ref $uo || !$uo->isa("Bio::EnsEMBL::UnmappedObject") ) {
293 throw("UnmappedObject must be an Ensembl UnmappedObject, " .
294 "not a [".ref($uo)."]");
295 }
296 if($uo->is_stored($db)){
297 next;
298 }
299
300 my $analysis = $uo->analysis();
301 throw("UnmappedObject must have an analysis object.".$uo->analysis."\n") if(!defined($analysis));
302  
303 my $analysis_id;
304 if($analysis->is_stored($db)) {
305 $analysis_id = $analysis->dbID();
306 }
307 else {
308 $analysis_id = $db->get_AnalysisAdaptor->store($analysis);
309 }
310
311  
312  
313 # Check if unmapped reason is cached
314 # Check if it has been added since the cache was created
315 # Try to store it
316  
317 if(!defined($desc_to_id{$uo->{'description'}})){
318 $sth_reason->bind_param(1,$uo->{'summary'},SQL_VARCHAR);
319 $sth_reason->bind_param(2,$uo->{'description'},SQL_VARCHAR);
320
321 if(! eval{ $sth_reason->execute(); 1 }){
322 # DBI Trace possible here?
323 warning($@); #
324 my $msg;
325 $msg .= "INSERT INTO unmapped_reason (summary_description, full_description) VALUES (";
326 $msg .= $uo->{'summary'} .','. $uo->{'description'}. ')';
327 # Temporary fix for naughty cross-dependency regulation code.
328 use Data::Dumper;
329 warning("Query: \n$msg");
330 print STDERR "UnmappedObject: \n";
331 print STDERR Dumper $uo;
332  
333 $sth_fetch_reason->execute($uo->{'description'});
334
335 my $unmapped_reasons = $sth_fetch_reason->fetchrow_arrayref();
336 if(! defined($unmapped_reasons)){
337 my $msg = $uo->{'description'}. " unable to store. Check MySQL schema, maybe PK not big enough?";
338 throw($msg);
339 }
340 if(scalar @$unmapped_reasons != 1){
341 throw("Multiple results for this description");
342 }
343 $uo->{'unmapped_reason_id'} = $unmapped_reasons->[0];
344  
345 }
346 else{
347 # print 'Last mohican: '.$self->last_insert_id ."\n";
348 $uo->{'unmapped_reason_id'} = $self->last_insert_id;
349 }
350 }
351 else{
352 $uo->{'unmapped_reason_id'} = $desc_to_id{$uo->{'description'}};
353 }
354 $sth_unmapped_object->bind_param(1,$uo->{'type'},SQL_VARCHAR);
355 $sth_unmapped_object->bind_param(2,$uo->analysis->dbID,SQL_INTEGER);
356 $sth_unmapped_object->bind_param(3,$uo->{'external_db_id'},SQL_INTEGER);
357 $sth_unmapped_object->bind_param(4,$uo->{'identifier'},SQL_VARCHAR);
358 $sth_unmapped_object->bind_param(5,$uo->{'unmapped_reason_id'},SQL_VARCHAR);
359 $sth_unmapped_object->bind_param(6,$uo->{'query_score'},SQL_DOUBLE);
360 $sth_unmapped_object->bind_param(7,$uo->{'target_score'},SQL_DOUBLE);
361 $sth_unmapped_object->bind_param(8,$uo->{'ensembl_id'},SQL_INTEGER);
362 $sth_unmapped_object->bind_param(9,$uo->{'ensembl_object_type'},SQL_VARCHAR);
363 $sth_unmapped_object->execute();
364 $uo->dbID($self->last_insert_id('unmapped_object_id', undef, 'unmapped_object'));
365 }
366 $sth_reason->finish();
367 return;
368 }
369  
370  
371 =head2 fetch_all_by_type
372  
373 Arg [1] : string type. The type of unmapped objects
374 Example : @unmapped_object = @{$uoa->fetch_all_by_type('xref')};
375 Description : Retrieves all the unmapped object for a particular
376 type. e.g. 'xref','cDNA', 'marker'
377 Returntype : Array ref of Bio::EnsEMBL::UnmappedObject
378 Exceptions : none
379 Caller : general
380 Status : At Risk
381  
382 =cut
383  
384 sub fetch_all_by_type {
385 my ($self, $type) = @_;
386
387 unless($type) {
388 throw("type argument is required");
389 }
390 $self->bind_param_generic_fetch($type,SQL_VARCHAR);
391 $self->generic_fetch("uo.type = ?");
392
393 }
394  
395 =head2 fetch_all_by_analysis
396  
397 Arg [1] : Bio:EnsEMBL::Analysis object
398 Arg [2] : (optional) string database name
399 Example : @unmapped_object = @{$uoa->fetch_all_by_analysis($analysis)};
400 Description : Retrieves all the unmapped object for a particular
401 analysis type with the the option of a particular
402 database type.
403 Returntype : array ref of Bio::EnsEMBL::UnmappedObject
404 Exceptions : thorws if first argument is not an anaylisi object
405 Caller : general
406 Status : At Risk
407  
408 =cut
409  
410 sub fetch_all_by_analysis {
411 my ($self, $analysis,$dbname) = @_;
412
413 unless($analysis) {
414 throw("analysis argument is required");
415 }
416 $self->bind_param_generic_fetch($analysis->dbID,SQL_INTEGER);
417 my $constraint = "uo.analysis_id = ?";
418 if(defined($dbname)){
419 my $db_id =0;
420 my $sth = $self->prepare('select external_db_id from external_db where db_name like "'.
421 $dbname.'"');
422 $sth->execute;
423 $sth->bind_columns(\$db_id);
424 $sth->fetch();
425 if(!defined($db_id) or $db_id == 0){
426 throw("$dbname could not be found in the external database table\n");
427 }
428 $self->bind_param_generic_fetch($db_id,SQL_INTEGER);
429 $constraint .= " AND uo.external_db_id = ?";
430 }
431 $self->generic_fetch($constraint);
432
433 }
434  
435 =head2 fetch_by_identifier
436  
437 Arg [1] : string type. The type of unmapped objects
438 Arg [2] : (optional) string database name
439 Example : @unmapped_object = @{$uoa->fetch_by_identifier('Q123345')};
440 Description : Retrieves the unmapped object for a particular
441 identifier/accession
442 Returntype : array ref of Bio::EnsEMBL::UnmappedObject
443 Exceptions : none
444 Caller : general
445 Status : At Risk
446  
447 =cut
448  
449 sub fetch_by_identifier {
450 my ($self, $identifier, $dbname) = @_;
451
452 unless($identifier) {
453 throw("identifier argument is required");
454 }
455 $self->bind_param_generic_fetch($identifier,SQL_VARCHAR);
456 my $constraint = 'uo.identifier like ?';
457  
458 if(defined($dbname)){
459 my $db_id =0;
460 my $sth = $self->prepare('select external_db_id from external_db where db_name like "'.
461 $dbname.'"');
462 $sth->execute;
463 $sth->bind_columns(\$db_id);
464 $sth->fetch();
465 if(!defined($db_id) or $db_id == 0){
466 throw("$dbname could not be found in the external database table\n");
467 }
468 $self->bind_param_generic_fetch($db_id,SQL_INTEGER);
469 $constraint .= " AND uo.external_db_id = ?";
470 }
471 return $self->generic_fetch($constraint);
472 }
473  
474 =head2 fetch_all_by_object_type_id
475  
476 Arg [1] : string - The object type of the ensembl object e.g. Gene
477 Arg [2] : int - The internal dbID of the ensembl object
478 Example : my @unmapped_objects = @{$uoa->fetch_all_by_object_type_id('Gene', 12341)};
479 Description : Retrieves the unmapped objects for a particular ensembl object
480 This is a base method which should be called by wrapper methods
481 defining the correct object type e.g. $uoa->fetch_all_by_Gene($gene)
482 Returntype : array ref of Bio::EnsEMBL::UnmappedObject objects
483 Exceptions : Throws if arguments are not defined
484 Caller : general
485 Status : At Risk
486  
487 =cut
488  
489 sub fetch_all_by_object_type_id {
490 my ($self, $object_type, $dbid) = @_;
491
492 if(! ($object_type && $dbid)){
493 throw("object_type and dbid arguments required");
494 }
495
496 $self->bind_param_generic_fetch($object_type, SQL_VARCHAR);
497 $self->bind_param_generic_fetch($dbid, SQL_INTEGER);
498  
499 my $constraint = 'uo.ensembl_object_type=? and uo.ensembl_id=?';
500  
501 return $self->generic_fetch($constraint);
502 }
503  
504  
505  
506 1;