ensembl-hive  2.8.1
CoordSystem.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 $db = Bio::EnsEMBL::DBSQL::DBAdaptor->new(...);
38 
39  my $csa = $db->get_CoordSystemAdaptor();
40 
41  #
42  # Get all coord systems in the database:
43  #
44  foreach my $cs ( @{ $csa->fetch_all() } ) {
45  my $str = join ':', $cs->name(), $cs->version(), $cs->dbID();
46  print "$str\n";
47  }
48 
49 =head1 DESCRIPTION
50 
51 This is a simple object which contains a few coordinate system attributes:
52 name, internal identifier, version. A coordinate system is uniquely defined
53 by its name and version. A version of a coordinate system applies to all
54 sequences within a coordinate system. This should not be confused with
55 individual sequence versions.
56 
57 Take for example the Human assembly. The version 'NCBI33' applies to
58 to all chromosomes in the NCBI33 assembly (that is the entire 'chromosome'
59 coordinate system). The 'clone' coordinate system in the same database would
60 have no version however. Although the clone sequences have their own sequence
61 versions, there is no version which applies to the entire set of clones.
62 
63 Coordinate system objects are immutable. Their name and version, and other
64 attributes may not be altered after they are created.
65 
66 =head1 METHODS
67 
68 =cut
69 
70 
71 use strict;
72 use warnings;
73 
74 package Bio::EnsEMBL::CoordSystem;
75 
77 
78 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
79 use Bio::EnsEMBL::Utils::Exception qw(throw);
80 
81 use vars qw(@ISA);
82 
83 @ISA = qw(Bio::EnsEMBL::Storable);
84 
85 
86 =head2 new
87 
88  Arg [..] : List of named arguments:
89  -NAME - The name of the coordinate system
90  -VERSION - (optional) The version of the coordinate system.
91  Note that if the version passed in is undefined,
92  it will be set to the empty string in the
93  resulting CoordSystem object.
94  -RANK - The rank of the coordinate system. The highest
95  level coordinate system should have rank 1, the
96  second highest rank 2 and so on. An example of
97  a high level coordinate system is 'chromosome' an
98  example of a lower level coordinate system is
99  'clone'.
100  -TOP_LEVEL - (optional) Sets whether this is a top-level coord
101  system. Default = 0. This should only be set to
102  true if you are creating an artificial toplevel
103  coordsystem by the name of 'toplevel'
104  -SEQUENCE_LEVEL - (optional) Sets whether this is a sequence
105  level coordinate system. Default = 0
106  -DEFAULT - (optional)
107  Whether this is the default version of the
108  coordinate systems of this name. Default = 0
109  -DBID - (optional) The internal identifier of this
110  coordinate system
111  -ADAPTOR - (optional) The adaptor which provides database
112  interaction for this object
113  -ALIAS_TO - (optional) Sets an alias for a coordsystem. If set
114  it should only be set to 'chromosome'
115  Example : $cs = Bio::EnsEMBL::CoordSystem->new(-NAME => 'chromosome',
116  -VERSION => 'NCBI33',
117  -RANK => 1,
118  -DBID => 1,
119  -ADAPTOR => adaptor,
120  -DEFAULT => 1,
121  -SEQUENCE_LEVEL => 0);
122  Description: Creates a new CoordSystem object representing a coordinate
123  system.
124  Returntype : Bio::EnsEMBL::CoordSystem
125  Exceptions : none
126  Caller : general
127  Status : Stable
128 
129 =cut
130 
131 sub new {
132  my $caller = shift;
133  my $class = ref($caller) || $caller;
134 
135  my $self = $class->SUPER::new(@_);
136 
137  my ( $name, $version, $top_level, $sequence_level, $default, $rank, $alias_to ) =
138  rearrange( [ 'NAME', 'VERSION',
139  'TOP_LEVEL', 'SEQUENCE_LEVEL',
140  'DEFAULT', 'RANK',
141  'ALIAS_TO' ],
142  @_ );
143 
144  $top_level = ($top_level) ? 1 : 0;
145  $sequence_level = ($sequence_level) ? 1 : 0;
146  $default = ($default) ? 1 : 0;
147  $rank ||= 0;
148 
149  if ( $top_level == 1 ) {
150  if ( $rank != 0 ) {
151  throw('RANK argument must be 0 if TOP_LEVEL is 1');
152  }
153 
154  if ( defined($name) ) {
155  if ( $name ne 'toplevel' ) {
156  throw('The NAME argument must be "toplevel" if TOP_LEVEL is 1');
157  }
158  } else {
159  $name = 'toplevel';
160  }
161 
162  if ( $sequence_level == 1 ) {
163  throw("SEQUENCE_LEVEL argument must be 0 if TOP_LEVEL is 1");
164  }
165 
166  $default = 0;
167 
168  } else {
169 
170  if ( $rank == 0 ) {
171  throw("RANK argument must be non-zero unless TOP_LEVEL is 1");
172  }
173 
174  if ( !defined($name) ) {
175  throw('The NAME argument is required');
176  } elsif ( $name eq 'toplevel' ) {
177  throw( "Cannot name coord system 'toplevel' "
178  . "unless TOP_LEVEL is 1" );
179  }
180 
181  }
182 
183  if ( $rank !~ /^\d+$/ ) {
184  throw('The RANK argument must be a positive integer');
185  }
186 
187  if ( defined($alias_to) ) {
188  throw("The ALIAS_TO argument can only be defined as 'chromosome'") unless $alias_to eq "chromosome";
189  }
190 
191  $self->{'version'} = $version;
192  $self->{'name'} = $name;
193  $self->{'top_level'} = $top_level;
194  $self->{'sequence_level'} = $sequence_level;
195  $self->{'default'} = $default;
196  $self->{'rank'} = $rank;
197  $self->{'alias_to'} = $alias_to;
198 
199  return $self;
200 } ## end sub new
201 
202 
203 =head2 name
204 
205  Arg [1] : (optional) string $name
206  Example : print $coord_system->name();
207  Description: Getter for the name of this coordinate system
208  Returntype : string
209  Exceptions : none
210  Caller : general
211  Status : Stable
212 
213 =cut
214 
215 sub name {
216  my $self = shift;
217  return $self->{'name'};
218 }
219 
220 
221 
222 =head2 version
223 
224  Arg [1] : none
225  Example : print $coord->version();
226  Description: Getter for the version of this coordinate system. This
227  will return an empty string if no version is defined for this
228  coordinate system.
229  Returntype : string
230  Exceptions : none
231  Caller : general
232  Status : Stable
233 
234 =cut
235 
236 sub version {
237  my $self = shift;
238 
239  return '' unless defined $self->{'version'};
240  return $self->{'version'};
241 }
242 
243 
244 
245 =head2 species
246 
247  Arg [1] : none
248  Example : print $coord->species();
249  Description: Shortcut method to get the species this CoordSystem refers to.
250  Returntype : string
251  Exceptions : none
252  Caller : general
253  Status : Stable
254 
255 =cut
256 
257 sub species {
258  my $self = shift;
259  return $self->adaptor->db->species;
260 }
261 
262 
263 
264 =head2 equals
265 
266  Arg [1] : Bio::EnsEMBL::CoordSystem $cs
267  The coord system to compare to for equality.
268  Example : if($coord_sys->equals($other_coord_sys)) { ... }
269  Description: Compares 2 coordinate systems and returns true if they are
270  equivalent. The definition of equivalent is sharing the same
271  name and version.
272  Returntype : string
273  Exceptions : none
274  Caller : general
275  Status : Stable
276 
277 =cut
278 
279 sub equals {
280  my $self = shift;
281  my $cs = shift;
282 
283  if(!$cs || !ref($cs) || !$cs->isa('Bio::EnsEMBL::CoordSystem')) {
284  if ($cs->isa('Bio::EnsEMBL::ExternalData::DAS::CoordSystem')) {
285  return $cs->equals($self);
286  }
287  throw('Argument must be a CoordSystem');
288  }
289 
290  if($self->version() eq $cs->version() && $self->{'name'} eq $cs->name()) {
291  return 1;
292  }
293 
294  return 0;
295 }
296 
297 
298 
299 
300 =head2 is_top_level
301 
302  Arg [1] : none
303  Example : if($coord_sys->is_top_level()) { ... }
304  Description: Returns true if this is the toplevel pseudo coordinate system.
305  The toplevel coordinate system is not a real coordinate system
306  which is stored in the database, but it is a placeholder that
307  can be used to request transformations or retrievals to/from
308  the highest defined coordinate system in a given region.
309  Returntype : 1 or 0
310  Exceptions : none
311  Caller : general
312  Status : Stable
313 
314 =cut
315 
316 sub is_top_level {
317  my $self = shift;
318  return $self->{'top_level'};
319 }
320 
321 
322 =head2 is_sequence_level
323 
324  Arg [1] : none
325  Example : if($coord_sys->is_sequence_level()) { ... }
326  Description: Returns true if this is a sequence level coordinate system
327  Returntype : 1 or 0
328  Exceptions : none
329  Caller : general
330  Status : Stable
331 
332 =cut
333 
334 sub is_sequence_level {
335  my $self = shift;
336  return $self->{'sequence_level'};
337 }
338 
339 
340 =head2 is_default
341 
342  Arg [1] : none
343  Example : if($coord_sys->is_default()) { ... }
344  Description: Returns true if this coordinate system is the default
345  version of the coordinate system of this name.
346  Returntype : 1 or 0
347  Exceptions : none
348  Caller : general
349  Status : Stable
350 
351 =cut
352 
353 sub is_default {
354  my $self = shift;
355  return $self->{'default'};
356 }
357 
358 
359 
360 
361 =head2 rank
362 
363  Arg [1] : none
364  Example : if($cs1->rank() < $cs2->rank()) {
365  print $cs1->name(), " is a higher level coord system than",
366  $cs2->name(), "\n";
367  }
368  Description: Returns the rank of this coordinate system. A lower number
369  is a higher coordinate system. The highest level coordinate
370  system has a rank of 1 (e.g. 'chromosome'). The toplevel
371  pseudo coordinate system has a rank of 0.
372  Returntype : int
373  Exceptions : none
374  Caller : general
375  Status : Stable
376 
377 =cut
378 
379 sub rank {
380  my $self = shift;
381  return $self->{'rank'};
382 }
383 
384 
385 
386 =head2 alias_to
387 
388  Arg [1] : string
389  Example : $coord->alias_to('chromosome');
390  Description: Getter/Setter for the alias of this coordinate system.
391  Returntype : Bio::EnsEMBL::CoordSystem
392  Exceptions : none
393  Caller : general
394  Status : Stable
395 
396 =cut
397 
398 sub alias_to {
399  my ($self, $alias_to) = @_;
400  if (defined($alias_to)) {
401  throw("The alias can only be set to 'chromosome'") unless $alias_to eq "chromosome";
402  }
403  $self->{'alias_to'} = $alias_to if defined $alias_to;
404  return $self->{'alias_to'};
405 }
406 
407 
408 1;
Bio::EnsEMBL::DBSQL::DBAdaptor
Definition: DBAdaptor.pm:40
Bio::EnsEMBL::CoordSystem::new
public Bio::EnsEMBL::CoordSystem new()
Bio::EnsEMBL::CoordSystem
Definition: CoordSystem.pm:40
Bio::EnsEMBL::Storable
Definition: Storable.pm:23
Bio::EnsEMBL::DBSQL::DBAdaptor::new
public Bio::EnsEMBL::DBSQL::DBAdaptor new()
compare
public compare()
Bio::EnsEMBL::Utils::Argument
Definition: Argument.pm:34
Bio::EnsEMBL::Utils::Exception
Definition: Exception.pm:68