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

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

# --- GEO supplementary files ---
wget -nc ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE80nnn/GSE80664/suppl/GSE80664_MISO.SE_S5407vS11.txt.gz -O GSE80664_MISO.SE_S5407vS11.txt.gz
wget -nc ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE80nnn/GSE80664/suppl/GSE80664_MISO.SE_S5407vS15.txt.gz -O GSE80664_MISO.SE_S5407vS15.txt.gz
wget -nc ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE80nnn/GSE80664/suppl/GSE80664_MISO.SE_S5407vS24.txt.gz -O GSE80664_MISO.SE_S5407vS24.txt.gz
wget -nc ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE80nnn/GSE80664/suppl/GSE80664_MISO.SE_S5408vS11.txt.gz -O GSE80664_MISO.SE_S5408vS11.txt.gz
wget -nc ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE80nnn/GSE80664/suppl/GSE80664_MISO.SE_S5408vS15.txt.gz -O GSE80664_MISO.SE_S5408vS15.txt.gz
wget -nc ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE80nnn/GSE80664/suppl/GSE80664_MISO.SE_S5408vS24.txt.gz -O GSE80664_MISO.SE_S5408vS24.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 SRR3499912
gz1="$(_compress_if_exists SRR3499912_1 || true)"
gz2="$(_compress_if_exists SRR3499912_2 || true)"
gzs="$(_compress_if_exists SRR3499912 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" 5407_NoIndex_L007_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" 5407_NoIndex_L007_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR3499913
gz1="$(_compress_if_exists SRR3499913_1 || true)"
gz2="$(_compress_if_exists SRR3499913_2 || true)"
gzs="$(_compress_if_exists SRR3499913 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" 5408_NoIndex_L008_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" 5408_NoIndex_L008_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR3499914
gz1="$(_compress_if_exists SRR3499914_1 || true)"
gz2="$(_compress_if_exists SRR3499914_2 || true)"
gzs="$(_compress_if_exists SRR3499914 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" 11_NoIndex_L001_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" 11_NoIndex_L001_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR3499915
gz1="$(_compress_if_exists SRR3499915_1 || true)"
gz2="$(_compress_if_exists SRR3499915_2 || true)"
gzs="$(_compress_if_exists SRR3499915 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" 15_NoIndex_L002_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" 15_NoIndex_L002_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR3499916
gz1="$(_compress_if_exists SRR3499916_1 || true)"
gz2="$(_compress_if_exists SRR3499916_2 || true)"
gzs="$(_compress_if_exists SRR3499916 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" 24_NoIndex_L003_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" 24_NoIndex_L003_R2_001.fastq.gz
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3499912/SRR3499912
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3499913/SRR3499913
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3499914/SRR3499914
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3499915/SRR3499915
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3499916/SRR3499916

# --- Additional data files ---
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3499914/SRR3499914
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3499915/SRR3499915
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3499916/SRR3499916
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3499912/SRR3499912
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3499913/SRR3499913

echo "Download complete. Files saved to $OUTDIR"