#!/bin/bash

# Print usage if no argument is given
if [ -z "$1" ]; then
cat <<EOU
Convert pedigree files, i.e., pedindex.{cde,out} and phi2.gz, to CSV.
If supplied, converts also household files, i.e., house.gz, to CSV.
Not a generic script. It applies to pedigree files generated by SOLAR.

Usage:
ped2csv pedindex.cde phi2.gz [house.gz]

_____________________________________
Anderson M. Winkler
Yale University / Institute of Living
Jul/2010
http://brainder.org
EOU
exit
fi

# Get the imputs
cdefile=$1
phifile=$2
pedfile=$(head -n 1 ${cdefile})

# Get the field widths
fw=$(gawk 'NR > 1 {print $1}' ${cdefile} | tr "\n" " ")
names=$(gawk -v FIELDWIDTHS="3 22 28 1" 'NR > 1 { print $3 }' ${cdefile} | tr "\n" " ")

# Read and write the headers
echo ${names} | sed 's/\ /,/g' | sed 's/,BLANK,/,/g' > ${cdefile%.cde}.csv
cat ${pedfile} | gawk -v FIELDWIDTHS="${fw}" -v OFS=',' '{ $1=$1 "" ; print }' | sed 's/,\ ,/,/g' | sed 's/\ //g' >> ${cdefile%.cde}.csv

# Uncompress phi2.gz, keeping the original & convert to CSV
gunzip -c ${phifile} | gawk -vOFS="," '$1=$1' > ${phifile%.gz}.csv

# Uncompress house.gz, keeping the original & convert to CSV
if [[ ${3} != "" ]] && [[ -f ${3} ]] ; then
housefile=$3
gunzip -c ${housefile} | gawk -vOFS="," '$1=$1' > ${housefile%.gz}.csv
fi
