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


my $output = "";
my $input = "";
my $number = 1;
my $prob = 0;
my $size = 0;
my $trimsize = 1;
my %new_counts = ();

my $version = 0.45;
my $vname = "trim_tags.pl";

if (!$ARGV[0] || $ARGV[0] eq "-h") {
	print   "\n\t-p <0-100>  \t:: set the probability of a tag be trimmed off by one base at the 3' end. This number must be defined within a range of 0 and 100 [default:$prob]\n",
	"\t-n <integer>  \t:: bases to cut [default:$trimsize]",
	"\t-t <integer>  \t:: trim all tags to a defined size <integer> (pb) instead of deleting the 3'end base [default=$size]\n",
	"\t-i <FILE> \t:: input file name[default:STDIN]\n",
	"\t-o <FILE> \t:: output file name[default:STDOUT]\n ",
	"\t-h      \t:: display help information\n",
	"\t-v      \t:: display 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 $argumento = shift(@ARGV)) {
	#
	#probability of cut
	#
	if ($argumento eq "-p") {
		if ($argumento = shift(@ARGV)) {
			$prob = $argumento;
			if ($prob < 0 || $prob > 100) {
				print STDERR "WARNING: Probability of cut out of range [0:100]\n";
			}
		}
	}
	#
	#output file
	#
	elsif ($argumento eq "-o") {
		if ($argumento = shift(@ARGV)) {
			$output = $argumento;
		}
		else {
			print STDERR "ERROR: OUTPUT NOT SPECIFIED!\n";
			exit;
		}
	}
	#
	#trim size	
	#
	elsif ($argumento eq "-n") {
		if ($argumento = shift(@ARGV)) {
			$trimsize= $argumento;
			if ($trimsize <= 0) {
				print STDERR "ERROR: SIZE NOT VALID [1:oo[\n";
				exit;
			}
		}
		else {
			print STDERR "ERROR: SIZE NOT VALID [1:oo[\n";
			exit;
		}
	}
	#
	#size of tags
	#
	elsif ($argumento eq "-t") {
		if ($argumento = shift(@ARGV)) {
			$size= $argumento;
			if ($size <= 0) {
				print STDERR "ERROR: SIZE NOT VALID [1:oo[\n";
				exit;
			}
		}
		else {
			print STDERR "ERROR: SIZE NOT VALID [1:oo[\n";
			exit;
		}
	}

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



if ($input) {
	open INPUT,"<$input" or die "COULD NOT OPEN $input : $!";
	$input = "<INPUT>";
}
else {
	$input = "<>";
}

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


while (my $entry = eval ($input)){
	chomp($entry);
	(my $tag, my $count) = split("=",$entry);
	for(my $i = 1; $i <= $count; $i++){
		my $new_tag;
		if ($size) {
			$new_tag = substr ($tag, 0, $size);
		}
		else {
			if (rand(100)< $prob) {
				$new_tag = substr($tag, 0,-$trimsize);
			}
			else {
				$new_tag = $tag;
			}
		}
		if (exists ($new_counts{$new_tag} )) {
			$new_counts{$new_tag} += 1;
		}
		else{
			$new_counts{$new_tag} = 1;
		}
	}
}

close INPUT;

my $key;
foreach $key (sort (keys(%new_counts))){
	&printout ("$key=" . $new_counts{$key} . "\n");
}

close OUTPUT;

sub printout {
	if ($output) {
		print OUTPUT shift();
	}
	else {
		print shift();
	}
}
