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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE112nnn/GSE112439/suppl/GSE112439_RAW.tar" -O "GSE112439_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 "SRR6908762"
gz1="$(_compress_if_exists 'SRR6908762_1' || true)"
gz2="$(_compress_if_exists 'SRR6908762_2' || true)"
gzs="$(_compress_if_exists 'SRR6908762' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ESC_sgCTRL.1.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ESC_sgCTRL.1.R2.fq.gz"
fi

fasterq-dump --split-files "SRR6908763"
gz1="$(_compress_if_exists 'SRR6908763_1' || true)"
gz2="$(_compress_if_exists 'SRR6908763_2' || true)"
gzs="$(_compress_if_exists 'SRR6908763' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ESC_sgCTRL.2.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ESC_sgCTRL.2.R2.fq.gz"
fi

fasterq-dump --split-files "SRR6908764"
gz1="$(_compress_if_exists 'SRR6908764_1' || true)"
gz2="$(_compress_if_exists 'SRR6908764_2' || true)"
gzs="$(_compress_if_exists 'SRR6908764' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ESC_sgCTRL.3.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ESC_sgCTRL.3.R2.fq.gz"
fi

fasterq-dump --split-files "SRR6908765"
gz1="$(_compress_if_exists 'SRR6908765_1' || true)"
gz2="$(_compress_if_exists 'SRR6908765_2' || true)"
gzs="$(_compress_if_exists 'SRR6908765' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ESC_sgCTRL.4.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ESC_sgCTRL.4.R2.fq.gz"
fi

fasterq-dump --split-files "SRR6908766"
gz1="$(_compress_if_exists 'SRR6908766_1' || true)"
gz2="$(_compress_if_exists 'SRR6908766_2' || true)"
gzs="$(_compress_if_exists 'SRR6908766' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ESC_sgDDX6.1.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ESC_sgDDX6.1.R2.fq.gz"
fi

fasterq-dump --split-files "SRR6908767"
gz1="$(_compress_if_exists 'SRR6908767_1' || true)"
gz2="$(_compress_if_exists 'SRR6908767_2' || true)"
gzs="$(_compress_if_exists 'SRR6908767' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ESC_sgDDX6.2.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ESC_sgDDX6.2.R2.fq.gz"
fi

fasterq-dump --split-files "SRR6908768"
gz1="$(_compress_if_exists 'SRR6908768_1' || true)"
gz2="$(_compress_if_exists 'SRR6908768_2' || true)"
gzs="$(_compress_if_exists 'SRR6908768' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ESC_sgDDX6.3.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ESC_sgDDX6.3.R2.fq.gz"
fi

fasterq-dump --split-files "SRR6908769"
gz1="$(_compress_if_exists 'SRR6908769_1' || true)"
gz2="$(_compress_if_exists 'SRR6908769_2' || true)"
gzs="$(_compress_if_exists 'SRR6908769' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ESC_sgDDX6.4.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ESC_sgDDX6.4.R2.fq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908762/SRR6908762"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908763/SRR6908763"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908764/SRR6908764"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908765/SRR6908765"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908766/SRR6908766"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908767/SRR6908767"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908768/SRR6908768"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908769/SRR6908769"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908762/SRR6908762"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908763/SRR6908763"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908764/SRR6908764"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908765/SRR6908765"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908766/SRR6908766"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908767/SRR6908767"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908768/SRR6908768"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6908769/SRR6908769"

echo "Download complete. Files saved to $OUTDIR"