Archive Ensembl HomeArchive Ensembl Home
SupportingFeatureAdaptor.pm
Go to the documentation of this file.
00001 =head1 LICENSE
00002 
00003   Copyright (c) 1999-2012 The European Bioinformatics Institute and
00004   Genome Research Limited.  All rights reserved.
00005 
00006   This software is distributed under a modified Apache license.
00007   For license details, please see
00008 
00009     http://www.ensembl.org/info/about/code_licence.html
00010 
00011 =head1 CONTACT
00012 
00013   Please email comments or questions to the public Ensembl
00014   developers list at <dev@ensembl.org>.
00015 
00016   Questions may also be sent to the Ensembl help desk at
00017   <helpdesk@ensembl.org>.
00018 
00019 =cut
00020 
00021 =head1 NAME
00022 
00023 Bio::EnsEMBL::DBSQL::SupportingFeatureAdaptor - Retrieves supporting
00024 features from the database.
00025 
00026 =head1 SYNOPSIS
00027 
00028   my $sfa =
00029     $registry->get_adaptor( 'Human', 'Core', 'SupportingFeature' );
00030 
00031   my @supporting_feats = @{ $sfa->fetch_all_by_Exon($exon) };
00032 
00033 =head1 METHODS
00034 
00035 =cut
00036 
00037 package Bio::EnsEMBL::DBSQL::SupportingFeatureAdaptor;
00038 
00039 use strict;
00040 
00041 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
00042 
00043 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
00044 
00045 use vars qw(@ISA);
00046 
00047 #inherits from BaseAdaptor
00048 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
00049 
00050 
00051 
00052 =head2 fetch_all_by_Exon
00053 
00054   Arg [1]    : Bio::EnsEMBL::Exon $exon 
00055                The exon to fetch supporting features.
00056   Example    : @sfs =
00057                 @{ $supporting_feat_adaptor->fetch_all_by_Exon($exon) };
00058   Description: Retrieves supporting features (evidence) for a given
00059                exon.
00060   Returntype : List of Bio::EnsEMBL::BaseAlignFeatures in the same
00061                coordinate system as the $exon argument
00062   Exceptions : Warning if $exon is not in the database (i.e. dbID
00063                not defined).
00064                Throw if a retrieved supporting feature is of unknown
00065                type.
00066   Caller     : Bio::EnsEMBL::Exon
00067   Status     : Stable
00068 
00069 =cut
00070 
00071 sub fetch_all_by_Exon {
00072   my ( $self, $exon )  = @_;
00073 
00074   my $out = [];
00075 
00076   unless($exon->dbID) {
00077     warning("Cannot retrieve evidence for exon without dbID");
00078     return [];
00079   }
00080 
00081   my $sth = $self->prepare("SELECT sf.feature_type, sf.feature_id
00082                             FROM   supporting_feature sf
00083                             WHERE  exon_id = ?");
00084 
00085   $sth->bind_param(1,$exon->dbID,SQL_INTEGER);
00086   $sth->execute();
00087 
00088   my $prot_adp = $self->db->get_ProteinAlignFeatureAdaptor;
00089   my $dna_adp  = $self->db->get_DnaAlignFeatureAdaptor;
00090 
00091   my $feature;
00092   while(my ($type, $feature_id) = $sth->fetchrow){
00093     if($type eq 'protein_align_feature'){
00094       $feature = $prot_adp->fetch_by_dbID($feature_id);
00095     } elsif($type eq 'dna_align_feature'){
00096       $feature = $dna_adp->fetch_by_dbID($feature_id);
00097     } else {
00098       warning("Unknown feature type [$type]\n");
00099     }
00100 
00101     if(!$feature) {
00102       warning("Supporting feature $type $feature_id does not exist in DB");
00103     } else {
00104       my $new_feature = $feature->transfer($exon->slice());
00105       push @$out, $new_feature if( $new_feature );
00106     }
00107   }
00108 
00109   $sth->finish();
00110 
00111   return $out;
00112 }
00113 
00114 =head2 store
00115 
00116   Arg [1]    : Int $exonsID
00117                The dbID of an EnsEMBL exon to associate with
00118                supporting features.
00119   Arg [2]    : Ref to array of Bio::EnsEMBL::BaseAlignFeature
00120                (the support)
00121   Example    : $sfa->store($exon_id, \@features);
00122   Description: Stores a set of alignment features and associates an
00123                EnsEMBL exon with them
00124   Returntype : none
00125   Exceptions : thrown when invalid dbID is passed to this method
00126   Caller     : TranscriptAdaptor
00127   Status     : Stable
00128 
00129 =cut
00130 
00131 sub store {
00132   my ( $self, $exon_dbID, $aln_objs ) = @_;
00133 
00134   my $pep_check_sql = 
00135       "SELECT protein_align_feature_id " . 
00136       "FROM protein_align_feature " . 
00137       "WHERE seq_region_id = ? " . 
00138       "AND   seq_region_start = ? " . 
00139       "AND   seq_region_end   = ? " .
00140       "AND   seq_region_strand = ? " . 
00141       "AND   hit_name = ? " . 
00142       "AND   hit_start = ? " . 
00143       "AND   hit_end   = ? " . 
00144       "AND   analysis_id = ? " . 
00145       "AND   cigar_line = ? ";
00146 
00147   my $dna_check_sql = 
00148       "SELECT dna_align_feature_id " . 
00149       "FROM dna_align_feature " . 
00150       "WHERE seq_region_id = ? " . 
00151       "AND   seq_region_start = ? " . 
00152       "AND   seq_region_end   = ? " .
00153       "AND   seq_region_strand = ? " . 
00154       "AND   hit_name = ? " . 
00155       "AND   hit_start = ? " . 
00156       "AND   hit_end   = ? " . 
00157       "AND   analysis_id = ? " . 
00158       "AND   cigar_line = ? " . 
00159       "AND   hit_strand = ? ";
00160 
00161   my $assoc_check_sql = 
00162       "SELECT * " .  
00163       "FROM  supporting_feature " . 
00164       "WHERE exon_id = $exon_dbID " . 
00165       "AND   feature_type = ? " . 
00166       "AND   feature_id   = ? ";
00167 
00168   my $assoc_write_sql = "INSERT into supporting_feature " . 
00169       "(exon_id, feature_id, feature_type) " . 
00170       "values(?, ?, ?)";
00171 
00172   my $pep_check_sth = $self->prepare($pep_check_sql);
00173   my $dna_check_sth = $self->prepare($dna_check_sql);
00174   my $assoc_check_sth = $self->prepare($assoc_check_sql);
00175   my $sf_sth = $self->prepare($assoc_write_sql);
00176 
00177   my $dna_adaptor = $self->db->get_DnaAlignFeatureAdaptor();
00178   my $pep_adaptor = $self->db->get_ProteinAlignFeatureAdaptor();
00179 
00180   foreach my $f (@$aln_objs) {
00181     # check that the feature is in toplevel coords
00182 
00183     if($f->slice->start != 1 || $f->slice->strand != 1) {
00184     #move feature onto a slice of the entire seq_region
00185       my $tls = $self->db->get_sliceAdaptor->fetch_by_region($f->slice->coord_system->name(),
00186                                                              $f->slice->seq_region_name(),
00187                                                              undef, #start
00188                                                              undef, #end
00189                                                              undef, #strand
00190                                                              $f->slice->coord_system->version());
00191       $f = $f->transfer($tls);
00192 
00193       if(!$f) {
00194         throw('Could not transfer Feature to slice of ' .
00195               'entire seq_region prior to storing');
00196       }
00197     }
00198 
00199     if(!$f->isa("Bio::EnsEMBL::BaseAlignFeature")){
00200       throw("$f must be an align feature otherwise" .
00201             "it can't be stored");
00202     }
00203     
00204     my ($sf_dbID, $type, $adap, $check_sth);
00205     
00206     my @check_args = ($self->db->get_SliceAdaptor->get_seq_region_id($f->slice),
00207                       $f->start,
00208                       $f->end,
00209                       $f->strand,
00210                       $f->hseqname,
00211                       $f->hstart,
00212                       $f->hend,
00213                       $f->analysis->dbID,
00214                       $f->cigar_string);
00215 
00216     if($f->isa("Bio::EnsEMBL::DnaDnaAlignFeature")){
00217       $adap = $dna_adaptor;      
00218       $check_sth = $dna_check_sth;
00219       $type = 'dna_align_feature';
00220       push @check_args, $f->hstrand;
00221     } elsif($f->isa("Bio::EnsEMBL::DnaPepAlignFeature")){
00222       $adap = $pep_adaptor;
00223       $check_sth = $pep_check_sth;
00224       $type = 'protein_align_feature';
00225     } else {
00226       warning("Supporting feature of unknown type. Skipping : [$f]\n");
00227       next;
00228     }
00229 
00230     $check_sth->execute(@check_args);
00231     $sf_dbID = $check_sth->fetchrow_array;
00232     if (not $sf_dbID) {
00233       $adap->store($f);
00234       $sf_dbID = $f->dbID;
00235     }
00236 
00237     # now check association
00238     $assoc_check_sth->execute($type,
00239                               $sf_dbID);
00240     if (not $assoc_check_sth->fetchrow_array) {    
00241       $sf_sth->bind_param(1, $exon_dbID, SQL_INTEGER);
00242       $sf_sth->bind_param(2, $sf_dbID, SQL_INTEGER);
00243       $sf_sth->bind_param(3, $type, SQL_VARCHAR);
00244       $sf_sth->execute();
00245     }
00246   }
00247 
00248   $dna_check_sth->finish;
00249   $pep_check_sth->finish;
00250   $assoc_check_sth->finish;
00251   $sf_sth->finish;
00252   
00253 }
00254 
00255 
00256 
00257 1;
00258