#!/usr/bin/perl
#dlg2trj written by Marty Pagel, IUMVF, 10/23/96
#set for a max of 32 torsions (AutoDock v2.4 only allows ~29 torsions anyway).

$counter = 0;
if ($ARGV[0] eq "") {
        print "\nYou must have a filename.arcs file in this directory.\n";
        print "filename PREFIX: ";
        chop($ARGV[0] = <STDIN>);
}       
$filename = $ARGV[0];

RUNTYPE:
if ($ARGV[1] eq "") {
        print "\nYou must choose Minimum or Last for the run type.\n";
        print "Type Minimum or Last: ";
        chop($ARGV[1] = <STDIN>);
}       
$runtype = $ARGV[1];

if ($runtype eq /Minimum/ || $runtype eq /Last/) {
	print "\nChoose Minimum or Last\n\n";
	goto RUNTYPE;
}
	
$filenamedlg = $filename . ".dlg";
$filenametrj = $filename . ".trj";
if (-f $filenametrj) {
	`rm $filenametrj`;
}
`touch $filenametrj`;

open (DLGFILE, "$filenamedlg");
open (TRJFILE, ">>$filenametrj");
while (<DLGFILE>) {
	chop;
        ($linetype,$energy,$T,$equal1,$coord,$Q,$equal2,$Qval,$deg,$qcoord,$Tor,$equal3,$torvals) = split(' ',$_,13);

	if ($linetype =~ /REMARK/ && $T =~ /active/ && $equal1 =~ /torsions/) {
		print TRJFILE "ntorsions $energy\n";
		print TRJFILE "run 1\n";
		print TRJFILE "cycle 1\n";
		print TRJFILE "temp 500.000000\n";
		$tornum = $energy + 2;
	}


	if ($linetype eq $runtype && $energy =~ /\d/) {
		$counter++;
                ($x, $y, $z) = split(/,/, $coord,3);
                $x =~ s/\(//;
                $z =~ s/\)//;
                ($qx, $qy, $qz) = split(/,/, $qcoord);
                $qx =~ s/\(//;
                $qz =~ s/\)//;
                $qz =~ s/\]//;
                $w =~ s/\[//;
		print TRJFILE "state $counter A $enrgy $energy $x $y $z $qx $qy $qz $Qval\n";
		print "$torvals\n";
		$torvals =~ s/deg/,/;
		$torvals =~ s/ //g;
		print "$torvals\n";
		($tor1,$tor2,$tor3,$tor4,$tor5,$tor6,$tor7,$tor8,$tor9,$tor10,$tor11,$tor12,$tor13,$tor14,$tor15,$tor16,$tor17,$tor18,$tor19,$tor20,$tor21,$tor22,$tor23,$tor24,$tor25,$tor26,$tor27,$tor28,$tor29,$tor30,$tor31,$tor32) = split(/,/,$torvals,32);
print "$tor1 $tor2 $tor3 $tor4 $tor5 $tor6 $tor7 $tor8\n";
		if ($tor1 =~ /\d/) {
			print TRJFILE "$tor1\n";
		}
                if ($tor2 =~ /\d/) {
                        print TRJFILE "$tor2\n";
                }
                if ($tor3 =~ /\d/) {
                        print TRJFILE "$tor3\n";
                }
                if ($tor4 =~ /\d/) {
                        print TRJFILE "$tor4\n";
                }
                if ($tor5 =~ /\d/) {
                        print TRJFILE "$tor5\n";
                }
                if ($tor6 =~ /\d/) {
                        print TRJFILE "$tor6\n";
                }
                if ($tor7 =~ /\d/) {
                        print TRJFILE "$tor7\n";
                }
                if ($tor8 =~ /\d/) {
                        print TRJFILE "$tor8\n";
                }
                if ($tor9 =~ /\d/) {
                        print TRJFILE "$tor9\n";
                }
                if ($tor10 =~ /\d/) {
                        print TRJFILE "$tor10\n";
                }
                if ($tor11 =~ /\d/) {
                        print TRJFILE "$tor11\n";
                }
                if ($tor12 =~ /\d/) {
                        print TRJFILE "$tor12\n";
                }
                if ($tor13 =~ /\d/) {
                        print TRJFILE "$tor13\n";
                }
                if ($tor14 =~ /\d/) {
                        print TRJFILE "$tor14\n";
                }
                if ($tor15 =~ /\d/) {
                        print TRJFILE "$tor15\n";
                }
                if ($tor16 =~ /\d/) {
                        print TRJFILE "$tor16\n";
                }
                if ($tor17 =~ /\d/) {
                        print TRJFILE "$tor17\n";
                }
                if ($tor18 =~ /\d/) {
                        print TRJFILE "$tor18\n";
                }
                if ($tor19 =~ /\d/) {
                        print TRJFILE "$tor19\n";
                }
                if ($tor20 =~ /\d/) {
                        print TRJFILE "$tor20\n";
                }
                if ($tor21 =~ /\d/) {
                        print TRJFILE "$tor21\n";
                }
                if ($tor22 =~ /\d/) {
                        print TRJFILE "$tor22\n";
                }
                if ($tor23 =~ /\d/) {
                        print TRJFILE "$tor23\n";
                }
                if ($tor24 =~ /\d/) {
                        print TRJFILE "$tor24\n";
                }
                if ($tor25 =~ /\d/) {
                        print TRJFILE "$tor25\n";
                }
                if ($tor26 =~ /\d/) {
                        print TRJFILE "$tor26\n";
                }
                if ($tor27 =~ /\d/) {
                        print TRJFILE "$tor27\n";
                }
                if ($tor28 =~ /\d/) {
                        print TRJFILE "$tor28\n";
                }
                if ($tor29 =~ /\d/) {
                        print TRJFILE "$tor29\n";
                }
                if ($tor30 =~ /\d/) {
                        print TRJFILE "$tor30\n";
                }
                if ($tor31 =~ /\d/) {
                        print TRJFILE "$tor31\n";
                }
                if ($tor32 =~ /\d/) {
                        print TRJFILE "$tor32\n";
                }
        }
}
