use strict;

my ($in_f1)=@ARGV;
open (infile1, "$in_f1");
open (infile2, "rsEGP.txt");
open (outfile1, ">$in_f1\_temp");

my %rs;
while(<infile2>)
{
	my $line=$_;
        if($line=~m/^#/){}
        else
        {
                if($line=~/notAvailable/){}
                else{
                        chomp($line);my @data=split(/\t/, $line);
                        my @data1=split(/-/, $data[1]);
                        if($#data1==1){}
                        else
                        {
                                my $gene=$data1[0];
                                for(my $i=1;$i<$#data1;$i++){$gene="$gene$data1[$i]";}
                                my $egpnum=$data1[$#data1];
				$data[1]="$gene-$egpnum";
                        }
			my @data2=split(/-/, $data[1]);
			$rs{uc($data2[0])}{$data2[1]}{rs}=lc($data[5]);
			$rs{uc($data2[0])}{$data2[1]}{pos}=lc($data[4]);
                }
        }
}
my $temp=<infile1>;
print outfile1 "$temp";
while(<infile1>)
{
	my $line=$_;chomp($line);$line=uc($line);my @data=split(/,/, $line);
	if(exists $rs{$data[0]}{$data[1]})
	{
		print outfile1 "$data[0],$rs{$data[0]}{$data[1]}{rs},$rs{$data[0]}{$data[1]}{pos}";
		for(my $i=3;$i<$#data;$i++){print outfile1 ",$data[$i]";}
		print outfile1 ",$data[$#data]\n";
	}
}
close(infile1);
close(infile2);
close(outfile1);
rename("$in_f1\_temp", "$in_f1");


