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


#### initialise parameters ####
$start_time = &RSAT::util::StartScript();
$in_format = "fasta";
$verbose = 0;

&ReadArguments;


#### check argument values ####


### open output file ###
if (defined($outfile{output})) {
  $out = &OpenOutputFile($outfile{output});
} else {
  $out = STDOUT;
}

##### read input #####
if (defined($infile{input})) {
  ($in, $input_dir) = &OpenInputFile($infile{input});
} else {
  $in = STDIN;
}

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

print $out "#seq", "\t", "length", "\n";

$sum = 0;
while ((($current_seq, $current_id) = &ReadNextSequence($in, $in_format, $input_dir)) &&
       (($current_seq ne "") || ($current_id ne ""))) {
  
  $length = length(&FoldSequence($current_seq,0));
  $sum+= $length;
  print $out $current_id, "\t", $length , "\n" 
      unless ($sum_only);
}

close $in if ($infile{input});

print $out "$sum\n" if ($sum_only);


################################################################
## Report execution time and close output stream
my $exec_time = &RSAT::util::ReportExecutionTime($start_time); ## This has to be exectuted by all scripts
print $main::out $exec_time if ($main::verbose >= 1); ## only report exec time if verbosity is specified
close $main::out if ($main::outfile{output});


exit(0);


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

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

        1999 by Jacques van Helden (jvanheld\@bigre.ulb.ac.be)
	
DESCRIPTION
	Return the lengths of each sequence from an input
	file. Otionally, return the sum of lengths.

CATEGORY
	sequences

USAGE
        sequence-lengths [-i inputfile] [-o outputfile] [-v]

OPTIONS
	-h	(must be first argument) display full help message
	-help	(must be first argument) display options
	-v	verbose
	-i inputfile
		if not specified, the standard input is used.
		This allows to place the command within a pipe.
	-o outputfile
		if not specified, the standard output is used.
		This allows to place the command within a pipe.
		   text-mode
	-sum	only return sum of sequene lengths 

End_of_help
  close HELP;
  exit;
}

sub PrintOptions {
#### display short help message #####
  open HELP, "| more";
  print HELP <<End_short_help;
sequence-lengths options
----------------
-h	(must be first argument) display full help message
-help	(must be first argument) display options
-i	input file
-o	output file
-v	verbose
-sum	only return sum of sequene lengths 
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;
      
      ### input file ###
    } elsif ($ARGV[$a] eq "-i") {
      $infile{input} = $ARGV[$a+1];
      
      ### output file ###
    } elsif ($ARGV[$a] eq "-o") {
      $outfile{output} = $ARGV[$a+1];
      
      ### sequence format ###
    } elsif ($ARGV[$a] eq "-format") {
      $in_format = $ARGV[$a+1];
      
      ### only return sum of lengths
    } elsif ($ARGV[$a] eq "-sum") {
	$sum_only = 1;
      
    }
  }
}

sub Verbose {
  print $out "; sequence-lengths ";
  &PrintArguments($out);
  if (%main::infile) {
    print $out "; Input files\n";
    while (($key,$value) = each %infile) {
      print $out ";\t$key\t$value\n";
    }
  }
  if (%main::outfile) {
    print $out "; Output files\n";
    while (($key,$value) = each %outfile) {
      print $out ";\t$key\t$value\n";
    }
  }
  print $out "; seq format\t$in_format\n";
}
