#!/bin/bash
#PBS -l walltime=01:00:00
#PBS -l ncpus=1,mem=2gb
#PBS -q uv2000
#PBS -N gather_quad_results
#PBS -j oe



### folder structure for the downstream analysis - created by NHS_WES_trio_setup.sh ###
BASE=/scratch/u035/u035/shared/trio_whole_exome/analysis
WORK_DIR=${BASE}/${PROJECT_ID}
NHS_DIR=${WORK_DIR}/${PLATE_ID}_${VERSION_N}_results

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 "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
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}
mkdir ${FAM_DIR}

# copy the LOG file
cp ${WORK_DIR}/LOG/go_quad.o${JOB_ID} ${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 file
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}



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

