#!/usr/bin/perl
use strict;
# For general information about the Ensembl Software see 
# http://www.ensembl.org/info/software/index.html
# to get the latest API (core|compara|variation)


# For API Installation see
# http://www.ensembl.org/info/software/api_installation.html

# Ensembl Core Perl API Tutorial at 
# http://www.ensembl.org/info/software/core/core_tutorial.html

# Ensembl Core Perl API Documentation at
# http://www.ensembl.org/info/software/Pdoc/ensembl/index.html

# For the Registry 
# http://www.ensembl.org/info/software/registry/index.html

# This script use .ensembl_init file to connect to the 
# Ensembl databases. 


use Bio::EnsEMBL::Registry;
my $reg = "Bio::EnsEMBL::Registry";

$reg->load_all();
# get the gene adaptor for human
my $gene_adaptor = $reg->get_adaptor("homo_sapiens","core","gene");

# fetch the gene(s) correspondign to that external id (here:Refseq)
my @genes = @{$gene_adaptor->fetch_all_by_external_name('NM_000059')};
 
# get the coordinates of the gene 
my ($chro, $end, $start);
foreach my $gene (@genes) {
    $chro  =  $gene->slice->seq_region_name();
    $start =  $gene->start;
    $end   =  $gene->end;
    &feature2string($gene);
}

# get a slice adaptor for human
# and move that slice upstream and downstream with a window of 1Mb
# and get the genes in that slice 
my $slice_adaptor = $reg->get_adaptor("homo_sapiens","core","slice");

#-------------------
# GET UPSTREAM GENES
#-------------------
my @upstream_genes ;
my $size = 0;
my $ustart = $end+1;
my $uend   = $end+1000001;
while ($size < 3){   
    my $slice = $slice_adaptor->fetch_by_region('chromosome',$chro,$ustart,$uend);
    #my $slice = $slice_adaptor->fetch_all('chromosome',$chro,$ustart,$uend);
    my @genes = @{$slice->get_all_Genes()};

    foreach my $gene (@genes) {
	push (@upstream_genes, $gene) if !exists $upstream_genes[$gene]; 
	$size = @upstream_genes;
	
    }
    $ustart = $uend;
    $uend   = $uend+1000000;
}

foreach my $up_gene (@upstream_genes) { print "up ";&feature2string($up_gene);}



#-------------------
# GET DOWNSTREAM GENES
#-------------------

my @downstream_genes ;
$size=0;
my $dend = $start-1;
my $dstart   = $start-1000001;
while ($size < 3){
    
    my $slice = $slice_adaptor->fetch_by_region('chromosome',$chro,$dstart,$dend);
    my @genes = @{$slice->get_all_Genes()};
    
    foreach my $gene (@genes) {
	push (@downstream_genes, $gene) if !exists $downstream_genes[$gene]; 
	$size = @downstream_genes;
    }
    
    $dend   = $dstart;
    $dstart = $dstart - 1000000 ;
}
foreach my $do_gene (@downstream_genes) {print "do ";&feature2string($do_gene);}


#------------------
sub feature2string {
    my $f = shift;
    
    my $projection = $f->project('chromosome');
    foreach my $segment (@$projection) {
	my $to_chro      = $segment->to_Slice();

	my $stable_id  = $f->stable_id();
	my $seq_region = $to_chro->seq_region_name();
	my $start      = $to_chro->start();
	my $end        = $to_chro->end();
	my $strand     = $to_chro->strand();
	my $ext_name   = $f->external_name();
	print "$stable_id : $seq_region:$start $end ($strand)\tExt_name:$ext_name\n";
    }
}

