#!/bin/bash

#This script automates CodingQuarry's pathogen mode (see manual)
#to predict effector-like genes in the "intergenic" regions of
#an existing annotation.

#Provide a genome and gff of annotation to run:
#run_CQ-PM_mine.sh myGenome.fa myAnnotations.gff
#See the manual for input format requirements. Note that CDS
#in your annotations file MUST included stops.

echo "Reading the annotations and extracting the CDS..."
CodingQuarry -f $1 -a $2 -1 -g none

echo "Translating and quality filtering the coding sequence..."
python $QUARRY_PATH/scripts/fastaTranslate.py out/Predicted_CDS.fa | sed 's/*$//g' > CQ_Proteins.fa
python $QUARRY_PATH/scripts/gene_errors_Xs.py CQ_Proteins.fa CQPMtmp.fa
mv CQPMtmp.fa CQ_Proteins.fa

echo "Running signalP..."
#Split the protein file up into smaller chunks
python $QUARRY_PATH/scripts/split_fasta.py CQ_Proteins.fa 200
i=0
for FILE in CQ_Proteins.fa-*
do
    #If signalP is not in your path, change the line below to specify its location
    signalp $FILE > CQ_Proteins_out_$i
    rm $FILE
    i=$(($i+1))
done
cat CQ_Proteins_out_* | grep -v "#" | awk '($10 == "Y"){print $1" "$5}' > Secretome.txt
rm CQ_Proteins_out_*
rm CQ_Proteins.fa

echo "Running CodingQuarry-PM..."
CodingQuarry -a $2 -f $1 -p 4 -g Secretome.txt -h
