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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE69nnn/GSE69584/suppl/GSE69584_DEAA.bam" -O "GSE69584_DEAA.bam"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE69nnn/GSE69584/suppl/GSE69584_K498A.bam" -O "GSE69584_K498A.bam"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE69nnn/GSE69584/suppl/GSE69584_WT.bam" -O "GSE69584_WT.bam"

# --- 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 "SRR2051421"
gz1="$(_compress_if_exists 'SRR2051421_1' || true)"
gz2="$(_compress_if_exists 'SRR2051421_2' || true)"
gzs="$(_compress_if_exists 'SRR2051421' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "FJM_W1_NoIndex_L001_R1.fastq.gz"
fi

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

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

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

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

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


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2051421/SRR2051421"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2051422/SRR2051422"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2051423/SRR2051423"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2051424/SRR2051424"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2051425/SRR2051425"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2051426/SRR2051426"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2051421/SRR2051421"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2051422/SRR2051422"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2051423/SRR2051423"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2051424/SRR2051424"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2051425/SRR2051425"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2051425/SRR2051425"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR2051426/SRR2051426"

echo "Download complete. Files saved to $OUTDIR"