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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE72nnn/GSE72501/suppl/GSE72501_GM12878submission_RPKMs.xls.gz" -O "GSE72501_GM12878submission_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 "SRR2192704"
gz1="$(_compress_if_exists 'SRR2192704_1' || true)"
gz2="$(_compress_if_exists 'SRR2192704_2' || true)"
gzs="$(_compress_if_exists 'SRR2192704' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LIB23505_SAM19353354_R1.fastq.gz"
fi

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

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

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

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

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

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

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

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

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


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192704/SRR2192704"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192705/SRR2192705"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192706/SRR2192706"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192707/SRR2192707"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192708/SRR2192708"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192709/SRR2192709"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192710/SRR2192710"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192711/SRR2192711"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192712/SRR2192712"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192713/SRR2192713"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192708/SRR2192708"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192712/SRR2192712"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192710/SRR2192710"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192704/SRR2192704"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192705/SRR2192705"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192709/SRR2192709"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192711/SRR2192711"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192713/SRR2192713"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192706/SRR2192706"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2192707/SRR2192707"

echo "Download complete. Files saved to $OUTDIR"