#!/usr/bin/perl
require ("/home/jf/bin/Utils.pl");


use Getopt::Std;
getopts("hm:a:");
&usage if ($opt_h);

if ($opt_m) {

open (MAP, opt_m) || die "Can't open map file with -m option\n";

}
else {
open (MAP, "/projects/HS_Sequencing/ILLUMINA/BUILD34_GENOTYPES/MarkerMap.Build34" ) ||  die "Can't open map file \n";

}
open (ALLELES, $opt_a) || die "Can't open alleles file with -a option\n";


while (<MAP>) {
chomp;
@data = split;
$Markers->{$data[4]} = $data[3];
$Chromosomes->{$data[4]} = $data[1];
}

while (<ALLELES>) {
chomp;
if (/^marker/) {
undef(@data);
@data = split;
if ($#data == 4) {
$AllelesMarkers->{$data[1]} = $data[4];
}
}
}



#foreach $marker (sort mysort keys %{$AllelesMarkers}) {
#print "Marker $marker\n";
#}

foreach $marker (sort {$AllelesMarkers->{$a} <=> $AllelesMarkers->{$b}} keys %{$AllelesMarkers}) {
  if (exists($Markers->{$marker})) {
    print "$Chromosomes->{$marker}\t$marker\t$Markers->{$marker}\t$AllelesMarkers->{$marker}\n";
  }
  else {
    print "$marker\tMISSING\n";
  }
}

sub usage {
die "MakeBasePairFiles.pl -a alleles file
default map file is in HS_Sequencing
otherwise use -m option\n";
}
