#!/bin/bash

#SBATCH --cpus-per-task=4
#SBATCH --mem-per-cpu=4GB
#SBATCH --time=4:00:00
#SBATCH --mail-type=BEGIN,END
#SBATCH --mail-user=eric.helm@ufl.edu
#SBATCH --qos=avram-b
#SBATCH --job-name=seqtk
#SBATCH --output seqtk.%j.out

date;pwd

module load seqtk
module load parallel

base="${PWD##*/}"

## CHECK IF READS ARE CONCATENATED ##
if [[ ! -f "$base"_forward.fastq.gz && "$(find . -name "*R1_001.fastq.gz" -type f -print -quit)" ]]; then # Checking if (1) concatenated forward read file already exists, and, if not, (2) if the unconcatenated forward read files are present.
	cat *R1_001.fastq.gz > "$base"_forward.fastq.gz # If this doesn't result in the creation of the concatenated file, make sure the unconcatenated fastq files follow standard Illumina naming convention (e.g. "$sample_name"_S4_L001_R1_001.fastq.gz)
fi

if [[ ! -f "$base"_reverse.fastq.gz && "$(find . -name "*R2_001.fastq.gz" -type f -print -quit)" ]]; then
	cat *R2_001.fastq.gz > "$base"_reverse.fastq.gz
fi

## TRIM READS WITH SEQTK AND COMPRESS OUTPUT ##
if [[ -f "$base"_forward.fastq.gz && ! -f "$base"_forward_trimmed.fastq.gz ]]; then # If the concatenated forward read file exists, proceed
    seqtk trimfq "$base"_forward.fastq.gz | parallel --pipe -k --block 10M gzip -9 > "$base"_forward_trimmed.fastq.gz
fi

if [[ -f "$base"_reverse.fastq.gz && ! -f "$base"_reverse_trimmed.fastq.gz ]]; then # If the concatenated reverse read file exists, proceed. Will not run if no reverse file was found (i.e. run was Single End)
    seqtk trimfq "$base"_reverse.fastq.gz | parallel --pipe -k --block 10M gzip -9 > "$base"_reverse_trimmed.fastq.gz
fi

echo "seqtk trimming complete...program terminating"

date
