#!/usr/bin/perl
use strict;
use warnings;

my $output = "";
my $input1 = "";
my $input2 = "";
my %count1;
my %count2;
my @fakes = ();
my @list = ();

my $version = 0.32;
my $vname = "compareCounts.pl";

if (!$ARGV[0] || $ARGV[0] eq "-h") {
	print   	"\n\t-o <FILE> \t:: file output prefix to <FILE> [default=$output]\n",
	"\t-ib <FILE> \t:: base input file [default=$input1]\n",
	"\t-it <FILE> \t:: test input file [default=$input2]\n",
	"\t-h      \t:: shows help information\n",
	"\t-v      \t:: shows current version\n",
	"\n\t$vname Version $version\n\n";
	exit;
}

if ($ARGV[0] eq "-v") {
	print ("\n\t$vname Version $version\n\n");
	exit;
}

#
#first read command line to collect arguments
#
while (my $args = shift(@ARGV)) {
	#
	#base input file
	#
	if ($args eq "-ib") {
		if ($args = shift(@ARGV)) {
			$input1 = $args;
		}
		else {
			print STDERR "FIRST COUNT NOT SPECIFIED!\n";
			exit;
		}
	}
	#
	#test input file
	#
	elsif ($args eq "-it") {
		if ($args = shift(@ARGV)) {
			$input2 = $args;
		}
		else {
			print STDERR "SECOND COUNT NOT SPECIFIED!\n";
			exit;
		}
	}
	#
	#output file
	#
	elsif ($args eq "-o") {
		if ($args = shift(@ARGV)) {
			$output = $args;
		}
		else {
			print STDERR "OUTPUT NOT SPECIFIED!\n";
			exit;
		}
	}


}


if ($input1) {
	open INPUT1, "<$input1" or die "COULD NOT OPEN $input1 : $!\n";
}
else {
	print "FIRST COUNT NOT SPECIFIED!\n";
	exit;
}

if ($input2) {
	open INPUT2, "<$input2" or die "COULD NOT OPEN $input2 : $!\n";
}
else {
	print "SECOND COUNT NOT SPECIFIED!\n";
	exit;
}

if (!$output) {
	$output = $input1."report";
}

my $total = 0;

while (my $entry = <INPUT1>) {

	chomp ($entry);

	my ($tag, $times)  = split ("=", $entry);
	$count1{$tag} = $times;
	push (@list, $tag);

	$total += $times;
}

close INPUT1;

while (my $entry = <INPUT2>) {

	chomp ($entry);

	my ($tag, $times)  = split ("=", $entry);
	$count2{$tag} = $times;
	if (!defined $count1{$tag}) {
		push (@list, $tag);
		push (@fakes, $tag);
	}
}

close INPUT2;


open OUTFULL, ">$output-full" or die "COULD NOT OPEN $output-full : $!\n";
open OUTPERCENT, ">$output-perc" or die "COULD NOT OPEN $output-perc : $!\n";
@list = sort (@list);
@fakes = sort (@fakes);

my $som = 0;
my $som2 = 0;
my $s1 = 0;
my $s2 = 0;

foreach my $tag (@list) {
	#
	#prints the full report of maching realtivecounts without any analysis
	#
	my $diff = 0;
	if (defined $count1{$tag}) {
		if (defined $count2{$tag}) {
			print OUTFULL "$tag = $count2{$tag}($count1{$tag}) == ", $count2{$tag} - $count1{$tag}, "\n";
			print OUTPERCENT "$tag=", $count2{$tag} / $count1{$tag}, "\n";
			$diff += $count1{$tag} - $count2{$tag};
			$s1 += $count1{$tag};
			$s2 += $count2{$tag};
		}
		else {
			print OUTFULL "$tag = 0 ($count1{$tag}) == ",  - $count1{$tag}, "\n";
			print OUTPERCENT "$tag=0 \n";
			$diff += $count1{$tag};
			$s1 += $count1{$tag};
		}
	}
	else {
		print OUTFULL "$tag = $count2{$tag}(0) == ", $count2{$tag}, "\n";
		$diff -= $count2{$tag};
		$s2 += $count2{$tag};
	}
	
	$som += $diff;
	$som2 += $diff**2;
}


print OUTFULL "\nTotal Difference = $som [$total]\n";
print OUTPERCENT "\nDifferences = ", $som/$total, "\n";

close OUTFULL;
close OUTPERCENT;

open SUMMARY, ">$output-summary" or die "COULD NOT OPEN $output-summary : $!\n";
print SUMMARY "Comparison $input1 x $input2\n\n";
my $total_s = int(@list) - 1;
print SUMMARY "Total Counts: Base = $s1 Test = $s2\nDifference mean = ", $som / int(@list),"\nStandard Deviation = ", sqrt (($som2 - ($som**2) / $total_s) / $total_s),"\nWith ", int(@fakes), " fakes\n\n";

print "$input2 $s2 ",  $som / int(@list), " ",  sqrt (($som2 - ($som**2) / $total_s) / $total_s), "\n";



#
#print the count of the fake tags
#
open FAKES, ">$output-fakes" or die "COULD NOT OPEN $output-fakes : $!\n";
foreach my $fake (@fakes) {
	print FAKES "$fake=$count2{$fake}\n";
}
close FAKES;


