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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE134nnn/GSE134597/suppl/GSE134597_msi_polysome_RPKM.csv.gz" -O "GSE134597_msi_polysome_RPKM.csv.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 "SRR9722509"
gz1="$(_compress_if_exists 'SRR9722509_1' || true)"
gz2="$(_compress_if_exists 'SRR9722509_2' || true)"
gzs="$(_compress_if_exists 'SRR9722509' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "GFP2_S26_L003_R1_001.fastq.gz"
fi

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

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

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

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

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

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

fasterq-dump --split-files "SRR9722516"
gz1="$(_compress_if_exists 'SRR9722516_1' || true)"
gz2="$(_compress_if_exists 'SRR9722516_2' || true)"
gzs="$(_compress_if_exists 'SRR9722516' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MSI3_poly_S36_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/SRR9722509/SRR9722509"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722510/SRR9722510"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722511/SRR9722511"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722512/SRR9722512"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722513/SRR9722513"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722514/SRR9722514"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722515/SRR9722515"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722516/SRR9722516"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722510/SRR9722510"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722509/SRR9722509"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722512/SRR9722512"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722511/SRR9722511"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722514/SRR9722514"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722513/SRR9722513"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722516/SRR9722516"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9722515/SRR9722515"

echo "Download complete. Files saved to $OUTDIR"