#!/usr/sbin/perl
#
#  Count and sort the noesy restraints in a sander listout file.
#  Restraints are sorted by residue into intra, sequential, medium,
#  long range, and inter-subunit interactions.
#
#  ./listout_noesy_sorter.pl infile.listout outputfile.txt residues_per_monomer
#
#                             D.E. VOLK  05/01/03
#

$infile  = shift(@ARGV);
$outfile = shift(@ARGV);
$monomer = shift(@ARGV);
#
   if (-e "$outfile") {
     unlink("$outfile");
   }
#
open(OUT,">$outfile") || die "Cannot open file $outfile";
#
#	COUNT NUMBER OF LINES IN THE LISTOUT FILE
#
          $num_lines = 0;
          open(LISTOUT, "$infile") || die "Cannot open file $infile";
          while (<LISTOUT>)
           { $num_lines++;}
          close(LISTOUT);
#
#
          open(LISTOUT, "$infile") || die "Cannot open file $infile";
          $j = 0;
	  while (<LISTOUT>) 
          {
	  $j++;
  	  if ( ($j > 11.0) && ( $j < ($num_lines - 4) ) )  #do only for restraint lines 
	    { 
              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+/,$_);

#
#		ONLY TO NOE RESTRAINTS FROM PROTONS TO PROTONS
#
        if ( ( ($v1 =~ /^H/) || ($v1 =~ /^\*/) ) && ( ($v5 =~ /^H/) || ($v5 =~ /^\*/) ) && ($v12 =~ /d/) )
	      {
	      $v7=substr($v7,0,-1);   #remove last character
	      $range = abs($v3 - $v7);
#
#	     CHANGE RANGE IF INTER SUBUNIT NOE
              if (  ( ($v3 < $monomer + 1) && ($v7 > $monomer) ) || ( ($v3 >= $monomer +1) && ($v7 < $monomer + 1) ) )
                { $range = -1; print "$v3 $v7 long-range moved to inter-subunit\n";}

#		INCREASE NOE COUNT IN THE APPROPRIATE PLACES
#
		if ($range == 0) {$NOEMAT{$v3}{1}= $NOEMAT{$v3}{1} + 1;}  # INTRA

		if ($range == 1) {$NOEMAT{$v3}{2}= $NOEMAT{$v3}{2} + 1;}  # SEQUENTIAL
		if ($range == 1) {$NOEMAT{$v7}{2}= $NOEMAT{$v7}{2} + 1;}  # SEQUENTIAL

		if ( ($range > 1) && ($range < 5) ) {$NOEMAT{$v3}{3}= $NOEMAT{$v3}{3} + 1;}  #MEDIUM RANGE
		if ( ($range > 1) && ($range < 5) ) {$NOEMAT{$v7}{3}= $NOEMAT{$v7}{3} + 1;}  #MEDIUM RANGE

		if ( $range > 4 ) {$NOEMAT{$v3}{4}= $NOEMAT{$v3}{4} + 1;}  # LONG RANGE
		if ( $range > 4 ) {$NOEMAT{$v7}{4}= $NOEMAT{$v7}{4} + 1;}  # LONG RANGE

		if ($range < 0.0) {$NOEMAT{$v3}{5}= $NOEMAT{$v3}{5} + 1;}  # INTER SUBUNIT
		if ($range < 0.0) {$NOEMAT{$v7}{5}= $NOEMAT{$v7}{5} + 1;}  # INTER SUBUNIT
              }
            }
          }
#
#		NOW PRINT OUT THE SORTED RESULTS WITH A HEADER
      print OUT " SORTED ACCOUNTING OF NOE PEAKS IN A SANDER LISTOUT FILE \n";
      print OUT " CREATED BY PROGRAM LISTOUT_NOESY_SORTER.PL BY D.E.VOLK, UTMB \n \n";
      print OUT " Res.  Intra  Sequential  Medium-Range  Long-Range  Inter-Subunit \n";
      print OUT "----------------------------------------------------------------- \n";

      foreach $residue (sort{$a<=>$b} keys %NOEMAT)
      {
      if ($NOEMAT{$residue}{1} == "") { $NOEMAT{$residue}{1} = 0;}
      if ($NOEMAT{$residue}{2} == "") { $NOEMAT{$residue}{2} = 0;}
      if ($NOEMAT{$residue}{3} == "") { $NOEMAT{$residue}{3} = 0;}
      if ($NOEMAT{$residue}{4} == "") { $NOEMAT{$residue}{4} = 0;}
      if ($NOEMAT{$residue}{5} == "") { $NOEMAT{$residue}{5} = 0;}
      printf OUT " %3s   ", $residue;
      printf OUT " %3s   ", $NOEMAT{$residue}{1};
      printf OUT " %5s     ", $NOEMAT{$residue}{2};
      printf OUT " %6s      ", $NOEMAT{$residue}{3};
      printf OUT " %7s    ", $NOEMAT{$residue}{4};
      printf OUT " %7s    ", $NOEMAT{$residue}{5};
      printf OUT "\n";
      }
#
# That's all folks!!
