From 5784cc0f4dcda3a6fd06785eac54bc1b457089c4 Mon Sep 17 00:00:00 2001
From: Andrew Yates <ayates@ebi.ac.uk>
Date: Fri, 17 May 2013 16:42:45 +0000
Subject: [PATCH] [ENSCORESW-473]. Adding a fetch all CoordSystems by version
 for regulation.

---
 .../Bio/EnsEMBL/DBSQL/CoordSystemAdaptor.pm   | 22 +++++++++++++++++++
 modules/t/coordSystemAdaptor.t                | 22 +++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/modules/Bio/EnsEMBL/DBSQL/CoordSystemAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/CoordSystemAdaptor.pm
index 685d8aea0a..49455208a4 100644
--- a/modules/Bio/EnsEMBL/DBSQL/CoordSystemAdaptor.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/CoordSystemAdaptor.pm
@@ -516,9 +516,31 @@ sub fetch_all_by_name {
   return $self->{'_name_cache'}->{$name} || [];
 }
 
+=head2 fetch_all_by_version
 
+  Arg [1]    : string $version
+               The version of the coordinate systems to retrieve.
+  Example    : foreach my $cs (@{$csa->fetch_all_by_version('GRCh37')}){
+                 print $cs->name(), ' ', $cs->version();
+               }
+  Description: Retrieves all coordinate systems of a particular version
+  Returntype : ArrayRef of Bio::EnsEMBL::CoordSystem objects
+  Exceptions : throw if no name argument provided
+  Caller     : general
+  Status     : Stable
 
+=cut
 
+sub fetch_all_by_version {
+  my ($self, $version) = @_;
+  throw "Version argument is required" if ! $version;
+  my $coord_systems = [
+    grep { $_->version() eq $version } 
+    map { $self->{_rank_cache}->{$_} } 
+    sort keys %{$self->{_rank_cache}}
+  ];
+  return $coord_systems;
+}
 
 =head2 fetch_by_dbID
 
diff --git a/modules/t/coordSystemAdaptor.t b/modules/t/coordSystemAdaptor.t
index 91e6080bcb..507055435d 100644
--- a/modules/t/coordSystemAdaptor.t
+++ b/modules/t/coordSystemAdaptor.t
@@ -3,6 +3,7 @@ use strict;
 use Test::More;
 use Bio::EnsEMBL::Test::MultiTestDB;
 use Bio::EnsEMBL::Test::TestUtils;
+use Test::Exception;
 
 our $verbose = 1; #set to 1 to turn on debug printouts
 
@@ -176,6 +177,27 @@ ok( @{$new_path} == 2 &&
 my $new_paths2 = $csa->store_mapping_path( $cs, $cln_cs );
 ok( @{$new_paths2} == 0 ); # Should not update second time round
 
+#
+# Do some inserting of mock coord systems and 
+# do version retrieval
+#
+my $newcs_two = Bio::EnsEMBL::CoordSystem->new(
+  -NAME            => 'newsystem_number_two',
+  -VERSION         => 'NCBI35',
+  -DEFAULT         => 0,
+  -SEQUENCE_LEVEL  => 0,
+  -RANK            => 11
+);
+$csa->store($newcs_two);
+
+dies_ok { $csa->fetch_all_by_version() } 'fetch_all_by_version should die if not given a version to check';
+is_deeply($csa->fetch_all_by_version('NCBI35'), [ $cs, $newcs_two ], 'Checking version rank retrieval works');
+is_deeply(
+  $csa->fetch_all_by_version('NCBI33'), 
+  [$csa->fetch_by_name('chromosome')], 
+  'Retrieval by name should return the same as version for NCBI33');
+is_deeply($csa->fetch_all_by_version('thisdoesnotexist'), [], 'Bogus coordinate system results in no results');
+
 $multi->restore('core', 'coord_system');
 $multi->restore('core', 'meta');
 
-- 
GitLab