#!/bin/bash

#Load binaries
module load bioinfo/samtools-1.10

ind=$1

#Make depth file (could also just pipe into awk if space issues or only needing to do it for the statistic)
samtools depth -a ${ind}_fastp20_to_PTriDNAZoo_BWA_f2q30_sort_markdup.bam > ${ind}_depth.txt

#Mean depth from sam/bam file
cat ${ind}_depth.txt | awk '{c++;s+=$3}END{print s/c}' > ${ind}_PTrimeandepth.txt

#Coverage across reference as a percent if depth is at 10 - change ten in if($3>10) to any other value of depth youre interested in (i.e. if you want more than 10x then ($3>10)).
#Or change > to < if you want to see smaller than or bigger than your depth

cat ${ind}_depth.txt | awk '{c++; if($3>10) total+=1}END{print (total/c)*100}' > ${ind}_PTricovered_depth10X.txt
cat ${ind}_depth.txt | awk '{c++; if($3>1) total+=1}END{print (total/c)*100}' > ${ind}_PTricovered_depth1X.txt


#remove depth file if not required
rm ${ind}_depth.txt