#!/usr/bin/perl
require  "/home/jf/bin/PedUtils.pl";
use Getopt::Std ;  
if (getopts("hs:p:")) {
  
}
else {
  die &usage;
} 


&usage if $opt_h;

$Datfile = $opt_s . ".basepairs";
$Pedfile = $opt_s . ".data";
if (defined($opt_p) ) {
$ProjectName = $opt_p;
}
else {

die "You must define the project name with the -p option\n";
}

open (DAT, $Datfile) || die " Can't open dat file $Datfile \n";
$linecount = 0;

while (<DAT>) {
  chomp;
  
  #  if (/^M/) {
  @data = split;
  $Markers->{$data[1]}= $linecount;
  $linecount++;
  #  }
}

open (PED, $Pedfile) || die " Can't open ped file $Pedfile \n";

while (<PED>) {
  chomp;
  ($family, $id, $m, $p, $gender, $trait, $genotypes  ) = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)$/;
  $Genotypes->{$id} = $genotypes;
}

#print "PROJECT NAME\tSAMPLE NAME\tMARKER NAME\tALLELE 1\tALLELE 2\tALLELE 1 BIN\tALLELE 2 BIN\tQUALITY SCORE\tPLATE NAME\tWELL NAME\n";

foreach $id ( sort mysort keys %{$Genotypes}){
  @data = split(/\s+/,  $Genotypes->{$id});
  foreach $marker ( keys %{$Markers}) {
    $column1 = $Markers->{$marker}*2;
    @data = split(/\s+/,  $Genotypes->{$id});$column2 = $column1 + 1;
    print  "$ProjectName\t$id\t$marker\t\t\t$data[$column1]\t$data[$column2]\t\t\t\n";
  }
}
sub usage {

die "PedToUpload.pl -s <ped file stem> -p <igs project name>
requires a dat file with the same stem name as the data file
e.g. -s c19  means look for c19.data and c19.basepairs \n";
}
