#!/usr/bin/perl
# prot_unfold
# Francis Peterson 12/14/02
# unalias 13C shifts in xeasy/dyana shift list (.prot file)
# 13C spectral width (in ppm) of spectrum in which aliased
# chemical shift values were generated is hardcoded below.
#
# syntax: noe_weeder sorted_upl_file noesy_peaklist > output_file 
#  

$file1 = $ARGV[0]; 
$file2 = $ARGV[1];

#  Search sorted upl files for bad sequential NOEs

    open (FILE1, "$file1") || die "Couldn't open file: $!\n";
    while (<FILE1>) {
        @fld = split;        #delimits by whitespace

#        print "$fld[0] $fld[1] $fld[2] $fld[3] $fld[4] $fld[5] $fld[6] $fld[7] $fld[8] $fld[9] \n";
#        print "$fld[3] $fld[0] $fld[5] \n";

# Selects subset of NOE to evaluate for violations
                unless(($fld[5] eq "HN") || ($fld[4] eq "PRO" && ($fld[5] eq "HD2" || $fld[5] eq "HD3" || $fld[5] eq "QD"))){
		   $res_diff=$fld[3]-$fld[0];
#                   print "$res_diff \n";

# Finds violations and stores peak number in an array
			if(($res_diff > 0 ) && ($res_diff < 2 )){
#                           print "$fld[0] $fld[1] $fld[2] $fld[3] $fld[4] $fld[5] $fld[6] $fld[7] $fld[8] $fld[9] \n";
#                           print "    \n";


			push @peak, $fld[8];
#                           print "@peak \n";



           }
           
        }

     }

close(FILE1);

# Opens peak list and resets assignment of bad sequential NOEs

    open (FILE2, "$file2") || die "Couldn't open file: $!\n";
    while (<FILE2>) {		
       @fld = split;        #delimits by whitespace


# identify and print header lines without change
#                if(/#/){
#
#                        print "$_";
#                       next;
#               }

#Reset bad sequentials to zero

$match=0;

	foreach $peak (@peak){
		if($peak==$fld[0]){
		   $fld[11]=0;
			#print "$peak $fld[0] $fld[11] \n";                 
     			$~ = "PEAKS";
			write; 
			$match=1;                 	
			 last;

                      } 

     
                  
                 }
		
		if($match==0){
			print $_;

		}
                            
           }


#define format for output
format PEAKS =
@>>> @##.### @##.### @##.### @ @         @>>>>>>>>>  @>>>>>>> @   @ @>>> @>>> @>>> 0
$fld[0], $fld[1], $fld[2], $fld[3], $fld[4], $fld[5], $fld[6], $fld[7], $fld[8], $fld[9], $fld[10], $fld[11], $fld[12]
.