#!/usr/bin/perl
#This script outputs .html code that produces a web page using Jmol to display up to 4 isosurfaces simultaneously.
#This script is distributed under the GNU Public License.  You are free to distribute this script to others.
#You may modify the script and add your name to the contributors list at the end of the script.  
#DO NOT REMOVE THE CONTRIBUTORS LIST.

#WARNING the output will be directed to STDOUT.  Thus
#if you wish it to go to a file it must be redirected.

#declare some variables:
#the name of the file containing the atom coordinates
my $moleculefile = ""; 
#a list of filenames for the cube files containing the orbitals
my @orbitalfiles = (); 

#Look at the argument list and decide whether to provide help, interactive mode or read the file list from a file.
my $numarg = @ARGV;
#print "Number of arguments = $numarg\n";
#print "The arguments are: @ARGV\n";
#exit;
my $mode = "";
if ($numarg == 0) {#user needs help
	$mode = "-h";
	} 
elsif ($numarg > 0) {
	$mode = $ARGV[0];
	}
#print "The mode is: $mode\n";
#exit;
#Now check the mode
if ($mode =~ /-i/) { #interactive mode
	print "This will be interactive mode...\n";
	exit;
	}
elsif ($mode =~ /-h/) { #display help
	print "This script outputs .html code that produces a web page using Jmol to display up to 4 isosurfaces simultaneously.\n\n";	
	print "Syntax:  orbtohtml.pl [mode] [filelist]\n";
	print "Mode: -i or -h.\n";
	print "     -i (Not yet working) Starts interactive mode.  Do not include filelist.\n";
	print "     -h Displays this help message.\n";
	print "filelist: either one file name or a list of file names. Do not precede with -i or -h.\n";
	print "     If only one file name is given this script assumes the file contains a list of files\n";
	print "     to be used to construct the html code.\n";
	print "         The first filename should be the file containing the atom coordinates.  Gzipped\n";
	print "         files in any format Jmol can read are recommended.\n";
	print "         Each following line should contain the name of one gaussian cube file (gzipped)\n";
	print "         for each orbital you want to be able to display.  The script will use the\n";
	print "         basename from each file as the orbital name in the menus.\n";
	print "     The same list can be contained on the command line.  The script assumes this is the\n";
	print "     case when more than one entry is found on the command line.\n";
	print "\n";
	print "WARNINGS:\n"; 
	print "1) This script directs all output to the terminal.  To create a file on a *NIX system\n";
	print qq{   you must redirect the output to a file.  This is usually done by typing ">newfilename"\n};
	print "   at the end of the command line.  Where the newfilename is replaced with your file name.\n";
	print "2) You must change the path to the Jmol applet in the generated .html to be correct for your\n";
	print "   server.\n";    
	exit;
	}
elsif ($numarg == 1) {# assume name of file containing list of input files is given.
	open (INFILE, $mode) or die "Can't open $mode: $!";
	#slurp in the file
	my @filelist = <INFILE>;
	close (INFILE); 
	$moleculefile = $filelist[0];
	chomp $moleculefile;
#	print "$moleculefile\n";
	my $numfiles = @filelist;
	my $counter = 1;
	while ($counter < $numfiles) {
		chomp $filelist[$counter];
		@orbitalfiles =(@orbitalfiles,"$filelist[$counter]");
#		print "@orbitalfiles\n";
		$counter = $counter + 1;
		}
#	exit; 
	}
elsif ($numarg > 1) {# assume first argument is atom coordinate file, second is 1rst orbtal file, third is 2nd orbital file, etc...
	$moleculefile = @ARGV[0];
	my $numfiles = $numarg;
	my $counter = 1;
	while ($counter < $numfiles) {
		@orbitalfiles= (@orbitalfiles, $ARGV[$counter]);
		$counter = $counter + 1;
		} 
	}

#now remove the path and extensions from the file names to use as the names of the orbitals.
use File::Basename;
my ($name, $path, $suffix) = ("","",""); #declare as null initially.
my $count = 0;
my @orbnames = @orbitalfiles;  #sets up array of same size.
my $numorb = @orbitalfiles;
while ($count < $numorb){
	($name, $path, $suffix)= fileparse($orbitalfiles[$count], qr{\..*});
	#remove any terminator characters.
	chomp $name;
	chomp $path;
	chomp $suffix;
	$orbnames[$count] = $name;
	$count = $count +1;
	}

print qq{<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n};
print qq{<html>\n};
print qq{<head>\n};
print qq{  <meta content="text/html; charset=ISO-8859-1"\n};
print qq{ http-equiv="content-type">\n};
print qq{  <title>Sufaces using jmol</title>\n};
print qq{  <meta content="J. Gutow" name="author">\n};
print qq{  <meta content="Orbital display using jmol" name="description">\n};
print qq{  <meta\n};
print qq{ content="Page design and layout by J. Gutow 5-2006, page written by perl script orbtohtml.pl"\n};
print qq{ name="details">\n};
print qq{  <meta\n};
print qq{ content="This page is designed to have the text and normal html edited in a standard web design"\n};
print qq{ name="instr1">\n};
print qq{  <meta\n};
print qq{ content="program.  The javascript is to control the jmol applet (see below).  It is best to use"\n};
print qq{ name="instr2">\n};
print qq{  <meta\n};
print qq{ content="the perl scripts to generate the proper java script to allow orbital display.  It is"\n};
print qq{ name="instr3">\n};
print qq{  <meta\n};
print qq{ content="important to have all the gzipped files in the same directory as the .html file.  The"\n};
print qq{ name="instr4">\n};
print qq{  <meta\n};
print qq{ content="files needed are: 1) a gzipped file with the atom coordinates, in any of the formats Jmol"\n};
print qq{ name="instr5">\n};
print qq{  <meta\n};
print qq{ content="can read. 2) a gzipped gaussian cube file for each of the orbitals to be displayed.  To"\n};
print qq{ name="instr6">\n};
print qq{  <meta\n};
print qq{ content="save loading time it seems to be better to avoid including the atom coordinates in the"\n};
print qq{ name="instr7">\n};
print qq{  <meta\n};
print qq{ content="cube files.  It is also recommended that the file names for the surfaces correspond to"\n};
print qq{ name="instr8">\n};
print qq{  <meta\n};
print qq{ content="reasonable names for the orbitals as the file names are used to generate the menu items"\n};
print qq{ name="instr9">\n};
print qq{  <meta content="that appear in the popup menus for selecting orbitals."\n};
print qq{ name="instr10">\n};
print qq{  <meta\n};
print qq{ content="The relative path to Jmol on your server must be correct below!!"\n};
print qq{ name="instr11">\n};
print qq{  <script src="../Applets/Java/Jmol/Jmol.js"></script>\n};
print qq{</head>\n};
print qq{<body>\n};
print qq{<div style="text-align: center;"><big><big><span\n};
print qq{ style="font-weight: bold;">&lt;Replace this text with your title&gt;<br>\n};
print qq{</span></big></big>\n};
print qq{<div style="text-align: left;"><big><big><span\n};
print qq{ style="font-weight: bold;"></span></big></big>&lt;Describe your\n};
print qq{molecule and orbitals here. Don't forget to mention that there is a\n};
print qq{live display below.&nbsp; The user's browser window may be too small to\n};
print qq{display your text and the table containing the Jmol applet without\n};
print qq{scrolling.&gt;<br>\n};
print qq{<br>\n};
print qq{<big><big><span style="font-weight: bold;"></span></big></big>\n};
print qq{<table style="width: 100%; text-align: left;" border="1" cellpadding="2"\n};
print qq{ cellspacing="2">\n};
print qq{  <tbody>\n};
print qq{    <tr>\n};
print qq{      <td style="vertical-align: top;">\n};
print qq{      <script>\n};
print qq{jmolInitialize("../Applets/Java/Jmol");\n};
print qq{jmolApplet(300, "load $moleculefile; spacefill 2%; background white"); \n};
print qq{jmolBr();\n};
print qq{jmolHtml("This image may be rotated and zoomed.  See below for more instructions.");\n};
print qq{        </script>\n};
print qq{      <br>\n};
print qq{      </td>\n};
print qq{      <td style="vertical-align: top;">\n};
print qq{      <div style="text-align: right;"> </div>\n};
print qq{      <form>\n};
print qq{        <div style="text-align: right;"> </div>\n};
print qq{        <table style="width: 100%; text-align: left;" border="0"\n};
print qq{ cellpadding="2" cellspacing="2">\n};
print qq{          <tbody>\n};
print qq{            <tr>\n};
print qq{              <td colspan="1" rowspan="1"\n};
print qq{ style="vertical-align: top; white-space: nowrap; text-align: right;"><span\n};
print qq{ style="font-weight: bold;">Orbital\n};
print qq{1:</span><br>\n};
print qq{              </td>\n};
print qq{              <td style="vertical-align: top;">\n};
print qq{              <script>\n};
print qq{		jmolMenu([\n};
print qq{		['isosurface neg1 delete; isosurface pos1 delete;','none'],\n};
#Loop the following line to make an entry for each orbital.
$count = 0;
while ($count < $numorb){
print qq{		['isosurface neg1 delete; isosurface pos1 delete; isosurface neg1 -0.06 "$orbitalfiles[$count]"; color isosurface red;isosurface pos1 0.06 "$orbitalfiles[$count]";color isosurface blue;',"$orbnames[$count]"],\n};
			$count = $count + 1;
			}
print qq{		]);\n};
print qq{		</script>\n};
print qq{              <br>\n};
print qq{              </td>\n};
print qq{            </tr>\n};
print qq{            <tr>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: right; white-space: nowrap;">Surface\n};
print qq{Type:<br>\n};
print qq{              </td>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: left; white-space: nowrap;">\n};
print qq{              <script>\n};
print qq{		jmolRadioGroup([['isosurface neg1 fill nomesh nodots; isosurface pos1 fill nomesh nodots;','Solid','true'],\n};
print qq{		['isosurface neg1 mesh nofill nodots; isosurface pos1 mesh nofill nodots;', 'Mesh'],\n};
print qq{		['isosurface neg1 dots nomesh nofill; isosurface pos1 dots nomesh nofill;', 'Dot']\n};
print qq{		 ]);\n};
print qq{		</script><br>\n};
print qq{              </td>\n};
print qq{            </tr>\n};
print qq{            <tr>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: right; white-space: nowrap;">Surface\n};
print qq{Color:<br>\n};
print qq{              </td>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: left; white-space: nowrap;">\n};
print qq{              <script>\n};
print qq{		jmolRadioGroup([['isosurface neg1; color isosurface red; isosurface pos1; color isosurface blue;',"blue/red", 'true'],\n};
print qq{		['isosurface neg1; color isosurface yellow; isosurface pos1; color isosurface green;',"green/yellow"],\n};
print qq{		['isosurface neg1; color isosurface cyan; isosurface pos1; color isosurface purple;',"purple/cyan"],\n};
print qq{		['isosurface neg1; color isosurface pink; isosurface pos1; color isosurface brown;',"brown/pink"]\n};
print qq{		]);\n};
print qq{		</script>\n};
print qq{              <br>\n};
print qq{              </td>\n};
print qq{            </tr>\n};
print qq{            <tr>\n};
print qq{              <td colspan="1" rowspan="1"\n};
print qq{ style="vertical-align: top; white-space: nowrap; text-align: right;"><span\n};
print qq{ style="font-weight: bold;">Orbital\n};
print qq{2:</span> </td>\n};
print qq{              <td style="vertical-align: top;">\n};
print qq{              <script>\n};
print qq{		jmolMenu([\n};
print qq{		['isosurface neg2 delete; isosurface pos2 delete;','none'],\n};
#Loop the following line to make an entry for each orbital.
$count = 0;
while ($count < $numorb){
print qq{		['isosurface neg2 delete; isosurface pos2 delete; isosurface neg2 -0.06 "$orbitalfiles[$count]"; color isosurface yellow;isosurface pos2 0.06 "$orbitalfiles[$count]";color isosurface green;',"$orbnames[$count]"],\n};
			$count = $count + 1;
			}
print qq{		]);</script><br>\n};
print qq{              </td>\n};
print qq{            </tr>\n};
print qq{            <tr>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: right; white-space: nowrap;">Surface\n};
print qq{Type:<br>\n};
print qq{              </td>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: left; white-space: nowrap;">\n};
print qq{              <script> \n};
print qq{		jmolRadioGroup([['isosurface neg2 fill nomesh nodots; isosurface pos2 fill nomesh nodots;','Solid','true'],\n};
print qq{		['isosurface neg2 mesh nofill nodots; isosurface pos2 mesh nofill nodots;', 'Mesh'],\n};
print qq{		['isosurface neg2 dots nomesh nofill; isosurface pos2 dots nomesh nofill;', 'Dot']\n};
print qq{		]);\n};
print qq{		</script>\n};
print qq{              <br>\n};
print qq{              </td>\n};
print qq{            </tr>\n};
print qq{            <tr>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: right; white-space: nowrap;">Surface\n};
print qq{Color:<br>\n};
print qq{              </td>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: left; white-space: nowrap;">\n};
print qq{              <script> \n};
print qq{		jmolRadioGroup([['isosurface neg2; color isosurface red; isosurface pos2; color isosurface blue;',"blue/red"],\n};
print qq{		['isosurface neg2; color isosurface yellow; isosurface pos2; color isosurface green;',"green/yellow", 'true'],\n};
print qq{		['isosurface neg2; color isosurface cyan; isosurface pos2; color isosurface purple;',"purple/cyan"],\n};
print qq{		['isosurface neg2; color isosurface pink; isosurface pos2; color isosurface brown;',"brown/pink"]\n};
print qq{		]);\n};
print qq{		</script>\n};
print qq{              <br>\n};
print qq{              </td>\n};
print qq{            </tr>\n};
print qq{            <tr>\n};
print qq{              <td colspan="1" rowspan="1"\n};
print qq{ style="vertical-align: top; white-space: nowrap; text-align: right;"><span\n};
print qq{ style="font-weight: bold;">Orbital\n};
print qq{3:</span><br>\n};
print qq{              </td>\n};
print qq{              <td style="vertical-align: top;">\n};
print qq{              <script>\n};
print qq{		jmolMenu([\n};
print qq{		['isosurface neg3 delete; isosurface pos3 delete;','none'],\n};
#Loop the following line to make an entry for each orbital.
$count = 0;
while ($count < $numorb){
print qq{		['isosurface neg3 delete; isosurface pos3 delete; isosurface neg3 -0.06 "$orbitalfiles[$count]"; color isosurface cyan;isosurface pos3 0.06 "$orbitalfiles[$count]";color isosurface purple;',"$orbnames[$count]"],\n};
			$count = $count + 1;
			}
print qq{		]);\n};
print qq{		</script>\n};
print qq{              <br>\n};
print qq{              </td>\n};
print qq{            </tr>\n};
print qq{            <tr>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: right; white-space: nowrap;">Surface\n};
print qq{Type:<br>\n};
print qq{              </td>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: left; white-space: nowrap;">\n};
print qq{              <script>\n};
print qq{	 	jmolRadioGroup([['isosurface neg3 fill nomesh nodots; isosurface pos3 fill nomesh nodots;','Solid','true'],\n};
print qq{		['isosurface neg3 mesh nofill nodots; isosurface pos3 mesh nofill nodots;', 'Mesh'],\n};
print qq{		['isosurface neg3 dots nomesh nofill; isosurface pos3 dots nomesh nofill;', 'Dot']\n};
print qq{		]);\n};
print qq{		</script>\n};
print qq{              <br>\n};
print qq{              </td>\n};
print qq{            </tr>\n};
print qq{            <tr>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: right; white-space: nowrap;">Surface\n};
print qq{Color:<br>\n};
print qq{              </td>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: left; white-space: nowrap;">\n};
print qq{              <script> \n};
print qq{		jmolRadioGroup([['isosurface neg3; color isosurface red; isosurface pos3; color isosurface blue;',"blue/red"],\n};
print qq{		['isosurface neg3; color isosurface yellow; isosurface pos3; color isosurface green;',"green/yellow"],\n};
print qq{		['isosurface neg3; color isosurface cyan; isosurface pos3; color isosurface purple;',"purple/cyan", 'true'],\n};
print qq{		['isosurface neg3; color isosurface pink; isosurface pos3; color isosurface brown;',"brown/pink"]\n};
print qq{		]);\n};
print qq{		</script>\n};
print qq{              <br>\n};
print qq{              </td>\n};
print qq{            </tr>\n};
print qq{            <tr>\n};
print qq{              <td colspan="1" rowspan="1"\n};
print qq{ style="vertical-align: top; white-space: nowrap; text-align: right;"><span\n};
print qq{ style="font-weight: bold;">Orbital\n};
print qq{4:</span><br>\n};
print qq{              </td>\n};
print qq{              <td style="vertical-align: top;">\n};
print qq{              <script>\n};
print qq{		jmolMenu([\n};
print qq{		['isosurface neg4 delete; isosurface pos4 delete;','none'],\n};
#Loop the following line to make an entry for each orbital.
$count = 0;
while ($count < $numorb){
print qq{		['isosurface neg4 delete; isosurface pos4 delete; isosurface neg4 -0.06 "$orbitalfiles[$count]"; color isosurface pink;isosurface pos4 0.06 "$orbitalfiles[$count]";color isosurface brown;',"$orbnames[$count]"],\n};
			$count = $count + 1;
			}
print qq{		]);\n};
print qq{		</script>\n};
print qq{              <br>\n};
print qq{              </td>\n};
print qq{            </tr>\n};
print qq{            <tr>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: right; white-space: nowrap;">Surface\n};
print qq{Type:<br>\n};
print qq{              </td>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: left; white-space: nowrap;">\n};
print qq{              <script>\n};
print qq{ 		jmolRadioGroup([['isosurface neg4 fill nomesh nodots; isosurface pos4 fill nomesh nodots;','Solid','true'],\n};
print qq{		['isosurface neg4 mesh nofill nodots; isosurface pos4 mesh nofill nodots;', 'Mesh'],\n};
print qq{		['isosurface neg4 dots nomesh nofill; isosurface pos4 dots nomesh nofill;', 'Dot']\n};
print qq{		]);\n};
print qq{	</script>\n};
print qq{              <br>\n};
print qq{              </td>\n};
print qq{            </tr>\n};
print qq{            <tr>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: right; white-space: nowrap;">Surface\n};
print qq{Color:<br>\n};
print qq{              </td>\n};
print qq{              <td\n};
print qq{ style="vertical-align: top; text-align: left; white-space: nowrap;">\n};
print qq{              <script> \n};
print qq{		jmolRadioGroup([['isosurface neg4; color isosurface red; isosurface pos4; color isosurface blue;',"blue/red"],\n};
print qq{		['isosurface neg4; color isosurface yellow; isosurface pos4; color isosurface green;',"green/yellow"],\n};
print qq{		['isosurface neg4; color isosurface cyan; isosurface pos4; color isosurface purple;',"purple/cyan"],\n};
print qq{		['isosurface neg4; color isosurface pink; isosurface pos4; color isosurface brown;',"brown/pink", 'true']\n};
print qq{		]);\n};
print qq{		</script>\n};
print qq{              <br>\n};
print qq{              </td>\n};
print qq{            </tr>\n};
print qq{          </tbody>\n};
print qq{        </table>\n};
print qq{      </form>\n};
print qq{      <small><a href="http://www.java.com">Javascript</a>\n};
$now_time = localtime;
print qq{generated by a perl script (orbtohtml.pl) on $now_time. </small><small><br>\n};
print qq{Original\n};
print qq{page composed by <a href="https://cms.gutow.uwosh.edu/gutow/">J.\n};
print qq{Gutow 4/2006</a>. </small> </td>\n};
print qq{    </tr>\n};
print qq{    <tr>\n};
print qq{      <td colspan="2" rowspan="1" style="vertical-align: top;">\n};
print qq{      <div style="text-align: center;"><span style="font-weight: bold;">Instructions\n};
print qq{for using Jmol to display\n};
print qq{orbitals</span>: <br>\n};
print qq{      </div>\n};
print qq{      <ol>\n};
print qq{        <li>Choose which orbitals to display by selecting them using the\n};
print qq{orbital popup menus.&nbsp; You can control orbital color and fill mode\n};
print qq{by selecting the appropriate options following each orbital menu.&nbsp;\n};
print qq{        </li>\n};
print qq{        <li>ROTATE the image by\n};
print qq{holding\n};
print qq{down the mouse button while moving the cursor over the image.&nbsp; </li>\n};
print qq{        <li>ZOOM by holding down the shift key while moving the cursor\n};
print qq{up\n};
print qq{(decrease magnification) or down (increase magnification) on top of the\n};
print qq{image.&nbsp; </li>\n};
print qq{        <li>Other options are available in the control menu accessible by\n};
print qq{holding\n};
print qq{the mouse button down while the cursor is over "Jmol" in the lower\n};
print qq{right corner (right click also works on a multibutton mouse).&nbsp; </li>\n};
print qq{        <li>For more info about Jmol go to <a target="_blank"\n};
print qq{ href="http://www.jmol.org">www.jmol.org.</a></li>\n};
print qq{      </ol>\n};
print qq{      </td>\n};
print qq{    </tr>\n};
print qq{  </tbody>\n};
print qq{</table>\n};
print qq{</div>\n};
print qq{</div>\n};
print qq{</body>\n};
print qq{</html>\n};

#Contributors list: Jonathan Gutow (original author), 
