#!/usr/sbin/perl  
# 
# Program to convert a GaussView .com file (with XYZ Coordinates) to a PDB.
#
# usage: gvxyzcom_2_pdb.pl input.com output.pdb
#
#                                D.E. Volk  01/03/02
$comfile = shift(@ARGV);
$pdbfile = shift(@ARGV);
if (-e "$pdbfile") {
    unlink("$pdbfile");
}

open(COM, "$comfile")  || die "Cannot open file $comfile";
open(PDB, ">$pdbfile") || die "Cannot open file $pdbfile";

$blankline_counter = 0;
$skip = 0;
$atomnumber = 0;

while (<COM>) {
   chomp;
   s/^\s+//;  #remove leading white spaces 
   $v1='';  # clear variables each time
   $v2='';
   $v3='';
   $v4='';
   ($v1,$v2,$v3,$v4) = split('\s+',$_);  #only keep first four numbers
#   print "$v1 $v2 $v3 $v4 \n";  LINE FOR TESTING ONLY
#
#  gaussview fields are seperated by blank lines, there are 2 before mult.,charge, then xyz data
#
   if ($v1 !~ /\S/)   # if v1 is not a non-white space character (if v1 is whitespace)
     {
     $blankline_counter++;    #Tracking blank lines which seperate fields in .com file
     }
   if ($blankline_counter == 2)   #Blank line before mult./charge line 
      {
       print PDB "TITLE   $pdbfile generated from GaussView file $comfile\n";
       print PDB "TITLE   by the program gvxyz_2_pdb.pl by D.E.VOLK\n";
       print PDB "MODEL        1\n";
       $skip = 1;                 #We need to skip the multiplicity/charge/monomer line!
       $blankline_counter = 3;    # Increas blankline_counter so TITLE printed only once
      }

#
#   Have we skipped enough lines to get to correct section, and skipped the first line of it
#   Skip =1 at second blank line, 2 at mult,charge line, and 3 at first XYZ data line
#
   if ( ($blankline_counter == 3) && ($skip >= 3) && ($v1 =~ /\S/) )
    {
     $atomnumber = $skip -2;
     if ($v1 == 1) {$v1 = "H";}  #Convert atomic number to atomic symbol:atomnum
     if ($v1 == 6) {$v1 = "C";}
     if ($v1 == 7) {$v1 = "N";}
     if ($v1 == 8) {$v1 = "O";}
     if ($v1 == 11) {$v1 = "Na";}
     if ($v1 == 12) {$v1 = "Mg";}
     if ($v1 == 15) {$v1 = "P";}
     if ($v1 == 16) {$v1 = "S";}
     if ($v1 == 19) {$v1 = "K";}
     if ($v1 == 20) {$v1 = "Ca";}
     $v1 = $v1.$atomnumber;
     write PDB;
    }
   if ($blankline_counter >= 2) {$skip++;}
}
print PDB "END\n";

close $pdbfile;
close $comfile;

#
format PDB =
ATOM  @#### @<<< HET A   1    @###.###@###.###@###.###
      $atomnumber,$v1,$v2,$v3,$v4
.

