#!/usr/local/bin/perl

# script to filter a standart output file, and get predictions between min and max probability or score value.
#author: C.Mathe, 22/02/99.
#usage : getscore.pl fileST -min <min> -max <max> >  file-min-max-ST


$last_seq=0;
$verif=0;
$min=0;

sub usage{ 
die "\nPerl script to filter standart output according to score or probability value.\nUSAGE:\n
getscore.pl -min <min score> -max <max score>\n\t\t (one or both options are allowed)\n
example: getscore.pl -min 0.5 -max 0.6 <seq01gs.txtST> seq01gs.txtST_05-06\n\n"}

$max=1000.00;

if($#ARGV>3) {&usage;} 


if ($ARGV[0] ne '-min' && $ARGV[0] ne '-max') {&usage;}

if ($#ARGV>1){
    if (($ARGV[2] ne '-min')&&($ARGV[2] ne '-max')) {&usage;}
}

for ($i=0;$i<$#ARGV;$i++) {
    if ($ARGV[$i] eq '-min') {$min=$ARGV[$i+1];
			      if ($min !~ /\d*.?\d*/) {&usage;}
			  }
    if ($ARGV[$i] eq '-max') {$max=$ARGV[$i+1];
			      if ($max !~ /\d*.?\d*/) {&usage;}
			  }}



if ($min>$max) {die "Incompatible values for min=$min and max=$max !\n";}
     
while (<STDIN>)
{
    if ($l==0){print $_;}
    
    elsif (($contig)=($_ =~ /^seq(\d+)/))
    {
	@proba=split;
	#print"$proba[$#proba]";
	if (($proba[$#proba])=($_ =~ /(\d+\.?\d*)$/))
	{
	    if (($proba[$#proba] <= $max) && ($proba[$#proba] >= $min)) 
	    {
		$contig = int $contig;
		if ($last_seq != $contig) 
		{
		    print "\n$_";
		    $last_seq=$contig;
		}
		else{
		    print $_;
		}
	    }
	}
	elsif (/H/) 
	{
	    if ($last_seq != $contig) 
	    {
		print "\n$_";
		$last_seq=$contig;
	    }
	    else{
		print $_;
	    }
	}
    }
    $l++;
}

