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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE205nnn/GSE205549/suppl/GSE205549_RAW.tar" -O "GSE205549_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 "SRR19560827"
gz1="$(_compress_if_exists 'SRR19560827_1' || true)"
gz2="$(_compress_if_exists 'SRR19560827_2' || true)"
gzs="$(_compress_if_exists 'SRR19560827' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "6_SI_LPL_S6_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "6_SI_LPL_S6_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR19560828"
gz1="$(_compress_if_exists 'SRR19560828_1' || true)"
gz2="$(_compress_if_exists 'SRR19560828_2' || true)"
gzs="$(_compress_if_exists 'SRR19560828' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "4_SI_IEL_S4_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "4_SI_IEL_S4_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR19560829"
gz1="$(_compress_if_exists 'SRR19560829_1' || true)"
gz2="$(_compress_if_exists 'SRR19560829_2' || true)"
gzs="$(_compress_if_exists 'SRR19560829' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "2_CLPL_S2_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "2_CLPL_S2_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR19560830"
gz1="$(_compress_if_exists 'SRR19560830_1' || true)"
gz2="$(_compress_if_exists 'SRR19560830_2' || true)"
gzs="$(_compress_if_exists 'SRR19560830' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "1_CIEL_S1_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "1_CIEL_S1_L001_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/SRR19560827/SRR19560827"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19560828/SRR19560828"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19560829/SRR19560829"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19560830/SRR19560830"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19560830/SRR19560830"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19560829/SRR19560829"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19560828/SRR19560828"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19560827/SRR19560827"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos8/sra-pub-zq-820/SRR019/19560/SRR19560828/SRR19560828.lite.1"

echo "Download complete. Files saved to $OUTDIR"