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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE136nnn/GSE136907/suppl/GSE136907_RAW.tar" -O "GSE136907_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 "SRR10071151"
gz1="$(_compress_if_exists 'SRR10071151_1' || true)"
gz2="$(_compress_if_exists 'SRR10071151_2' || true)"
gzs="$(_compress_if_exists 'SRR10071151' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ZNH_NoIndex_L008_R1.R12_AGO1_hypoxia_HighRNase_H.fastq.gz"
fi

fasterq-dump --split-files "SRR10071152"
gz1="$(_compress_if_exists 'SRR10071152_1' || true)"
gz2="$(_compress_if_exists 'SRR10071152_2' || true)"
gzs="$(_compress_if_exists 'SRR10071152' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ZNL_NoIndex_L007_R1.R12_AGO1_hypoxia_HighRNase_L.fastq.gz"
fi

fasterq-dump --split-files "SRR10071153"
gz1="$(_compress_if_exists 'SRR10071153_1' || true)"
gz2="$(_compress_if_exists 'SRR10071153_2' || true)"
gzs="$(_compress_if_exists 'SRR10071153' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ZNH_NoIndex_L008_R1.R11_AGO1_hypoxia_LowRNAse_H.fastq.gz"
fi

fasterq-dump --split-files "SRR10071154"
gz1="$(_compress_if_exists 'SRR10071154_1' || true)"
gz2="$(_compress_if_exists 'SRR10071154_2' || true)"
gzs="$(_compress_if_exists 'SRR10071154' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ZNL_NoIndex_L007_R1.R11_AGO1_hypoxia_LowRNAse_L.fastq.gz"
fi

fasterq-dump --split-files "SRR10071155"
gz1="$(_compress_if_exists 'SRR10071155_1' || true)"
gz2="$(_compress_if_exists 'SRR10071155_2' || true)"
gzs="$(_compress_if_exists 'SRR10071155' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ZNH_NoIndex_L008_R1.R10_AGO1_normoxia_HighRNAse_H.fastq.gz"
fi

fasterq-dump --split-files "SRR10071156"
gz1="$(_compress_if_exists 'SRR10071156_1' || true)"
gz2="$(_compress_if_exists 'SRR10071156_2' || true)"
gzs="$(_compress_if_exists 'SRR10071156' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ZNL_NoIndex_L007_R1.R10_AGO1_normoxia_HighRNAse_L.fastq.gz"
fi

fasterq-dump --split-files "SRR10071157"
gz1="$(_compress_if_exists 'SRR10071157_1' || true)"
gz2="$(_compress_if_exists 'SRR10071157_2' || true)"
gzs="$(_compress_if_exists 'SRR10071157' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ZNH_NoIndex_L008_R1.R09_AGO1_normoxia_LowRNAse_H.fastq.gz"
fi

fasterq-dump --split-files "SRR10071158"
gz1="$(_compress_if_exists 'SRR10071158_1' || true)"
gz2="$(_compress_if_exists 'SRR10071158_2' || true)"
gzs="$(_compress_if_exists 'SRR10071158' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "ZNL_NoIndex_L007_R1.R09_AGO1_normoxia_LowRNAse_L.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071151/SRR10071151"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071152/SRR10071152"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071153/SRR10071153"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071154/SRR10071154"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071155/SRR10071155"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071156/SRR10071156"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071157/SRR10071157"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071158/SRR10071158"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071157/SRR10071157"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071155/SRR10071155"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071153/SRR10071153"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071151/SRR10071151"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071158/SRR10071158"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071156/SRR10071156"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071154/SRR10071154"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10071152/SRR10071152"

echo "Download complete. Files saved to $OUTDIR"