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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE86nnn/GSE86043/suppl/GSE86043_RAW.tar" -O "GSE86043_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 "SRR4175328"
gz1="$(_compress_if_exists 'SRR4175328_1' || true)"
gz2="$(_compress_if_exists 'SRR4175328_2' || true)"
gzs="$(_compress_if_exists 'SRR4175328' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "A1-SC_ATTACTCG_L002_R1.fastq.gz"
fi

fasterq-dump --split-files "SRR4175329"
gz1="$(_compress_if_exists 'SRR4175329_1' || true)"
gz2="$(_compress_if_exists 'SRR4175329_2' || true)"
gzs="$(_compress_if_exists 'SRR4175329' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "A2-SC_TCCGGAGA_L002_R1.fastq.gz"
fi

fasterq-dump --split-files "SRR4175330"
gz1="$(_compress_if_exists 'SRR4175330_1' || true)"
gz2="$(_compress_if_exists 'SRR4175330_2' || true)"
gzs="$(_compress_if_exists 'SRR4175330' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "A3-SC_CGCTCATT_L002_R1.fastq.gz"
fi

fasterq-dump --split-files "SRR4175331"
gz1="$(_compress_if_exists 'SRR4175331_1' || true)"
gz2="$(_compress_if_exists 'SRR4175331_2' || true)"
gzs="$(_compress_if_exists 'SRR4175331' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "D1-SC_GAGATTCC_L002_R1.fastq.gz"
fi

fasterq-dump --split-files "SRR4175332"
gz1="$(_compress_if_exists 'SRR4175332_1' || true)"
gz2="$(_compress_if_exists 'SRR4175332_2' || true)"
gzs="$(_compress_if_exists 'SRR4175332' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "D2-SC_ATTCAGAA-TATAGCCT_L003_R1.fastq.gz"
fi

fasterq-dump --split-files "SRR4175333"
gz1="$(_compress_if_exists 'SRR4175333_1' || true)"
gz2="$(_compress_if_exists 'SRR4175333_2' || true)"
gzs="$(_compress_if_exists 'SRR4175333' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "D3-SC_GAATTCGT-TATAGCCT_L003_R1.fastq.gz"
fi

fasterq-dump --split-files "SRR4175334"
gz1="$(_compress_if_exists 'SRR4175334_1' || true)"
gz2="$(_compress_if_exists 'SRR4175334_2' || true)"
gzs="$(_compress_if_exists 'SRR4175334' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "D4-SC_CTGAAGCT-TATAGCCT_L003_R1.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175328/SRR4175328"
curl -L -O -C - "https://sra-pub-src-1.s3.amazonaws.com/SRR4175329/A2-SC_TCCGGAGA_L002_R1.fastq.gz.1"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175330/SRR4175330"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175331/SRR4175331"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175332/SRR4175332"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175333/SRR4175333"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175334/SRR4175334"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175328/SRR4175328"
curl -L -O -C - "https://sra-pub-src-1.s3.amazonaws.com/SRR4175329/A2-SC_TCCGGAGA_L002_R1.fastq.gz.1"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175330/SRR4175330"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175331/SRR4175331"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175332/SRR4175332"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175333/SRR4175333"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175334/SRR4175334"

echo "Download complete. Files saved to $OUTDIR"