#!/usr/sbin/perl
# Compares listout files from multiple AMBER listout files for 
# Average violation, maximum, and minimum. 
#
#  An ASTERISK by the AVE indicates that the AVE is > 5.0 kcal/mol
#  and that the AVE/MAX > 0.25 (i.e. a single large violation is not flagged)
#
# OLD USAGE: ./comp_listout.pl #restraints outfile `ls -x *.listout` 
# NEW USAGE: ./comp_listout.pl outfile `ls -x *.listout`
#
#                             D.E. VOLK  11/20/02
#
$i = 1;
#    DONT USE MANUAL INPUT OF NUMBER OF RESTRAINTS
#    $num_restraints = shift(@ARGV);
$outfile = shift(@ARGV);
#
# Remove the old outfile file if it exits:
#
   if (-e "$outfile") {
     unlink("$outfile");
   }
#
open(OUT,">$outfile") || die "Cannot open file $outfile";
print OUT "FILE CREATED BY COMP_LISTOUT.PL BY D.E.VOLK, UTMB \n";
print OUT " \n";
#
# READ IN EACH .LISTOUT FILE, GET PENALTY, KEEP RUNNING MIN, MAX, AND TOTAL FOR EACH PEAK
#
foreach (@ARGV) 
        {
	$currentfile= $_;
	print "PROCESSING ARGV[$i] = $currentfile\n";
#
#	COUNT LINES IN THE FIRST FILE
#
	if ($i == 1)
	{
 	  $num_lines = 0;
	  open(LISTOUT, "$currentfile") || die "Cannot open file $currentfile";
	  while (<LISTOUT>)
	   { $num_lines++;}
          close(LISTOUT);
#	print "Number of lines in the first file is $num_lines\n";
        }	
#
	print OUT "PROCESSING FILE ARGV[$i] = $currentfile\n";
	open(LISTOUT, "$currentfile") || die "Cannot open file $currentfile";
        $j = 0;
        while (<LISTOUT>) {
	  $j++;
#	  if ( ($j > 11.0) && ( $j < (12 + $num_restraints) ) ) 
  	  if ( ($j > 11.0) && ( $j < ($num_lines - 4) ) )  #use auto line counter 
	    { 
              chomp;         # get rid of newline
              s/^\s+//;
	      $v13 = " ";  # some lines do not have peak number info
#
              ($v1,$v2,$v3,$v4,$v5,$v6,$v7,$v8,$v9,$v10,$v11,$v12,$v13)= split(/\s+/,$_);
#
#             add to matrix, position  Peak number = j - 11; filenumber
#
	      $HOLDER{$j - 11}{$i}= $v11; 
              if ($i == 1) { 
                 $PEAKLABEL{$j -11} = $v13;
                 $MIN{$j - 11} = $v11; 
                 $MAX{$j - 11} = $v11; };
	      $TOTAL{$j - 11} =  $TOTAL{$j - 11} + $v11;
              if ($v11 > $MAX{$j - 11} ) { $MAX{$j - 11} = $v11 };
              if ($v11 < $MIN{$j - 11} ) { $MIN{$j - 11} = $v11 };
#
# TESTING READ/STORAGE/CALCULATIONS ONLY
#             write OUT; 
#
             }
          }  
        close(LISTOUT);
	$i++;
        }   
close(OUT);
$maxfile = $i - 1;
print "Number of files used is $maxfile\n";
#
#   CREATE SUMMARY FILE
        open(OUT2,">>$outfile") || die "Cannot open file $outfile";
	print OUT2 "---------------------------------------------------------------------------------\n";
	print OUT2 "                     SUMMARY INFORMATION                      SUMMARY INFORMATION\n";
	print OUT2 "---------------------------------------------------------------------------------\n";
	print OUT2 "                                                                                 \n";
#       for ($myloop = 12; $myloop < (12 + $num_restraints); $myloop++ ) 
        for ($myloop = 12; $myloop < ($num_lines - 4); $myloop++ )   # for each restraint
          {
	   for ($otherloop = 1; $otherloop <= $maxfile; $otherloop++)  # for each file
            {
	     if ($otherloop == 1) 
               {
	         $AVE{$myloop-11} = $TOTAL{$myloop-11}/$maxfile;
                 if ( ($MAX{$myloop-11} > 0.0) && ($AVE{$myloop-11}/$MAX{$myloop-11} > 0.25) && ($AVE{$myloop-11} > 4.9) )  
                   {
                    printf OUT2 "line# %4.0f:    MAX=%6.1f    TOTAL=%7.1f  AVE=%6.1f *  ",
                    $myloop,$MAX{$myloop-11},$TOTAL{$myloop-11},$AVE{$myloop-11}
                   }
		 else 
		   {
		    printf OUT2 "line# %4.0f:    MAX=%6.1f    TOTAL=%7.1f  AVE=%6.1f    ",
                    $myloop,$MAX{$myloop-11},$TOTAL{$myloop-11},$AVE{$myloop-11}
                   };
                 printf OUT2 "%8s ", $PEAKLABEL{$myloop-11};
	       };
	     printf OUT2 "%6.1f ",
	     $HOLDER{$myloop-11}{$otherloop};
            }
	     print OUT2 "\n";
          }
close(OUT2);
#
format OUT =
Line = @<<<  Peak number= @<<< PENALTY =@###.##    MIN = @###.##   MAX = @###.##   TOTAL = @####.##  AVERAGE = @###.##
$j,               $j-11,        $HOLDER{$j-11}{$i},  $MIN{$j-11},   $MAX{$j-11},   $TOTAL{$j-11}    $TOTAL{$j-11}/$i
.
# That's all folks!!
