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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE202nnn/GSE202202/suppl/GSE202202_RAW.tar" -O "GSE202202_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 "SRR19078900"
gz1="$(_compress_if_exists 'SRR19078900_1' || true)"
gz2="$(_compress_if_exists 'SRR19078900_2' || true)"
gzs="$(_compress_if_exists 'SRR19078900' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "211122DIV17DendMG132_S12_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR19078901"
gz1="$(_compress_if_exists 'SRR19078901_1' || true)"
gz2="$(_compress_if_exists 'SRR19078901_2' || true)"
gzs="$(_compress_if_exists 'SRR19078901' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "211122DIV17SomaMG132_S10_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR19078902"
gz1="$(_compress_if_exists 'SRR19078902_1' || true)"
gz2="$(_compress_if_exists 'SRR19078902_2' || true)"
gzs="$(_compress_if_exists 'SRR19078902' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "211122DIV17DendCtrl_S11_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR19078903"
gz1="$(_compress_if_exists 'SRR19078903_1' || true)"
gz2="$(_compress_if_exists 'SRR19078903_2' || true)"
gzs="$(_compress_if_exists 'SRR19078903' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "211122DIV17SomaCtrl_S9_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR19078904"
gz1="$(_compress_if_exists 'SRR19078904_1' || true)"
gz2="$(_compress_if_exists 'SRR19078904_2' || true)"
gzs="$(_compress_if_exists 'SRR19078904' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "211115DIV17DendMG132_S8_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR19078905"
gz1="$(_compress_if_exists 'SRR19078905_1' || true)"
gz2="$(_compress_if_exists 'SRR19078905_2' || true)"
gzs="$(_compress_if_exists 'SRR19078905' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "211115DIV17SomaMG132_S6_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR19078906"
gz1="$(_compress_if_exists 'SRR19078906_1' || true)"
gz2="$(_compress_if_exists 'SRR19078906_2' || true)"
gzs="$(_compress_if_exists 'SRR19078906' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "211115DIV17DendCtrl_S7_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR19078907"
gz1="$(_compress_if_exists 'SRR19078907_1' || true)"
gz2="$(_compress_if_exists 'SRR19078907_2' || true)"
gzs="$(_compress_if_exists 'SRR19078907' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "211115DIV17SomaCtrl_S5_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR19078908"
gz1="$(_compress_if_exists 'SRR19078908_1' || true)"
gz2="$(_compress_if_exists 'SRR19078908_2' || true)"
gzs="$(_compress_if_exists 'SRR19078908' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "211012DIV18DendMG132_S4_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR19078909"
gz1="$(_compress_if_exists 'SRR19078909_1' || true)"
gz2="$(_compress_if_exists 'SRR19078909_2' || true)"
gzs="$(_compress_if_exists 'SRR19078909' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "211012DIV18SomaMG132_S2_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR19078910"
gz1="$(_compress_if_exists 'SRR19078910_1' || true)"
gz2="$(_compress_if_exists 'SRR19078910_2' || true)"
gzs="$(_compress_if_exists 'SRR19078910' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "211012DIV18DendCtrl_S3_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR19078911"
gz1="$(_compress_if_exists 'SRR19078911_1' || true)"
gz2="$(_compress_if_exists 'SRR19078911_2' || true)"
gzs="$(_compress_if_exists 'SRR19078911' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "211012DIV18SomaCtrl_S1_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/SRR19078900/SRR19078900"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078901/SRR19078901"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078902/SRR19078902"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078903/SRR19078903"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078904/SRR19078904"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078905/SRR19078905"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078906/SRR19078906"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078907/SRR19078907"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078908/SRR19078908"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078909/SRR19078909"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078910/SRR19078910"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078911/SRR19078911"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078910/SRR19078910"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078908/SRR19078908"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078911/SRR19078911"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078909/SRR19078909"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078906/SRR19078906"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078904/SRR19078904"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078907/SRR19078907"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078905/SRR19078905"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078902/SRR19078902"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078900/SRR19078900"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078903/SRR19078903"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19078901/SRR19078901"

echo "Download complete. Files saved to $OUTDIR"