#!/bin/bash
set -euo pipefail

mkdir -p fastq
cd fastq

# Format:
# SRR    subdir    GSM         label
samples=(
"SRR7073313 003 GSM3119072 ERRg_ChIP_ERRgKO1"
"SRR7073314 004 GSM3119073 ERRg_ChIP_ERRgKO2"
"SRR7073317 007 GSM3119076 Input_ERRgKO1"
"SRR7073318 008 GSM3119077 Input_ERRgKO2"
)

echo -e "SRR\tGSM\tlabel\turl\tlocal_file" > download_manifest.tsv

for row in "${samples[@]}"; do
    read -r srr subdir gsm label <<< "$row"

    url="https://ftp.sra.ebi.ac.uk/vol1/fastq/SRR707/${subdir}/${srr}/${srr}.fastq.gz"
    outfile="${gsm}_${label}_${srr}.fastq.gz"

    echo "Checking: $srr $gsm $label"
    wget --spider "$url"

    echo "Downloading: $outfile"
    wget -c -O "$outfile" "$url"

    echo -e "${srr}\t${gsm}\t${label}\t${url}\t${outfile}" >> download_manifest.tsv

    echo "Testing gzip integrity: $outfile"
    gzip -t "$outfile"

    echo "Done: $outfile"
    echo
done

echo "All downloads finished."
echo "Manifest:"
cat download_manifest.tsv
