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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE133nnn/GSE133479/suppl/GSE133479_RAW.tar" -O "GSE133479_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 "SRR9610546"
gz1="$(_compress_if_exists 'SRR9610546_1' || true)"
gz2="$(_compress_if_exists 'SRR9610546_2' || true)"
gzs="$(_compress_if_exists 'SRR9610546' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "smNPCWT_S1_L002_R1_001.fastq.gz"
fi

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

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

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

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

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


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9610546/SRR9610546"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9610547/SRR9610547"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9610548/SRR9610548"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9610549/SRR9610549"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9610550/SRR9610550"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9610551/SRR9610551"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9610550/SRR9610550"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9610551/SRR9610551"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9610548/SRR9610548"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9610549/SRR9610549"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9610546/SRR9610546"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9610547/SRR9610547"

echo "Download complete. Files saved to $OUTDIR"