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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE136nnn/GSE136908/suppl/GSE136908_RAW.tar" -O "GSE136908_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 "SRR10071159"
gz1="$(_compress_if_exists 'SRR10071159_1' || true)"
gz2="$(_compress_if_exists 'SRR10071159_2' || true)"
gzs="$(_compress_if_exists 'SRR10071159' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "JK_A1_R1_merged.fastq.gz"
fi

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

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

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

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

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

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

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


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071159/SRR10071159"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071160/SRR10071160"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071161/SRR10071161"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071162/SRR10071162"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071163/SRR10071163"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071164/SRR10071164"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071165/SRR10071165"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071166/SRR10071166"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071159/SRR10071159"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071160/SRR10071160"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071161/SRR10071161"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071162/SRR10071162"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071163/SRR10071163"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071164/SRR10071164"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071165/SRR10071165"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071166/SRR10071166"

echo "Download complete. Files saved to $OUTDIR"