3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
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
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.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
40 -host =>
'ensembldb.ensembl.org',
48 # Get all coord systems in the database:
50 foreach my $cs ( @{ $csa->fetch_all() } ) {
51 print $cs->name,
' ', $cs->version,
"\n";
58 # use the default version of coord_system 'chromosome' (e.g. NCBI33):
59 $cs = $csa->fetch_by_name(
'chromosome');
61 # get an explicit version of coord_system 'chromosome':
62 $cs = $csa->fetch_by_name(
'chromsome',
'NCBI34' );
64 # get all coord_systems of name 'chromosome':
65 foreach $cs ( @{ $csa->fetch_all_by_name(
'chromosome') } ) {
66 print $cs->name,
' ', $cs->version,
"\n";
72 $cs = $csa->fetch_by_rank(2);
75 # Fetching the pseudo coord system 'toplevel'
78 # Get the default top_level coord system:
79 $cs = $csa->fetch_top_level();
81 # can also use an alias in fetch_by_name:
82 $cs = $csa->fetch_by_name(
'toplevel');
84 # can also request toplevel using rank=0
85 $cs = $csa->fetch_by_rank(0);
88 # Fetching by sequence level:
91 # Get the coord system which is used to store sequence:
92 $cs = $csa->fetch_sequence_level();
94 # can also use an alias in fetch_by_name:
95 $cs = $csa->fetch_by_name(
'seqlevel');
100 $cs = $csa->fetch_by_dbID(1);
105 This adaptor allows the querying of information from the coordinate
108 Note that many coordinate systems
do not have a concept of a version
109 for the entire coordinate system (though they may have a per-sequence
110 version). The
'chromosome' coordinate system usually has a version
111 (i.e. the assembly version) but the clonal coordinate system does not
112 (despite having individual sequence versions). In the
case where a
113 coordinate system does not have a version an empty string (
'') is used
126 use
Bio::
EnsEMBL::Utils::Exception qw(throw warning);
136 Arg [1] : See
BaseAdaptor for arguments (none specific to this
138 Example : $cs = $db->get_CoordSystemAdaptor();
#better than new()
139 Description: Creates a
new CoordSystem adaptor and caches the contents
140 of the coord_system table in memory.
149 my ( $proto, @args ) = @_;
151 my $class = ref($proto) || $proto;
152 my $self = $class->SUPER::new(@args);
155 # Cache the entire contents of the coord_system table cross-referenced
159 # keyed on name, list of coord_system value
160 $self->{
'_name_cache'} = {};
162 # keyed on id, coord_system value
163 $self->{
'_dbID_cache'} = {};
166 $self->{
'_rank_cache'} = {};
168 # keyed on id, 1/undef values
169 $self->{
'_is_sequence_level'} = {};
170 $self->{
'_is_default_version'} = {};
172 #cache to store the seq_region_mapping information
173 #from internal->external
174 $self->{
'_internal_seq_region_mapping'} = {};
175 #from external->internal
176 $self->{
'_external_seq_region_mapping'} = {};
179 'SELECT `coord_system_id`, `name`, `rank`, `version`, `attrib` '
180 .
'FROM coord_system '
181 .
'WHERE species_id = ?' );
183 $sth->bind_param( 1, $self->species_id(), SQL_INTEGER );
186 my ( $dbID, $name, $rank, $version, $attrib );
187 $sth->bind_columns( \( $dbID, $name, $rank, $version, $attrib ) );
189 while ( $sth->fetch() ) {
193 if ( defined($attrib) ) {
194 foreach my $attrib ( split(
',', $attrib ) ) {
195 $self->{
"_is_$attrib"}->{$dbID} = 1;
196 if ( $attrib eq
'sequence_level' ) {
198 } elsif ( $attrib eq
'default_version' ) {
208 -VERSION => $version,
210 -SEQUENCE_LEVEL => $seq_lvl,
211 -DEFAULT => $default );
213 $self->{
'_dbID_cache'}->{$dbID} = $cs;
215 $self->{
'_name_cache'}->{ lc($name) } ||= [];
216 $self->{
'_rank_cache'}->{$rank} = $cs;
218 push @{ $self->{
'_name_cache'}->{ lc($name) } }, $cs;
220 } ## end
while ( $sth->fetch() )
223 $self->_cache_mapping_paths();
225 $self->_cache_seq_region_mapping();
230 sub _cache_seq_region_mapping {
232 # This cache will load the information from the seq_region_table, if
233 # any, to allow mapping between internal and external seq_region_id.
238 # For a given core database, will return the schema_build information.
239 my $schema_build = $self->db->_get_schema_build();
241 # Prepare the query to get relation for the current database being
244 SELECT s.internal_seq_region_id,
245 s.external_seq_region_id
246 FROM seq_region_mapping s,
250 WHERE ms.mapping_set_id = s.mapping_set_id
251 AND ms.internal_schema_build = ?
252 AND s.internal_seq_region_id = sr.seq_region_id
253 AND sr.coord_system_id = cs.coord_system_id
254 AND cs.species_id = ?);
256 my $sth = $self->prepare($sql);
258 $sth->bind_param( 1, $schema_build, SQL_VARCHAR );
259 $sth->bind_param( 2, $self->species_id(), SQL_INTEGER );
264 foreach my $row ( @{ $sth->fetchall_arrayref() } ) {
266 $self->{
'_internal_seq_region_mapping'}->{ $row->[0] } = $row->[1];
268 $self->{
'_external_seq_region_mapping'}->{ $row->[1] } = $row->[0];
273 } ## end sub _cache_seq_region_mapping
276 sub _cache_mapping_paths {
277 # Retrieve a list of available mappings from the meta table. This
278 # may eventually be moved a table of its own if this proves too
284 my $mc = $self->db()->get_MetaContainer();
288 my $map_path ( @{ $mc->list_value_by_key(
'assembly.mapping') } )
290 my @cs_strings = split( /[|#]/, $map_path );
292 if ( scalar(@cs_strings) < 2 ) {
293 warning(
"Incorrectly formatted assembly.mapping value in meta "
294 .
"table: $map_path" );
299 foreach my $cs_string (@cs_strings) {
300 my ( $name, $version ) = split( /:/, $cs_string );
302 my $cs = $self->fetch_by_name( $name, $version );
304 if ( !defined($cs) ) {
305 warning(
"Unknown coordinate system specified in meta table "
306 .
" assembly.mapping:\n $name:$version" );
310 push( @coord_systems, $cs );
313 # If the delimiter is a '#' we want a special case, multiple parts
314 # of the same component map to the same assembly part. As this
315 # looks like the "long" mapping, we just make the path a bit longer
318 if ( index( $map_path,
'#' ) != -1 && scalar(@coord_systems) == 2 )
320 splice( @coord_systems, 1, 0, (undef) );
323 my $cs1 = $coord_systems[0];
324 my $cs2 = $coord_systems[$#coord_systems];
326 my $key1 = $cs1->name();
327 $key1 .=
':' . $cs1->version()
if $cs1->version();
328 my $key2 = $cs2->name();
329 $key2 .=
':' . $cs2->version()
if $cs2->version();
331 if ( exists( $mapping_paths{
"$key1|$key2"} ) ) {
332 warning(
"Meta table specifies multiple mapping paths between "
333 .
"coord systems $key1 and $key2.\n"
334 .
"Choosing shorter path arbitrarily." );
336 if ( scalar( @{ $mapping_paths{
"$key1|$key2"} } ) <
337 scalar(@coord_systems) )
343 $mapping_paths{
"$key1|$key2"} = \@coord_systems;
344 } ## end
foreach my $map_path ( @{ $mc...
346 # Create the pseudo coord system
'toplevel' and cache it so that only
347 # one of these is created for each database.
354 $self->{
'_top_level'} = $toplevel;
355 $self->{
'_mapping_paths'} = \%mapping_paths;
358 } ## end sub _cache_mapping_paths
363 Example :
foreach my $cs (@{$csa->fetch_all()}) {
364 print $cs->
name(),
' ', $cs->version(),
"\n";
366 Description: Retrieves every coordinate system defined in the DB.
367 These will be returned in ascending order of rank. I.e.
368 The highest coordinate system with rank=1 would be first in the
370 Returntype : listref of Bio::EnsEMBL::CoordSystems
382 #order the array by rank in ascending order
383 foreach my $rank (sort {$a <=> $b} keys %{$self->{
'_rank_cache'}}) {
384 push @coord_systems, $self->{
'_rank_cache'}->{$rank};
387 return \@coord_systems;
395 Example : my $cs = $coord_sys_adaptor->fetch_by_rank(1);
396 Description: Retrieves a CoordinateSystem via its rank. 0 is a special
397 rank reserved
for the pseudo coordinate system
'toplevel'.
398 undef is returned
if no coordinate system of the specified rank
411 throw(
"Rank argument must be defined.")
if(!defined($rank));
412 throw(
"Rank argument must be a non-negative integer.")
if($rank !~ /^\d+$/);
415 return $self->fetch_top_level();
418 return $self->{
'_rank_cache'}->{$rank};
424 Arg [1] :
string $name
425 The name of the coordinate system to retrieve. Alternatively
426 this may be an alias
for a real coordinate system. Valid
427 aliases are
'toplevel' and
'seqlevel'.
428 Arg [2] :
string $version (optional)
429 The version of the coordinate system to retrieve. If not
430 specified the
default version will be used.
431 Example : $coord_sys = $csa->fetch_by_name(
'clone');
432 $coord_sys = $csa->fetch_by_name(
'chromosome',
'NCBI33');
433 # toplevel is an pseudo coord system representing the highest
434 # coord system in a given region
435 # such as the chromosome coordinate system
436 $coord_sys = $csa->fetch_by_name(
'toplevel');
437 #seqlevel is an alias for the sequence level coordinate system
438 #such as the clone or contig coordinate system
439 $coord_sys = $csa->fetch_by_name(
'seqlevel');
440 Description: Retrieves a coordinate system by its name
442 Exceptions :
throw if no name argument provided
443 warning
if no version provided and
default does not exist
451 my $name = lc(shift); #
case insensitve matching
454 throw(
'Name argument is required.')
if(!$name);
456 $version = lc($version)
if($version);
459 if($name eq
'seqlevel') {
460 return $self->fetch_sequence_level();
461 } elsif($name eq
'toplevel') {
462 return $self->fetch_top_level($version);
465 if(!exists($self->{
'_name_cache'}->{$name})) {
467 warning(
"Did you mean 'toplevel' coord system instead of '$name'?");
468 } elsif($name =~ /seq/) {
469 warning(
"Did you mean 'seqlevel' coord system instead of '$name'?");
474 my @coord_systems = @{$self->{
'_name_cache'}->{$name}};
476 foreach my $cs (@coord_systems) {
478 return $cs
if(lc($cs->version()) eq $version);
479 } elsif($self->{
'_is_default_version'}->{$cs->dbID()}) {
485 #the specific version we were looking for was not found
489 #didn't find a default, just take first one
490 my $cs = shift @coord_systems;
491 my $v = $cs->version();
492 warning(
"No default version for coord_system [$name] exists. " .
493 "Using version [$v] arbitrarily");
499 =head2 fetch_all_by_name
501 Arg [1] :
string $name
502 The name of the coordinate system to retrieve. This can be
503 the name of an actual coordinate system or an alias
for a
504 coordinate system. Valid aliases are
'toplevel' and
'seqlevel'.
505 Example :
foreach my $cs (@{$csa->fetch_all_by_name(
'chromosome')}){
506 print $cs->name(),
' ', $cs->version();
508 Description: Retrieves all coordinate systems of a particular name
510 Exceptions :
throw if no name argument provided
516 sub fetch_all_by_name {
518 my $name = lc(shift); #
case insensitive matching
520 throw(
'Name argument is required')
if(!$name);
522 if($name eq
'seqlevel') {
523 return [$self->fetch_sequence_level()];
524 } elsif($name eq
'toplevel') {
525 return [$self->fetch_top_level()];
528 return $self->{
'_name_cache'}->{$name} || [];
531 =head2 fetch_all_by_version
533 Arg [1] :
string $version
534 The version of the coordinate systems to retrieve.
535 Example :
foreach my $cs (@{$csa->fetch_all_by_version(
'GRCh37')}){
536 print $cs->name(),
' ', $cs->version();
538 Description: Retrieves all coordinate systems of a particular version
540 Exceptions :
throw if no name argument provided
546 sub fetch_all_by_version {
547 my ($self, $version) = @_;
548 throw "Version argument is required" if ! $version;
551 foreach my $rank (sort {$a <=> $b} keys %{$self->{
'_rank_cache'}}) {
552 if ($self->{
'_rank_cache'}->{$rank}->version()) {
553 if ($self->{
'_rank_cache'}->{$rank}->version() eq $version) {
554 push @coord_systems, $self->{
'_rank_cache'}->{$rank};
558 push @coord_systems, $self->{
'_rank_cache'}->{$rank};
562 return \@coord_systems;
568 Example : $cs = $csa->fetch_by_dbID(4);
569 Description: Retrieves a coord_system via its
internal
570 identifier, or undef
if no coordinate system with the provided
573 Exceptions : thrown
if no coord_system exists
for specified dbID
583 throw(
'dbID argument is required')
if(!$dbID);
585 my $cs = $self->{
'_dbID_cache'}->{$dbID};
587 return undef
if(!$cs);
594 =head2 fetch_top_level
597 Example : $cs = $csa->fetch_top_level();
598 Description: Retrieves the toplevel pseudo coordinate system.
606 sub fetch_top_level {
609 return $self->{
'_top_level'};
613 =head2 fetch_sequence_level
616 Example : ($id, $name, $version) = $csa->fetch_sequence_level();
617 Description: Retrieves the coordinate system at which sequence
620 Exceptions :
throw if no sequence_level coord system exists at all
621 throw if multiple sequence_level coord systems exists
627 sub fetch_sequence_level {
630 my @dbIDs = keys %{$self->{
'_is_sequence_level'}};
632 throw(
'No sequence_level coord_system is defined')
if(!@dbIDs);
635 throw(
'Multiple sequence_level coord_systems are defined.' .
636 'Only one is currently supported');
639 return $self->{
'_dbID_cache'}->{$dbIDs[0]};
641 =head2 get_default_version
644 Example : $version = $csa->get_default_version();
645 Description: Retrieves the
default version of the assembly
647 Exceptions :
throw if no
default version is defined
653 sub get_default_version {
657 foreach my $dbID (keys %{$self->{
'_is_default_version'}}) {
658 if ($self->{
'_dbID_cache'}->{$dbID}->version) {
659 $version = $self->{
'_dbID_cache'}->{$dbID}->
version;
669 =head2 get_all_versions
672 Example : @versions = $csa->get_all_versions();
673 Description: Retrieves all the available versions of assemblies
674 Returntype : Listref of versions (strings)
675 Exceptions :
throw if no version is defined
681 sub get_all_versions {
687 foreach my $dbID (sort {$a <=> $b} keys %{$self->{
'_rank_cache'}}) {
688 if ($self->{
'_rank_cache'}->{$dbID}->version) {
689 $version = $self->{
'_rank_cache'}->{$dbID}->version;
690 if (!$hash_versions{$version}) {
691 $hash_versions{$version} = 1;
692 push @versions, $version;
697 throw(
'No versions found')
if(!scalar(@versions));
703 =head2 get_mapping_path
707 Example :
foreach my $cs @{$csa->get_mapping_path($cs1,$cs2);
708 Description: Given two coordinate systems
this will
return a mapping path
709 between them
if one has been defined. Allowed Mapping paths are
710 explicitly defined in the meta table. The following is an
713 mysql> select * from meta where meta_key =
'assembly.mapping';
714 +---------+------------------+--------------------------------------+
715 | meta_id | meta_key | meta_value |
716 +---------+------------------+--------------------------------------+
717 | 20 | assembly.mapping | chromosome:NCBI34|contig |
718 | 21 | assembly.mapping | clone|contig |
719 | 22 | assembly.mapping | supercontig|contig |
720 | 23 | assembly.mapping | chromosome:NCBI34|contig|clone |
721 | 24 | assembly.mapping | chromosome:NCBI34|contig|supercontig |
722 | 25 | assembly.mapping | supercontig|contig|clone |
723 +---------+------------------+--------------------------------------+
725 For a one-step mapping path to be valid there needs to be
726 a relationship between the two coordinate systems defined in
727 the assembly table. Two step mapping paths work by building
728 on the one-step mapping paths which are already defined.
730 The first coordinate system in a one step mapping path must
731 be the assembled coordinate system and the second must be
735 my $cs1 = $cs_adaptor->fetch_by_name(
'contig');
736 my $cs2 = $cs_adaptor->fetch_by_name(
'chromosome');
738 my @path = @{$cs_adaptor->get_mapping_path($cs1,$cs2)};
741 print
"No mapping path.";
744 print
"2 step mapping path.";
745 print
"Assembled = " . $path[0]->
name() .
"\n";
746 print
"Component = " . $path[1]->name() .
"\n";
748 print
"Multi step mapping path\n";
759 sub get_mapping_path {
764 if(!ref($cs1) || !ref($cs2) ||
765 !$cs1->isa(
'Bio::EnsEMBL::CoordSystem') ||
766 !$cs2->isa(
'Bio::EnsEMBL::CoordSystem')) {
767 throw(
'Two Bio::EnsEMBL::CoordSystem arguments expected.');
770 my $key1 = $cs1->
name();
771 $key1 .=
':' . $cs1->version()
if ($cs1->version());
772 my $key2 = $cs2->name();
773 $key2 .=
':' . $cs2->version()
if ($cs2->version());
775 my $path = $self->{
'_mapping_paths'}->{
"$key1|$key2"};
777 return $path
if($path);
779 $path = $self->{
'_mapping_paths'}->{
"$key2|$key1"};
782 # No path was explicitly defined, but we might be able to guess a
783 # suitable path. We only guess for missing 2 step paths.
788 foreach my $path (values(%{$self->{
'_mapping_paths'}})) {
789 next
if(@$path != 2);
793 if($path->[0]->equals($cs1)) {
795 } elsif($path->[1]->equals($cs1)) {
799 if(defined($match)) {
800 my $mid = $path->[$match];
801 my $midkey = $mid->name() .
':' . $mid->version();
803 # is the same cs mapped to by other cs?
805 my $path = [$cs1,$mid,$cs2];
806 $self->{
'_mapping_paths'}->{
"$key1|$key2"} = $path;
810 warning(
"Using implicit mapping path between '$key1' and '$key2' " .
812 "An explicit 'assembly.mapping' entry should be added " .
813 "to the meta table.\nExample: " .
814 "'$key1|$midkey|$key2'\n");
817 $mid1{$midkey} = $mid;
823 if($path->[0]->equals($cs2)) {
825 } elsif($path->[1]->equals($cs2)) {
830 if(defined($match)) {
831 my $mid = $path->[$match];
832 my $midkey = $mid->name() .
':' . $mid->version();
834 # is the same cs mapped to by other cs?
836 my $path = [$cs2,$mid,$cs1];
837 $self->{
'_mapping_paths'}->{
"$key2|$key1"} = $path;
842 warning(
"Using implicit mapping path between '$key1' and '$key2' " .
844 "An explicit 'assembly.mapping' entry should be added " .
845 "to the meta table.\nExample: " .
846 "'$key1|$midkey|$key2'\n");
850 $mid2{$midkey} = $mid;
859 =head2 store_mapping_path
864 Example : my $pathref = $csa->store_mapping_path($cs1,$cs2);
865 Description: Given two or more coordinate systems
this will store
866 mapping paths between them in the database.
868 For example,
if $cs1 represents chrs of version
869 V1, $cs2 represents contigs, and $cs3 clones then, unless
870 they already exist, the following entries will be created
872 +------------------+---------------------+
873 | meta_key | meta_value |
874 +------------------+---------------------+
875 | assembly.mapping | chr:V1|clone |
876 | assembly.mapping | clone|contig |
877 | assembly.mapping | chr:V1|clone|contig |
878 +------------------+---------------------+
881 For a one-step mapping path to be valid there needs to be
882 a relationship between the two coordinate systems defined in
883 the assembly table. Two step mapping paths work by building
884 on the one-step mapping paths which are already defined.
886 The first coordinate system in a one step mapping path must
887 be the assembled coordinate system and the second must be
890 Returntype : reference to a list of lists of
new meta_value mapping strings
891 created
for assembly.mapping
892 Exceptions : CoordSystems with no rank/duplicated rank
894 Status : Experimental
898 sub store_mapping_path{
902 # Validate and sort the args
904 @csystems >= 2 or
throw(
'Need two or more CoordSystems');
906 ref($_[0]) && $_[0]->isa(
'Bio::EnsEMBL::CoordSystem') or
907 throw(
'CoordSystem argument expected.');
908 my $rank = $_[0]->
rank ||
909 throw(
'CoordSystem has no rank: '.$_[0]->name);
910 $seen_ranks{$rank} &&
911 throw(
'CoordSystem '.$_[0]->name.
" shares rank $rank with ".
912 $seen_ranks{$rank}->name);
913 $seen_ranks{$rank} = $_[0];
915 @csystems =
map{&{$validate}($_)} @csystems;
917 foreach my $cs (@csystems) {
919 if ($cs->version()) {
920 $key .=
":" . $cs->version();
925 # For each pair in the sorted list, store in the DB
926 my $meta = $self->db->get_MetaContainer;
928 for( my $i=1; $i<@keys; $i++ ){
929 for( my $j=0; $j<(@keys-$i); $j++ ){
930 my $mapping = join(
"|", @keys );
932 my $mapping_key = join(
"|", @keys );
934 next
if $self->{
'_mapping_paths'}->{$mapping_key};
936 # Update the database
937 $meta->store_key_value(
'assembly.mapping',$mapping);
938 push @retlist, $mapping;
943 # Update mapping path cache
944 $self->_cache_mapping_paths;
947 # Return the mappings that we have just created
952 =head2 store_multiple_mapping_path
957 Example : my $pathref = $csa->store_multiple_mapping_path($cs1,$cs2);
958 Description: Given two or more coordinate systems
this will store
959 multiple mapping paths between them in the database.
961 Works similarly to the store_mapping_path method
962 But will presume every coord system can be mapped in multiple
963 ways to the other coord systems
964 This is represented by the use of
'#' instead of
'|'
967 Returntype : reference to a list of lists of
new meta_value mapping strings
968 created
for assembly.mapping
969 Exceptions : CoordSystems with no rank/duplicated rank
971 Status : Experimental
975 sub store_multiple_mapping_path{
979 # Validate and sort the args
981 @csystems >= 2 or
throw(
'Need two or more CoordSystems');
983 ref($_[0]) && $_[0]->isa(
'Bio::EnsEMBL::CoordSystem') or
984 throw(
'CoordSystem argument expected.');
985 my $rank = $_[0]->
rank ||
986 throw(
'CoordSystem has no rank: '.$_[0]->name);
987 $seen_ranks{$rank} &&
988 throw(
'CoordSystem '.$_[0]->name.
" shares rank $rank with ".
989 $seen_ranks{$rank}->name);
990 $seen_ranks{$rank} = $_[0];
992 @csystems =
map{&{$validate}($_)} @csystems;
994 foreach my $cs (@csystems) {
996 if ($cs->version()) {
997 $key .=
":" . $cs->version();
1001 # For each pair in the sorted list, store in the DB
1002 my $meta = $self->db->get_MetaContainer;
1004 for( my $i=1; $i<@keys; $i++ ){
1005 for( my $j=0; $j<(@keys-$i); $j++ ){
1006 my $mapping = join(
"#", @keys );
1008 my $mapping_key = join(
"#", @keys );
1010 next
if $self->{
'_mapping_paths'}->{$mapping_key};
1012 # Update the database
1013 $meta->store_key_value(
'assembly.mapping',$mapping) unless $meta->key_value_exists(
'assembly.mapping',$mapping);
1014 push @retlist, $mapping;
1019 # Update mapping path cache
1020 $self->_cache_mapping_paths;
1023 # Return the mappings that we have just created
1029 =head2 fetch_by_attrib
1031 Arg [1] :
string attrib
1032 Arg [2] : (optional)
string version
1033 Example : $csa->fetch_by_attrib(
'default_version',
'NCBIM37');
1034 Description: Retrieves a CoordSystem
object from the database that have the specified
1035 attrib and version,
if no version is specified, returns the
default version
1037 Exceptions :
throw when attrib not present
1043 sub fetch_by_attrib {
1046 my $version = shift;
1048 $version = lc($version)
if($version);
1050 my @dbIDs = keys %{$self->{
"_is_$attrib"}};
1052 throw(
"No $attrib coordinate system defined")
if(!@dbIDs);
1054 foreach my $dbID (@dbIDs) {
1055 my $cs = $self->{
'_dbID_cache'}->{$dbID};
1057 return $cs
if(lc($version) eq $cs->version());
1058 } elsif($self->{
'_is_default_version'}->{$dbID}) {
1063 #specifically requested attrib system was not found
1065 throw(
"$attrib coord_system with version [$version] does not exist");
1068 #coordsystem with attrib exists but no default is defined:
1069 my $dbID = shift @dbIDs;
1070 my $cs = $self->{
'_dbID_cache'}->{$dbID};
1072 warning(
"No default version for $attrib coord_system exists. " .
1073 "Using version [$v] arbitrarily");
1079 =head2 fetch_all_by_attrib
1081 Arg [1] :
string attrib
1082 Example : $csa->fetch_all_by_attrib(
'default_version');
1083 Description: Retrieves all CoordSystem
object from the database that have the specified
1086 Exceptions :
throw when attrib not present
1092 sub fetch_all_by_attrib {
1096 my @coord_systems = ();
1097 foreach my $dbID (keys %{$self->{
"_is_$attrib"}}) {
1098 push @coord_systems, $self->{
"_dbID_cache"}->{$dbID};
1101 return \@coord_systems;
1107 Example : $csa->store($coord_system);
1108 Description: Stores a CoordSystem
object in the database.
1110 Exceptions : Warning
if CoordSystem is already stored in
this database.
1120 if(!$cs || !ref($cs) || !$cs->isa(
'Bio::EnsEMBL::CoordSystem')) {
1121 throw(
'CoordSystem argument expected.');
1124 my $db = $self->db();
1125 my $name = $cs->
name();
1126 my $version = $cs->version();
1127 my $rank = $cs->rank();
1129 my $seqlevel = $cs->is_sequence_level();
1130 my $default = $cs->is_default();
1132 my $toplevel = $cs->is_top_level();
1135 throw(
"The toplevel CoordSystem cannot be stored");
1139 # Do lots of sanity checking to prevent bad data from being entered
1142 if($cs->is_stored($db)) {
1143 warning(
"CoordSystem $name $version is already in db.\n");
1147 if($name eq
'toplevel' || $name eq
'seqlevel' || !$name) {
1148 throw(
"[$name] is not a valid name for a CoordSystem.");
1151 if($seqlevel && keys(%{$self->{
'_is_sequence_level'}})) {
1152 throw(
"There can only be one sequence level CoordSystem.");
1155 if(exists $self->{
'_name_cache'}->{lc($name)}) {
1156 my @coord_systems = @{$self->{
'_name_cache'}->{lc($name)}};
1157 foreach my $c (@coord_systems) {
1158 if(lc($c->version()) eq lc($version)) {
1159 warning(
"CoordSystem $name $version is already in db.\n");
1162 if($default && $self->{
'_is_default_version'}->{$c->dbID()}) {
1163 throw(
"There can only be one default version of CoordSystem $name");
1168 if($rank !~ /^\d+$/) {
1169 throw(
"Rank attribute must be a positive integer not [$rank]");
1172 throw(
"Only toplevel CoordSystem may have rank of 0.");
1175 if(defined($self->{
'_rank_cache'}->{$rank})) {
1176 throw(
"CoordSystem with rank [$rank] already exists.");
1181 push @attrib,
'default_version' if($default);
1182 push @attrib,
'sequence_level' if($seqlevel);
1184 my $attrib_str = (@attrib) ? join(
',', @attrib) : undef;
1187 # store the coordinate system in the database
1191 $db->dbc->prepare(
'INSERT INTO coord_system '
1192 .
'( `name`, `version`, `attrib`, `rank`, `species_id` ) '
1193 .
'VALUES ( ?, ?, ?, ?, ? )' );
1195 $sth->bind_param( 1, $name, SQL_VARCHAR );
1196 $sth->bind_param( 2, $version, SQL_VARCHAR );
1197 $sth->bind_param( 3, $attrib_str, SQL_VARCHAR );
1198 $sth->bind_param( 4, $rank, SQL_INTEGER );
1199 $sth->bind_param( 5, $self->species_id(), SQL_INTEGER );
1202 my $dbID = $self->last_insert_id(
'coord_system_id', undef,
'coord_system');
1206 throw(
"Did not get dbID from store of CoordSystem.");
1210 $cs->adaptor($self);
1213 # update the internal caches that are used for fetching
1215 $self->{
'_is_default_version'}->{$dbID} = 1
if($default);
1216 $self->{
'_is_sequence_level'}->{$dbID} = 1
if($seqlevel);
1218 $self->{
'_name_cache'}->{lc($name)} ||= [];
1219 push @{$self->{
'_name_cache'}->{lc($name)}}, $cs;
1221 $self->{
'_dbID_cache'}->{$dbID} = $cs;
1222 $self->{
'_rank_cache'}->{$rank} = $cs;
1230 Example : $csa->remove($coord_system);
1231 Description: Removes a CoordSystem
object from the database.
1233 Exceptions : Warning
if CoordSystem is not stored in
this database.
1243 if(!$cs || !ref($cs) || !$cs->isa(
'Bio::EnsEMBL::CoordSystem')) {
1244 throw(
'CoordSystem argument expected.');
1247 my $db = $self->db();
1248 my $name = $cs->
name();
1249 my $version = $cs->version();
1250 my $dbID = $cs->dbID();
1251 my $rank = $cs->rank();
1254 # Do lots of sanity checking to prevent bad data from being entered
1257 if(!$cs->is_stored($db)) {
1258 warning(
"CoordSystem $name $version does not exist in db.\n");
1262 if($name eq
'toplevel' || $name eq
'seqlevel' || !$name) {
1263 throw(
"[$name] is not a valid name for a CoordSystem.");
1267 # remove the coordinate system from in the database
1270 my $sql =
"DELETE FROM coord_system WHERE name = ?";
1272 $sql .=
" AND version = ?";
1275 my $sth = $db->dbc->prepare($sql);
1277 $sth->bind_param( 1, $name, SQL_VARCHAR );
1278 $sth->bind_param( 2, $version, SQL_VARCHAR )
if $version;
1283 delete $self->{
'_name_cache'}->{lc($name)};
1285 delete $self->{
'_dbID_cache'}->{$dbID};
1286 delete $self->{
'_rank_cache'}->{$rank};
1287 delete $self->{
'_is_sequence_level'}->{$dbID};
1288 delete $self->{
'_is_default_version'}->{$dbID};
1290 $cs->adaptor(undef);