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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE266nnn/GSE266924/suppl/GSE266924_FUS_HEKs.reproducible_enriched_re.tsv.gz" -O "GSE266924_FUS_HEKs.reproducible_enriched_re.tsv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE266nnn/GSE266924/suppl/GSE266924_FUS_HEKs.reproducible_enriched_windows.tsv.gz" -O "GSE266924_FUS_HEKs.reproducible_enriched_windows.tsv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE266nnn/GSE266924/suppl/GSE266924_RAW.tar" -O "GSE266924_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 "SRR28928314"
gz1="$(_compress_if_exists 'SRR28928314_1' || true)"
gz2="$(_compress_if_exists 'SRR28928314_2' || true)"
gzs="$(_compress_if_exists 'SRR28928314' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "FUS_IP2_S4_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "FUS_IP2_S4_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR28928315"
gz1="$(_compress_if_exists 'SRR28928315_1' || true)"
gz2="$(_compress_if_exists 'SRR28928315_2' || true)"
gzs="$(_compress_if_exists 'SRR28928315' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "FUS_IP1_S3_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "FUS_IP1_S3_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR28928316"
gz1="$(_compress_if_exists 'SRR28928316_1' || true)"
gz2="$(_compress_if_exists 'SRR28928316_2' || true)"
gzs="$(_compress_if_exists 'SRR28928316' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "FUS_IN2_S2_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "FUS_IN2_S2_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR28928317"
gz1="$(_compress_if_exists 'SRR28928317_1' || true)"
gz2="$(_compress_if_exists 'SRR28928317_2' || true)"
gzs="$(_compress_if_exists 'SRR28928317' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "FUS_IN1_S1_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "FUS_IN1_S1_L001_R2_001.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR28928314/SRR28928314"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR28928315/SRR28928315"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR28928316/SRR28928316"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR28928317/SRR28928317"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR28928317/SRR28928317"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR28928316/SRR28928316"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR28928315/SRR28928315"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR28928314/SRR28928314"

echo "Download complete. Files saved to $OUTDIR"