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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE198nnn/GSE198899/suppl/GSE198899_Ctrl_polyVSinput_0.diffexp.tsv.gz" -O "GSE198899_Ctrl_polyVSinput_0.diffexp.tsv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE198nnn/GSE198899/suppl/GSE198899_ORF9c_polyVSinput_0.diffexp.tsv.gz" -O "GSE198899_ORF9c_polyVSinput_0.diffexp.tsv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE198nnn/GSE198899/suppl/GSE198899_counts_for_pub.txt.gz" -O "GSE198899_counts_for_pub.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 "SRR18360416"
gz1="$(_compress_if_exists 'SRR18360416_1' || true)"
gz2="$(_compress_if_exists 'SRR18360416_2' || true)"
gzs="$(_compress_if_exists 'SRR18360416' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Ctrl-input-2_S44_L002_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR18360417"
gz1="$(_compress_if_exists 'SRR18360417_1' || true)"
gz2="$(_compress_if_exists 'SRR18360417_2' || true)"
gzs="$(_compress_if_exists 'SRR18360417' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Ctrl-input-1_S43_L002_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR18360418"
gz1="$(_compress_if_exists 'SRR18360418_1' || true)"
gz2="$(_compress_if_exists 'SRR18360418_2' || true)"
gzs="$(_compress_if_exists 'SRR18360418' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Ctrl-poly-2_S50_L002_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR18360419"
gz1="$(_compress_if_exists 'SRR18360419_1' || true)"
gz2="$(_compress_if_exists 'SRR18360419_2' || true)"
gzs="$(_compress_if_exists 'SRR18360419' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Ctrl-poly-1_S49_L002_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR18360420"
gz1="$(_compress_if_exists 'SRR18360420_1' || true)"
gz2="$(_compress_if_exists 'SRR18360420_2' || true)"
gzs="$(_compress_if_exists 'SRR18360420' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ORF9c-input-2_S42_L002_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR18360421"
gz1="$(_compress_if_exists 'SRR18360421_1' || true)"
gz2="$(_compress_if_exists 'SRR18360421_2' || true)"
gzs="$(_compress_if_exists 'SRR18360421' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ORF9c-input-1_S41_L002_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR18360422"
gz1="$(_compress_if_exists 'SRR18360422_1' || true)"
gz2="$(_compress_if_exists 'SRR18360422_2' || true)"
gzs="$(_compress_if_exists 'SRR18360422' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ORF9c-poly-2_S48_L002_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR18360423"
gz1="$(_compress_if_exists 'SRR18360423_1' || true)"
gz2="$(_compress_if_exists 'SRR18360423_2' || true)"
gzs="$(_compress_if_exists 'SRR18360423' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ORF9c-poly-1_S47_L002_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/SRR18360416/SRR18360416"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360417/SRR18360417"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360418/SRR18360418"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360419/SRR18360419"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360420/SRR18360420"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360421/SRR18360421"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360422/SRR18360422"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360423/SRR18360423"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360417/SRR18360417"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360416/SRR18360416"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360419/SRR18360419"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360418/SRR18360418"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360421/SRR18360421"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360420/SRR18360420"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360423/SRR18360423"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR18360422/SRR18360422"

echo "Download complete. Files saved to $OUTDIR"