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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE201nnn/GSE201895/suppl/GSE201895_Celf2_Control1_6m_reproducible_peaks.bed.gz" -O "GSE201895_Celf2_Control1_6m_reproducible_peaks.bed.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE201nnn/GSE201895/suppl/GSE201895_Celf2_DM1200_6m_reproducible_peaks.bed.gz" -O "GSE201895_Celf2_DM1200_6m_reproducible_peaks.bed.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE201nnn/GSE201895/suppl/GSE201895_Celf2_DM600_6m_reproducible_peaks.bed.gz" -O "GSE201895_Celf2_DM600_6m_reproducible_peaks.bed.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE201nnn/GSE201895/suppl/GSE201895_RAW.tar" -O "GSE201895_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 "SRR18973017"
gz1="$(_compress_if_exists 'SRR18973017_1' || true)"
gz2="$(_compress_if_exists 'SRR18973017_2' || true)"
gzs="$(_compress_if_exists 'SRR18973017' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "DM600_6m_rep2_INPUT.fastq.gz"
fi

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973017/SRR18973017"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973018/SRR18973018"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973019/SRR18973019"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973020/SRR18973020"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973021/SRR18973021"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973022/SRR18973022"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973023/SRR18973023"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973024/SRR18973024"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973025/SRR18973025"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973026/SRR18973026"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973027/SRR18973027"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973028/SRR18973028"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973029/SRR18973029"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973030/SRR18973030"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973031/SRR18973031"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973032/SRR18973032"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973032/SRR18973032"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973031/SRR18973031"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973027/SRR18973027"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973026/SRR18973026"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973028/SRR18973028"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973025/SRR18973025"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973029/SRR18973029"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973024/SRR18973024"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973030/SRR18973030"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973023/SRR18973023"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973019/SRR18973019"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973020/SRR18973020"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973018/SRR18973018"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973021/SRR18973021"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973017/SRR18973017"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18973022/SRR18973022"

echo "Download complete. Files saved to $OUTDIR"