# Stop on any error. Print the commands as they execute.
set -uex

# NCBI BioProject ID.
PRJN=PRJNA601630

# Obtain the run information.
esearch -db sra -query $PRJN | efetch -format runinfo > runinfo.csv

# Isolate just the RUN ids.
cat runinfo.csv | cut -f 1 -d , | grep SRR > ids

# Store data in a common location for subsequent runs.
SRA=/export/sra

# Create a directory for the sequencing data.
mkdir -p $SRA

# Download the FASTQ data for each SRR number.
cat ids | parallel fastq-dump --split-files  --outdir $SRA {} >> log.txt

# Save quality control plots into a separate directory.
mkdir -p fastqc

# Run fastqc on each file. Generate the output into the fastqc directory.
cat ids | parallel fastqc -q -o fastqc $SRA/{}_1.fastq 2>> log.txt

# Run a sequence statistics on each downloaded fastq file
cat ids | parallel -X -j 1 seqkit stats $SRA/{}_1.fastq