#######################################################################
# 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_Quality;

require ByTempl;

my %g;
my %b;
my %n;
my @gd;
my @bd;
my @nrm;
my $D;

##################################################################################################
sub Get_Good{
    %g;
}
sub Get_Bad{
    %b;
}
sub Get_Norm{
    %n;
}
###################################################################################################
sub Quality{

    my ($max,$xxt,$Data) = @_;
  
    $D = $Data;
    %CR = &ByTempl::Get_CR;
    # works on assembly summary table.....
    # only need about 500 points.
 
    print "Crow : Calculating Quality \n";
   
    my $pnum = $xxt/5;
    my $div = $max / 150;
    my $inc = int($max / $div);

    my $i;
    undef (@gd);
    undef (@bd); 
    undef (@nrm);
    &GoodQual($max,$inc);

    &NormQual($max,$inc);

    &BadQual($max,$inc);
   

    %g = @gd;
    %b = @bd;
    %n = @nrm;

#    my $t2 = time() - $t;
#    print "time = $t2 \n";
    undef (@gd);
    undef (@bd);
    undef (@nrm);
}
######################################################################################################
sub GoodQual{
    my ($max,$inc) = @_;

    my @gData = grep((($_->[5] == 1)&&($_->[8] == 0)&&($_->[6] == 1) && ($_->[7] ==1)),@{$D});

    my $i;  
    my $num;
    my $done;
    my $cLne =0;
    my $strt = 0;
    my @cData;
    my $cnt =0;
    my $cont;

    @gData = sort by_ls1 @gData;
  
    for($i=0; $i < $max; $i+= $inc)
    {
       	$num = grep(((($_->[9] <= $i) || ($_->[11] <= $i)) && (($_->[10] > $i) || ($_->[12] > $i))),@gData[0..30]);
	push(@gd,$i,$num);

	$cnt += $num;
	if(($cnt > 30) && ($#gData > 30))
	{
	    $done = grep((($_->[11] < $i) && ($_->[12] < $i)),@gData);
	    splice(@gData,0,$done) if($done > 1);
	    $cnt =0;
	}
    }
   
}
######################################################################################################
sub BadQual{
    my ($max,$inc) = @_;
#    my @bData = grep((($_->[5] == 1) && (($_->[8] == 1) || ($_->[6] == 0) || ($_->[7] ==0))),@{$D});
    my @bData = grep(($_->[5] == 1) && ($_->[8] == 1),@{$D});
    my $i;
    my $num;
    my $done;
    my $cnt =0;
    my @cData;

    @bData = sort by_ls1 @bData;
  
    for($i=0; $i < $max; $i+= $inc)
    {
	$num = grep(((($_->[9] <= $i) || ($_->[10] <= $i)) && (($_->[9] > $i) || ($_->[10] > $i)))
		    ||((($_->[11] <= $i) || ($_->[12] <= $i)) && (($_->[11] > $i) || ($_->[12] > $i))) 
		    ,@bData[0..30]);
	$cnt += $num;
	if(($cnt > 30) && ($#bData > 30))
	{
	    $done = grep((($_->[11] < $i) && ($_->[12] < $i)) ||(($_->[9] < $i) && ($_->[10] < $i)),@bData);
	    splice(@bData,0,$done);
	    $cnt =0;
	}
	push(@bd,$i,$num);
    }
    
}
#####################################################################################################################
sub NormQual{

  my ($max,$inc) = @_;
  my @nData = grep(($_->[5] == 0) ,@{$D});
  my $i;
  my $size = $#nData;
  my $num;
  my $done;
  my $cnt =0;
  my @cData;
 
  @nData = sort by_ls1 @nData;
				# 
  for($i=0; $i < $max; $i += $inc)
  {
      $num = grep((($_->[9] <= $i) || ($_->[10] < $i)) && (($_->[12] >= $i) || ($_->[11] >= $i))
		    ,@nData[0..50]);
      $cnt += $num;
      if(($cnt > 50) && ($#nData > 50))
      {
	  $done = grep(($_->[11] < $i) && ($_->[12] < $i),@nData);
	  splice(@nData,0,$done);
	  $cnt =0;
      }
    #  $n{$i} = $num;
   #   print "$i $num \n";
      push(@nrm,$i,$num);
  }
  


}
######################################################################################################
sub by_ls1{
    $a->[9]<=>$b->[9];
}

1;
