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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE173nnn/GSE173507/suppl/GSE173507_RAW.tar" -O "GSE173507_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 "SRR14346737"
gz1="$(_compress_if_exists 'SRR14346737_1' || true)"
gz2="$(_compress_if_exists 'SRR14346737_2' || true)"
gzs="$(_compress_if_exists 'SRR14346737' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "A549-APO-moi-3_S9_L004_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "A549-APO-moi-3_S9_L004_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR14346738"
gz1="$(_compress_if_exists 'SRR14346738_1' || true)"
gz2="$(_compress_if_exists 'SRR14346738_2' || true)"
gzs="$(_compress_if_exists 'SRR14346738' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Veros-APO-moi-0p1_S1_L004_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Veros-APO-moi-0p1_S1_L004_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR14346739"
gz1="$(_compress_if_exists 'SRR14346739_1' || true)"
gz2="$(_compress_if_exists 'SRR14346739_2' || true)"
gzs="$(_compress_if_exists 'SRR14346739' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT-1__S34_L004_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WT-1__S34_L004_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR14346740"
gz1="$(_compress_if_exists 'SRR14346740_1' || true)"
gz2="$(_compress_if_exists 'SRR14346740_2' || true)"
gzs="$(_compress_if_exists 'SRR14346740' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT-2_S21_L004_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WT-2_S21_L004_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/SRR14346737/SRR14346737"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR14346738/SRR14346738"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR14346739/SRR14346739"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR14346740/SRR14346740"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR14346737/SRR14346737"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR14346738/SRR14346738"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR14346739/SRR14346739"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR14346740/SRR14346740"

echo "Download complete. Files saved to $OUTDIR"