#!/usr/bin/perl 
############################################################
#
# $Id: calc-neighbour-limits,v 1.12 2010/06/09 23:06:11 jvanheld Exp $
#
# Time-stamp: <2003-07-06 20:15:37 jvanheld>
#
############################################################

#use strict;;
if ($0 =~ /([^(\/)]+)$/) {
    push (@INC, "$`lib/");
}
require "RSA.lib";

#### initialise parameters ####
local $start_time = &AlphaDate;

local $infile{input} = "";
local $outfile{output} = "";

local $verbose = 0;
local $in = STDIN;
local $out = STDOUT;

&ReadArguments;


#### check argument values ####

#### accepted feature types
unless (defined %accepted_feature_types) {
    %accepted_feature_types = %supported_feature_types;
}
$feature_types = join ",", keys (%accepted_feature_types);


die "Error: you should specify an organism\n$supported_organisms\n"
    unless ($organism_name);

### open output file ###
$out = &OpenOutputFile($outfile{output});

#### verbose ####
&Verbose if ($verbose);

###### execute the command #########
&CheckOrganism($organism_name);
&ReadFeatures($organism_name,"",$feature_types);
&CalcNeighbourLimits();

###### print output ######

#### header
print $out (join ("\t",
		  ";ORF",
		  "name",
		  "ctgom",
		  "left",
		  "right",
		  "strand",
		  "UP_limit",
		  "UP_sze",
		  "DWN_lim",
		  "DWN_sze",
		  "\n"
		  ));
foreach my $ctg (sort keys %contig) {
    my @orfs = sort { $left{$a} <=> $left{$b} } $contig{$ctg}->get_genes();
    foreach $orf (@orfs) {
	print $out (join ("\t",
			  $orf,
			  $name{$orf},
			  $ctg,
			  $left{$orf},
			  $right{$orf},
			  $strand{$orf},
			  $upstr_limit{$orf},
			  $upstr_size{$orf},
			  $downstr_limit{$orf},
			  $downstr_size{$orf},
			  "\n"
		      ));
    }
}

###### close output file ######
my $exec_time = &RSAT::util::ReportExecutionTime($start_time);
print $main::out $exec_time if ($main::verbose >= 1);
close $out if ($outfile{output});


exit(0);


########################## subroutine definition ############################

sub PrintHelp {
#### display full help message #####
  open HELP, "| more";
  print HELP <<End_of_help;
NAME
	calc-neighbour-limits

        1999 by Jacques van Helden (jvanheld\@bigre.ulb.ac.be)
	
USAGE
        calc-neighbour-limits [-v] -org genus_species

DESCRIPTION
	Calculates the relative positions of neighbour CDS for all
	genes of a selected organism.

CATEGORY
	genomics

OPTIONS
	-h	(must be first argument) display full help message
	-help	(must be first argument) display options
	-v	verbose
	-org	organism
End_of_help
  close HELP;
  exit;
}

sub PrintOptions {
#### display short help message #####
  open HELP, "| more";
  print HELP <<End_short_help;
calc-neighbour-limits options
----------------
-h	(must be first argument) display full help message
-help	(must be first argument) display options
-v	verbose
-org	organism
End_short_help
  close HELP;
  exit;
}


sub ReadArguments {
#### read arguments ####
  foreach my $a (0..$#ARGV) {
    ### verbose ###
    if ($ARGV[$a] eq "-v") {
      if (&IsNatural($ARGV[$a+1])) {
	$verbose = $ARGV[$a+1];
      } else {
	$verbose = 1;
      }
      
      ### detailed help
    } elsif ($ARGV[$a] eq "-h") {
      &PrintHelp;
      
      ### list of options
    } elsif ($ARGV[$a] eq "-help") {
      &PrintOptions;
      
      ### organism
    } elsif ($ARGV[$a] eq "-org") {
      $organism_name = $ARGV[$a+1];
      unless (defined($supported_organism{$organism_name})) {
	  die ("Error: organism '$organism_name' is not supported on this site",
	       $supported_organisms,
	       "\n");
      }
      
    }
  }
}

sub Verbose {
  print $out "; calc-neighbour-limits ";
  &PrintArguments($out);
  if (defined(%infile)) {
    print $out "; Input files\n";
    while (($key,$value) = each %infile) {
      print $out ";\t$key\t$value\n";
    }
  }
  if (defined(%outfile)) {
    print $out "; Output files\n";
    while (($key,$value) = each %outfile) {
      print $out ";\t$key\t$value\n";
    }
  }
}
