#!/bin/bash
#SBATCH --cpus-per-task=1
#SBATCH --mem=2GB
#SBATCH --time=01:00:00
#SBATCH --job-name=gather_shared_results
#SBATCH --output=gather_shared_results.%A_%a.out
#SBATCH --error=gather_shared_results.%A_%a.err



### folder structure for the downstream analysis - created by trio_setup.sh ###
BASE=/home/u035/u035/shared/analysis/work
WORK_DIR=${BASE}/${PROJECT_ID}
NHS_DIR=${WORK_DIR}/${BATCH_NUM}_${VERSION_N}_results



echo "BATCH_NUM = ${BATCH_NUM}"         # the numerical part of the BATCH_ID						e.g. 19650
echo "PLATE_ID = ${PLATE_ID}"           # the PCR plate ID of the batch being currently processed,              	e.g. 19285
echo "PROJECT_ID = ${PROJECT_ID}"       # this the the folder (${BASE}/${PROJECT_ID}) where the downstream analysis will be done
echo "FAMILY_ID = ${FAMILY_ID}"         # the family ID of this family with affected probands
echo "VERSION_N = ${VERSION_N}"         # the version of the alignment and genotyping analysis
echo "JOB_ID = ${JOB_ID}"		# the numerical part of the Job id generated by EPCC when running process_NHS_WES_aff_probands.sh for this family


# check if ${NHS_DIR} already exists - if not, exits and asks to run the standard trio-based analysis first
if [ ! -d "${NHS_DIR}" ]; then
  echo "${NHS_DIR} does not exist - need to run standard trio-based analysis first!!!!"
  exit
fi


# create the family folder for the shared results for this family
FAM_DIR=${NHS_DIR}/${PLATE_ID}_${FAMILY_ID}_shared
if [ -d "${FAM_DIR}" ]; then
  echo "${FAM_DIR} already exists - delete if you want to overwrite!!!!"
  exit
fi
mkdir ${FAM_DIR}


# copy the LOG files
cp ${WORK_DIR}/LOG/process_shared.${JOB_ID}_*.err ${FAM_DIR}
cp ${WORK_DIR}/LOG/process_shared.${JOB_ID}_*.out ${FAM_DIR}


# copy the G2P family html report
cp ${WORK_DIR}/G2P/${PLATE_ID}_${FAMILY_ID}_LOG_DIR/${PLATE_ID}_${FAMILY_ID}.report.html ${FAM_DIR}


# copy all the DECIPHER files for bulk upload
cp ${WORK_DIR}/DECIPHER/*_${FAMILY_ID}_DEC_FLT.csv ${FAM_DIR}
cp ${WORK_DIR}/DECIPHER/*_${FAMILY_ID}_DECIPHER_v10.xlsx ${FAM_DIR}


# copy the variant snapshots
cp ${WORK_DIR}/DECIPHER/IGV/${PLATE_ID}_${FAMILY_ID}/*.png ${FAM_DIR}


# copy the coverage files
cp ${WORK_DIR}/COV/*_${FAMILY_ID}.DD15.COV.txt ${FAM_DIR}
cp ${WORK_DIR}/COV/*_${FAMILY_ID}.REC_SNP_COV.txt ${FAM_DIR}


echo ""
echo ""
echo "OK: Results for ${FAMILY_ID} are stored in ${FAM_DIR}"

