#!/usr/bin/perl -w

use strict;

my %idmap;
open IN, "hsa2cel.entrez.txt" || die "$!";
while(<IN>) {
   chomp;
   my @tem = split(/\t/);
   if($tem[0] =~ /^\d+$/) {
	 $idmap{$tem[0]} = $tem[1];
   }
}
close IN;

my %records;
opendir(DIR,"./");
while(my $file = readdir(DIR)){
   if ($file =~ /.gmt$/) {
      open IN, $file|| die "$!";
      open OUT,">$file.cel" || die "$!";
      while(<IN>){
         chomp;
         my @tem = split(/\t/);
         print OUT $tem[0],"\t",$tem[1];
         for(my $i=2; $i<@tem; $i++) {
           if($tem[$i] =~ /^\d+$/) {
             if(exists $idmap{$tem[$i]}) {
               print OUT "\t".$idmap{$tem[$i]};
             }
           }
         }
         print OUT "\n";
      }
      close IN;
      close OUT;
   }
}
closedir(DIR);
