Archive Ensembl HomeArchive Ensembl Home
TranscriptSupportingFeatureAdaptor.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::TranscriptSupportingFeatureAdaptor - Retrieves
00024 supporting features from the database.
00025 
00026 =head1 SYNOPSIS
00027 
00028   $supporting_feature_adaptor =
00029     $database_adaptor->get_TranscriptSupportingFeatureAdaptor;
00030 
00031   @supporting_feats =
00032     @{ $supporting_feat_adaptor->fetch_all_by_Transcript($transcript) };
00033 
00034 =head1 METHODS
00035 
00036 =cut
00037 
00038 package Bio::EnsEMBL::DBSQL::TranscriptSupportingFeatureAdaptor;
00039 
00040 use strict;
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_Transcript
00053 
00054   Arg [1]    : Bio::EnsEMBL::Transcript $transcript 
00055                The transcript to fetch supporting features for
00056   Example    : @sfs = @{$supporting_feat_adaptor->fetch_all_by_Transcript($transcript)};
00057   Description: Retrieves supporting features (evidence) for a given transcript. 
00058   Returntype : list of Bio::EnsEMBL::BaseAlignFeatures in the same coordinate
00059                system as the $transcript argument
00060   Exceptions : warning if $transcript is not in the database (i.e. dbID not defined)
00061                throw if a retrieved supporting feature is of unknown type 
00062   Caller     : Bio::EnsEMBL::Transcript
00063   Status     : Stable
00064 
00065 =cut
00066 
00067 sub fetch_all_by_Transcript {
00068   my ( $self, $transcript )  = @_;
00069 
00070   my $out = [];
00071 
00072   unless($transcript->dbID) {
00073     warning("Cannot retrieve evidence for transcript without dbID");
00074     return [];
00075   }
00076 
00077   my $sth = $self->prepare("SELECT tsf.feature_type, tsf.feature_id
00078                             FROM   transcript_supporting_feature tsf
00079                             WHERE  transcript_id = ?");
00080 
00081 
00082   $sth->bind_param(1,$transcript->dbID,SQL_INTEGER);
00083   $sth->execute();
00084 
00085   my $prot_adp = $self->db->get_ProteinAlignFeatureAdaptor;
00086   my $dna_adp  = $self->db->get_DnaAlignFeatureAdaptor;
00087 
00088   my $feature;
00089   while(my ($type, $feature_id) = $sth->fetchrow){
00090     if($type eq 'protein_align_feature'){
00091       $feature = $prot_adp->fetch_by_dbID($feature_id);
00092     } elsif($type eq 'dna_align_feature'){
00093       $feature = $dna_adp->fetch_by_dbID($feature_id);
00094     } else {
00095       warning("Unknown feature type [$type]\n");
00096     }
00097 
00098     if(!$feature) {
00099       warning("Supporting feature $type $feature_id does not exist in DB");
00100     } else {
00101       my $new_feature = $feature->transfer($transcript->slice());
00102       push @$out, $new_feature if( $new_feature );
00103     }
00104   }
00105 
00106   $sth->finish();
00107 
00108   return $out;
00109 }
00110 
00111 
00112 
00113 =head2 store
00114   Arg [2]    : Int $transID
00115                The dbID of an EnsEMBL transcript to associate with supporting
00116                features
00117   Arg [1]    : Ref to array of Bio::EnsEMBL::BaseAlignFeature (the support)
00118   Example    : $dbea->store($transcript_id, \@features);
00119   Description: Stores a set of alignment features and associates an EnsEMBL transcript
00120                with them
00121   Returntype : none
00122   Exceptions : thrown when invalid dbID is passed to this method
00123   Caller     : TranscriptAdaptor
00124   Status     : Stable
00125 
00126 =cut
00127 
00128 sub store {
00129   my ( $self, $tran_dbID, $aln_objs ) = @_;
00130 
00131   my $pep_check_sql = 
00132       "SELECT protein_align_feature_id " . 
00133       "FROM protein_align_feature " . 
00134       "WHERE seq_region_id = ? " . 
00135       "AND   seq_region_start = ? " . 
00136       "AND   seq_region_end   = ? " .
00137       "AND   seq_region_strand = ? " . 
00138       "AND   hit_name = ? " . 
00139       "AND   hit_start = ? " . 
00140       "AND   hit_end   = ? " . 
00141       "AND   analysis_id = ? " . 
00142       "AND   cigar_line = ? " .
00143       "AND   hcoverage = ? ";
00144 
00145   my $dna_check_sql = 
00146       "SELECT dna_align_feature_id " . 
00147       "FROM  dna_align_feature " . 
00148       "WHERE seq_region_id = ? " . 
00149       "AND   seq_region_start = ? " . 
00150       "AND   seq_region_end   = ? " .
00151       "AND   seq_region_strand = ? " . 
00152       "AND   hit_name = ? " . 
00153       "AND   hit_start = ? " . 
00154       "AND   hit_end   = ? " . 
00155       "AND   analysis_id = ? " . 
00156       "AND   cigar_line = ? " .
00157       "AND   hcoverage = ? " . 
00158       "AND   hit_strand = ? ";
00159 
00160   my $assoc_check_sql = 
00161       "SELECT * " .  
00162       "FROM  transcript_supporting_feature " . 
00163       "WHERE transcript_id = $tran_dbID " . 
00164       "AND   feature_type = ? " . 
00165       "AND   feature_id   = ? ";
00166 
00167   my $assoc_write_sql = "INSERT into transcript_supporting_feature " . 
00168       "(transcript_id, feature_id, feature_type) " . 
00169       "values(?, ?, ?)";
00170 
00171   my $pep_check_sth = $self->prepare($pep_check_sql);
00172   my $dna_check_sth = $self->prepare($dna_check_sql);
00173   my $assoc_check_sth = $self->prepare($assoc_check_sql);
00174   my $sf_sth = $self->prepare($assoc_write_sql);
00175 
00176   my $dna_adaptor = $self->db->get_DnaAlignFeatureAdaptor();
00177   my $pep_adaptor = $self->db->get_ProteinAlignFeatureAdaptor();
00178 
00179   foreach my $f (@$aln_objs) {
00180     # check that the feature is in toplevel coords
00181 
00182     if($f->slice->start != 1 || $f->slice->strand != 1) {
00183     #move feature onto a slice of the entire seq_region
00184       my $tls = $self->db->get_sliceAdaptor->fetch_by_region($f->slice->coord_system->name(),
00185                                                              $f->slice->seq_region_name(),
00186                                                              undef, #start
00187                                                              undef, #end
00188                                                              undef, #strand
00189                                                              $f->slice->coord_system->version());
00190       $f = $f->transfer($tls);
00191 
00192       if(!$f) {
00193         throw('Could not transfer Feature to slice of ' .
00194               'entire seq_region prior to storing');
00195       }
00196     }
00197 
00198     if(!$f->isa("Bio::EnsEMBL::BaseAlignFeature")){
00199       throw("$f must be an align feature otherwise" .
00200             "it can't be stored");
00201     }
00202        
00203     my ($sf_dbID, $type, $adap, $check_sth);
00204     
00205     my @check_args = ($self->db->get_SliceAdaptor->get_seq_region_id($f->slice),
00206                       $f->start,
00207                       $f->end,
00208                       $f->strand,
00209                       $f->hseqname,
00210                       $f->hstart,
00211                       $f->hend,
00212                       $f->analysis->dbID,
00213                       $f->cigar_string,
00214               $f->hcoverage);
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     
00233     if (not $sf_dbID) {
00234  
00235       $adap->store($f);
00236       $sf_dbID = $f->dbID;
00237     }
00238 
00239     # now check association
00240     $assoc_check_sth->execute($type,
00241                               $sf_dbID);
00242     if (not $assoc_check_sth->fetchrow_array) {    
00243       $sf_sth->bind_param(1, $tran_dbID, SQL_INTEGER);
00244       $sf_sth->bind_param(2, $sf_dbID, SQL_INTEGER);
00245       $sf_sth->bind_param(3, $type, SQL_VARCHAR);
00246       $sf_sth->execute();
00247     }
00248   }
00249 
00250   $dna_check_sth->finish;
00251   $pep_check_sth->finish;
00252   $assoc_check_sth->finish;
00253   $sf_sth->finish;
00254   
00255 }
00256 
00257 
00258 1;
00259