#!/usr/bin/env bash
# Bulk download script for GSE73853
# Generated from Yeo Lab Publications Database
# Total files: 27

OUTDIR="GSE73853"
mkdir -p "$OUTDIR"
cd "$OUTDIR"

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE73nnn/GSE73853/suppl/GSE73853_RAW.tar" -O "GSE73853_RAW.tar"

# --- SRA run files ---
# Tip: use 'fasterq-dump' from SRA Toolkit for FASTQ conversion
#      with optional renaming to original submitted FASTQ names.
# Set to 1 to rename SRR outputs (e.g. SRR_1.fastq.gz -> sample_R1.fastq.gz)
RENAME_SRA_TO_ORIGINAL=1

_compress_if_exists() {
  local stem="$1"
  if [ -f "${stem}.fastq" ]; then
    gzip -f "${stem}.fastq"
    echo "${stem}.fastq.gz"
    return 0
  fi
  if [ -f "${stem}.fq" ]; then
    gzip -f "${stem}.fq"
    echo "${stem}.fq.gz"
    return 0
  fi
  return 1
}

# Option A: Download via SRA Toolkit (recommended)
fasterq-dump --split-files "SRR2747462"
gz1="$(_compress_if_exists 'SRR2747462_1' || true)"
gz2="$(_compress_if_exists 'SRR2747462_2' || true)"
gzs="$(_compress_if_exists 'SRR2747462' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "HFF_Mock_48h.fastq.gz"
fi

fasterq-dump --split-files "SRR2747463"
gz1="$(_compress_if_exists 'SRR2747463_1' || true)"
gz2="$(_compress_if_exists 'SRR2747463_2' || true)"
gzs="$(_compress_if_exists 'SRR2747463' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "HFF_InfectedHCMVTB40E_48h.fastq.gz"
fi

fasterq-dump --split-files "SRR2747464"
gz1="$(_compress_if_exists 'SRR2747464_1' || true)"
gz2="$(_compress_if_exists 'SRR2747464_2' || true)"
gzs="$(_compress_if_exists 'SRR2747464' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "HFF_InfectedHCMVTB40E_96h.fastq.gz"
fi

fasterq-dump --split-files "SRR2747465"
gz1="$(_compress_if_exists 'SRR2747465_1' || true)"
gz2="$(_compress_if_exists 'SRR2747465_2' || true)"
gzs="$(_compress_if_exists 'SRR2747465' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "EC_Mock_48h.fastq.gz"
fi

fasterq-dump --split-files "SRR2747466"
gz1="$(_compress_if_exists 'SRR2747466_1' || true)"
gz2="$(_compress_if_exists 'SRR2747466_2' || true)"
gzs="$(_compress_if_exists 'SRR2747466' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "EC_InfectedHCMVTB40E_48h.fastq.gz"
fi

fasterq-dump --split-files "SRR2747467"
gz1="$(_compress_if_exists 'SRR2747467_1' || true)"
gz2="$(_compress_if_exists 'SRR2747467_2' || true)"
gzs="$(_compress_if_exists 'SRR2747467' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "EC_InfectedHCMVTB40E_96h.fastq.gz"
fi

fasterq-dump --split-files "SRR2747468"
gz1="$(_compress_if_exists 'SRR2747468_1' || true)"
gz2="$(_compress_if_exists 'SRR2747468_2' || true)"
gzs="$(_compress_if_exists 'SRR2747468' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "NPC_Mock_48h.fastq.gz"
fi

fasterq-dump --split-files "SRR2747469"
gz1="$(_compress_if_exists 'SRR2747469_1' || true)"
gz2="$(_compress_if_exists 'SRR2747469_2' || true)"
gzs="$(_compress_if_exists 'SRR2747469' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "NPC_InfectedHCMVTB40E_48h.fastq.gz"
fi

fasterq-dump --split-files "SRR2747470"
gz1="$(_compress_if_exists 'SRR2747470_1' || true)"
gz2="$(_compress_if_exists 'SRR2747470_2' || true)"
gzs="$(_compress_if_exists 'SRR2747470' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "NPC_InfectedHCMVTB40E_96h.fastq.gz"
fi

fasterq-dump --split-files "SRR2747471"
gz1="$(_compress_if_exists 'SRR2747471_1' || true)"
gz2="$(_compress_if_exists 'SRR2747471_2' || true)"
gzs="$(_compress_if_exists 'SRR2747471' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "HFF_MockforHSV_8h.fastq.gz"
fi

fasterq-dump --split-files "SRR2747472"
gz1="$(_compress_if_exists 'SRR2747472_1' || true)"
gz2="$(_compress_if_exists 'SRR2747472_2' || true)"
gzs="$(_compress_if_exists 'SRR2747472' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "HFF_InfectedHSV2_8h.fastq.gz"
fi

fasterq-dump --split-files "SRR4209731"
gz1="$(_compress_if_exists 'SRR4209731_1' || true)"
gz2="$(_compress_if_exists 'SRR4209731_2' || true)"
gzs="$(_compress_if_exists 'SRR4209731' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "Tcell_Control_5d.fastq.gz"
fi

fasterq-dump --split-files "SRR4209732"
gz1="$(_compress_if_exists 'SRR4209732_1' || true)"
gz2="$(_compress_if_exists 'SRR4209732_2' || true)"
gzs="$(_compress_if_exists 'SRR4209732' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "Tcell_InfectedHIV1_5d.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747462/SRR2747462"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747463/SRR2747463"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747464/SRR2747464"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747465/SRR2747465"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747466/SRR2747466"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747467/SRR2747467"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747468/SRR2747468"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747469/SRR2747469"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747470/SRR2747470"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747471/SRR2747471"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747472/SRR2747472"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4209731/SRR4209731"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4209732/SRR4209732"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747466/SRR2747466"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747467/SRR2747467"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747465/SRR2747465"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747463/SRR2747463"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747464/SRR2747464"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747472/SRR2747472"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747462/SRR2747462"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747471/SRR2747471"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747469/SRR2747469"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747470/SRR2747470"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2747468/SRR2747468"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4209731/SRR4209731"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4209732/SRR4209732"

echo "Download complete. Files saved to $OUTDIR"