#!/usr/local/bin/perl -w

BEGIN 
{
	push(@INC, "/Perl/lib");
	#push(@INC, "/Perl/site/lib");
}

use strict;
use FileHandle;
use Cwd;

pymolDisp();

sub pymolDisp
{
	my($currentDir, $line, @path);
	my($structPath, $colorPath, $imagePath, $idx, $arg);
	
	my $pathToDisp = shift;
	my $chain = shift;
	@path = split(/ /, $pathToDisp);
	
	$currentDir = cwd();
   	$structPath = $currentDir . "/struct.pdb";
   	$imagePath = $currentDir . "/data/image.png";
   	$colorPath = $currentDir . "/data/colorpath.py";

	if($chain eq "null")
	{
		open(COLOR, ">data/colorpath.py") || die("Can't open file for writing python script for coloring path in pymol");
		print COLOR "import time\n";
		print COLOR "cmd.bg_color(\"white\")\n";
		print COLOR "cmd.load(\"", $structPath, "\")\n";
		print COLOR "cmd.hide(\"all\")\n";
		print COLOR "cmd.show(\"cartoon\")\n";
		print COLOR "cmd.color(\"gray70\")\n";
		print COLOR "cmd.show(\"spheres\", \"resi ", $path[0], "+", $path[$#path], "\")\n";
		print COLOR "cmd.select(\"path\", \"resi ", join("+", @path), "\")\n";
		print COLOR "cmd.deselect()\n";
		print COLOR "cmd.color(\"teal\", \"path\")\n";
		print COLOR "cmd.color(\"green\", \"resi ", $path[0], "\")\n";
		print COLOR "cmd.color(\"red\", \"resi ", $path[$#path], "\")\n";
		print COLOR "cmd.hide(\"spheres\", \"resn HOH\")\n";
		print COLOR "cmd.png(\"", $imagePath, "\", \"400\")\n";
		print COLOR "cmd.mset(\"1 x360\")\n";
		print COLOR "cmd.util.mroll(\"1\",\"360\",\"1\")\n";
		print COLOR "cmd.mplay()\n";
	}
	else
	{
		open(COLOR, ">data/colorpath.py") || die("Can't open file for writing python script for coloring path in pymol");
		print COLOR "import time\n";
		print COLOR "cmd.bg_color(\"white\")\n";
		print COLOR "cmd.load(\"", $structPath, "\")\n";
		print COLOR "cmd.hide(\"all\")\n";
		print COLOR "cmd.show(\"cartoon\")\n";
		print COLOR "cmd.color(\"gray70\")\n";
		print COLOR "cmd.show(\"spheres\", \"resi ", $path[0], "+", $path[$#path], " & chain ", "$chain", "\")\n";
		print COLOR "cmd.select(\"path\", \"resi ", join("+", @path), " & chain ", "$chain", "\")\n";
		print COLOR "cmd.deselect()\n";
		print COLOR "cmd.color(\"teal\", \"path\")\n";
		print COLOR "cmd.color(\"green\", \"resi ", $path[0], " & chain ", "$chain", "\")\n";
		print COLOR "cmd.color(\"red\", \"resi ", $path[$#path], " & chain ", "$chain", "\")\n";
		print COLOR "cmd.hide(\"spheres\", \"resn HOH\")\n";
		print COLOR "cmd.png(\"", $imagePath, "\", \"400\")\n";
		print COLOR "cmd.mset(\"1 x360\")\n";
		print COLOR "cmd.util.mroll(\"1\",\"360\",\"1\")\n";
		print COLOR "cmd.mplay()\n";
	}


	close(COLOR);

	#$arg = 'tskill pymol';
	#system($arg);
	
	chdir('\Program Files/DeLano Scientific/PyMOL') || die ( "Cannot find directory for Pymol Application.");
	$arg = 'start PyMOL \"' . $colorPath . '\"';
	system($arg);
	chdir($currentDir);

}
	
	