#!CHANGEJNET

open(FILE, "$ARGV[0]");
open OUT, ">$ARGV[0].pssm";

# grep out the position specific scoring matrix

# find howmany passes there were in the original and jump to the last one

$sumsucc = `grep "Position-based scoring matrix used" $ARGV[0] | wc -l`;

$sumsucc=~ s/ //g;
chop $sumsucc;
print STDERR "$sumsucc position frequency tables were found in PSIBlast file $ARGV[0]\n";

$charfreq=0;$done=0;$c=0;
while (<FILE>){
  if (/^Position-based scoring matrix used/){
    $charfreq++;
  }
  if ($charfreq == $sumsucc && $done == 0){
   # print STDERR "Found frequency profile number $charfreq of $sumsucc, extracting profile...\n\n";	
    while (($ln2 = <FILE>) !~ /^\n/){
     $profile[$c] = $ln2;
     $c++;
    }
    $done=1;
  }
}
$c=0;
foreach $odh (@profile){
  if ($c > 0){
	
  (@thisline)=split(" ",$odh);
	for ($i=0; $i < 20; $i++){
               #	if ($thisline[$i] > 10){
			# something wrong!
	       #		print STDERR "ERROR: $ARGV[0] - PROFILE LOOKS WRONG!\n EXIT!\n";
	       #	}
		# scale the output to between 1 and 0
		$out=1/(1+(exp(-$thisline[$i])));
          printf OUT ("%2.8f ", $out);
	  $xsum=$xsum+$thisline[$i];
	}
	print OUT "\n";
  undef @thisline;
}
  $c++;
}

if ($xsum == 0){
	print STDERR "ERROR: $ARGV[0] PROFILE CONTAINS NO USEFUL DATA!\n EXIT\n";
	close OUT;
        system ("rm $ARGV[0].pssm");


}

