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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE134nnn/GSE134164/suppl/GSE134164_RAW.tar" -O "GSE134164_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 "SRR9672169"
gz1="$(_compress_if_exists 'SRR9672169_1' || true)"
gz2="$(_compress_if_exists 'SRR9672169_2' || true)"
gzs="$(_compress_if_exists 'SRR9672169' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CTRL-TESR-1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CTRL-TESR-1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR9672170"
gz1="$(_compress_if_exists 'SRR9672170_1' || true)"
gz2="$(_compress_if_exists 'SRR9672170_2' || true)"
gzs="$(_compress_if_exists 'SRR9672170' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CTRL-TESR-2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CTRL-TESR-2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR9672171"
gz1="$(_compress_if_exists 'SRR9672171_1' || true)"
gz2="$(_compress_if_exists 'SRR9672171_2' || true)"
gzs="$(_compress_if_exists 'SRR9672171' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CTRL-TGFBI-1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CTRL-TGFBI-1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR9672172"
gz1="$(_compress_if_exists 'SRR9672172_1' || true)"
gz2="$(_compress_if_exists 'SRR9672172_2' || true)"
gzs="$(_compress_if_exists 'SRR9672172' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CTRL-TGFBI-2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CTRL-TGFBI-2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR9672173"
gz1="$(_compress_if_exists 'SRR9672173_1' || true)"
gz2="$(_compress_if_exists 'SRR9672173_2' || true)"
gzs="$(_compress_if_exists 'SRR9672173' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "DDXOE-TESR-2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "DDXOE-TESR-2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR9672174"
gz1="$(_compress_if_exists 'SRR9672174_1' || true)"
gz2="$(_compress_if_exists 'SRR9672174_2' || true)"
gzs="$(_compress_if_exists 'SRR9672174' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "DDXOE-TESR-1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "DDXOE-TESR-1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR9672175"
gz1="$(_compress_if_exists 'SRR9672175_1' || true)"
gz2="$(_compress_if_exists 'SRR9672175_2' || true)"
gzs="$(_compress_if_exists 'SRR9672175' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "DDXOE-TGFBI-2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "DDXOE-TGFBI-2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR9672176"
gz1="$(_compress_if_exists 'SRR9672176_1' || true)"
gz2="$(_compress_if_exists 'SRR9672176_2' || true)"
gzs="$(_compress_if_exists 'SRR9672176' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "DDXOE-TGFBI-1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "DDXOE-TGFBI-1_R2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672169/SRR9672169"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672170/SRR9672170"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672171/SRR9672171"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672172/SRR9672172"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672173/SRR9672173"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672174/SRR9672174"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672175/SRR9672175"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672176/SRR9672176"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672169/SRR9672169"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672170/SRR9672170"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672171/SRR9672171"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672172/SRR9672172"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672174/SRR9672174"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672173/SRR9672173"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672176/SRR9672176"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9672175/SRR9672175"

echo "Download complete. Files saved to $OUTDIR"