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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE203nnn/GSE203089/suppl/GSE203089_RAW.tar" -O "GSE203089_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 "SRR19218122"
gz1="$(_compress_if_exists 'SRR19218122_1' || true)"
gz2="$(_compress_if_exists 'SRR19218122_2' || true)"
gzs="$(_compress_if_exists 'SRR19218122' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CD8_Day4_2_S6_L005_R1_001.fastq.gz"
fi

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

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

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

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

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

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

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

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

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

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

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


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218122/SRR19218122"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218123/SRR19218123"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218124/SRR19218124"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218125/SRR19218125"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218126/SRR19218126"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218127/SRR19218127"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218128/SRR19218128"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218129/SRR19218129"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218130/SRR19218130"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218131/SRR19218131"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218132/SRR19218132"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218133/SRR19218133"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218132/SRR19218132"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218133/SRR19218133"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218128/SRR19218128"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218129/SRR19218129"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218130/SRR19218130"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218131/SRR19218131"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218126/SRR19218126"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218127/SRR19218127"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218122/SRR19218122"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218123/SRR19218123"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218124/SRR19218124"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19218125/SRR19218125"

echo "Download complete. Files saved to $OUTDIR"