#!/bin/bash
#PBS -l walltime=48:00:00
#PBS -l ncpus=16,mem=8gb
#PBS -q uv2000
#PBS -N trio_whole_exome_bcbio
#PBS -j oe

# enable running singletons
if [ -z $PBS_ARRAY_INDEX ]
then
  if [ -z $INDEX ]
  then
    export PBS_ARRAY_INDEX=1
  else
    export PBS_ARRAY_INDEX=$INDEX
  fi
fi

# Expects environment variables to be set
# PROJECT_ID - e.g. 12345_LastnameFirstname
# CONFIG_SH - absolute path to configuration script setting environment variables

source $CONFIG_SH

FAMILY_ID=`head -n $PBS_ARRAY_INDEX $PARAMS_DIR/$PROJECT_ID.family_ids.txt | tail -n 1`

CONFIG_FILE=$CONFIG_DIR/${PROJECT_ID}_${FAMILY_ID}.yaml
mkdir -p $WORK_DIR/$FAMILY_ID
cd $WORK_DIR/$FAMILY_ID

bcbio_nextgen.py $CONFIG_FILE -n 16 -t local

DATE=$(basename `tail log/bcbio-nextgen.log | grep 'Storing in local filesystem' | tail -n 1 | awk '{ print $6 }' | perl -pe "s/_${PROJECT_ID}.+//"`)

if [ -e $OUTPUT_DIR/${DATE}_${PROJECT_ID}_${FAMILY_ID} ]
then
  for INDV in `cut -f 2 $PARAMS_DIR/${PROJECT_ID}_${FAMILY_ID}.ped`
  do
    mv $OUTPUT_DIR/$INDV $OUTPUT_DIR/${DATE}_${PROJECT_ID}_${FAMILY_ID}/
  done

  # fix VCF output file names
  cd $OUTPUT_DIR/${DATE}_${PROJECT_ID}_${FAMILY_ID}
  if [ ! -e ${FAMILY_ID}-gatk-haplotype-annotated.vcf.gz ]
  then
    PREFIX=`echo $FAMILY_ID | cut -d '_' -f 1`
    SUFFIX=`echo $FAMILY_ID | cut -d '_' -f 2`
    mv ${PREFIX}${SUFFIX}-gatk-haplotype-annotated.vcf.gz ${FAMILY_ID}-gatk-haplotype-annotated.vcf.gz
    mv ${PREFIX}${SUFFIX}-gatk-haplotype-annotated.vcf.gz.tbi ${FAMILY_ID}-gatk-haplotype-annotated.vcf.gz.tbi
  fi

else
  echo $OUTPUT_DIR/${DATE}_${PROJECT_ID}_${FAMILY_ID} does not exist.
fi

