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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE179nnn/GSE179634/suppl/GSE179634_RAW.tar" -O "GSE179634_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 "SRR15058673"
gz1="$(_compress_if_exists 'SRR15058673_1' || true)"
gz2="$(_compress_if_exists 'SRR15058673_2' || true)"
gzs="$(_compress_if_exists 'SRR15058673' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SRSF1.SRSF1_WT_CLIP_A.r1.reformatted.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SRSF1.SRSF1_WT_CLIP_A.r2.reformatted.fq.gz"
fi

fasterq-dump --split-files "SRR15058674"
gz1="$(_compress_if_exists 'SRR15058674_1' || true)"
gz2="$(_compress_if_exists 'SRR15058674_2' || true)"
gzs="$(_compress_if_exists 'SRR15058674' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SRSF1.SRSF1_WT_CLIP_B.r1.reformatted.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SRSF1.SRSF1_WT_CLIP_B.r2.reformatted.fq.gz"
fi

fasterq-dump --split-files "SRR15058675"
gz1="$(_compress_if_exists 'SRR15058675_1' || true)"
gz2="$(_compress_if_exists 'SRR15058675_2' || true)"
gzs="$(_compress_if_exists 'SRR15058675' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SRSF1_WTA_INPUT_S10_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SRSF1.SRSF1_WTA_INPUT.NIL.r2.fq.gz.addrandomer.fq.gz"
fi

fasterq-dump --split-files "SRR15058676"
gz1="$(_compress_if_exists 'SRR15058676_1' || true)"
gz2="$(_compress_if_exists 'SRR15058676_2' || true)"
gzs="$(_compress_if_exists 'SRR15058676' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SRSF1_WTB_INPUT_S11_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SRSF1.SRSF1_WTB_INPUT.NIL.r2.fq.gz.addrandomer.fq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15058673/SRR15058673"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos9/sra-pub-zq-924/SRR015/15058/SRR15058674/SRR15058674.lite.1"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15058675/SRR15058675"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15058676/SRR15058676"

# --- Additional data files ---
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos9/sra-pub-zq-924/SRR015/15058/SRR15058674/SRR15058674.lite.1"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15058675/SRR15058675"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15058676/SRR15058676"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR15058673/SRR15058673"

echo "Download complete. Files saved to $OUTDIR"