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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE117nnn/GSE117291/suppl/GSE117291_293T_KO_UBAP2L.polysome_RPKM.csv.gz" -O "GSE117291_293T_KO_UBAP2L.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 "SRR7547726"
gz1="$(_compress_if_exists 'SRR7547726_1' || true)"
gz2="$(_compress_if_exists 'SRR7547726_2' || true)"
gzs="$(_compress_if_exists 'SRR7547726' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "EC_Input_293T_1_S1_L001_R1_001.fastq.gz"
fi

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

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

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

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

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

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

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

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

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

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

fasterq-dump --split-files "SRR7547737"
gz1="$(_compress_if_exists 'SRR7547737_1' || true)"
gz2="$(_compress_if_exists 'SRR7547737_2' || true)"
gzs="$(_compress_if_exists 'SRR7547737' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "EC_Poly_KO9_2_S12_L001_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/SRR7547726/SRR7547726"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547727/SRR7547727"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547728/SRR7547728"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547729/SRR7547729"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547730/SRR7547730"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547731/SRR7547731"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547732/SRR7547732"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547733/SRR7547733"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547734/SRR7547734"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547735/SRR7547735"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547736/SRR7547736"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547737/SRR7547737"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547726/SRR7547726"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547727/SRR7547727"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547728/SRR7547728"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547729/SRR7547729"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547730/SRR7547730"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547731/SRR7547731"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547732/SRR7547732"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547733/SRR7547733"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547734/SRR7547734"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547735/SRR7547735"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547736/SRR7547736"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7547737/SRR7547737"

echo "Download complete. Files saved to $OUTDIR"