use strict;
use warnings;


my $file = $ARGV[0];
my $identityMin = 95; 
my $baseMin = 20; 
my $mismatchMax = 1;

my %hash;      
open(BOWTIE,"<$file") || die;
while (<BOWTIE>)
{
   chomp;
   if (/Reported/)
   {
      next;
   } else
   {
      my ($seqname, $strand, $ref, $start, $seq, $qual, $blah, $mismatches) = split (/\t/, $_);
      $start++;
      my ($tissue, $condition, $seqnumber, $copy_number) = split( /\_/, $seqname);
      my $qlen = length($seq);
      my $stop = $start+length($seq)-1;
      my @mismatches = split (/\,/, $mismatches);
      my $misnum = scalar(@mismatches);
      my $misper = 100*$misnum/$qlen;
      my $pid = 100 - $misper;
      my $match = $qlen - $misnum;
      if ($pid >= $identityMin && $qlen >= $baseMin  && $misnum <=$mismatchMax  )
      {       
	 
	 #printf OUT  join ("\t", $seqname, $chr, sprintf ("%.2f",$pid), $qlen, $match, $misnum, '-' , '-', '-', $start, $stop, "PVALUE", "-", "-", $strand)."\n";
	 if ($strand eq '+')
	 {	    
	    #$seq = &revcom($seq);
	    $hash{$ref}+= $copy_number; #$ref is the name of the element in the database that was aligned to.
	 }
      }	
   }
}
close(BOWTIE);
my @sorted = sort {$hash{$b}<=>$hash{$a}} keys %hash;

foreach my $ref (@sorted)
{
   print $ref."\t".$hash{$ref}."\n";
}
