#!/usr/bin/perl

$basepairs = $ARGV[0];
$ribose_upl = $ARGV[1];
$ribose_lol = $ARGV[2];

open(IN, "<$basepairs") or die "Reqd args: baseprs.in rib.upl rib.lol\n";
open(OUT_UPL, ">$ribose_upl") or die "Reqd args: baseprs.in rib.upl rib.lol\n";
open(OUT_LOL, ">$ribose_lol") or die "Reqd args: baseprs.in rib.upl rib.lol\n";

while (<IN>) {
   ($base1, $res1, $base2, $res2) = split; 
      if (($base2 eq "RGUA") or ($base2 eq "RADE") or ($base2 eq "RCYT") or ($base2 eq "URA")) {

      printf OUT_UPL "
# $base1 $res1
$res1  $base1   C4\'    $res1  $base1  O4\'    1.46  1.00E+00
$res1  $base1   C5\'    $res1  $base1  O4\'    2.42  1.00E+00
$res1  $base1   C5\'    $res1  $base1  C3\'    2.58  1.00E+00
\n
# $base2 $res2
$res2  $base2   C4\'    $res2  $base2  O4\'    1.46  1.00E+00
$res2  $base2   C5\'    $res2  $base2  O4\'    2.42  1.00E+00
$res2  $base2   C5\'    $res2  $base2  C3\'    2.58  1.00E+00
\n";

      printf OUT_LOL "
# $base1 $res1
$res1  $base1   C4\'    $res1  $base1  O4\'    1.44  1.00E+00
$res1  $base1   C5\'    $res1  $base1  O4\'    2.32  1.00E+00
$res1  $base1   C5\'    $res1  $base1  C3\'    2.48  1.00E+00
\n
# $base2 $res2
$res2  $base2   C4\'    $res2  $base2  O4\'    1.44  1.00E+00
$res2  $base2   C5\'    $res2  $base2  O4\'    2.32  1.00E+00
$res2  $base2   C5\'    $res2  $base2  C3\'    2.48  1.00E+00
\n";

     } else {

      printf OUT_UPL "
# $base1 $res1
$res1  $base1   C4\'    $res1  $base1  O4\'    1.46  1.00E+00
$res1  $base1   C5\'    $res1  $base1  O4\'    2.42  1.00E+00
$res1  $base1   C5\'    $res1  $base1  C3\'    2.58  1.00E+00
\n";

      printf OUT_LOL "
# $base1 $res1
$res1  $base1   C4\'    $res1  $base1  O4\'    1.44  1.00E+00
$res1  $base1   C5\'    $res1  $base1  O4\'    2.32  1.00E+00
$res1  $base1   C5\'    $res1  $base1  C3\'    2.48  1.00E+00
\n";

     }
}

close(IN);
close(OUT_RIB);
