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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE135nnn/GSE135012/suppl/GSE135012_sleuth5.txt.gz" -O "GSE135012_sleuth5.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE135nnn/GSE135012/suppl/GSE135012_tpm_values.txt.gz" -O "GSE135012_tpm_values.txt.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 "SRR9853389"
gz1="$(_compress_if_exists 'SRR9853389_1' || true)"
gz2="$(_compress_if_exists 'SRR9853389_2' || true)"
gzs="$(_compress_if_exists 'SRR9853389' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CRT1_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CRT1_2.fastq.gz"
fi

fasterq-dump --split-files "SRR9853390"
gz1="$(_compress_if_exists 'SRR9853390_1' || true)"
gz2="$(_compress_if_exists 'SRR9853390_2' || true)"
gzs="$(_compress_if_exists 'SRR9853390' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CRT2_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CRT2_2.fastq.gz"
fi

fasterq-dump --split-files "SRR9853391"
gz1="$(_compress_if_exists 'SRR9853391_1' || true)"
gz2="$(_compress_if_exists 'SRR9853391_2' || true)"
gzs="$(_compress_if_exists 'SRR9853391' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CRT3_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CRT3_2.fastq.gz"
fi

fasterq-dump --split-files "SRR9853392"
gz1="$(_compress_if_exists 'SRR9853392_1' || true)"
gz2="$(_compress_if_exists 'SRR9853392_2' || true)"
gzs="$(_compress_if_exists 'SRR9853392' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "STAU1_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "STAU1_2.fastq.gz"
fi

fasterq-dump --split-files "SRR9853393"
gz1="$(_compress_if_exists 'SRR9853393_1' || true)"
gz2="$(_compress_if_exists 'SRR9853393_2' || true)"
gzs="$(_compress_if_exists 'SRR9853393' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "STAU2_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "STAU2_2.fastq.gz"
fi

fasterq-dump --split-files "SRR9853394"
gz1="$(_compress_if_exists 'SRR9853394_1' || true)"
gz2="$(_compress_if_exists 'SRR9853394_2' || true)"
gzs="$(_compress_if_exists 'SRR9853394' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "STAU3_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "STAU3_2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9853389/SRR9853389"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9853390/SRR9853390"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9853391/SRR9853391"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9853392/SRR9853392"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9853393/SRR9853393"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9853394/SRR9853394"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9853389/SRR9853389"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9853390/SRR9853390"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9853391/SRR9853391"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9853392/SRR9853392"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9853393/SRR9853393"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR9853394/SRR9853394"

echo "Download complete. Files saved to $OUTDIR"