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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE72nnn/GSE72502/suppl/GSE72502_PBMC_RPKMs.xls.gz" -O "GSE72502_PBMC_RPKMs.xls.gz"

# --- 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 "SRR2192716"
gz1="$(_compress_if_exists 'SRR2192716_1' || true)"
gz2="$(_compress_if_exists 'SRR2192716_2' || true)"
gzs="$(_compress_if_exists 'SRR2192716' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LIB8212_SAM2707860_L1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LIB8212_SAM2707860_L1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR2192717"
gz1="$(_compress_if_exists 'SRR2192717_1' || true)"
gz2="$(_compress_if_exists 'SRR2192717_2' || true)"
gzs="$(_compress_if_exists 'SRR2192717' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LIB8215_SAM2707863_L1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LIB8215_SAM2707863_L1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR2192718"
gz1="$(_compress_if_exists 'SRR2192718_1' || true)"
gz2="$(_compress_if_exists 'SRR2192718_2' || true)"
gzs="$(_compress_if_exists 'SRR2192718' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LIB8209_SAM2707857_L1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LIB8209_SAM2707857_L1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR2192719"
gz1="$(_compress_if_exists 'SRR2192719_1' || true)"
gz2="$(_compress_if_exists 'SRR2192719_2' || true)"
gzs="$(_compress_if_exists 'SRR2192719' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LIB8566_SAM2707864_L5_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LIB8566_SAM2707864_L5_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR2192720"
gz1="$(_compress_if_exists 'SRR2192720_1' || true)"
gz2="$(_compress_if_exists 'SRR2192720_2' || true)"
gzs="$(_compress_if_exists 'SRR2192720' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LIB8213_SAM2707861_L1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LIB8213_SAM2707861_L1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR2192721"
gz1="$(_compress_if_exists 'SRR2192721_1' || true)"
gz2="$(_compress_if_exists 'SRR2192721_2' || true)"
gzs="$(_compress_if_exists 'SRR2192721' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LIB8210_SAM2707858_L1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LIB8210_SAM2707858_L1_R2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192716/SRR2192716"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192717/SRR2192717"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192718/SRR2192718"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192719/SRR2192719"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192720/SRR2192720"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192721/SRR2192721"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192718/SRR2192718"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192721/SRR2192721"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192716/SRR2192716"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192720/SRR2192720"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192717/SRR2192717"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192719/SRR2192719"

echo "Download complete. Files saved to $OUTDIR"