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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE78nnn/GSE78508/suppl/GSE78508_RAW.tar" -O "GSE78508_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 "SRR3187417"
gz1="$(_compress_if_exists 'SRR3187417_1' || true)"
gz2="$(_compress_if_exists 'SRR3187417_2' || true)"
gzs="$(_compress_if_exists 'SRR3187417' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ACMW_4_TGACCA_L006_R1.fastq.gz"
fi

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

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

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

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


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3187417/SRR3187417"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3187418/SRR3187418"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3187419/SRR3187419"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3187420/SRR3187420"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3187421/SRR3187421"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3187421/SRR3187421"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3187417/SRR3187417"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3187418/SRR3187418"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3187419/SRR3187419"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3187420/SRR3187420"

echo "Download complete. Files saved to $OUTDIR"