#!/usr/bin/perl

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

open(IN, "<$basepairs") or die "Rqd args: filename.in filename.aco\n";
open(OUT_ACO, ">$outfile") or die "Rqd args: bp.in filename.aco\n";

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

while (<IN>) {
   ($base1, $res1, $base2, $res2) = split; 
      if (($base1 eq "RGUA") or ($base1 eq "RADE") or ($base1 eq "RCYT") or ($base1 eq "URA")) {
        if ($prevbase1 eq "null") {
        printf OUT_ACO "
# $base1 $res1
#$res1  $base1   ZETA     -98.0    -48.0  1.00E+00
$res1  $base1   ALPHA    -87.0    -37.0  1.00E+00
$res1  $base1   BETA     155.0    205.0  1.00E+00
$res1  $base1   GAMMA     23.0     73.0  1.00E+00
$res1  $base1   DELTA     58.0    108.0  1.00E+00
$res1  $base1   EPSI    -177.0   -127.0  1.00E+00
";
        } elsif ($base2 eq "BULGE") {
        printf OUT_ACO "

# $prevres1  $prevbase1   DELTA     58.0    108.0  1.00E+00  FIX ME
# $prevres1  $prevbase1   EPSI    -177.0   -127.0  1.00E+00  FIX ME

# $base1 $res1
#$res1  $base1   ZETA     -98.0    -48.0  1.00E+00
#$res1  $base1   ALPHA    -87.0    -37.0  1.00E+00
#$res1  $base1   BETA     155.0    205.0  1.00E+00
#$res1  $base1   GAMMA     23.0     73.0  1.00E+00
#$res1  $base1   DELTA     58.0    108.0  1.00E+00
#$res1  $base1   EPSI    -177.0   -127.0  1.00E+00
";
        } elsif ($prevbase2 eq "BULGE") {
        printf OUT_ACO "
# $base1 $res1
#$res1  $base1   ZETA     -98.0    -48.0  1.00E+00
#$res1  $base1   ALPHA    -87.0    -37.0  1.00E+00
#$res1  $base1   BETA     155.0    205.0  1.00E+00
#$res1  $base1   GAMMA     23.0     73.0  1.00E+00
$res1  $base1   DELTA     58.0    108.0  1.00E+00
$res1  $base1   EPSI    -177.0   -127.0  1.00E+00
";
        } else {
      printf OUT_ACO "
# $base1 $res1
$res1  $base1   ZETA     -98.0    -48.0  1.00E+00
$res1  $base1   ALPHA    -87.0    -37.0  1.00E+00
$res1  $base1   BETA     155.0    205.0  1.00E+00
$res1  $base1   GAMMA     23.0     73.0  1.00E+00
$res1  $base1   DELTA     58.0    108.0  1.00E+00
$res1  $base1   EPSI    -177.0   -127.0  1.00E+00
";
        }
     }
$prevres1 = $res1;
$prevbase1 = $base1;
$prevres2 = $res2;
$prevbase2 = $base2;
}

close(IN);
close(OUT_ACO);

open(IN, "/usr/bin/tac $basepairs|") or die "Rqd args: filename.in filename.aco\n";
open(OUT_ACO, ">>$outfile") or die "Rqd args: bp.in filename.aco\n";

while (<IN>) {
   ($base1, $res1, $base2, $res2) = split;
      if (($base2 eq "RGUA") or ($base2 eq "RADE") or ($base2 eq "RCYT") or ($base2 eq "URA")) {
        if ($prevbase2 eq "null") {
        printf OUT_ACO "
# $base2 $res2
#$res2  $base2   ZETA     -98.0    -48.0  1.00E+00
$res2  $base2   ALPHA    -87.0    -37.0  1.00E+00
$res2  $base2   BETA     155.0    205.0  1.00E+00
$res2  $base2   GAMMA     23.0     73.0  1.00E+00
$res2  $base2   DELTA     58.0    108.0  1.00E+00
$res2  $base2   EPSI    -177.0   -127.0  1.00E+00
";
        } elsif ($base1 eq "BULGE") {
        printf OUT_ACO "

#$prevres2  $prevbase2   DELTA     58.0    108.0  1.00E+00  FIX ME
#$prevres2  $prevbase2   EPSI    -177.0   -127.0  1.00E+00  FIX ME

# $base2 $res2
#$res2  $base2   ZETA     -98.0    -48.0  1.00E+00
#$res2  $base2   ALPHA    -87.0    -37.0  1.00E+00
#$res2  $base2   BETA     155.0    205.0  1.00E+00
#$res2  $base2   GAMMA     23.0     73.0  1.00E+00
#$res2  $base2   DELTA     58.0    108.0  1.00E+00
#$res2  $base2   EPSI    -177.0   -127.0  1.00E+00
";
        } elsif ($prevbase1 eq "BULGE") {
        printf OUT_ACO "
# $base2 $res2
#$res2  $base2   ZETA     -98.0    -48.0  1.00E+00
#$res2  $base2   ALPHA    -87.0    -37.0  1.00E+00
#$res2  $base2   BETA     155.0    205.0  1.00E+00
#$res2  $base2   GAMMA     23.0     73.0  1.00E+00
$res2  $base2   DELTA     58.0    108.0  1.00E+00
$res2  $base2   EPSI    -177.0   -127.0  1.00E+00
";
        } else {
      printf OUT_ACO "
# $base2 $res2
$res2  $base2   ZETA     -98.0    -48.0  1.00E+00
$res2  $base2   ALPHA    -87.0    -37.0  1.00E+00
$res2  $base2   BETA     155.0    205.0  1.00E+00
$res2  $base2   GAMMA     23.0     73.0  1.00E+00
$res2  $base2   DELTA     58.0    108.0  1.00E+00
$res2  $base2   EPSI    -177.0   -127.0  1.00E+00
";
          }
     }
$prevres1 = $res1;
$prevbase1 = $base1;
$prevres2 = $res2;
$prevbase2 = $base2;
}

close(IN);
close(OUT_ACO);
