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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE180nnn/GSE180129/suppl/GSE180129_RAW.tar" -O "GSE180129_RAW.tar"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE180nnn/GSE180129/suppl/GSE180129_cellsB_rep1.vs.cellsB_rep2.bed.gz" -O "GSE180129_cellsB_rep1.vs.cellsB_rep2.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 "SRR15142308"
gz1="$(_compress_if_exists 'SRR15142308_1' || true)"
gz2="$(_compress_if_exists 'SRR15142308_2' || true)"
gzs="$(_compress_if_exists 'SRR15142308' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Au2_S2_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Au2_S2_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR15142309"
gz1="$(_compress_if_exists 'SRR15142309_1' || true)"
gz2="$(_compress_if_exists 'SRR15142309_2' || true)"
gzs="$(_compress_if_exists 'SRR15142309' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Au3_S3_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Au3_S3_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR15142310"
gz1="$(_compress_if_exists 'SRR15142310_1' || true)"
gz2="$(_compress_if_exists 'SRR15142310_2' || true)"
gzs="$(_compress_if_exists 'SRR15142310' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Au6_S6_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Au6_S6_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR15142311"
gz1="$(_compress_if_exists 'SRR15142311_1' || true)"
gz2="$(_compress_if_exists 'SRR15142311_2' || true)"
gzs="$(_compress_if_exists 'SRR15142311' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Au7_S7_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Au7_S7_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR15142312"
gz1="$(_compress_if_exists 'SRR15142312_1' || true)"
gz2="$(_compress_if_exists 'SRR15142312_2' || true)"
gzs="$(_compress_if_exists 'SRR15142312' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Au13_S13_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Au13_S13_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR15142313"
gz1="$(_compress_if_exists 'SRR15142313_1' || true)"
gz2="$(_compress_if_exists 'SRR15142313_2' || true)"
gzs="$(_compress_if_exists 'SRR15142313' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Au9_S9_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Au9_S9_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR15236468"
gz1="$(_compress_if_exists 'SRR15236468_1' || true)"
gz2="$(_compress_if_exists 'SRR15236468_2' || true)"
gzs="$(_compress_if_exists 'SRR15236468' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Au1_S1_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Au1_S1_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR15236469"
gz1="$(_compress_if_exists 'SRR15236469_1' || true)"
gz2="$(_compress_if_exists 'SRR15236469_2' || true)"
gzs="$(_compress_if_exists 'SRR15236469' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Au5_S5_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Au5_S5_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR17259466"
gz1="$(_compress_if_exists 'SRR17259466_1' || true)"
gz2="$(_compress_if_exists 'SRR17259466_2' || true)"
gzs="$(_compress_if_exists 'SRR17259466' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Au14_S14_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Au14_S14_L001_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/SRR15142308/SRR15142308"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15142309/SRR15142309"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15142310/SRR15142310"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15142311/SRR15142311"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15142312/SRR15142312"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15142313/SRR15142313"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15236468/SRR15236468"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15236469/SRR15236469"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17259466/SRR17259466"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15236468/SRR15236468"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15236469/SRR15236469"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15142312/SRR15142312"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17259466/SRR17259466"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15142308/SRR15142308"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15142309/SRR15142309"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15142310/SRR15142310"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15142311/SRR15142311"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15142313/SRR15142313"

echo "Download complete. Files saved to $OUTDIR"