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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE230nnn/GSE230097/suppl/GSE230097_RAW.tar" -O "GSE230097_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 "SRR24219249"
gz1="$(_compress_if_exists 'SRR24219249_1' || true)"
gz2="$(_compress_if_exists 'SRR24219249_2' || true)"
gzs="$(_compress_if_exists 'SRR24219249' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample16_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample16_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219250"
gz1="$(_compress_if_exists 'SRR24219250_1' || true)"
gz2="$(_compress_if_exists 'SRR24219250_2' || true)"
gzs="$(_compress_if_exists 'SRR24219250' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample15_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample15_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219251"
gz1="$(_compress_if_exists 'SRR24219251_1' || true)"
gz2="$(_compress_if_exists 'SRR24219251_2' || true)"
gzs="$(_compress_if_exists 'SRR24219251' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample14_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample14_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219252"
gz1="$(_compress_if_exists 'SRR24219252_1' || true)"
gz2="$(_compress_if_exists 'SRR24219252_2' || true)"
gzs="$(_compress_if_exists 'SRR24219252' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample13_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample13_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219253"
gz1="$(_compress_if_exists 'SRR24219253_1' || true)"
gz2="$(_compress_if_exists 'SRR24219253_2' || true)"
gzs="$(_compress_if_exists 'SRR24219253' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample12_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample12_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219254"
gz1="$(_compress_if_exists 'SRR24219254_1' || true)"
gz2="$(_compress_if_exists 'SRR24219254_2' || true)"
gzs="$(_compress_if_exists 'SRR24219254' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample11_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample11_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219255"
gz1="$(_compress_if_exists 'SRR24219255_1' || true)"
gz2="$(_compress_if_exists 'SRR24219255_2' || true)"
gzs="$(_compress_if_exists 'SRR24219255' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample10_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample10_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219256"
gz1="$(_compress_if_exists 'SRR24219256_1' || true)"
gz2="$(_compress_if_exists 'SRR24219256_2' || true)"
gzs="$(_compress_if_exists 'SRR24219256' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample9_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample9_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219257"
gz1="$(_compress_if_exists 'SRR24219257_1' || true)"
gz2="$(_compress_if_exists 'SRR24219257_2' || true)"
gzs="$(_compress_if_exists 'SRR24219257' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample8_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample8_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219258"
gz1="$(_compress_if_exists 'SRR24219258_1' || true)"
gz2="$(_compress_if_exists 'SRR24219258_2' || true)"
gzs="$(_compress_if_exists 'SRR24219258' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample7_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample7_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219259"
gz1="$(_compress_if_exists 'SRR24219259_1' || true)"
gz2="$(_compress_if_exists 'SRR24219259_2' || true)"
gzs="$(_compress_if_exists 'SRR24219259' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample6_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample6_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219260"
gz1="$(_compress_if_exists 'SRR24219260_1' || true)"
gz2="$(_compress_if_exists 'SRR24219260_2' || true)"
gzs="$(_compress_if_exists 'SRR24219260' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample5_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample5_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219261"
gz1="$(_compress_if_exists 'SRR24219261_1' || true)"
gz2="$(_compress_if_exists 'SRR24219261_2' || true)"
gzs="$(_compress_if_exists 'SRR24219261' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample4_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample4_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219262"
gz1="$(_compress_if_exists 'SRR24219262_1' || true)"
gz2="$(_compress_if_exists 'SRR24219262_2' || true)"
gzs="$(_compress_if_exists 'SRR24219262' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample3_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample3_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219263"
gz1="$(_compress_if_exists 'SRR24219263_1' || true)"
gz2="$(_compress_if_exists 'SRR24219263_2' || true)"
gzs="$(_compress_if_exists 'SRR24219263' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample2_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample2_r2.fq.gz"
fi

fasterq-dump --split-files "SRR24219264"
gz1="$(_compress_if_exists 'SRR24219264_1' || true)"
gz2="$(_compress_if_exists 'SRR24219264_2' || true)"
gzs="$(_compress_if_exists 'SRR24219264' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10249_sample1_r1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10249_sample1_r2.fq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219249/SRR24219249"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219250/SRR24219250"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219251/SRR24219251"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219252/SRR24219252"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219253/SRR24219253"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219254/SRR24219254"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219255/SRR24219255"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219256/SRR24219256"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219257/SRR24219257"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219258/SRR24219258"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219259/SRR24219259"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219260/SRR24219260"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219261/SRR24219261"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219262/SRR24219262"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219263/SRR24219263"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219264/SRR24219264"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219255/SRR24219255"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219254/SRR24219254"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219253/SRR24219253"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219252/SRR24219252"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219251/SRR24219251"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219250/SRR24219250"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219249/SRR24219249"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219264/SRR24219264"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219263/SRR24219263"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219262/SRR24219262"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219261/SRR24219261"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219260/SRR24219260"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219259/SRR24219259"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219258/SRR24219258"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219257/SRR24219257"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR24219256/SRR24219256"

echo "Download complete. Files saved to $OUTDIR"