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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE213nnn/GSE213467/suppl/GSE213467_RAW.tar" -O "GSE213467_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 "SRR21594540"
gz1="$(_compress_if_exists 'SRR21594540_1' || true)"
gz2="$(_compress_if_exists 'SRR21594540_2' || true)"
gzs="$(_compress_if_exists 'SRR21594540' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "12-IgG_C2_S47_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "12-IgG_C2_S47_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR21594541"
gz1="$(_compress_if_exists 'SRR21594541_1' || true)"
gz2="$(_compress_if_exists 'SRR21594541_2' || true)"
gzs="$(_compress_if_exists 'SRR21594541' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "6-IgG_A2_S41_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "6-IgG_A2_S41_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR21594542"
gz1="$(_compress_if_exists 'SRR21594542_1' || true)"
gz2="$(_compress_if_exists 'SRR21594542_2' || true)"
gzs="$(_compress_if_exists 'SRR21594542' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "8-H3K4me3_C2_S43_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "8-H3K4me3_C2_S43_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR21594543"
gz1="$(_compress_if_exists 'SRR21594543_1' || true)"
gz2="$(_compress_if_exists 'SRR21594543_2' || true)"
gzs="$(_compress_if_exists 'SRR21594543' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "2-H3K4me3_A2_S37_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "2-H3K4me3_A2_S37_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR21594544"
gz1="$(_compress_if_exists 'SRR21594544_1' || true)"
gz2="$(_compress_if_exists 'SRR21594544_2' || true)"
gzs="$(_compress_if_exists 'SRR21594544' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10-H3K27me3_C2_S45_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10-H3K27me3_C2_S45_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR21594545"
gz1="$(_compress_if_exists 'SRR21594545_1' || true)"
gz2="$(_compress_if_exists 'SRR21594545_2' || true)"
gzs="$(_compress_if_exists 'SRR21594545' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "4-H3K27me3_A2_S39_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "4-H3K27me3_A2_S39_L002_R2_001.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594540/SRR21594540"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594541/SRR21594541"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594542/SRR21594542"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594543/SRR21594543"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594544/SRR21594544"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594545/SRR21594545"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594544/SRR21594544"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594540/SRR21594540"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594543/SRR21594543"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594545/SRR21594545"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594541/SRR21594541"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594542/SRR21594542"

echo "Download complete. Files saved to $OUTDIR"