#! /usr/bin/perl

use IO::File;
use strict;

my $map_file  = $ARGV[0];
my $dico_file = $ARGV[1];

my (%MAP,%DICO,%IDICO);

my $stream = new IO::File($map_file, "r") || die;

while(<$stream>) {
   chomp;
   if (/^(\d+)\t(\S+)\t(\S+)/) {
      $MAP{$1}{$2}=$3;
   }
}

$stream = new IO::File($dico_file, "r") || die;

while(<$stream>) {
   chomp;
   if (/^([^\t]+)\t(.+)$/) {
      $DICO{$1}=$2;
      $IDICO{$2}=$1;
   }
}

foreach my $group (sort {$a<=>$b} keys(%MAP)) {
   foreach my $marker (sort {$MAP{$group}{$a} <=> $MAP{$group}{$b}} keys(%{$MAP{$group}})) {
      my $pos = $MAP{$group}{$marker};
      if ($marker =~ /^sc(.+)$/) {
         $marker = "gsy".$1 if (exists $DICO{"gsy".$1});
	 $marker = "gsy".$1 if (exists $IDICO{"gsy".$1});
	 $marker = "csu".$1 if (exists $DICO{"csu".$1});
	 $marker = "csu".$1 if (exists $IDICO{"csu".$1});
      }
      print $group,"\t",$marker,"\t",$pos,"\n";
   }
}
