#!/bin/bash
# -*- coding: utf-8 -*-


###############################################################################
#   bashpart Triplefin_out07
#
#   Use FASconCAT-G_v1 to recode all nucleotide positions , and to remove 3rd codon positions
#
# Make sure the FASconCAT-G_v1.02.pl is downloaded and placed in the WD
# Before executing this bash-code
# The FASconCAT-G_v1 perl code can be obtained here:
# https://www.zfmk.de/en/research/research-centres-and-groups/fasconcat-g
###############################################################################

##get the present directory
WD=$(pwd)

OUTDIR1="PDV_out02_FASconCAT_3rd_codon_removed/"
#OUTDIR1="tobis_23_FASconCAT_3rd_codon_removed"
rm -rf "${OUTDIR1}" 
#make a new directory for the results from FASconCAT-G_v1
mkdir "${OUTDIR1}"
#change dir
cd "${OUTDIR1}"
#define output directory
OUTDIR2="PDV_out02b_resulting_3pos_reject_files"
rm -rf "$OUTDIR2"
mkdir "$OUTDIR2"
#mkdir resulting_allpos_RY_coded_files
cd "$WD"

#define a output file name postfix
OUTP_FNM_CHNG="PDV_out02"

INDIR01="PDV_inp01_fasta"

rm -rf "$INDIR01"
mkdir "$INDIR01"
#Define inputfile
INFILE01="20200819CDVPDValgn02.fasta"

cp "$INFILE01" ./"$INDIR01"/.

cp "$WD"/FASconCAT-G_v1.02.pl /"$WD"/"${OUTDIR1}"/FASconCAT-G_v1.02.pl
# because there are 5 different matrices with different combinations of genes and species
# Each FASconCAT analysis will require a single copy of the FASconCAT-G_v1.02.pl code
# work individually in each directory.
# The FASconCAT-G_v1.02.pl code works on all fasta files it finds in the same directory
# which means they cannot be analysed together in a single directory
#copy the FASconCAT-G_v1 code into the folder where you will be running FASconCAT-G_v1
#iterate over numbers of files to scp get tar.gz files w alignments

 END="1"
# #iterate over the different matrices with different combinations of genes and species
for i in $(seq 1 $END)
do
  cd "$WD"/"${OUTDIR1}"/
  mkdir PDV02_0"$i"
  cd "$WD"
#   #cp "$WD"/FASconCAT-G_v1.02.pl /"$WD"/"${OUTDIR1}"/Tripl_07_0"$i"/FASconCAT-G_v1.02.pl
  cp "$WD"/FASconCAT-G_v1.02.pl /"$WD"/"${OUTDIR1}"/PDV02_0"$i"/FASconCAT-G_v1.02.pl
done

#the block below copies the input-alignment files from the MAFFT alignments
#and recodes all 3rd codon positions to RY
#using the FASconCAT-G perl code, and using the -b -b command

#move to the folder that holds the alignment fasta-files
cd "$WD"/"$INDIR01"
#cd $WD/tobis_aliscore_results
#iterate over numbers of files to scp get tar.gz files w alignments
END="1"
for i in $(seq 1 $END)
do
  cd "$WD"/"$INDIR01"
  for FNM in *.fasta
  do
    cd "$WD"/"$INDIR01"
    #echo "$FNM"
    cp "$FNM" "$WD"/"${OUTDIR1}"/PDV02_0"$i"/.
    newFNM=$(echo $FNM | \
  	sed 's/\.fasta/\.trm\.fas/g')
    echo "$newFNM"
    cp "$FNM" "$WD"/"${OUTDIR1}"/PDV02_0"$i"/"$newFNM"
    cd  "$WD"/"${OUTDIR1}"/PDV02_0"$i"/
    #FASconCAT-G commands: -b -b gives RY Coding 3rd Codon Position
    #FASconCAT-G commands: -b gives RY Coding Complete Sequence
    #FASconCAT-G commands: -d rejects 3rd codon positions
    perl "$WD"/"${OUTDIR1}"/PDV02_0"$i"/FASconCAT-G_v1.02.pl -s -d -n -n \
    1> stdout_FASconCAT_"$newFNM".txt 2> stderr_FASconCAT_"$newFNM".txt
  
    #rename all outpout-files
    #mv FcC_supermatrix.phy FcC_"$OUTP_FNM_CHNG"_"$newFNM".phy
    mv FcC_supermatrix.fas "$OUTP_FNM_CHNG"_3pos_remove_"$newFNM"
    mv FcC_supermatrix.nex FcC_"$OUTP_FNM_CHNG"_3pos_remove_"$newFNM".nex
    #mv FcC_supermatrix.fas "$OUTP_FNM_CHNG"_3pos_remove_"$newFNM"
    #mv FcC_supermatrix_partition.txt FcC_partition_"$OUTP_FNM_CHNG"_"$newFNM".txt
    #this move of resulting fasta file is important, as any fasta file present in the directory will handled by the next 
    #iteration in this loop. If the fasta files are not moved out it will make FASconCAT try and combine all fasta files
    #once it has completed the first set of iterations
    mv *"$newFNM"* "$WD"/"${OUTDIR1}"/"$OUTDIR2"/.
   cd "$WD"/"$INDIR01"
  done
done

#change directory to the folder that holds the copied inputfiles
cd "$WD"/"${OUTDIR1}"/
#remove all of these input-files
#rm *.txt

#the block below copies the input-alignment files from the MAFFT alignments
#and recodes all nucleotide positions to RY
#using the FASconCAT-G perl code, and using the -b command

#change directory the folder that holds the alignment fasta-files
cd "$WD"/"$INDIR01"

#MARE needs the fas-file as an input-file
#RAxML need the phy-file as an input
#RAxML and partitionfineder will need the partition-file as input-files
