#!/usr/bin/perl

use strict;

# input: 1:blaster-file
#        2:out-file 
my $mipf = $ARGV[0] or die $!;
my $blasta = $mipf.".blastout";
my $out_f = $blasta.".fa";
my $fastaFile = $mipf.".fa";
my $db_name;    
my $seq;
my $start;
my $end;
my $startalign;
my $endalign;
my $line;
my $viech;
my $mir;
my %doppleHelper; #nicht sehr elegant Oo
open(F, $blasta) or die $!;
open(O,">",$out_f) or die $!;

my %hash; #Speicher um doppelte Typen yu vermeiden

$line=<F>;
while($line) {
  
  #db auslesen	
  chomp($line);
  my %mstarts;
  my %mends;
  my %dir;
  my %mir;
  my %seq;
  my %orga;
  my $n = 0;
  
  
  #Auslesen des Viechs
  $db_name = `echo $line | gawk  '{print \$4}'`; 
  chomp($db_name);
  $viech = `echo $line | gawk  '{print \$2}'`;
  chomp($viech);
  
  while($line=<F>) {
    
    if(index($line,"VIECH",0)!=-1)
      {
	last;
      }
    else 
      {
	chomp($line);
	
	$seq{$n} = `echo '$line' | gawk '{print \$2}'`; 
	chomp($seq{$n});   
	$start = `echo '$line' | gawk '{print \$9}'`; 
	chomp($start);
	$end = `echo '$line' | gawk '{print \$10}'`; 
	chomp($end);
	$startalign = `echo '$line' | gawk '{print \$7}'`; 
	chomp($startalign);
	$endalign = `echo '$line' | gawk '{print \$8}'`; 
	chomp($endalign);
	$mir{$n} = `echo '$line' | gawk '{print \$1}'`; 
	chomp($mir{$n});
	my $orgat=(split(/-/,$mir{$n}))[0];
	chomp($orgat);
	my $Query = `cat $fastaFile | grep -A1 '>$mir{$n}'|grep  -v '>'`;
	my $qlength = length($Query);
	my $percentage= `echo '$line' | gawk '{print \$3}'`; 
	chomp ($percentage);
	my $matchlength=`echo '$line' | gawk '{print \$4}'`;
	chomp($matchlength);


	# annotierte mir gefunden
	if($viech eq $orgat && $percentage > 99 && $matchlength > 70)
	  {
	    #printf "Toll $mir{$n}\n";
	    $orga{$n}=$mir{$n};
	  }
	
	
	if($start < $end)
	  {
	    #print "Strang 1";
	    $start-=$startalign;
	    $end +=$qlength-$endalign;
	    $dir{$n}=1;
	  }
	else
	  {
	   # print "strang 2";
	    my $temp = $start;
	    $start = $end;
	    $end = $temp;		
	    $dir{$n}=2;
	    $start-=$qlength-$endalign;
	    $end +=$startalign;
	  }
	#korrektur
	if($start < 0)
	  {
	    $start = 0;
	  }
	$mstarts{$n}=$start;
	$mends{$n}=$end;
	
	$n++;
   	
	
      }
  }
  $n--;
  
  #loesche doppelte vorkommen
  foreach my $m (sort(keys %mstarts)){
    if(!defined($mstarts{$m})){
      next;
    }
    else
      {
	my $nextloop = $m+1;
	my $middle = ($mstarts{$m}+$mends{$m})/2;
	while($nextloop <= $n)
	  {
	    if($middle > $mstarts{$nextloop} && $middle < $mends{$nextloop})
	      {
	#	print "delete \n";
		delete $mstarts{$nextloop};
		delete $mends{$nextloop};

		if(exists($orga{$nextloop}))
		  {
		    $orga{$m}=$orga{$nextloop};
		  }
	      }
	    
	    $nextloop++;
	  }
      }
    
  }
  #fastacmd ausfuehren fuer verbleibende sequenzen
  foreach my $m (sort(keys %mstarts))
    {
      # print  $m;print $seq{$m};
      my $fastacmd;
      $fastacmd = "fastacmd -d $db_name -s '".$seq{$m}."' -L $mstarts{$m},$mends{$m} -tT -S$dir{$m}";
      print $fastacmd;
    print "\n";
      my $out=`$fastacmd`;
      my $myDNASeq=`echo '$out' | grep -v '>'`;
      my $header= ">".$viech."|".$orga{$m}."|".substr($seq{$m},0,12)."|".$mstarts{$m}."-".$mends{$m}."  ".$seq{$m};
      $header =~ s/Mscaffold/Mc/;
      $header =~ s/contig/cn/;
      if(exists($doppleHelper{$header}))
	{
	  #tue nix
      }
      else
	{
	  $doppleHelper{$header}=1;
	  print O $header."\n".$myDNASeq;
	} 
      
    }
}
close(F);
close(O);
#search for more duplicates




