#!/usr/local/bin/perl -w

# Defaults:
$want_residue_names = 1;  # Input correlation matrix file name based on residue?
$want_legend_image  = 1;  # Do we want a legend image?
$want_title         = 1;  # Do we want a title?
$residue_title      = 1;  # Make the title be based on mutation residue
$want_custom_range  = 0;  # Range for colors customized for this data set

# Do command line parsing here:

%title_hash = (
	       'CYS' => 'H16C Mutant of 2END',
	       'SER' => 'H16S Mutant of 2END',
	       'ALA' => 'H16A Mutant of 2END',
	       'ASP' => 'H16D Mutant of 2END',
	       'GLU' => 'H16E Mutant of 2END',
	       'LYS' => 'H16K Mutant of 2END',
	       '1vas-protein' => 'Protein Component of T4PDG/TT-DNA Cocrystal (1VAS)',
	       '2end-protein' => 'T4PDG (2END)',
	       );

if ($want_residue_names) {
  $residue  = $ARGV[0];
  $dfn = "${residue}.correl.matrix.dat";
} else {
  $dfn = $ARGV[0];
}

if ($want_title) {
  if ($want_residue_names && $residue_title) {
    $title = $title_hash{$residue};
  } else {
    $title = "Displacement Correlation";
  }
}

$palette_caption = "Correlation Coefficient";
$labelspacing    = "20";
$griscrfn = "/tmp/myscript.gri";
$gripsfn  = "./heatplot.ps";
$sline = '';
open  DFN, "${dfn}";
while (<DFN>) {
  last if ( $_ =~ /^\s+$/ );
  $sline .= $_;
}
close DFN;

@datary = split " ", $sline;
$dimension = sqrt(scalar @datary);
$dimensionm1 = $dimension - 1.0;

if ($want_custom_range) {
  $smallest =  1.0;
  #$largest  = -1.0;
  foreach $n (0..$#datary) {
    if ($datary[$n] < $smallest) {
      $smallest = $datary[$n];
    }
    #  if ($datary[$n] > $largest)  {$largest  = $datary[$n];}
  }
} else {
  $smallest = -1.0;
}

$griscript = <<EOF;
.lower.   = $smallest
.upper.   = 1.0

.xstart.  = 1
.xinc.    = 1
.xend.    = $dimension
#.xend.    = $dimensionm1

.ystart.  = 1
.yinc.    = 1
.yend.    = $dimension
#.yend.    = $dimensionm1

EOF

$griscript_tot = $griscript;
if ($want_title) {
  $griscript_tot .= "\\tit      = \"${title}\"\n";
}

$griscript = <<EOF;
\\yan      = "Residue Number"
\\xan      = "Residue Number"
\\cap      = "${palette_caption}"
\\palstyle = "%.1f"
.defgl.   = ..graylevel..

.increment.  = 0.2
.paletteinc. = 0.5

open "cat |"
read columns x y z
close
x += .xstart.
y += .ystart.

set tics in
set x size default
set x axis .xstart. .xend. $labelspacing
set x grid .xstart. .xend. .xinc.
set x name "\\xan"

set y size default
set y axis .ystart. .yend. $labelspacing
set y axis decreasing
set y grid .ystart. .yend. .yinc.
set y name "\\yan"
convert columns to grid neighbor

set image range .lower. .upper.
#XXXset image colorscale rgb 0 0 1 .lower. rgb 1 0 0 .upper.
set image colorscale hsb .666 1 1 .lower. hsb 0 1 1 .upper.
#XXXset image grayscale white .lower. black .upper. .increment.
#convert grid to image
convert grid to image size $dimensionm1 $dimensionm1
draw image

EOF

$griscript_tot .= $griscript;

if ($want_legend_image) {
$griscript = <<EOF;
set font size 9
set x format "\\palstyle"
draw image palette left .lower. right .upper. increment .paletteinc.

set font size 9
.key_topleft_x. = 3.425
.key_topleft_y. = -0.9
.dy_inc. = {rpn ..fontsize.. pttocm 1.5 *}
draw label "\\cap" at    \\
    {rpn ..xleft.. xusertocm .key_topleft_x. +} \\
    {rpn ..ytop.. yusertocm .key_topleft_y. -} cm
EOF

  $griscript_tot .= $griscript;
}

if ($want_title) {
  $griscript_tot .= "set font size 12\ndraw title \"\\tit\"\n";
}

open  GRISCRIPT, "> ${griscrfn}";
print GRISCRIPT  $griscript_tot;
close GRISCRIPT;

open GRI, "| gri -output ${gripsfn} ${griscrfn}";
foreach $n ( 0..${dimensionm1} ) {
  foreach $m ( 0..${dimensionm1} ) {
    print GRI "\t", $n, "\t", $m, "\t", shift @datary, "\n";
  }
}
close GRI;
system "ps2epsi ${gripsfn}";

#unlink $griscrfn;

