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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE124nnn/GSE124023/suppl/GSE124023_DDX5.IDR_reproducible_peaks.txt.gz" -O "GSE124023_DDX5.IDR_reproducible_peaks.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE124nnn/GSE124023/suppl/GSE124023_GT_G.IDR_reproducible_peaks.txt.gz" -O "GSE124023_GT_G.IDR_reproducible_peaks.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE124nnn/GSE124023/suppl/GSE124023_RAW.tar" -O "GSE124023_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 "SRR11590535"
gz1="$(_compress_if_exists 'SRR11590535_1' || true)"
gz2="$(_compress_if_exists 'SRR11590535_2' || true)"
gzs="$(_compress_if_exists 'SRR11590535' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "IP_G1_S37_L003_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "IP_G1_S37_L003_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11590536"
gz1="$(_compress_if_exists 'SRR11590536_1' || true)"
gz2="$(_compress_if_exists 'SRR11590536_2' || true)"
gzs="$(_compress_if_exists 'SRR11590536' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "INPUT_G1_S36_L003_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "INPUT_G1_S36_L003_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11590537"
gz1="$(_compress_if_exists 'SRR11590537_1' || true)"
gz2="$(_compress_if_exists 'SRR11590537_2' || true)"
gzs="$(_compress_if_exists 'SRR11590537' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "IP_G2_S39_L003_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "IP_G2_S39_L003_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11590538"
gz1="$(_compress_if_exists 'SRR11590538_1' || true)"
gz2="$(_compress_if_exists 'SRR11590538_2' || true)"
gzs="$(_compress_if_exists 'SRR11590538' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "INPUT_G2_S38_L003_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "INPUT_G2_S38_L003_R2_001.fastq.gz"
fi

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

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

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

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

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

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

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

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


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11590535/SRR11590535"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11590536/SRR11590536"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11590537/SRR11590537"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11590538/SRR11590538"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591014/SRR11591014"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591015/SRR11591015"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591016/SRR11591016"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591017/SRR11591017"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591018/SRR11591018"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591019/SRR11591019"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591020/SRR11591020"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591021/SRR11591021"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11590536/SRR11590536"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11590538/SRR11590538"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11590535/SRR11590535"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11590537/SRR11590537"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591016/SRR11591016"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591017/SRR11591017"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591014/SRR11591014"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591015/SRR11591015"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591020/SRR11591020"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591021/SRR11591021"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591018/SRR11591018"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11591019/SRR11591019"

echo "Download complete. Files saved to $OUTDIR"