#!/usr/bin/perl 
#-w
##############################################################################
# retrieve ID mapping information based on a list
############################################################################
# Usage: retrIDmap id.map specific.id retrieved.GO
# 
############################################ Body of the code ################################

if ($#ARGV != 2)
{
	print "Usage: retrGO.pl id.map specific.id retrieved.GO\n\n";
	exit;
}

print "In progress ...\n";

$idF1 = '';
$idF2 = '';
$outF = '';

$idF1 = $ARGV[0];
$idF2 = $ARGV[1];
$outF = $ARGV[2];

unless( open(IN, $idF2))
{
	print STDERR "Cannot open file $e2gF file\n\n";
    exit;
}
@idA = <IN>;
close IN;

my %idHash = (); # Initalize hash
foreach my $item(@idA)
{
	chomp ($item); # Our input list had an extra \n so couldn't directly input to hash
	@tmp = split(/\t/,$item);
	#$id = substr($tmp[0], 1); 
	$id = $tmp[1]; $id =~ s/\s//;
	$idHash{$id} = 1; # Set key to 1, we need search only values
}

unless(open(IN, $idF1) ) {
print STDERR "Cannot open file $idF1\n\n";
exit;
}
%h2 = ();
while (<IN>)
{
	$line = $_; print $i++."\n";
	chomp ($line);
	@tmpA = split(/\t/, $line);
	#$id1 = substr($tmpA[0], 1);
	$id1 = $tmpA[0]; $id1 =~ s/\s//;
	$go = $tmpA[6];
	if (exists $idHash{$id1})
	{
		$h2{$id1} = $go;
	#	push (@outA, $id1."\t".$go."\n");
	#	next;
	}			
}		
close (IN);

foreach my $item(@idA)
{
	chomp ($item); # Our input list had an extra \n so couldn't directly input to hash
	@tmp = split(/\t/,$item);
	$id = $tmp[1]; $id =~ s/\s//;
	$go = $h2{$id}; 
	push (@outA, $id."\t".$go."\n");
}

open (OUT, ">$outF");
print OUT (@outA);
close OUT;

print "The job is done!!\n\n";
exit;

