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



if ($ARGV[0] eq "-h") {
#### display full help message #####
  open HELP, "| more";
  print HELP <<End_of_help;
NAME
	features-from-dnapat

        1998 by Jacques van Helden (jacques\@ucmb.ulb.ac.be)
	
USAGE
        features-from-dnapat [-i inputfile] [-o outputfile] [-v]

DESCRIPTION
	converts the output from the program dna-pattern into a file
	that can be used as input for the program feature-map.

CATEGORY
	util
	conversion
	drawing

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.
	-strand	D | R | DR
		forces all features to be on the indicated strand.

INPUT FORMAT
	See the web site for dna-pattern at
	http://copan.cifn.unam.mx/~yeast/dna-pattern.html
	
OUTPUT FORMAT
	The output is a file that corresponds to the input format of 
	feature-map.
	http://copan.cifn.unam.mx/~yeast/feature-map.html
	
EXAMPLES
       features-from-dnapat -v -i mydata -o myresult
	
End_of_help
  close HELP;
  exit;
}

if ($ARGV[0] eq "-help") {
#### display short help message #####
  open HELP, "| more";
  print HELP <<End_short_help;
features-from-dnapat 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
-strand	strand
End_short_help
  close HELP;
  exit;
}

$start_time = &RSAT::util::StartScript(); 
$date = $start_time;

#### initialise parameters ####
$type="dnapat";

#### read arguments ####
foreach $a (0..$#ARGV) {
    ### verbose ###
    if ($ARGV[$a] eq "-v") {
	$verbose = 1;

    ### input file ###
    } elsif ($ARGV[$a] eq "-i") {
	$inputfile = $ARGV[$a+1];

    ### output file ###
    } elsif ($ARGV[$a] eq "-o") {
	$outputfile = $ARGV[$a+1];

	### strand ###
    } elsif ($ARGV[$a] eq "-strand") {
	$strand = $ARGV[$a+1];
	$force_strand = 1;

    }
}

### open input file ###
($in, $input_dir) = &OpenInputFile($inputfile);

### open output file ###
$out = &OpenOutputFile($outputfile);

#### verbose ####
if ($verbose) {
    print $out ";features-from-dnapat result\n";
    if ($inputfile ne "") {
	print $out ";Input file	$inputfile\n";
    }
    if ($outputfile ne "") {
	print $out ";Output file	$outputfile\n";
    }
}

###### execute the command #########

while (<$in>) {
    next if (/^;/);
    next unless (/\S/);
    chomp;

    if (/\t/) {
	@fields = split, "\t";
    } else {
 	@fields = split, /\s+/;
    }
    $id = $fields[0];
    unless ($force_strand) {
	$strand = $fields[1];
	$strand =~ s/W/D/i;
	$strand =~ s/C/R/i;
    }
    $map = $fields[3];
    $start = $fields[4];
    $end = $fields[5];
    $seq = $fields[6];
    $score = $fields[7];
    $descr = "$seq";
    $descr .= " m=$mismatches" if ($mismatches > 0);

    &PrintFeature();
}


###### close input file ######
close $in unless ($inputfile eq "");


################################################################
## 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 ($outputfile);


exit(0);


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

sub PrintFeature {
    print $out $map;
    print $out "\t$type";
    print $out "\t$id";
    print $out "\t$strand";
    print $out "\t$start";
    print $out "\t$end";
    print $out "\t$descr";
    print $out "\t$score";
    print $out "\n";
}
