#!/usr/local/bin/perl
#
#This script accepts 2 arguments, range and file
#range is the amplitute around the ideal long sage concatamer size of 34
#that tags will be checked for.(eg. range 5 will check from concatamers
#with 29 bases to concatamers with 39 bases)
#
#


$range = 1;
$imput = "";
$output = "";

my $version = 0.2;
my $vname = "statConcat.pl";


if (!$ARGV[0] || $ARGV[0] eq "-h") {
	print  "\n\t-a <integer>\t\t:: defines the amplitude to use [default=$range]\n",
	"\t-i <FILE> \t\t:: input file name [default=STDIN]\n",
	"\t-o <FILE> \t\t:: output file name [default=STDOUT]\n",
	"\t-h      \t\t:: shows help information\n",
	"\t-v      \t\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 $arg = shift(@ARGV)) {
	#
	#range, amplitude
	#
	if ($arg eq "-a") {
		if ($arg = shift(@ARGV)) {
			$range = $arg;
			if ($range < 0) {
				print STDERR "ERROR: INVALID AMPLITUDE\n";
			}
		}
		else {
			$range = 0;
		}
	}

	#
	#output file
	#
	elsif ($arg eq "-o") {
		if ($arg = shift(@ARGV)) {
			$output = $arg;
		}
		else {
			print STDERR "ERROR: OUTPUT FILE NOT SPECIFIED\n";
			exit;
		}
	}

	#
	#input file
	#
	elsif ($arg eq "-i") {
		if ($arg = shift(@ARGV)) {
			$input = $arg;
		}
		else {
			print STDERR "ERROR: INPUT FILE NOT SPECIFIED\n";
			exit;
		}
	}
}

if ($output) {
	open OUTPUT, ">$output" or die "COULD NOT OPEN $output : $!";
}   

print "Window\tGood\tJoin\tSmall\tLarge\tUnique\tKeyConflicts\tTagConflicts\tTotalCount\tGoodCount\n";
for ($i = 0; $i <= $range; $i++){
	$start = 34 - $i;
	$end = 34 + $i;
	#
	#first generate ditags and statitistics on concatamers
	#
	print STDERR "concatemers2.pl -min $start -max $end -i $file -o out$start$end -r erro$start$end\n" ;
	system("concatemers2.pl -min $start -max $end -i $file -o out$start$end -r erro$start$end\n");
	system("grep GOOD erro$start$end | wc > temp");
	system("grep JOIN erro$start$end | wc >> temp");
	system("grep BAD erro$start$end | grep Small |wc >> temp");
	system("grep BAD erro$start$end | grep Large |wc >> temp");
	open(TEMP,"temp");
	$ln = <TEMP>;
	($trash, $good) = split(/[\s\t]+/,$ln);
	$ln = <TEMP>;
	($trash, $join) = split(/[\s\t]+/,$ln);
	$ln = <TEMP>;
	($trash,$small) = split(/[\s\t]+/,$ln);
	$ln = <TEMP>;
	($trash,$large) = split(/[\s\t]+/,$ln);
	close(TEMP);
	#
	#now compute ditags and print data
	#
	system("tags.pl ".($start/2)." < out$start$end > tags$start$end 2> tagsStat$start$end");
	open (TEMP,"tagsStat$start$end");
	$/= "====\n";
	my $data = <TEMP>;
	chomp($data);
	close(TEMP);
	$/= "\n";
	my @dataLines = split("\n",$data);
	my $lines = @dataLines;
	($trash,my $unique) = split("=",$dataLines[$lines-2]);
	($trash,my $conflicts) = split("=",$dataLines[$lines-1]);
	(my $keysInConflict,my $tagsInConflict,my $totalTags, my $totalGoodTags) = split(",",$conflicts);
	&printout ("$start"."x"."$end\t$good\t$join\t$small\t$large\t$unique\t$keysInConflict\t\t$tagsInConflict\t\t$totalTags\t\t$totalGoodTags\n");
	print STDERR "good=$good;bad=$bad;join=$join\n";

}


sub printout {
	if ($output) {
		while (my $p = shift()) {
			print OUTPUT $p;
		}
	}
	else {
		while (my $p = shift()) {
			print $p;
		}
	}
}


