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

my @Population;
my @PertData;
my @template;
my %ContReads;
my $genum;

##########################################################################################################
sub Populate{
    my ($size,@conts) = @_;
    my @Data = &ByTempl::Get_Assem;
    
    @PertData = grep(($_->[5] == 1)&&($_->[8] == 0),@Data);
    
    undef(@Data);
    foreach $c (@conts){push(@template,$c) if(grep(($_->[13] eq $c) ||($_->[14] eq $c) ,@PertData));}
    @PertData = grep($_->[13] ne $_->[14],@PertData);
    @PertData = sort by_cont(@PertData);
    @template = sort by_contig (@template);
    $genum = $#template + 1;
  
    foreach $c (@template)
    {
	my $right = grep(($_->[13] eq $c) && ($_->[17] < $_->[18]),@PertData);
        my $left = grep(($_->[13] eq $c) && ($_->[17] > $_->[18]),@PertData);
        $right += grep(($_->[14] eq $c) && ($_->[19] < $_->[20]),@PertData);
	$left += grep(($_->[14] eq $c) && ($_->[19] > $_->[20]),@PertData);
	print "$c $left $right \n";
        push(@{$ContReads{$c}}, $left, $right);
	
    }exit;
   
    my ($i);
    my $num;
   
    my %g;

    for($i=0; $i <= $#template; $i++){$g{$i}++;}

    for($i =0; $i < $size ; $i++)
    {
	my @chromosome;
        my @vals = keys(%g); 
	push(@chromosome,0);

        $num = rand;
	while(@vals)
	{ 
	    $num = rand;
	    $temp = splice(@vals,rand($#vals+1),1);
	    if(($num > 0.4999) && ($num != 0)){$temp = RevComp($temp);}
	    push(@chromosome,$temp);
	}
	    
       	push(@Population,\@chromosome);
	
	foreach (@chromosome){ print "$_ $template[abs($_)] ";}
	print "\n"; #
    }
  
    &Fitness;
}
##########################################################################################################
sub Fitness{

    my $lim = &main::GetLim;
    
}
##########################################################################################################
sub PointTo{
  
    my ($fs1,$fs2,$ss1,$ss2) =@_;
    
    if((($fs1 < $fs2) && ($ss1 > $ss2)) ||(($fs1 > $fs2) && ($ss1 < $ss2))){return (1)};
    return(0);
}
##########################################################################################################
sub RevComp{
    my ($cont) =@_;
    my $temp;
    my $cname = $template[abs($cont)];

    if($cont > 0)
    {
	$cont = -1 * $cont; 
	
    }else{
	$cont = abs($cont);
    }
    $temp = $ContReads{$cname}->[0];
    $ContReads{$cname}->[0] = $ContReads{$cname}->[1];
    $ContReads{$cname}->[1] = $temp;
    $cont;       
}
##########################################################################################################
sub by_cont{
    $a->[13] cmp $b->[13];
}
sub by_contig{
    $a cmp $b;
}
sub by_fit{

}
##########################################################################################################
1;


