#!/usr/sbin/perl
# Reformat AMBER PDB file to RCSB PDB format
#
# USAGE amber_pdb_to_rcsb.pl infile outfile strandlength
# amber_pdb_to_rcsb.pl 
# 
#                             D.E. VOLK  10/30/2001
$infile = shift(@ARGV);
$outfile = shift(@ARGV);
$length = shift(@ARGV);
#
open(INFILE, "$infile") || die "Cannot open file $infile1";
#
# Remove the old outfile file if it exits:
#
   if (-e "$outfile") {
     unlink("$outfile");
   }
#
open(OUT,">$outfile") || die "Cannot open file $outfile";
#
# Print the outfile file header:
#
#
#	READ THE OLD AMBER FILE 
$i = 0;
$chain="A";
foreach (<INFILE>)
  {
   /^TER/ && (print OUT "$_");   # preserved TER
   /^MODEL/ && (print OUT "$_");   # preserved MODEL
   /^ENDMDL/ && (print OUT "$_");   # preserved ENDMDL
   next unless (/ATOM/ || /HETATM/);  # only do ATOM containing lines
   /\bQ/ && next;
   s/\+/ /;
   s/H2\'1/1H2\'/;         # change atom names 
   s/H2\'2/2H2\'/;
   s/H5\'1/1H5\'/;
   s/H5\'2/2H5\'/;
   s/C7 /C5M/;
   s/H71 /1H5M/;
   s/H72 /2H5M/;
   s/H73 /3H5M/;
   s/H41/1H4/;
   s/H42/2H4/;
   s/H21/1H2/;
   s/H22/2H2/;
   s/ DA /  A /;          #change residue names
   s/ DC /  C /;
   s/ DG /  G /;
   s/ DT /  T /;
   s/DA3/ A /;
   s/DC3/ C /;
   s/DG3/ G /;
   s/DT3/ T /;
   s/DA5/ A /;
   s/DC5/ C /;
   s/DG5/ G /;
   s/DT5/ T /;
   s/\'/\*/;           # last step, change all quotes to stars 
   	chomp;         # get rid of newline
	s/^\s+//;
   	($v1,$v2,$v3,$v4,$v5,$v6,$v7,$v8)= split(/\s+/,$_);
        if ($v1=~/TITLE/) {
        $i++;
        print OUT "MODEL $i\n"; }
        else {
        if ($v5 > $length) {$chain="B"}
	  else {$chain="A"}; 
        write OUT;}
   }
close(OUT);
close(INFILE);
#
format OUT =
@<<<<<@>>>> @<<< @>> @@>>>    @>>>>>>>@>>>>>>>@>>>>>>>
$v1,   $v2, $v3,$v4,$chain,$v5, $v6,    $v7,     $v8    
.
# That's all folks!!
