#!/usr/bin/perl

$basepairs = $ARGV[0];
$noe_upl = $ARGV[1];

open(IN, "<$basepairs") or die "Reqd Args: basepairs.in noe.upl\n";
open(OUT_NOE, ">$noe_upl") or die "Reqd Args: basepairs.in noe.upl\n";

$h86h86 = "5.0";
$h86h5 = "5.0";
$h2ph86 = "2.7";
$h3ph86 = "3.3";
$h2h1p = "5.0";
$h1ph1p = "6.0";

$h2ph1p = "5.0";
$h1ph5 = "6.0";
$h5h5 = "5.0";

$intra86h2p = "4.0";
$intra86h3p = "3.0";

$prevres1 = "0";
$prevbase1 = "null";
$prevres2 = "0";
$prevbase2 = "null";

while (<IN>) {
   ($base1, $res1, $base2, $res2) = split; 
          if ($prevbase1 eq "null") {
          printf OUT_NOE " ";

        } else  {
          if (($prevbase1 eq "RCYT" or $prevbase1 eq "URA") and ($base1 eq "RGUA" or $base1 eq "RADE")) {
          printf OUT_NOE "
$prevres1  $prevbase1  H6     $res1  $base1  H8      $h86h86  1.00E+00
$prevres1  $prevbase1  H2\'    $res1  $base1  H8      $h2ph86  1.00E+00
$prevres1  $prevbase1  H3\'    $res1  $base1  H8      $h3ph86  1.00E+00
$prevres1  $prevbase1  H1\'    $res1  $base1  H1\'     $h1ph1p  1.00E+00
$prevres1  $prevbase1  H2\'    $res1  $base1  H1\'     $h2ph1p  1.00E+00
";

        } elsif (($prevbase1 eq "RGUA") and ($base1 eq "RGUA" or $base1 eq "RADE")) {
          printf OUT_NOE "
$prevres1  $prevbase1  H8     $res1  $base1  H8      $h86h86  1.00E+00
$prevres1  $prevbase1  H2\'    $res1  $base1  H8      $h2ph86  1.00E+00
$prevres1  $prevbase1  H3\'    $res1  $base1  H8      $h3ph86  1.00E+00
$prevres1  $prevbase1  H1\'    $res1  $base1  H1\'     $h1ph1p  1.00E+00
$prevres1  $prevbase1  H2\'    $res1  $base1  H1\'     $h2ph1p  1.00E+00
";

        } elsif (($prevbase1 eq "RADE") and ($base1 eq "RGUA" or $base1 eq "RADE")) {
          printf OUT_NOE "
$prevres1  $prevbase1  H8     $res1  $base1  H8      $h86h86  1.00E+00
$prevres1  $prevbase1  H2\'    $res1  $base1  H8      $h2ph86  1.00E+00
$prevres1  $prevbase1  H3\'    $res1  $base1  H8      $h3ph86  1.00E+00
$prevres1  $prevbase1  H1\'    $res1  $base1  H1\'     $h1ph1p  1.00E+00
$prevres1  $prevbase1  H2\'    $res1  $base1  H1\'     $h2ph1p  1.00E+00
$prevres1  $prevbase1  H2     $res1  $base1  H1\'     $h2h1p  1.00E+00
";

         } elsif (($prevbase1 eq "RCYT" or $prevbase1 eq "URA") and ($base1 eq "RCYT" or $base1 eq "URA")) {
          printf OUT_NOE "
$prevres1  $prevbase1  H6     $res1  $base1  H6      $h86h86  1.00E+00
$prevres1  $prevbase1  H6     $res1  $base1  H5      $h86h86  1.00E+00
$prevres1  $prevbase1  H5     $res1  $base1  H5      $h5h5  1.00E+00
$prevres1  $prevbase1  H2\'    $res1  $base1  H6      $h2ph86  1.00E+00
$prevres1  $prevbase1  H3\'    $res1  $base1  H6      $h3ph86  1.00E+00
$prevres1  $prevbase1  H1\'    $res1  $base1  H1\'     $h1ph1p  1.00E+00
$prevres1  $prevbase1  H1\'    $res1  $base1  H5      $h1ph5  1.00E+00
$prevres1  $prevbase1  H2\'    $res1  $base1  H1\'     $h2ph1p  1.00E+00
";

        } elsif (($prevbase1 eq "RGUA") and ($base1 eq "RCYT" or $base1 eq "URA")) {
          printf OUT_NOE "
$prevres1  $prevbase1  H8     $res1  $base1  H6      $h86h86  1.00E+00
$prevres1  $prevbase1  H2\'    $res1  $base1  H6      $h2ph86  1.00E+00
$prevres1  $prevbase1  H3\'    $res1  $base1  H6      $h3ph86  1.00E+00
$prevres1  $prevbase1  H1\'    $res1  $base1  H1\'     $h1ph1p  1.00E+00
$prevres1  $prevbase1  H1\'    $res1  $base1  H5      $h1ph5  1.00E+00
$prevres1  $prevbase1  H2\'    $res1  $base1  H1\'     $h2ph1p  1.00E+00
";

        } elsif (($prevbase1 eq "RADE") and ($base1 eq "RCYT" or $base1 eq "URA")) {
          printf OUT_NOE "
$prevres1  $prevbase1  H8     $res1  $base1  H6      $h86h86  1.00E+00
$prevres1  $prevbase1  H8     $res1  $base1  H5      $h86h86  1.00E+00
$prevres1  $prevbase1  H2\'    $res1  $base1  H6      $h2ph86  1.00E+00
$prevres1  $prevbase1  H3\'    $res1  $base1  H6      $h3ph86  1.00E+00
$prevres1  $prevbase1  H1\'    $res1  $base1  H1\'     $h1ph1p  1.00E+00
$prevres1  $prevbase1  H1\'    $res1  $base1  H5      $h1ph5  1.00E+00
$prevres1  $prevbase1  H2\'    $res1  $base1  H1\'     $h2ph1p  1.00E+00
$prevres1  $prevbase1  H2     $res1  $base1  H1\'     $h2h1p  1.00E+00
";
        }
     }

      if ($base1 eq "RGUA" or $base1 eq "RADE") {
printf OUT_NOE "
#  $base1  $res1
$res1  $base1  H8     $res1  $base1  H2\'     $intra86h2p  1.00E+00
$res1  $base1  H8     $res1  $base1  H3\'     $intra86h3p  1.00E+00
";

    } elsif ($base1 eq "RCYT" or $base1 eq "URA") {
printf OUT_NOE "
#  $base1  $res1
$res1  $base1  H6     $res1  $base1  H2\'     $intra86h2p  1.00E+00
$res1  $base1  H6     $res1  $base1  H3\'     $intra86h3p  1.00E+00
";
   }
      if (($base1 ne "BULGE") and ($base1 ne "STACK")) {
$prevres1 = $res1;
$prevbase1 = $base1;
$prevres2 = $res2;
$prevbase2 = $base2;
   }
}

close(IN);
close(OUT_NOE);
                 
open(IN, "/usr/bin/tac $basepairs|") or die "Rqd args: infile.in outfile.noe\n";
open(OUT_NOE, ">>$noe_upl") or die "filename problem\n";

$prevres2 = "0";
$prevbase2 = "null";
$prevres1 = "0";
$prevbase1 = "null";

while (<IN>) {
   ($base1, $res1, $base2, $res2) = split;
          if ($prevbase2 eq "null") {
          printf OUT_NOE " ";

        } else  {
          if (($prevbase2 eq "RCYT" or $prevbase2 eq "URA") and ($base2 eq "RGUA" or $base2 eq "RADE") and ($prevbase1 ne "BULGE")) {
          printf OUT_NOE "
$prevres2  $prevbase2  H6     $res2  $base2  H8      $h86h86  1.00E+00
$prevres2  $prevbase2  H2\'    $res2  $base2  H8      $h2ph86  1.00E+00
$prevres2  $prevbase2  H3\'    $res2  $base2  H8      $h3ph86  1.00E+00
$prevres2  $prevbase2  H1\'    $res2  $base2  H1\'     $h1ph1p  1.00E+00
";

        } elsif (($prevbase2 eq "RGUA") and ($base2 eq "RGUA" or $base2 eq "RADE") and ($prevbase1 ne "BULGE")) {
          printf OUT_NOE "
$prevres2  $prevbase2  H8     $res2  $base2  H8      $h86h86  1.00E+00
$prevres2  $prevbase2  H2\'    $res2  $base2  H8      $h2ph86  1.00E+00
$prevres2  $prevbase2  H3\'    $res2  $base2  H8      $h3ph86  1.00E+00
$prevres2  $prevbase2  H1\'    $res2  $base2  H1\'     $h1ph1p  1.00E+00
";

        } elsif (($prevbase2 eq "RADE") and ($base2 eq "RGUA" or $base2 eq "RADE") and ($prevbase1 ne "BULGE")) {
          printf OUT_NOE "
$prevres2  $prevbase2  H8     $res2  $base2  H8      $h86h86  1.00E+00
$prevres2  $prevbase2  H2\'    $res2  $base2  H8      $h2ph86  1.00E+00
$prevres2  $prevbase2  H3\'    $res2  $base2  H8      $h3ph86  1.00E+00
$prevres2  $prevbase2  H1\'    $res2  $base2  H1\'     $h1ph1p  1.00E+00
$prevres2  $prevbase2  H2     $res2  $base2  H1\'     $h2h1p  1.00E+00
";

         } elsif (($prevbase2 eq "RCYT" or $prevbase2 eq "URA") and ($base2 eq "RCYT" or $base2 eq "URA") and ($prevbase1 ne "BULGE")) {
          printf OUT_NOE "
$prevres2  $prevbase2  H6     $res2  $base2  H6      $h86h86  1.00E+00
$prevres2  $prevbase2  H6     $res2  $base2  H5      $h86h86  1.00E+00
$prevres2  $prevbase2  H2\'    $res2  $base2  H6      $h2ph86  1.00E+00
$prevres2  $prevbase2  H3\'    $res2  $base2  H6      $h3ph86  1.00E+00
$prevres2  $prevbase2  H1\'    $res2  $base2  H1\'     $h1ph1p  1.00E+00
";

        } elsif (($prevbase2 eq "RGUA") and ($base2 eq "RCYT" or $base2 eq "URA") and ($prevbase1 ne "BULGE")) {
          printf OUT_NOE "
$prevres2  $prevbase2  H8     $res2  $base2  H6      $h86h86  1.00E+00
$prevres2  $prevbase2  H2\'    $res2  $base2  H6      $h2ph86  1.00E+00
$prevres2  $prevbase2  H3\'    $res2  $base2  H6      $h3ph86  1.00E+00
$prevres2  $prevbase2  H1\'    $res2  $base2  H1\'     $h1ph1p  1.00E+00
";

        } elsif (($prevbase2 eq "RADE") and (($base2 eq "RCYT") or ($base2 eq "URA")) and ($prevbase1 ne "BULGE")) {
          printf OUT_NOE "
$prevres2  $prevbase2  H8     $res2  $base2  H6      $h86h86  1.00E+00
$prevres2  $prevbase2  H8     $res2  $base2  H5      $h86h86  1.00E+00
$prevres2  $prevbase2  H2\'    $res2  $base2  H6      $h2ph86  1.00E+00
$prevres2  $prevbase2  H3\'    $res2  $base2  H6      $h3ph86  1.00E+00
$prevres2  $prevbase2  H1\'    $res2  $base2  H1\'     $h1ph1p  1.00E+00
$prevres2  $prevbase2  H2     $res2  $base2  H1\'     $h2h1p  1.00E+00
";
        }
     }
printf OUT_NOE "
#  $base2  $res2 ";

      if ((($base2 eq "RGUA") or ($base2 eq "RADE")) and ($base1 ne "BULGE")) {
printf OUT_NOE "
$res2  $base2  H8     $res2  $base2  H2\'     $intra86h2p  1.00E+00
$res2  $base2  H8     $res2  $base2  H3\'     $intra86h3p  1.00E+00
";

    } elsif ((($base2 eq "RCYT") or ($base2 eq "URA")) and ($base1 ne "BULGE")) {
    printf OUT_NOE "
$res2  $base2  H6     $res2  $base2  H2\'     $intra86h2p  1.00E+00
$res2  $base2  H6     $res2  $base2  H3\'     $intra86h3p  1.00E+00
    ";
 
    } elsif ($base1 eq "BULGE") {
    printf OUT_NOE "
#  BULGE                  FIX RESTRAINTS FOR THIS & ADJANCENT RESIDUES
#                         FIX RESTRAINTS
    ";
    } 
   
    if (($base2 ne "BULGE") and ($base2 ne "STACK")) {
$prevres2 = $res2;
$prevbase2 = $base2;
  }

$prevres1 = $res1;
$prevbase1 = $base1;

}

close(IN);
close(OUT_NOE);

              

