From 249fb7795e4e112f0c03c3a25a2076ff6240db14 Mon Sep 17 00:00:00 2001
From: Kieron Taylor <ktaylor@ebi.ac.uk>
Date: Mon, 29 Sep 2014 14:49:53 +0100
Subject: [PATCH] [ENSCORESW-1050] Cure issues with get_nearest_Gene caused by
 minor invocation bugs.

---
 .../Bio/EnsEMBL/DBSQL/BaseFeatureAdaptor.pm   |  4 ++--
 modules/Bio/EnsEMBL/Feature.pm                |  5 ++--
 modules/t/getNearestFeature.t                 | 24 ++++++++++++-------
 3 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/modules/Bio/EnsEMBL/DBSQL/BaseFeatureAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/BaseFeatureAdaptor.pm
index 7f85c0e044..31aa4e6c86 100644
--- a/modules/Bio/EnsEMBL/DBSQL/BaseFeatureAdaptor.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/BaseFeatureAdaptor.pm
@@ -1476,8 +1476,8 @@ sub remove_by_feature_id {
 sub fetch_nearest_by_Feature {
   my $self = shift;
   my $feature = shift;
-  my @hits = @{ $self->fetch_all_by_outward_search(-FEATURE => $feature, -MAX_RANGE => 1000000, -RANGE => 1000 -LIMIT => 1) };
-  return $hits[0] if (@hits > 0);
+  my @hits = @{ $self->fetch_all_by_outward_search(-FEATURE => $feature, -MAX_RANGE => 1000000, -RANGE => 1000, -LIMIT => 1) };
+  return $hits[0] if (scalar @hits > 0);
   return;
 }
 
diff --git a/modules/Bio/EnsEMBL/Feature.pm b/modules/Bio/EnsEMBL/Feature.pm
index 3f406705ce..b4b6e2dc92 100644
--- a/modules/Bio/EnsEMBL/Feature.pm
+++ b/modules/Bio/EnsEMBL/Feature.pm
@@ -1440,7 +1440,7 @@ sub get_overlapping_Genes{
 =head2 get_nearest_Gene
 
   Description: Get the nearest genes to the feature
-  Returntype : listref of Bio::EnsEMBL::Gene
+  Returntype : Bio::EnsEMBL::Gene
   Caller     : general
   Status     : At risk
 
@@ -1449,7 +1449,8 @@ sub get_overlapping_Genes{
 sub get_nearest_Gene {
   my $self = shift; 
   my $ga = Bio::EnsEMBL::Registry->get_adaptor($self->adaptor->db->species,'core','Gene');
-  return $ga->fetch_nearest_by_Feature($self);
+  my ($gene, $distance) = @{ $ga->fetch_nearest_by_Feature($self) };
+  return $gene;
 }
 
 =head2 summary_as_hash
diff --git a/modules/t/getNearestFeature.t b/modules/t/getNearestFeature.t
index 6d8af00c25..34962e1063 100644
--- a/modules/t/getNearestFeature.t
+++ b/modules/t/getNearestFeature.t
@@ -245,19 +245,13 @@ cmp_ok($results[3]->[0]->display_id, 'eq', 'c', 'Check for a feature found outsi
 
 ### Issues/Todos ###
 #
-#2 Ordering is wrong and seemingly arbitrary when dealing with negative numbers. Drop
-#  negative numbers, such that the distances are standardised.
-#  This will be useful for filtering, which will be a common task, and leave up/downsteam
-#  detection to users, as this will be much less common?
-
 #6 For streamed queries we probably do not want to return features where the target measurement point is enclosed
 #  by the source feature. This suggests we should change the functionality of -NOT_OVERLAPPING to -INCLUDE_ENCLOSED
 #  Including overlaps should be the default for non-streamed queries
-# 8 Strand tests seem lacking, so we need to add a target feature on the negative strand for each context?
 
 # 10 STRAND could be renamed, as this is a little ambiguous to. Could be -TARGET_STRAND ?
 #    Also take +/- aswell as 1/-1?
-# 13 Add iterator
+
 
 note("Nathan's tests");
 my $g = Bio::EnsEMBL::SimpleFeature->new(
@@ -458,11 +452,23 @@ sub print_what_you_got {
   note ("Results: ".scalar @$results." features");
   if (scalar(@$results) == 0) { note("No hits"); return;}
   for (my $i =0; $i<scalar(@$results);$i++) {
-    my ($feature,$distance,$effective,$length,$dbID) = @{$results->[$i]};
+    my ($feature,$distance) = @{$results->[$i]};
     no warnings 'uninitialized';
-    note("Feature: ".$feature->display_id." at ".$distance.", ".$effective.", length: ".$length);
+    note("Feature: ".$feature->display_id." at ".$distance);
   }
 }
 
+# test utility functions get_nearest_Gene and 
+$dba = $db->get_DBAdaptor('core');
+my $tra = $dba->get_TranscriptAdaptor();
+my $ga = $dba->get_GeneAdaptor();
+my $enst = $tra->fetch_by_stable_id('ENST00000300415');
+my $gene;
+($gene,$distance) = @{ $ga->fetch_nearest_by_Feature($enst) };
+is($gene->stable_id, 'ENSG00000101331','Simple usecase for fetch_nearest_by_Feature');
+my $ensg = $enst->get_nearest_Gene;
+# diag(Dumper $ensg);
+is($ensg->stable_id,'ENSG00000101331','Simple usecase for get_nearest_Gene');
+
 done_testing;
 
-- 
GitLab