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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE237nnn/GSE237099/suppl/GSE237099_1_unloading_reloading_Reloading_vs_Control_deseq2.txt.gz" -O "GSE237099_1_unloading_reloading_Reloading_vs_Control_deseq2.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE237nnn/GSE237099/suppl/GSE237099_1_unloading_reloading_Reloading_vs_Unloading_deseq2.txt.gz" -O "GSE237099_1_unloading_reloading_Reloading_vs_Unloading_deseq2.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE237nnn/GSE237099/suppl/GSE237099_1_unloading_reloading_Unloading_vs_Control_deseq2.txt.gz" -O "GSE237099_1_unloading_reloading_Unloading_vs_Control_deseq2.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE237nnn/GSE237099/suppl/GSE237099_featureCounts_output.txt.gz" -O "GSE237099_featureCounts_output.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 "SRR25238416"
gz1="$(_compress_if_exists 'SRR25238416_1' || true)"
gz2="$(_compress_if_exists 'SRR25238416_2' || true)"
gzs="$(_compress_if_exists 'SRR25238416' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "sus_2_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "sus_2_2.fastq.gz"
fi

fasterq-dump --split-files "SRR25238417"
gz1="$(_compress_if_exists 'SRR25238417_1' || true)"
gz2="$(_compress_if_exists 'SRR25238417_2' || true)"
gzs="$(_compress_if_exists 'SRR25238417' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "sus_1_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "sus_1_2.fastq.gz"
fi

fasterq-dump --split-files "SRR25238418"
gz1="$(_compress_if_exists 'SRR25238418_1' || true)"
gz2="$(_compress_if_exists 'SRR25238418_2' || true)"
gzs="$(_compress_if_exists 'SRR25238418' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "rel_4_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "rel_4_2.fastq.gz"
fi

fasterq-dump --split-files "SRR25238419"
gz1="$(_compress_if_exists 'SRR25238419_1' || true)"
gz2="$(_compress_if_exists 'SRR25238419_2' || true)"
gzs="$(_compress_if_exists 'SRR25238419' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "rel_3_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "rel_3_2.fastq.gz"
fi

fasterq-dump --split-files "SRR25238420"
gz1="$(_compress_if_exists 'SRR25238420_1' || true)"
gz2="$(_compress_if_exists 'SRR25238420_2' || true)"
gzs="$(_compress_if_exists 'SRR25238420' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "rel_2_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "rel_2_2.fastq.gz"
fi

fasterq-dump --split-files "SRR25238421"
gz1="$(_compress_if_exists 'SRR25238421_1' || true)"
gz2="$(_compress_if_exists 'SRR25238421_2' || true)"
gzs="$(_compress_if_exists 'SRR25238421' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ctrl_4_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ctrl_4_2.fastq.gz"
fi

fasterq-dump --split-files "SRR25238422"
gz1="$(_compress_if_exists 'SRR25238422_1' || true)"
gz2="$(_compress_if_exists 'SRR25238422_2' || true)"
gzs="$(_compress_if_exists 'SRR25238422' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ctrl_3_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ctrl_3_2.fastq.gz"
fi

fasterq-dump --split-files "SRR25238423"
gz1="$(_compress_if_exists 'SRR25238423_1' || true)"
gz2="$(_compress_if_exists 'SRR25238423_2' || true)"
gzs="$(_compress_if_exists 'SRR25238423' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ctrl_2_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ctrl_2_2.fastq.gz"
fi

fasterq-dump --split-files "SRR25238424"
gz1="$(_compress_if_exists 'SRR25238424_1' || true)"
gz2="$(_compress_if_exists 'SRR25238424_2' || true)"
gzs="$(_compress_if_exists 'SRR25238424' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ctrl_1_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ctrl_1_2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238416/SRR25238416"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238417/SRR25238417"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238418/SRR25238418"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238419/SRR25238419"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238420/SRR25238420"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238421/SRR25238421"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238422/SRR25238422"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238423/SRR25238423"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238424/SRR25238424"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238424/SRR25238424"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238423/SRR25238423"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238422/SRR25238422"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238421/SRR25238421"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238420/SRR25238420"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238419/SRR25238419"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238418/SRR25238418"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238417/SRR25238417"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR25238416/SRR25238416"

echo "Download complete. Files saved to $OUTDIR"