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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE180nnn/GSE180955/suppl/GSE180955_RAW.tar" -O "GSE180955_RAW.tar"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE180nnn/GSE180955/suppl/GSE180955_RBM17_K562_rep1_rep2_reproducible_peaks.bed.gz" -O "GSE180955_RBM17_K562_rep1_rep2_reproducible_peaks.bed.gz"

# --- 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 "SRR15265463"
gz1="$(_compress_if_exists 'SRR15265463_1' || true)"
gz2="$(_compress_if_exists 'SRR15265463_2' || true)"
gzs="$(_compress_if_exists 'SRR15265463' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "K562-RBM17-Input_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "K562-RBM17-Input_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR15265464"
gz1="$(_compress_if_exists 'SRR15265464_1' || true)"
gz2="$(_compress_if_exists 'SRR15265464_2' || true)"
gzs="$(_compress_if_exists 'SRR15265464' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "K562-RBM17-eCLIP1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "K562-RBM17-eCLIP1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR15265465"
gz1="$(_compress_if_exists 'SRR15265465_1' || true)"
gz2="$(_compress_if_exists 'SRR15265465_2' || true)"
gzs="$(_compress_if_exists 'SRR15265465' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "K562-RBM17-eCLIP2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "K562-RBM17-eCLIP2_R2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15265463/SRR15265463"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15265464/SRR15265464"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15265465/SRR15265465"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15265464/SRR15265464"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15265465/SRR15265465"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15265463/SRR15265463"

echo "Download complete. Files saved to $OUTDIR"