#!/usr/bin/perl -w

######################################GENERATE CLUSTER FILES#######################################################
#####                                                                                                         #####
###################################################################################################################

open(LIST, "< Grouped_Blat_Clustered/Buffer_Files/ortho_genes_clean_table.txt"); #no_dpl_list
open(BLATOUT, "< Grouped_Blat_Clustered/Buffer_Files/filtered_extended.blatout");	#blatout_filtered_lengths

$gene="undefined";
$org="undefined";
$found=0;
$counter=0;
$numlines=`wc -l < Grouped_Blat_Clustered/Buffer_Files/ortho_genes_clean_table.txt`;

	###############################GENERATE HITS FROM EXTENDED FILTERED BLATOUT########################################
	#####                                                                                                         #####
	###################################################################################################################

while(<LIST>)
{
	my @columns = split(" ", $_);
	
	for($i=0; $i<scalar(@columns); $i++)
	{
		if($columns[$i] =~ /(\S*\_)(\S*)#/)
		{
			$org=$1;
			$gene=$2;

				while(<BLATOUT>)
				{
					if($_ =~ /$gene\s/)
					{
						open(ORTHOFILE, ">> Grouped_Blat_Clustered/Buffer_Files/Grouped_Blat/dpl_ortho_hits_$counter.blatout");
						print ORTHOFILE $_;
						close(ORTHOFILE);
						$found=1;
					}
				}
				seek(BLATOUT,0,0);
		}
	}

	#################################GENERATE CLUSTER FILES#############################################
	#####                                                                                          #####
	####################################################################################################
	
	if(open(ORTHOFILE, "< Grouped_Blat_Clustered/Buffer_Files/Grouped_Blat/dpl_ortho_hits_$counter.blatout"))
	{

		my @contigs_pl = ("IS_FIRST");
		my @seq_start_pl = (0);
		my @seq_end_pl = (0);
		my @hit_cnt_pl = (0);
		my @hit_list_pl = ("TEST");
		my @ori_list_pl = ("TEST");

		my @contigs_mi = ("IS_FIRST");
		my @seq_start_mi = (0);
		my @seq_end_mi = (0);
		my @hit_cnt_mi = (0);
		my @hit_list_mi = ("TEST");
		my @ori_list_mi = ("TEST");

	while(<ORTHOFILE>)
	{
		my @columns2 = split(" ", $_);

		if($columns2[14]=~/\+/)
		{
			if($contigs_pl[0]=~"IS_FIRST")
			{
				$contigs_pl[0]=$columns2[1];
				$hit_list_pl[0]=$columns2[0];
				$seq_start_pl[0]=$columns2[8];
				$seq_end_pl[0]=$columns2[9];	
				$ori_list_pl[0]=$columns2[14];	
			}
			elsif("@contigs_pl" !~ /$columns2[1]/)
			{
				push(@contigs_pl, $columns2[1]);
				push(@seq_start_pl, $columns2[8]);
				push(@seq_end_pl, $columns2[9]);
				push(@hit_cnt_pl, 0);
				push(@hit_list_pl, $columns2[0]);
				push(@ori_list_pl, $columns2[14]);
			}
		}
		else
		{
			if($contigs_mi[0]=~"IS_FIRST")
			{
				$contigs_mi[0]=$columns2[1];
				$hit_list_mi[0]=$columns2[0];
				$seq_start_mi[0]=$columns2[8];
				$seq_end_mi[0]=$columns2[9];	
				$ori_list_mi[0]=$columns2[14];	
			}
			elsif("@contigs_mi" !~ /$columns2[1]/)
			{
				push(@contigs_mi, $columns2[1]);
				push(@seq_start_mi, $columns2[8]);
				push(@seq_end_mi, $columns2[9]);
				push(@hit_cnt_mi, 0);
				push(@hit_list_mi, $columns2[0]);
				push(@ori_list_mi, $columns2[14]);
			}
		}
	}
	seek(ORTHOFILE,0,0);

	while(<ORTHOFILE>)
	{
		my @columns3 = split(" ", $_);

		if($columns3[14]=~/\+/)
		{
			for($i=0; $i<scalar(@contigs_pl); $i++)
			{
				if($columns3[1]=~$contigs_pl[$i])
				{
					if($columns3[8]<=$seq_start_pl[$i])
					{
						$seq_start_pl[$i]=$columns3[8];
					}
					if($columns3[9]>=$seq_end_pl[$i])
					{
						$seq_end_pl[$i]=$columns3[9];
					}

					$hit_cnt_pl[$i]=($hit_cnt_pl[$i]+1);

					if("$hit_list_pl[$i]" !~ /$columns3[0]/)
					{
						$hit_list_pl[$i]= $hit_list_pl[$i].",".$columns3[0];
					}
				}
			}
		}
		else
		{
			for($i=0; $i<scalar(@contigs_mi); $i++)
			{
				if($columns3[1]=~$contigs_mi[$i])
				{

					if($columns3[8]>=$seq_start_mi[$i])
					{
						$seq_start_mi[$i]=$columns3[8];
					}
					if($columns3[9]<=$seq_end_mi[$i])
					{
						$seq_end_mi[$i]=$columns3[9];
					}

					$hit_cnt_mi[$i]=($hit_cnt_mi[$i]+1);

					if("$hit_list_mi[$i]" !~ /$columns3[0]/)
					{
						$hit_list_mi[$i]= $hit_list_mi[$i].",".$columns3[0];
					}
				}
			}
		}		
	}
	close(ORTHOFILE);	
	
	if($contigs_pl[0]!~"IS_FIRST")
	{
		

		for($i=0; $i<scalar(@contigs_pl); $i++)
		{

			my $pass=1;

			if($hit_cnt_pl[$i]==1)
			{
				my $onehitident = `grep '$hit_list_pl[$i]\t$contigs_pl[$i]' Grouped_Blat_Clustered/Buffer_Files/Grouped_Blat/dpl_ortho_hits_$counter.blatout | awk '{if(\$15 ~ /\+/){print \$14;}}'`;
				chomp($onehitident);
				$onehitident=int($onehitident);
				
				if($onehitident<=50)
				{
					$pass=0;
				}
			}

			if($hit_cnt_pl[$i]>=1 && $pass==1)
			{
				open(ORTHOCLUSTER, ">> Grouped_Blat_Clustered/Cluster/dpl_ortho_cluster_$counter.blatout");
				print ORTHOCLUSTER $hit_cnt_pl[$i]."\t".$contigs_pl[$i]."\t".$seq_start_pl[$i]."\t".$seq_end_pl[$i]."\t".$hit_list_pl[$i]."\t".$ori_list_pl[$i]."\n";
				close(ORTHOCLUSTER);
			}
		}
		
	}

	if($contigs_mi[0]!~"IS_FIRST")
	{
		

		for($i=0; $i<scalar(@contigs_mi); $i++)
		{

			my $pass=1;

			if($hit_cnt_mi[$i]==1)
			{
				#print "DATEI ".$counter." :\n";
				my $onehitident = `grep '$hit_list_mi[$i]\t$contigs_mi[$i]' Grouped_Blat_Clustered/Buffer_Files/Grouped_Blat/dpl_ortho_hits_$counter.blatout | awk '{if(\$15 ~ /\-/){print \$14;}}'`;
				chomp($onehitident);
				$onehitident=int($onehitident);
				#print $onehitident."\n";
				
				if($onehitident<=50)
				{
					$pass=0;
				}
			}

			if($hit_cnt_mi[$i]>=1 && $pass==1)
			{	
				open(ORTHOCLUSTER, ">> Grouped_Blat_Clustered/Cluster/dpl_ortho_cluster_$counter.blatout");
				print ORTHOCLUSTER $hit_cnt_mi[$i]."\t".$contigs_mi[$i]."\t".$seq_start_mi[$i]."\t".$seq_end_mi[$i]."\t".$hit_list_mi[$i]."\t".$ori_list_mi[$i]."\n";
				close(ORTHOCLUSTER);
			}
		}

		
	}
	

	}

	$counter++;
}



close(LIST);
close(BLATOUT);