ensembl-hive  2.8.1
KaryotypeBandAdaptor.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  $kary_adaptor = $db_adaptor->get_KaryotypeBandAdaptor();
38 
39  foreach $band ( @{ $kary_adaptor->fetch_all_by_Slice($slice) } ) {
40  # do something with band
41  }
42 
43  $band = $kary_adaptor->fetch_by_dbID($id);
44 
45  my @bands = @{ $kary_adaptor->fetch_all_by_chr_name('X') };
46 
47  my $band = $kary_adaptor->fetch_by_chr_band( '4', 'q23' );
48 
49 =head1 DESCRIPTION
50 
51 Database adaptor to provide access to KaryotypeBand objects
52 
53 =head1 METHODS
54 
55 =cut
56 
57 package Bio::EnsEMBL::DBSQL::KaryotypeBandAdaptor;
58 
59 use strict;
60 
61 use vars qw(@ISA);
62 
64 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
66 
68 
69 =head2 store
70 
71  Arg [1] : list of Bio::EnsEMBL::KaryotypeBand @k
72  the karyotype bands to store in the database
73  Example : $karyotype_adaptor->store(@karyotype_band);
74  Description: Stores a list of karyotype band objects in the database
75  Returntype : none
76  Exceptions : thrown if @k is not defined, if any of the features do not
77  have an attached slice.
78  or if any elements of @k are not Bio::EnsEMBL::KaryotypeBand
79  Caller : general
80  Status : Stable
81 
82 =cut
83 
84 sub store{
85  my ($self,@k) = @_;
86 
87  if( scalar(@k) == 0 ) {
88  throw("Must call store with list of karyotype bands");
89  }
90 
91  my $sth = $self->prepare
92  ("INSERT INTO karyotype (seq_region_id, seq_region_start, " .
93  "seq_region_end, band, " .
94  "stain) " .
95  "VALUES (?,?,?,?,?)");
96 
97  my $db = $self->db();
98  my $slice_adaptor = $db->get_SliceAdaptor();
99 
100  BAND: foreach my $k ( @k ) {
101 
102  if( !ref $k || !$k->isa("Bio::EnsEMBL::KaryotypeBand") ) {
103  throw("KaryotypeBand must be an Ensembl KaryotypeBand, " .
104  "not a [".ref($k)."]");
105  }
106 
107  if($k->is_stored($db)) {
108  warning("KaryotypeBand [".$k->dbID."] is already stored" .
109  " in this database.");
110  next BAND;
111  }
112 
113  my $original = $k;
114  my $seq_region_id;
115  ($k, $seq_region_id) = $self->_pre_store($k);
116 
117  $sth->bind_param(1,$seq_region_id,SQL_INTEGER);
118  $sth->bind_param(2,$k->start,SQL_INTEGER);
119  $sth->bind_param(3,$k->end,SQL_INTEGER);
120  $sth->bind_param(4,$k->name,SQL_VARCHAR);
121  $sth->bind_param(5,$k->stain,SQL_VARCHAR);
122 
123  $sth->execute();
124 
125  $original->dbID($self->last_insert_id('karyotype_id', undef, 'karyotype'));
126  $original->adaptor($self);
127  }
128 }
129 
130 
131 #_tables
132 #
133 # Arg [1] : none
134 # Example : none
135 # Description: PROTECTED Implementation of abstract superclass method to
136 # provide the name of the tables to query
137 # Returntype : string
138 # Exceptions : none
139 # Caller : internal
140 
141 
142 sub _tables {
143  my $self = shift;
144 
145  return (['karyotype','k'])
146 }
147 
148 
149 #_columns
150 
151 # Arg [1] : none
152 # Example : none
153 # Description: PROTECTED Implementation of abstract superclass method to
154 # provide the name of the columns to query
155 # Returntype : list of strings
156 # Exceptions : none
157 # Caller : internal
158 
159 sub _columns {
160  my $self = shift;
161 
162  #warning _objs_from_sth implementation depends on ordering
163  return qw (
164  k.karyotype_id
165  k.seq_region_id
166  k.seq_region_start
167  k.seq_region_end
168  k.band
169  k.stain );
170 }
171 
172 
173 sub _objs_from_sth {
174  my ($self, $sth) = @_;
175  my $db = $self->db();
176  my $slice_adaptor = $db->get_SliceAdaptor();
177 
178  my @features;
179  my %slice_cache;
180 
181  my($karyotype_id,$seq_region_id,$seq_region_start,$seq_region_end,
182  $band,$stain);
183 
184  $sth->bind_columns(\$karyotype_id, \$seq_region_id, \$seq_region_start,
185  \$seq_region_end, \$band, \$stain);
186 
187  while ( $sth->fetch() ) {
188  #need to get the internal_seq_region, if present
189  $seq_region_id = $self->get_seq_region_id_internal($seq_region_id);
190 
191  my $slice = $slice_cache{$seq_region_id} ||=
192  $slice_adaptor->fetch_by_seq_region_id($seq_region_id);
193 
194  push( @features,
195  $self->_create_feature( 'Bio::EnsEMBL::KaryotypeBand', {
196  -START => $seq_region_start,
197  -END => $seq_region_end,
198  -SLICE => $slice,
199  -ADAPTOR => $self,
200  -DBID => $karyotype_id,
201  -NAME => $band,
202  -STAIN => $stain
203  } ) );
204 
205  }
206 
207  return \@features;
208 }
209 
210 
211 
212 =head2 fetch_all_by_chr_name
213 
214  Arg [1] : string $chr_name
215  Name of the chromosome from which to retrieve band objects
216  Example : @bands=@{$karyotype_band_adaptor->fetch_all_by_chr_name('X')};
217  Description: Fetches all the karyotype band objects from the database for the
218  given chromosome.
219  Returntype : listref of Bio::EnsEMBL::KaryotypeBand in chromosomal
220  (assembly) coordinates
221  Exceptions : none
222  Caller : general
223  Status : Stable
224 
225 =cut
226 
227 sub fetch_all_by_chr_name {
228  my ($self,$chr_name) = @_;
229 
230  throw('Chromosome name argument expected') if(!$chr_name);
231 
232  my $slice =
233  $self->db->get_SliceAdaptor->fetch_by_region(undef, $chr_name);
234  unless ($slice){
235  warning("Cannot retrieve chromosome $chr_name");
236  return;
237  }
238  return $self->fetch_all_by_Slice($slice);
239 }
240 
241 
242 
243 sub fetch_all_by_chr_band {
244  my ($self, $chr_name, $band) = @_;
245 
246  throw('Chromosome name argument expected') if(!$chr_name);
247  throw('Band argument expected') if(!$band);
248 
249  my $slice = $self->db->get_SliceAdaptor->fetch_by_region(undef,
250  $chr_name);
251 
252  my $constraint = "k.band like '$band%'";
253  return $self->fetch_all_by_Slice_constraint($slice,$constraint);
254 }
255 
256 
257 =head2 list_dbIDs
258 
259  Arg [1] : none
260  Example : @kary_ids = @{$karyotype_band_adaptor->list_dbIDs()};
261  Description: Gets an array of internal ids for all karyotype bands in the
262  current db
263  Arg[1] : <optional> int. not 0 for the ids to be sorted by the seq_region.
264  Returntype : reference to a list of ints
265  Exceptions : none
266  Caller : ?
267  Status : Stable
268 
269 =cut
270 
271 sub list_dbIDs {
272  my ($self, $ordered) = @_;
273 
274  return $self->_list_dbIDs("karyotype",undef, $ordered);
275 }
276 
277 
278 1;
Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor
Definition: BaseFeatureAdaptor.pm:24
Bio::EnsEMBL::DBSQL::BaseAdaptor::fetch_by_dbID
public Bio::EnsEMBL::Feature fetch_by_dbID()
Bio::EnsEMBL::DBSQL::KaryotypeBandAdaptor
Definition: KaryotypeBandAdaptor.pm:28
Bio::EnsEMBL::KaryotypeBand
Definition: KaryotypeBand.pm:45
Bio::EnsEMBL::Utils::Exception
Definition: Exception.pm:68