#!/usr/bin/perl
######################################################
#written by Ian Byrell Stanaway bard@u.washington.edu#
######################################################
use strict;
use warnings;
my %arg;
&parseCommandLine;
my $picked_mips_file = $arg{-picked_mips_file};
my $mip_line_count = 0;
my %rank_character;
my $mip_snp_count = 0;

&get_picked_mips_file;

my $outfile = $picked_mips_file . ".picked_rank_catergories";
open (OUTFILE, ">$outfile");
foreach my $rank_characters (keys %rank_character)
{
	my $count = $rank_character{$rank_characters};
	my $fraction = $count / $mip_line_count;
	print OUTFILE "$fraction\tfraction_$rank_characters\n";
	print OUTFILE "$count\tcount_$rank_characters\n";
}

my $fraction = $mip_snp_count / $mip_line_count;
print OUTFILE "$fraction\tfraction_snps\n";
print OUTFILE "$mip_snp_count\tcount_snps\n";

close OUTFILE;

sub get_picked_mips_file
{
	open (FILE, $picked_mips_file) or die "can't find the mip file list";
	while (<FILE>)
	{
		my $lines = $_;
		if ($lines =~ m/^>|^#|^\n|^\s/g)#header
		{}
		else
		{
			chomp $lines;
			$mip_line_count++;
			#print $mip_line_count . " $lines\n";
			my @line_contents = split(/\s+/, $lines);
			my $rank = $line_contents[1];
			if (exists $rank_character{$rank})
			{
				$rank_character{$rank}++;
			}
			else
			{
				$rank_character{$rank} = 1;
			}
			if ($lines =~ m/snp/g)#header
			{
				$mip_snp_count++;
			}
		}
	}
	close FILE;
}

sub parseCommandLine 
{
    my ( $useage ) = "

-picked_mips_file

";

        for (my $i = 0; $i <= $#ARGV; $i++)
        {
                if ($ARGV[$i] =~ /^-/)
                {
                        $arg{$ARGV[$i]} = $ARGV[$i+1];
                }
        }
        die($useage) if (!($arg{-picked_mips_file}));
}

