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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE273nnn/GSE273094/suppl/GSE273094_RAW.tar" -O "GSE273094_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 "SRR29988222"
gz1="$(_compress_if_exists 'SRR29988222_1' || true)"
gz2="$(_compress_if_exists 'SRR29988222_2' || true)"
gzs="$(_compress_if_exists 'SRR29988222' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mIgG_CTRL3_S3_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mIgG_CTRL3_S3_2.fastq.gz"
fi

fasterq-dump --split-files "SRR29988223"
gz1="$(_compress_if_exists 'SRR29988223_1' || true)"
gz2="$(_compress_if_exists 'SRR29988223_2' || true)"
gzs="$(_compress_if_exists 'SRR29988223' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mIgG_CTRL2_S2_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mIgG_CTRL2_S2_2.fastq.gz"
fi

fasterq-dump --split-files "SRR29988224"
gz1="$(_compress_if_exists 'SRR29988224_1' || true)"
gz2="$(_compress_if_exists 'SRR29988224_2' || true)"
gzs="$(_compress_if_exists 'SRR29988224' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mIgG_CTRL1_S1_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mIgG_CTRL1_S1_2.fastq.gz"
fi

fasterq-dump --split-files "SRR29988225"
gz1="$(_compress_if_exists 'SRR29988225_1' || true)"
gz2="$(_compress_if_exists 'SRR29988225_2' || true)"
gzs="$(_compress_if_exists 'SRR29988225' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Input3_S7_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Input3_S7_2.fastq.gz"
fi

fasterq-dump --split-files "SRR29988226"
gz1="$(_compress_if_exists 'SRR29988226_1' || true)"
gz2="$(_compress_if_exists 'SRR29988226_2' || true)"
gzs="$(_compress_if_exists 'SRR29988226' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "FLAG_3_S6_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "FLAG_3_S6_2.fastq.gz"
fi

fasterq-dump --split-files "SRR29988227"
gz1="$(_compress_if_exists 'SRR29988227_1' || true)"
gz2="$(_compress_if_exists 'SRR29988227_2' || true)"
gzs="$(_compress_if_exists 'SRR29988227' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "FLAG_2_S5_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "FLAG_2_S5_2.fastq.gz"
fi

fasterq-dump --split-files "SRR29988228"
gz1="$(_compress_if_exists 'SRR29988228_1' || true)"
gz2="$(_compress_if_exists 'SRR29988228_2' || true)"
gzs="$(_compress_if_exists 'SRR29988228' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "FLAG_1_S4_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "FLAG_1_S4_2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29988222/SRR29988222"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29988223/SRR29988223"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29988224/SRR29988224"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29988225/SRR29988225"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos6/sra-pub-zq-40/SRR029/29988/SRR29988226/SRR29988226.lite.1"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29988227/SRR29988227"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29988228/SRR29988228"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29988228/SRR29988228"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29988227/SRR29988227"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29988226/SRR29988226"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29988225/SRR29988225"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29988224/SRR29988224"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29988223/SRR29988223"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29988222/SRR29988222"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos6/sra-pub-zq-40/SRR029/29988/SRR29988226/SRR29988226.lite.1"

echo "Download complete. Files saved to $OUTDIR"