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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE134nnn/GSE134971/suppl/GSE134971_764.01v02.IDR.out.0102merged.bed.narrowPeak.bed.gz" -O "GSE134971_764.01v02.IDR.out.0102merged.bed.narrowPeak.bed.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE134nnn/GSE134971/suppl/GSE134971_RAW.tar" -O "GSE134971_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 "SRR9847672"
gz1="$(_compress_if_exists 'SRR9847672_1' || true)"
gz2="$(_compress_if_exists 'SRR9847672_2' || true)"
gzs="$(_compress_if_exists 'SRR9847672' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Stau2K562_IP1_2_S1_L000_R1_001.A01_764_01_STAU2.addbarcode.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Stau2K562_IP1_2_S1_L000_R2_001.A01_764_01_STAU2.addrandomer.fastq.gz"
fi

fasterq-dump --split-files "SRR9847673"
gz1="$(_compress_if_exists 'SRR9847673_1' || true)"
gz2="$(_compress_if_exists 'SRR9847673_2' || true)"
gzs="$(_compress_if_exists 'SRR9847673' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Stau2K562_IP1_2_S1_L000_R1_001.B06_764_01_STAU2.addbarcode.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Stau2K562_IP1_2_S1_L000_R2_001.B06_764_01_STAU2.addrandomer.fastq.gz"
fi

fasterq-dump --split-files "SRR9847674"
gz1="$(_compress_if_exists 'SRR9847674_1' || true)"
gz2="$(_compress_if_exists 'SRR9847674_2' || true)"
gzs="$(_compress_if_exists 'SRR9847674' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Stau2K562_IP1_2_S1_L000_R1_001.C01_764_02_STAU2.addbarcode.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Stau2K562_IP1_2_S1_L000_R2_001.C01_764_02_STAU2.addrandomer.fastq.gz"
fi

fasterq-dump --split-files "SRR9847675"
gz1="$(_compress_if_exists 'SRR9847675_1' || true)"
gz2="$(_compress_if_exists 'SRR9847675_2' || true)"
gzs="$(_compress_if_exists 'SRR9847675' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Stau2K562_IP1_2_S1_L000_R1_001.D08fixed_764_02_STAU2.addbarcode.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Stau2K562_IP1_2_S1_L000_R2_001.D08fixed_764_02_STAU2.addrandomer.fastq.gz"
fi

fasterq-dump --split-files "SRR9847676"
gz1="$(_compress_if_exists 'SRR9847676_1' || true)"
gz2="$(_compress_if_exists 'SRR9847676_2' || true)"
gzs="$(_compress_if_exists 'SRR9847676' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Stau2K562_input_S2_L000_R1_001.unassigned.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Stau2K562_input_S2_L000_R2_001.unassigned.addrandomer.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9847672/SRR9847672"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9847673/SRR9847673"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9847674/SRR9847674"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9847675/SRR9847675"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9847676/SRR9847676"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9847676/SRR9847676"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9847672/SRR9847672"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9847673/SRR9847673"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9847674/SRR9847674"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9847675/SRR9847675"

echo "Download complete. Files saved to $OUTDIR"