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