#!/usr/sbin/perl  
# 
# Program to convert a Gaussian98.out file (with XYZ Coordinates) to multiple PDB files.
# (ONLY FIRST 55 (H to Cs) ATOMIC NUMBERS INCLUDED. MUST ADD ENTRY NEAR BOTTOM FOR OTHERS)
#
# usage: g98allpdbs.pl gaussian98.out basename 
#
#   D.E. Volk  05/02/02 University of Texas Medical Branch at Galveston 
#


# Print usage info
if ((@ARGV < 2) || ($ARGV[0] =~ /^-h/))
  {
  print "\n";
  print "\n";
  print "  usage: g98allpdbs.pl gaussian98.out basename \n";
  print "\n";
  print "\n";
  exit(1);
  }




$g98file = shift(@ARGV);
$base = shift(@ARGV);
if (-e "$base") { unlink("$base"); }

open(G98, "$g98file")  || die "Cannot open file $g98file";

$skip = 0;
$atomnumber = 0;
$orientation_counter = 0;
$stopprint = "yes";


# OPEN G98 FILE AGAIN AND GET OUR DATA
#
open(G98, "$g98file")  || die "Cannot open file $g98file";
#
 while (<G98>) {
   chomp;
   s/^\s+//;  #remove leading white spaces 
   $v1='';  # clear variables each time
   $v2='';
   $v3='';
   $v4='';
   $v5='';
   $v6='';
#
#	IF A NEW INPUT ORIENTATION IS FOUND
    if (/Input orientation/) 
#	IF A NEW STANDARD ORIENTATION IS FOUND
#    if (/Standard orientation/) 
      {
      $orientation_counter++;
      $stopprint = "no";
      $skip = 0;
      $atomnumber = 0;
      open(PDB, ">$base$orientation_counter.pdb") || die "Cannot open file $base.$orientation_counter.pdb";
      }
#
#  IF WE HAVE GOTTEN TO A STANDARD ORIENTATION 
#
   ($v1,$v2,$v3,$v4,$v5,$v6) = split('\s+',$_);  #only keep first six numbers
     if ($skip == 0) 		#PRINT HEADER OF PDB FILE ONLY ONCE (SKIP=0)  
        {
         print PDB "TITLE   $base$orientation_counter.pdb generated from Gaussian98 file $g98file\n";
         print PDB "TITLE   by the program g98allpdbs.pl by D.E.VOLK\n";
         print PDB "MODEL        $orientation_counter\n";
        }

#  IF WE HAVE SKIPPED THE STANDARD ORIENTATION LINE AND THE NEXT 4 BUT HAVEN'T
#  REACHED THE ------ LINE (stopprint = "no"), WRITE TO THE PDB


          if ( ($skip >= 5) && ($stopprint !~ /yes/) )
        {
	$atomnumber = $v1;	  	
#
#  CONVERT ATOMIC NUMBERS TO ATOMIC SYMBOLS FOR ATOM NAME
#
        if ($v2 == 1) {$v2 = "H";}  
        if ($v2 == 2) {$v2 = "He";}  
        if ($v2 == 3) {$v2 = "Li";}  
        if ($v2 == 4) {$v2 = "Be";} 
        if ($v2 == 5) {$v2 = "B";} 
        if ($v2 == 6) {$v2 = "C";}
        if ($v2 == 7) {$v2 = "N";}
        if ($v2 == 8) {$v2 = "O";}
        if ($v2 == 9) {$v2 = "F";}
        if ($v2 == 10) {$v2 = "Ne";}
        if ($v2 == 11) {$v2 = "Na";}
        if ($v2 == 12) {$v2 = "Mg";}
        if ($v2 == 13) {$v2 = "Al";}
        if ($v2 == 14) {$v2 = "Si";}
        if ($v2 == 15) {$v2 = "P";}
        if ($v2 == 16) {$v2 = "S";}
        if ($v2 == 17) {$v2 = "Cl";}
        if ($v2 == 18) {$v2 = "Ar";}
        if ($v2 == 19) {$v2 = "K";}
        if ($v2 == 20) {$v2 = "Ca";}
        if ($v2 == 21) {$v2 = "Sc";}
        if ($v2 == 22) {$v2 = "Ti";}
        if ($v2 == 23) {$v2 = "V";}
        if ($v2 == 24) {$v2 = "Cr";}
        if ($v2 == 25) {$v2 = "Mn";}
        if ($v2 == 26) {$v2 = "Fe";}
        if ($v2 == 27) {$v2 = "Co";}
        if ($v2 == 28) {$v2 = "Ni";}
        if ($v2 == 29) {$v2 = "Cu";}
        if ($v2 == 30) {$v2 = "Zn";}
        if ($v2 == 31) {$v2 = "Ga";}
        if ($v2 == 32) {$v2 = "Ge";}
        if ($v2 == 33) {$v2 = "As";}
        if ($v2 == 34) {$v2 = "Se";}
        if ($v2 == 35) {$v2 = "Br";}
        if ($v2 == 36) {$v2 = "Kr";}
        if ($v2 == 37) {$v2 = "Rb";}
        if ($v2 == 38) {$v2 = "Sr";}
        if ($v2 == 39) {$v2 = "Y";}
        if ($v2 == 40) {$v2 = "Zr";}
        if ($v2 == 41) {$v2 = "Nb";}
        if ($v2 == 42) {$v2 = "Mo";}
        if ($v2 == 43) {$v2 = "Tc";}
        if ($v2 == 44) {$v2 = "Ru";}
        if ($v2 == 45) {$v2 = "Rh";}
        if ($v2 == 46) {$v2 = "Pd";}
        if ($v2 == 47) {$v2 = "Ag";}
        if ($v2 == 48) {$v2 = "Cd";}
        if ($v2 == 49) {$v2 = "In";}
        if ($v2 == 50) {$v2 = "Sn";}
        if ($v2 == 51) {$v2 = "Sb";}
        if ($v2 == 52) {$v2 = "Te";}
        if ($v2 == 53) {$v2 = "I";}
        if ($v2 == 54) {$v2 = "Xe";}
        if ($v2 == 55) {$v2 = "Cs";}
#
#  APPEND ATOM NUMBER TO ATOMIC SYMBOL TO CREATE ATOM NAME (V2)
#
        $v2 = $v2.$atomnumber;
        if ($v1 !~ /--------/) 		#If not at ---- line, write to PDB, else kill
           {write PDB;}
         else 
          {
           $stopprint = "yes";
           $skip = 0;
           print PDB "END\n";
           close $base.$orientation_counter.pdb;
          }
        }
     $skip++;  		                  #count lines at orientation
}

close $g98file;

#
format PDB =
ATOM  @#### @<<< HET     1    @###.###@###.###@###.###
      $atomnumber,$v2,$v4,$v5,$v6
.

