ensembl-hive  2.8.1
SupportingFeatureAdaptor.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 features from the database.
35 
36 =head1 SYNOPSIS
37 
38  my $sfa =
39  $registry->get_adaptor( 'Human', 'Core', 'SupportingFeature' );
40 
41  my @supporting_feats = @{ $sfa->fetch_all_by_Exon($exon) };
42 
43 =head1 METHODS
44 
45 =cut
46 
47 package Bio::EnsEMBL::DBSQL::SupportingFeatureAdaptor;
48 
49 use strict;
50 
52 
53 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
54 
55 use vars qw(@ISA);
56 
57 #inherits from BaseAdaptor
59 
60 
61 
62 =head2 fetch_all_by_Exon
63 
64  Arg [1] : Bio::EnsEMBL::Exon $exon
65  The exon to fetch supporting features.
66  Example : @sfs =
67  @{ $supporting_feat_adaptor->fetch_all_by_Exon($exon) };
68  Description: Retrieves supporting features (evidence) for a given
69  exon.
70  Returntype : List of Bio::EnsEMBL::BaseAlignFeatures in the same
71  coordinate system as the $exon argument
72  Exceptions : Warning if $exon is not in the database (i.e. dbID
73  not defined).
74  Throw if a retrieved supporting feature is of unknown
75  type.
76  Caller : Bio::EnsEMBL::Exon
77  Status : Stable
78 
79 =cut
80 
81 sub fetch_all_by_Exon {
82  my ( $self, $exon ) = @_;
83 
84  my $out = [];
85 
86  unless($exon->dbID) {
87  warning("Cannot retrieve evidence for exon without dbID");
88  return [];
89  }
90 
91  my $sth = $self->prepare("SELECT sf.feature_type, sf.feature_id
92  FROM supporting_feature sf
93  WHERE exon_id = ?");
94 
95  $sth->bind_param(1,$exon->dbID,SQL_INTEGER);
96  $sth->execute();
97 
98  my $prot_adp = $self->db->get_ProteinAlignFeatureAdaptor;
99  my $dna_adp = $self->db->get_DnaAlignFeatureAdaptor;
100 
101  my $feature;
102  while(my ($type, $feature_id) = $sth->fetchrow){
103  if($type eq 'protein_align_feature'){
104  $feature = $prot_adp->fetch_by_dbID($feature_id);
105  } elsif($type eq 'dna_align_feature'){
106  $feature = $dna_adp->fetch_by_dbID($feature_id);
107  } else {
108  warning("Unknown feature type [$type]\n");
109  }
110 
111  if(!$feature) {
112  warning("Supporting feature $type $feature_id does not exist in DB");
113  } else {
114  my $new_feature = $feature->transfer($exon->slice());
115  push @$out, $new_feature if( $new_feature );
116  }
117  }
118 
119  $sth->finish();
120 
121  return $out;
122 }
123 
124 =head2 store
125 
126  Arg [1] : Int $exonsID
127  The dbID of an EnsEMBL exon to associate with
128  supporting features.
129  Arg [2] : Ref to array of Bio::EnsEMBL::BaseAlignFeature
130  (the support)
131  Example : $sfa->store($exon_id, \@features);
132  Description: Stores a set of alignment features and associates an
133  EnsEMBL exon with them
134  Returntype : none
135  Exceptions : thrown when invalid dbID is passed to this method
136  Caller : TranscriptAdaptor
137  Status : Stable
138 
139 =cut
140 
141 sub store {
142  my ( $self, $exon_dbID, $aln_objs ) = @_;
143 
144  my $pep_check_sql =
145  "SELECT protein_align_feature_id " .
146  "FROM protein_align_feature " .
147  "WHERE seq_region_id = ? " .
148  "AND seq_region_start = ? " .
149  "AND seq_region_end = ? " .
150  "AND seq_region_strand = ? " .
151  "AND hit_name = ? " .
152  "AND hit_start = ? " .
153  "AND hit_end = ? " .
154  "AND analysis_id = ? " .
155  "AND cigar_line = ? ";
156 
157  my $dna_check_sql =
158  "SELECT dna_align_feature_id " .
159  "FROM dna_align_feature " .
160  "WHERE seq_region_id = ? " .
161  "AND seq_region_start = ? " .
162  "AND seq_region_end = ? " .
163  "AND seq_region_strand = ? " .
164  "AND hit_name = ? " .
165  "AND hit_start = ? " .
166  "AND hit_end = ? " .
167  "AND analysis_id = ? " .
168  "AND cigar_line = ? " .
169  "AND hit_strand = ? ";
170 
171  my $assoc_check_sql =
172  "SELECT * " .
173  "FROM supporting_feature " .
174  "WHERE exon_id = $exon_dbID " .
175  "AND feature_type = ? " .
176  "AND feature_id = ? ";
177 
178  my $assoc_write_sql = "INSERT into supporting_feature " .
179  "(exon_id, feature_id, feature_type) " .
180  "values(?, ?, ?)";
181 
182  my $pep_check_sth = $self->prepare($pep_check_sql);
183  my $dna_check_sth = $self->prepare($dna_check_sql);
184  my $assoc_check_sth = $self->prepare($assoc_check_sql);
185  my $sf_sth = $self->prepare($assoc_write_sql);
186 
187  my $dna_adaptor = $self->db->get_DnaAlignFeatureAdaptor();
188  my $pep_adaptor = $self->db->get_ProteinAlignFeatureAdaptor();
189 
190  foreach my $f (@$aln_objs) {
191  # check that the feature is in toplevel coords
192 
193  if($f->slice->start != 1 || $f->slice->strand != 1) {
194  #move feature onto a slice of the entire seq_region
195  my $tls = $self->db->get_sliceAdaptor->fetch_by_region($f->slice->coord_system->name(),
196  $f->slice->seq_region_name(),
197  undef, #start
198  undef, #end
199  undef, #strand
200  $f->slice->coord_system->version());
201  $f = $f->transfer($tls);
202 
203  if(!$f) {
204  throw('Could not transfer Feature to slice of ' .
205  'entire seq_region prior to storing');
206  }
207  }
208 
209  if(!$f->isa("Bio::EnsEMBL::BaseAlignFeature")){
210  throw("$f must be an align feature otherwise" .
211  "it can't be stored");
212  }
213 
214  my ($sf_dbID, $type, $adap, $check_sth);
215 
216  my @check_args = ($self->db->get_SliceAdaptor->get_seq_region_id($f->slice),
217  $f->start,
218  $f->end,
219  $f->strand,
220  $f->hseqname,
221  $f->hstart,
222  $f->hend,
223  $f->analysis->dbID,
224  $f->cigar_string);
225 
226  if($f->isa("Bio::EnsEMBL::DnaDnaAlignFeature")){
227  $adap = $dna_adaptor;
228  $check_sth = $dna_check_sth;
229  $type = 'dna_align_feature';
230  push @check_args, $f->hstrand;
231  } elsif($f->isa("Bio::EnsEMBL::DnaPepAlignFeature")){
232  $adap = $pep_adaptor;
233  $check_sth = $pep_check_sth;
234  $type = 'protein_align_feature';
235  } else {
236  warning("Supporting feature of unknown type. Skipping : [$f]\n");
237  next;
238  }
239 
240  $check_sth->execute(@check_args);
241  $sf_dbID = $check_sth->fetchrow_array;
242  if (not $sf_dbID) {
243  $adap->store($f);
244  $sf_dbID = $f->dbID;
245  }
246 
247  # now check association
248  $assoc_check_sth->execute($type,
249  $sf_dbID);
250  if (not $assoc_check_sth->fetchrow_array) {
251  $sf_sth->bind_param(1, $exon_dbID, SQL_INTEGER);
252  $sf_sth->bind_param(2, $sf_dbID, SQL_INTEGER);
253  $sf_sth->bind_param(3, $type, SQL_VARCHAR);
254  $sf_sth->execute();
255  }
256  }
257 
258  $dna_check_sth->finish;
259  $pep_check_sth->finish;
260  $assoc_check_sth->finish;
261  $sf_sth->finish;
262 
263 }
264 
265 
266 
267 1;
268 
EnsEMBL
Definition: Filter.pm:1
Bio::EnsEMBL::BaseAlignFeature
Definition: BaseAlignFeature.pm:90
Bio::EnsEMBL::DBSQL::SupportingFeatureAdaptor
Definition: SupportingFeatureAdaptor.pm:15
exon
public exon()
Bio::EnsEMBL::Exon
Definition: Exon.pm:42
Bio::EnsEMBL::DBSQL::BaseAdaptor
Definition: BaseAdaptor.pm:71
Bio::EnsEMBL::DBSQL::SupportingFeatureAdaptor::fetch_all_by_Exon
public List fetch_all_by_Exon()
Bio::EnsEMBL::Utils::Exception
Definition: Exception.pm:68