#!/bin/bash
#SBATCH --cpus-per-task=1
#SBATCH --mem=2GB
#SBATCH --time=01:00:00
#SBATCH --job-name=gather_quad_results
#SBATCH --output=gather_quad_results.%A_%a.out
#SBATCH --error=gather_quad_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
echo "PLATE_ID = ${PLATE_ID}"           # the PCR plate ID of the batch being currently processed,                      e.g. 16862
echo "PROJECT_ID = ${PROJECT_ID}"       # this the the folder (${BASE}/${PROJECT_ID}) where the downstream analysis will be done
echo "VERSION_N = ${VERSION_N}"         # the version of the alignment and genotyping analysis
echo "FAMILY_ID = ${FAMILY_ID}"         # the family ID of this family with affected probands
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
echo "KID_1_ID = ${KID_1_ID}"
echo "KID_2_ID = ${KID_2_ID}"


# check if ${NHS_DIR} already exists - if not, exit and ask to be created
if [ ! -d "${NHS_DIR}" ]; then
  echo "${NHS_DIR} does not exist - need to create it before running this script!!!!"
  exit
fi



# create the family folder for the results
FAM_DIR=${NHS_DIR}/${PLATE_ID}_${FAMILY_ID}
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_quad.${JOB_ID}_*.err ${FAM_DIR}
cp ${WORK_DIR}/LOG/process_quad.${JOB_ID}_*.out ${FAM_DIR}


# copy the G2P html reports for the two trios and the affected sib-pair
cp ${WORK_DIR}/G2P/${PLATE_ID}_${FAMILY_ID}_${KID_1_ID}_LOG_DIR/${PLATE_ID}_${FAMILY_ID}_${KID_1_ID}.report.html ${FAM_DIR}
cp ${WORK_DIR}/G2P/${PLATE_ID}_${FAMILY_ID}_${KID_2_ID}_LOG_DIR/${PLATE_ID}_${FAMILY_ID}_${KID_2_ID}.report.html ${FAM_DIR}
cp ${WORK_DIR}/G2P/${PLATE_ID}_${FAMILY_ID}_shared_LOG_DIR/${PLATE_ID}_${FAMILY_ID}_shared.report.html ${FAM_DIR}


# copy (VASE) de novo variants in each proband VCF file
cp ${WORK_DIR}/VASE/${PLATE_ID}_${FAMILY_ID}_${KID_1_ID}.ready.denovo.vcf ${FAM_DIR}
cp ${WORK_DIR}/VASE/${PLATE_ID}_${FAMILY_ID}_${KID_2_ID}.ready.denovo.vcf ${FAM_DIR}


# copy the DECIPHER files for bulk upload
cp ${WORK_DIR}/DECIPHER/${KID_1_ID}_${FAMILY_ID}_DEC_FLT.csv ${FAM_DIR}
cp ${WORK_DIR}/DECIPHER/${KID_1_ID}_${FAMILY_ID}_DECIPHER_v10.xlsx ${FAM_DIR}
cp ${WORK_DIR}/DECIPHER/${KID_2_ID}_${FAMILY_ID}_DEC_FLT.csv ${FAM_DIR}
cp ${WORK_DIR}/DECIPHER/${KID_2_ID}_${FAMILY_ID}_DECIPHER_v10.xlsx ${FAM_DIR}
cp ${WORK_DIR}/DECIPHER/${KID_1_ID}_${FAMILY_ID}_shared_DEC_FLT.csv ${FAM_DIR}
cp ${WORK_DIR}/DECIPHER/${KID_1_ID}_${FAMILY_ID}_shared_DECIPHER_v10.xlsx ${FAM_DIR}
cp ${WORK_DIR}/DECIPHER/${KID_2_ID}_${FAMILY_ID}_shared_DEC_FLT.csv ${FAM_DIR}
cp ${WORK_DIR}/DECIPHER/${KID_2_ID}_${FAMILY_ID}_shared_DECIPHER_v10.xlsx ${FAM_DIR}


# copy the variant snapshots
IGV_SNAP_DIR=${FAM_DIR}/IGV_snapshots
mkdir ${IGV_SNAP_DIR}
cp -r ${WORK_DIR}/DECIPHER/IGV/${PLATE_ID}_${FAMILY_ID}_${KID_1_ID} ${IGV_SNAP_DIR}
cp -r ${WORK_DIR}/DECIPHER/IGV/${PLATE_ID}_${FAMILY_ID}_${KID_2_ID} ${IGV_SNAP_DIR}
cp -r ${WORK_DIR}/DECIPHER/IGV/${PLATE_ID}_${FAMILY_ID}_shared ${IGV_SNAP_DIR}


# copy proband coverage files
cp ${WORK_DIR}/COV/${KID_1_ID}_${FAMILY_ID}.DD15.COV.txt ${FAM_DIR}
cp ${WORK_DIR}/COV/${KID_2_ID}_${FAMILY_ID}.DD15.COV.txt ${FAM_DIR}
cp ${WORK_DIR}/COV/${KID_1_ID}_${FAMILY_ID}.REC_SNP_COV.txt ${FAM_DIR}
cp ${WORK_DIR}/COV/${KID_2_ID}_${FAMILY_ID}.REC_SNP_COV.txt ${FAM_DIR}



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

