if [ -z "$1" ] || [ "$1" = "-h" ] 
then
  echo "joinped.sh:  Takes table of genotypes sent from Genotyper and"
  echo "             joins it to a pedigree file.  The personal IDs must "
  echo "             all be unique."
  echo "             Outputs joinped.ped, and two list of loci, locus_list,"
  echo "             and skel_job."
  echo "usage:       joinped.sh  |clean."
  exit
elif [ "$1" = "clean" ]
then
  test -f joinped.ped && rm -f joinped.ped
  test -f master_id && rm -f master_id
  test -f locus_list && rm -f locus_list
  test -f datafile && rm -f datafile
  test -f skel_job && rm -f skel_job
  exit
elif [ ! -f $1 ] || [ ! -f $2 ]
then
  echo "need to specify pedigree file and genotypes file"
  echo "try joinped.sh -h"
  exit
fi
AWK=/usr/bin/awk

test -f joinped.ped && rm -f joinped.ped
test -f master_id && rm -f master_id
test -f locus_list && rm -f locus_list
test -f datafile && rm -f datafile
test -f skel_job && rm -f skel_job

$AWK '
$1 !~ /[#!]/ { printf $2 " " $1
  for(i=3;i<=NF;i++) {
    printf " " $i
  }
  printf "\n"
}' $1 | sort -k1,1 -k2,2 > key$$.ped


#
# File contains ID locus_name All_1 All_2
#
sort -k2,2 -k1,1 $2 | \
$AWK ' 
BEGIN { locus="dummy"
        G1=3; G2=4
}
NF>1  { if ($2!=locus) {
          close(locus)
          locus=$2
          print locus > "locus_list"
        }
        print $1 > "master_id"
        print $1,$(G1),$(G2) > locus
}'

sort -u < master_id > tmp$$; mv -f tmp$$ master_id

cp master_id datafile
for i in `cat locus_list`
do
	join -a 1 -a 2 -o 2.2,2.3 -e "x" master_id $i > tmp$$; mv -f tmp$$ $i
	paste -d" " datafile $i > tmp$$; mv -f tmp$$ datafile
 	rm -f $i
done
gawk '{print "set loc " $1 " mar"}' locus_list > skel_job

join -a1 key$$.ped datafile > tmp$$
pad tmp$$ | sort -k2,2 -k1,1 | $AWK '
{ printf $2 " " $1
  for(i=3;i<=NF;i++) {
    printf " " $i
  }
  printf "\n"
}' | bester > joinped.ped

rm -f tmp$$ key$$.ped datafile master_id