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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE271nnn/GSE271652/suppl/GSE271652_RAW.tar" -O "GSE271652_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 "SRR29727696"
gz1="$(_compress_if_exists 'SRR29727696_1' || true)"
gz2="$(_compress_if_exists 'SRR29727696_2' || true)"
gzs="$(_compress_if_exists 'SRR29727696' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "cKOLPS3_C03_ZHO2243A9-1_S9_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "cKOLPS3_C03_ZHO2243A9-1_S9_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727697"
gz1="$(_compress_if_exists 'SRR29727697_1' || true)"
gz2="$(_compress_if_exists 'SRR29727697_2' || true)"
gzs="$(_compress_if_exists 'SRR29727697' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "cKOLPS2_C02_ZHO2243A8-1_S8_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "cKOLPS2_C02_ZHO2243A8-1_S8_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727698"
gz1="$(_compress_if_exists 'SRR29727698_1' || true)"
gz2="$(_compress_if_exists 'SRR29727698_2' || true)"
gzs="$(_compress_if_exists 'SRR29727698' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WTLPS3_B03_ZHO2243A6-1_S6_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WTLPS3_B03_ZHO2243A6-1_S6_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727699"
gz1="$(_compress_if_exists 'SRR29727699_1' || true)"
gz2="$(_compress_if_exists 'SRR29727699_2' || true)"
gzs="$(_compress_if_exists 'SRR29727699' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WTLPS1_B01_ZHO2243A4-1_S4_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WTLPS1_B01_ZHO2243A4-1_S4_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727700"
gz1="$(_compress_if_exists 'SRR29727700_1' || true)"
gz2="$(_compress_if_exists 'SRR29727700_2' || true)"
gzs="$(_compress_if_exists 'SRR29727700' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT3_A03_ZHO2243A3-1_S3_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WT3_A03_ZHO2243A3-1_S3_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727701"
gz1="$(_compress_if_exists 'SRR29727701_1' || true)"
gz2="$(_compress_if_exists 'SRR29727701_2' || true)"
gzs="$(_compress_if_exists 'SRR29727701' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WTLPS2_B02_ZHO2243A5-1_S5_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WTLPS2_B02_ZHO2243A5-1_S5_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727702"
gz1="$(_compress_if_exists 'SRR29727702_1' || true)"
gz2="$(_compress_if_exists 'SRR29727702_2' || true)"
gzs="$(_compress_if_exists 'SRR29727702' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "cKOLPS1_C01_ZHO2243A7-1_S7_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "cKOLPS1_C01_ZHO2243A7-1_S7_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727703"
gz1="$(_compress_if_exists 'SRR29727703_1' || true)"
gz2="$(_compress_if_exists 'SRR29727703_2' || true)"
gzs="$(_compress_if_exists 'SRR29727703' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT2_A02_ZHO2243A2-1_S2_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WT2_A02_ZHO2243A2-1_S2_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727704"
gz1="$(_compress_if_exists 'SRR29727704_1' || true)"
gz2="$(_compress_if_exists 'SRR29727704_2' || true)"
gzs="$(_compress_if_exists 'SRR29727704' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT1_A01_ZHO2243A1-1_S1_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WT1_A01_ZHO2243A1-1_S1_L002_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/SRR29727696/SRR29727696"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727697/SRR29727697"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727698/SRR29727698"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727699/SRR29727699"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727700/SRR29727700"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727701/SRR29727701"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727702/SRR29727702"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727703/SRR29727703"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727704/SRR29727704"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727702/SRR29727702"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727697/SRR29727697"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727696/SRR29727696"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727704/SRR29727704"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727703/SRR29727703"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727700/SRR29727700"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727699/SRR29727699"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727701/SRR29727701"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727698/SRR29727698"

echo "Download complete. Files saved to $OUTDIR"