#######################################################################
# This software has been created by Genome Research Limited (GRL).    # 
# GRL hereby grants permission to use, copy, modify and distribute    # 
# this software and its documentation for non-commercial purposes     # 
# without fee at the user's own risk on the basis set out below.      #
# GRL neither undertakes nor accepts any duty whether contractual or  # 
# otherwise in connection with the software, its use or the use of    # 
# any derivative, and makes no representations or warranties, express #
# or implied, concerning the software, its suitability, fitness for   #
# a particular purpose or non-infringement.                           #
# In no event shall the authors of the software or GRL be responsible # 
# or liable for any loss or damage whatsoever arising in any way      # 
# directly or indirectly out of the use of this software or its       # 
# derivatives, even if advised of the possibility of such damage.     #
# Our software can be freely distributed under the conditions set out # 
# above, and must contain this copyright notice.                      #
#######################################################################

package Crow_MottPlot;
require ByTempl;
require Draw_Contig;

#########################################################################################################
# Grid plot for contig link types and quantities

my @Data;
my $PW;             # (Info window)          
my $cnv; 
my $PF;
my $width = 700;
my $height = 600;
my $scwidth = $width * 2;
my $scheight = $height * 2;
my $PW_vscroll;
my $PW_hscroll;  
my $menuBar_file;
my $menuBar;
my $menuBar_Zoom;
my $status_bar;
my $status_label;
my %ContPairs;

#########################################################################################################
sub Create{

  if(!defined($PW))
    {
	
	$PW = MainWindow->new;	# new window
	$menuBar = $PW->Frame;
	$PF = $PW->Frame;
	$menuBar->pack(-side => 'top', -fill => 'x');
	$status_bar = $PW->Frame(-relief => 'sunken', -borderwidth => '1');
	$status_bar->pack( -side => 'bottom', -fill => 'x', -ipady => '3',  -ipadx => '10', -padx => '10', -pady => '5');


	$status_label= $status_bar->Label (-font => '*times*-r*20*');
	$status_label->pack(-side => 'left', -ipadx => '10');
        ## File Menu

	$menuBar_file = $menuBar->Menubutton(
					     -text      => 'File',
					     -underline => 0,
					     );

	$menuBar_file->command(
			       -label     => 'Print',
			       -underline => 0,
#			       -command   => \&Report,
			       );
	$menuBar_file->separator;
	$menuBar_file->command(
			       -label     => 'Close',
			       -underline => 0,
			       -command   => \&Destroy,
			       );
	$menuBar_file->pack(-side => 'left');
	$menuBar_Zoom=$menuBar->Menubutton(
					   -text     => 'Zoom',
					   -underline => 0,
					   );
	$menuBar_Zoom->command(
			       -label     => 'Zoom In',
			       -underline => 0,
			       -command   => \&ZoomIn,
			       );
	$menuBar_Zoom->command(
			       -label     => 'Zoom Out',
			       -underline => 0,
			       -command   => \&ZoomOut,
			       );
	$menuBar_Zoom->pack(-side => 'left');

	$cnv = $PW->Canvas(
			 -width        => $width,
			 -height       => $height,
			 -relief       => 'sunken',
			 -bd           => 2,
			 -scrollregion => [0, 0, $width *2 , $height*2 ],
			 -xscrollincrement => 10,
			 -yscrollincrement => 20,
			 );
	$PW_vscroll = $PW->Scrollbar(-command => [$cnv => 'yview']);
	$PW_hscroll = $PW->Scrollbar(-command =>  [$cnv => 'xview'], -orient => 'horiz');
	$cnv->configure(-xscrollcommand => [$PW_hscroll => 'set'],-yscrollcommand => [$PW_vscroll => 'set']);
	$PW_vscroll->pack(-side => 'right', -fill => 'y');  
	$PW_hscroll->pack(-side => 'bottom', -fill => 'x');	 
	$cnv->pack(-side => 'top',-expand => 'no',-fill => 'y');
    }
 
}
#########################################################################################################
sub Destroy{
    $cnv->delete('all');
    $PW->destroy;
    undef(@Data);
    undef($PW);
    &main::NoGrid;
}
#########################################################################################################



sub Grid{


&Create if(!defined($PW));
my @Assem = &ByTempl::Get_ContigOrd;

my $x = 50;
my $y = 150;
my $max = $#Assem * 20;
$max = 700 if($max > 700);
$cnv->delete('all');
my $step = int ($max / ($#Assem + 1));
my $c1;
my $c2;

#@Data = &ByTempl::Get_Assem;
@Assem = &ByTempl::Get_ContigOrd;
@Data = grep(($_->[5] == 1), &ByTempl::Get_Assem);

$cnv->create('text',100,110,-text => "\tContigs -> in assembly order ");
$cnv->create('arc',10,10,70,50,-extent => 50, 
		   -fill => 'green',-style => 'pieslice');
$cnv->create('text',200,20,-text => "\tGood (confirmatory) read pairs (or links) ");
$cnv->create('arc',10,60,70,100,-extent => 50, 
		   -fill => 'red',-style => 'pieslice');
$cnv->create('text',200,70,-text => "\tBad(dis-confirmatory) read pairs (or links) ");


foreach (@Assem)
{
    $id = $cnv->create('oval',$x,120,$x+$step,140,-outline => 'black',-fill => 'white');
    $cnv->bind($id,'<Any-Enter>',[\&sbar,$_]);
    $id = $cnv->create('oval',20,$y,40,$y+$step,-outline => 'black',-fill => 'white');
    $cnv->bind($id,'<Any-Enter>',[\&sbar,$_]);
    $x += $step;
    $y += $step;
}

$x = 50;
		 
$y = 150;

my $i;
my $j;
&notifyWait($PW);
for($i=0; $i <= $#Assem; $i++)
{
    $c1 = $Assem[$i];	       
    
    for($j=$i; $j <= $#Assem; $j++)
    {
	$c2 = $Assem[$j];
	my @tData = grep((($_->[13] eq $c1) && ($_->[14] eq $c2)) || (($_->[14] eq $c1) && ($_->[13] eq $c2)) ,@Data); 
	my $g = grep(($_->[8] == 0)&&($_->[6] == 1) && ($_->[7] ==1),@tData);
	my $b = grep((($_->[6] == 0) || ($_->[7] == 0) || ($_->[8] == 1)), @tData); 
	
	&Graph($cnv,$x,$y,$step,$g,$b,($g + $b),$c1,$c2);
	$cnv->update;
	$x += $step;
    }
    $y += $step;
    $x = 50 + ($step * ($i+1));
}				 
	&notifyActive($PW);			# 
}
###########################################################################################################
sub Graph{
my ($cnv,$x1, $y1 , $size , $gnum, $bnum, $maxval,$con1,$con2) = @_;

$id = $cnv->create('rectangle',$x1,$y1,($x1+$size),($y1+$size),-outline => 'black');
$cnv->bind($id,'<Any-Enter>' => [\&sbar,$con1.':=>'.$con2]);

return if ($maxval == 0);
 
my $xcentre = $x1 + (($x1 + $size)/2);
my $ycentre = $y1 + (($y1 + $size)/2);
my $degree = 360 / $maxval;
my $gdeg = $degree * $gnum;
my $bdeg = $degree * $bnum;

$gdeg = 359.8 if($gdeg >= 360);
$bdeg = 359.8 if($bdeg >= 360);
if($gnum > 0)
{
    $id = $cnv->create('arc',$x1+1,$y1+1,($x1+$size)-1,($y1+$size)-1,-extent => $gdeg, 
		       -outline => 'black',-fill => 'green',-style => 'pieslice');
    $cnv->bind($id,'<Any-Enter>',[\&sbar,$con1.':=>'.$con2.'   '.$gnum.' : Good Read Links']);
}
if($bnum > 0)
{
    $id =$cnv->create('arc',$x1+1,$y1+1,($x1+$size)-1,($y1+$size)-1,
		      -extent => -$bdeg, -outline => 'black',-fill => 'red',-style => 'pieslice');
			  $cnv->bind($id,'<Any-Enter>',[\&sbar,$con1.':=>'.$con2.'   '.$bnum.' : Bad Read Links']);
}
}
###########################################################################################################
sub sbar{
    my ($junk,$message)= @_;
   
    $PW->update;
    $status_label->configure(-text => $message);
}
sub ZoomIn{
    $cnv->scale('all',0,0,2,2);
    $scwidth *= 2;
    $scheight *= 2;
    $cnv->configure(-scrollregion => [0, 0, $scwidth  , $scheight ]);
}
sub ZoomOut{
    $cnv->scale('all',0,0,0.5,0.5);
    $scwidth *= 0.5;
    $scheight *= 0.5;
    $cnv->configure(-scrollregion => [0, 0, $scwidth  , $scheight ]);
}
sub notifyWait {
    foreach (@_) {
	$_->configure(-cursor =>'watch');
	$_->update();
    }
}

sub notifyActive {
    foreach (@_) {
	$_->configure(-cursor =>'top_left_arrow');
	$_->update();
    }
}
###########################################################################################################
1;
