#!/usr/bin/perl

use strict;
my $bp2cm = 500/120e6;

foreach my $fil ( glob( "allele*.txt") ) {
  open (ALLELE, $fil) || die "Could not open $fil\n";
  my ($chr) = $fil =~ /(\d+)/;
  my $outfile ="chr$chr.MAGIC.alleles";
  open(OUT, ">$outfile") || die "Could not open $outfile\n";
  while(<ALLELE>) {
    chomp;
    s/\s+$//;
    s/\sND\s/ NA /g;
    s/strain\s/strains /;
    my @a = split;
    if ( /^marker\s+/ ) {
      $a[4] = $a[3]*$bp2cm;
      $a[3] = $chr;
    }	
    elsif ( $#a==2 ) {
      @a = ("marker", $a[0], $a[1], $chr, $a[2]*$bp2cm )
    }	
    print OUT join( "\t", @a ) . "\n";
  }
}
