#!/usr/sbin/perl  
# testing DV
# Rebuild a AMBER PDB file into a CHARMM PDB file.
#
# usage: amber_2_charm.pl input.pdb output.pdb
#
#                                B.A. Luxon  01/26/00
$pdbfile = shift(@ARGV);
$outfile = shift(@ARGV);
if (-e "$outfile") {
    unlink("$outfile");
}

open(PDB, "$pdbfile")  || die "Cannot open file $pdbfile";
open(OUT, ">$outfile") || die "Cannot open file $outfile";

foreach (<PDB>) {
   /^TER/ && (print OUT "$_");   # preserved TER
   next unless /ATOM/;	# only do ATOM containing lines
   /\bQ/ && next;
   s/\+/ /;
   s/\bH1  /HT1 /;   # fix N-terminal protons 
   s/\bH2  /HT2 /;
   s/\bH3  /HT3 /;
   s/OXT /OT2 /;	# fix C-terminal Oxy
   s/HIP/HIS/;
   s/HID/HIS/;
   s/\bH   /HN  /;
   s/\bHA3 /HA1 /;
   s/\bHB3 /HB1 /;
   s/\bHG3 /HG1 /;
   s/\bHD3 /HD1 /;
   s/\bHE3 /HE1 /;
   s/\bHG13/HG11 /;
   /TRP/ && s/HE2 /HE3 /;
   /ILE/ && s/CD  /CD1 /;
   chomp;
   ($a,$anum,$anam,$rnam,$rnum,$x,$y,$z) = split('\s+',$_);
   ($anam =~ /1H/) && next;
   write OUT;
}

close $outfile;
close $pdbfile;

#
format OUT =
ATOM  @#### @<<< @<<< @###    @###.###@###.###@###.###
      $anum,$anam,$rnam,$rnum,$x,     $y,     $z
.

