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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE136nnn/GSE136911/suppl/GSE136911_RAW.tar" -O "GSE136911_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 "SRR10071167"
gz1="$(_compress_if_exists 'SRR10071167_1' || true)"
gz2="$(_compress_if_exists 'SRR10071167_2' || true)"
gzs="$(_compress_if_exists 'SRR10071167' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT_1_SAT_S52_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR10071168"
gz1="$(_compress_if_exists 'SRR10071168_1' || true)"
gz2="$(_compress_if_exists 'SRR10071168_2' || true)"
gzs="$(_compress_if_exists 'SRR10071168' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT_2_SAT_S51_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR10071169"
gz1="$(_compress_if_exists 'SRR10071169_1' || true)"
gz2="$(_compress_if_exists 'SRR10071169_2' || true)"
gzs="$(_compress_if_exists 'SRR10071169' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT_3_SAT_S74_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR10071170"
gz1="$(_compress_if_exists 'SRR10071170_1' || true)"
gz2="$(_compress_if_exists 'SRR10071170_2' || true)"
gzs="$(_compress_if_exists 'SRR10071170' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "AGO_1_Hete_SAT_S54_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR10071171"
gz1="$(_compress_if_exists 'SRR10071171_1' || true)"
gz2="$(_compress_if_exists 'SRR10071171_2' || true)"
gzs="$(_compress_if_exists 'SRR10071171' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "AGO_2_Hete_SAT_S56_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR10071172"
gz1="$(_compress_if_exists 'SRR10071172_1' || true)"
gz2="$(_compress_if_exists 'SRR10071172_2' || true)"
gzs="$(_compress_if_exists 'SRR10071172' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "AGO_3_Hete_SAT_S55_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR10071173"
gz1="$(_compress_if_exists 'SRR10071173_1' || true)"
gz2="$(_compress_if_exists 'SRR10071173_2' || true)"
gzs="$(_compress_if_exists 'SRR10071173' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "AGO_1_Homo_SAT_S59_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR10071174"
gz1="$(_compress_if_exists 'SRR10071174_1' || true)"
gz2="$(_compress_if_exists 'SRR10071174_2' || true)"
gzs="$(_compress_if_exists 'SRR10071174' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "AGO_2_Homo_SAT_S57_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR10071175"
gz1="$(_compress_if_exists 'SRR10071175_1' || true)"
gz2="$(_compress_if_exists 'SRR10071175_2' || true)"
gzs="$(_compress_if_exists 'SRR10071175' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "AGO_3_Homo_SAT_S58_R1_001.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071167/SRR10071167"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071168/SRR10071168"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071169/SRR10071169"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071170/SRR10071170"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071171/SRR10071171"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071172/SRR10071172"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071173/SRR10071173"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071174/SRR10071174"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071175/SRR10071175"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071170/SRR10071170"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071173/SRR10071173"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071171/SRR10071171"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071174/SRR10071174"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071172/SRR10071172"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071175/SRR10071175"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071167/SRR10071167"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071168/SRR10071168"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071169/SRR10071169"

echo "Download complete. Files saved to $OUTDIR"