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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE51nnn/GSE51556/suppl/GSE51556_RAW.tar" -O "GSE51556_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 "SRR1015363"
gz1="$(_compress_if_exists 'SRR1015363_1' || true)"
gz2="$(_compress_if_exists 'SRR1015363_2' || true)"
gzs="$(_compress_if_exists 'SRR1015363' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "Sample_ADR12.fastq.gz"
fi

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

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

fasterq-dump --split-files "SRR1015366"
gz1="$(_compress_if_exists 'SRR1015366_1' || true)"
gz2="$(_compress_if_exists 'SRR1015366_2' || true)"
gzs="$(_compress_if_exists 'SRR1015366' || true)"

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

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

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

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


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015363/SRR1015363"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015364/SRR1015364"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015365/SRR1015365"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015366/SRR1015366"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015367/SRR1015367"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015368/SRR1015368"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015369/SRR1015369"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015370/SRR1015370"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015363/SRR1015363"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015364/SRR1015364"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015365/SRR1015365"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015367/SRR1015367"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015368/SRR1015368"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015369/SRR1015369"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015370/SRR1015370"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1015366/SRR1015366"

echo "Download complete. Files saved to $OUTDIR"