#!/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 $region_to_get = $arg{-region_to_get};

my @region_to_get_contents;
@region_to_get_contents = split(/:|-/, $region_to_get);
my $chr_to_get = $region_to_get_contents[0];
$chr_to_get =~ s/chr//g;
my $start_to_get = $region_to_get_contents[1];
my $stop_to_get = $region_to_get_contents[2];
my %target_to_get;

for my $base_pos ($start_to_get .. $stop_to_get)
{
	$target_to_get{$chr_to_get}{$base_pos} = 0;
}

my $mip_line_count = 0;
my %rank_character;
my $mip_snp_count = 0;

&get_picked_mips_file;



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 $chr = $line_contents[2];
			my $target_start = $line_contents[14];
			my $tartget_stop = $line_contents[15];
			my $trigger = 0;

			if ($chr eq $chr_to_get)
			{
				for my $base_pos ($target_start .. $tartget_stop)
				{
					if (exists $target_to_get{$chr}{$base_pos})
					{
						$trigger = 1;
					}
				}
				if ($trigger == 1)
				{
					print $lines . "\n";
				}
			}
		}
	}
	close FILE;
}

sub parseCommandLine 
{
    my ( $useage ) = "

-picked_mips_file

-region_to_get

";

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

