#!/bin/sh
#PBS -j oe
#PBS -l walltime=5:00:00
#PBS -l mem=25gb
#PBS -m bea
#PBS -M anaqib2@uic.edu


cd /MergedFASTAFiles
#Folder where all the merged FASTA files have been stored
FILES=/MergedFASTAFiles/*.fna
#Only files with extension *.fna to be selected

mkdir PrimerTemplateCounts
mkdir OutputFolder

for T in $FILES
do
	Orgname=`basename $T .fna`
	echo $Orgname
	num=2
	while [ $num -gt 1 ] && [ $num -lt 642 ]
				do
					Read1=`awk -v i=$num 'FNR == i {print $1}' PrimerTemplateCombinations.txt`
					Read2=`awk -v j=$num 'FNR == j {print $2}' PrimerTemplateCombinations.txt | sed s'/.$//'`
					PrimerCount=`grep -Ec $Read1.*$Read2 $T`
					echo "$Read1:$Read2:$PrimerCount" >> PrimerTemplateCounts/$Orgname.PrimersTemplateCount.txt
					echo "$PrimerCount" >> OutputFolder/$Orgname.PrimersTemplateCount.txt
					num=`expr $num + 1`
				done
done

paste OutputFolder/*.txt | column -s $'\t' -t > FinalCount.txt