#!/usr/bin/perl -w

# 
# Usage:  pdbcat_1letR.pl <in_stem> <in_start_no> <in_end_no> <in_step> <file_out> <model_start_no> <model_step>
#
#  Outpur <file_out>
#
#  This script reads a seies of pdb files in the amber dialect and rewrites them in standard pdb. 
#  Each file is converted to a model, all the models are concatenated into a single file. 
#  The output file has no header or END line.  This allows files to be concatenated, if necessary.
#  The names of the files to be read are assumed to have the form <in_stem>.#
#      <in_stem>         Constant part of the pdb file to be read (don't include dot)
#      <in_start_no>     The number of the first pdb file to be read
#      <in_end_no>       The number of the last  pdb file to be read
#      <in_step>         The step size between files to be read
#      <file_out>        The name of the single output file.
#      <model_start_no>  The number of the first model 
#      <model_step>      The size of the step between model numbers.
#
#  Mike Bruist  July 14, 2008
#
#

if (@ARGV != 7) {
  die
  "1 Usage:  pdbcat_1letR.pl <in_stem> <in_start_no> <in_end_no> <in_step> 
     <file_out> <model_start_no> <model_step>\n";
}     # end if (@ARGV

# collect arguments

$stem       = $ARGV[0];
$n          = $ARGV[1];
$fileEnd    = $ARGV[2];
$fileStep   = $ARGV[3];
$outFile    = $ARGV[4];
$m          = $ARGV[5];
$modelStep  = $ARGV[6];

open(OUT, ">$outFile") || die "2 Cannot create $outFile\n";

while ( $n <= $fileEnd ) {
  printf OUT "MODEL %8u  \n", $m;
  open(PDB, $stem.".".$n) || die "3 Cannot open $stem",".$n\n";
  @pbd = <PDB>;

  foreach (@pbd) {
    if (m/^ATOM/) {
      $_ =~ s/RA[35] |RA  /  A /;
      $_ =~ s/RC[35] |RC  /  C /;
      $_ =~ s/RG[35] |RG  /  G /;
      $_ =~ s/RU[35] |RU  /  U /;
      print OUT $_;
    }         #  end if (m/^ATOM/)
  }   #  end foreach (@pbd)

  print OUT "TER\n";
  print OUT "ENDMDL \n";
  $n += $fileStep;
  $m += $modelStep;
}       #  end while
