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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE92nnn/GSE92602/suppl/GSE92602_Differential_Expression_Results_HFD_vs_ND.xlsx" -O "GSE92602_Differential_Expression_Results_HFD_vs_ND.xlsx"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE92nnn/GSE92602/suppl/GSE92602_Novel_transcripts.xlsx" -O "GSE92602_Novel_transcripts.xlsx"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE92nnn/GSE92602/suppl/GSE92602_mouse_genes_counts_normalized.txt.gz" -O "GSE92602_mouse_genes_counts_normalized.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE92nnn/GSE92602/suppl/GSE92602_raw_counts.txt.gz" -O "GSE92602_raw_counts.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE92nnn/GSE92602/suppl/GSE92602_transcripts.gtf.gz" -O "GSE92602_transcripts.gtf.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 "SRR5121119"
gz1="$(_compress_if_exists 'SRR5121119_1' || true)"
gz2="$(_compress_if_exists 'SRR5121119_2' || true)"
gzs="$(_compress_if_exists 'SRR5121119' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ND1_L7_R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ND1_L7_R2.fq.gz"
fi

fasterq-dump --split-files "SRR5121120"
gz1="$(_compress_if_exists 'SRR5121120_1' || true)"
gz2="$(_compress_if_exists 'SRR5121120_2' || true)"
gzs="$(_compress_if_exists 'SRR5121120' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ND2_L5_R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ND2_L5_R2.fq.gz"
fi

fasterq-dump --split-files "SRR5121121"
gz1="$(_compress_if_exists 'SRR5121121_1' || true)"
gz2="$(_compress_if_exists 'SRR5121121_2' || true)"
gzs="$(_compress_if_exists 'SRR5121121' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ND3_L6_R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "ND3_L6_R2.fq.gz"
fi

fasterq-dump --split-files "SRR5121122"
gz1="$(_compress_if_exists 'SRR5121122_1' || true)"
gz2="$(_compress_if_exists 'SRR5121122_2' || true)"
gzs="$(_compress_if_exists 'SRR5121122' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "HDR1_L4_R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "HDR1_L4_R2.fq.gz"
fi

fasterq-dump --split-files "SRR5121123"
gz1="$(_compress_if_exists 'SRR5121123_1' || true)"
gz2="$(_compress_if_exists 'SRR5121123_2' || true)"
gzs="$(_compress_if_exists 'SRR5121123' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "HDR2_L5_R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "HDR2_L5_R2.fq.gz"
fi

fasterq-dump --split-files "SRR5121124"
gz1="$(_compress_if_exists 'SRR5121124_1' || true)"
gz2="$(_compress_if_exists 'SRR5121124_2' || true)"
gzs="$(_compress_if_exists 'SRR5121124' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "HDR3_L6_R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "HDR3_L6_R2.fq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5121119/SRR5121119"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5121120/SRR5121120"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos5/sra-pub-zq-14/SRR005/121/SRR5121121.sralite.1"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos5/sra-pub-zq-14/SRR005/121/SRR5121122.sralite.1"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos5/sra-pub-zq-14/SRR005/121/SRR5121123.sralite.1"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos5/sra-pub-zq-14/SRR005/121/SRR5121124.sralite.1"

# --- Additional data files ---
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos5/sra-pub-zq-14/SRR005/121/SRR5121124.sralite.1"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5121119/SRR5121119"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5121120/SRR5121120"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos5/sra-pub-zq-14/SRR005/121/SRR5121121.sralite.1"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos5/sra-pub-zq-14/SRR005/121/SRR5121122.sralite.1"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos5/sra-pub-zq-14/SRR005/121/SRR5121123.sralite.1"

echo "Download complete. Files saved to $OUTDIR"