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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE124nnn/GSE124072/suppl/GSE124072_RAW.tar" -O "GSE124072_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 "SRR8351040"
gz1="$(_compress_if_exists 'SRR8351040_1' || true)"
gz2="$(_compress_if_exists 'SRR8351040_2' || true)"
gzs="$(_compress_if_exists 'SRR8351040' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NPC.shCTRL_1.R1.fastq.gz"
fi

fasterq-dump --split-files "SRR8351041"
gz1="$(_compress_if_exists 'SRR8351041_1' || true)"
gz2="$(_compress_if_exists 'SRR8351041_2' || true)"
gzs="$(_compress_if_exists 'SRR8351041' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NPC.shCTRL_2.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NPC.shCTRL_2.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR8351042"
gz1="$(_compress_if_exists 'SRR8351042_1' || true)"
gz2="$(_compress_if_exists 'SRR8351042_2' || true)"
gzs="$(_compress_if_exists 'SRR8351042' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NPC.shDDX6_1.R1.fastq.gz"
fi

fasterq-dump --split-files "SRR8351043"
gz1="$(_compress_if_exists 'SRR8351043_1' || true)"
gz2="$(_compress_if_exists 'SRR8351043_2' || true)"
gzs="$(_compress_if_exists 'SRR8351043' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NPC.shDDX6_2.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NPC.shDDX6_2.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR8351044"
gz1="$(_compress_if_exists 'SRR8351044_1' || true)"
gz2="$(_compress_if_exists 'SRR8351044_2' || true)"
gzs="$(_compress_if_exists 'SRR8351044' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Myoblast.shCTRL_1.R1.fastq.gz"
fi

fasterq-dump --split-files "SRR8351045"
gz1="$(_compress_if_exists 'SRR8351045_1' || true)"
gz2="$(_compress_if_exists 'SRR8351045_2' || true)"
gzs="$(_compress_if_exists 'SRR8351045' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Myoblast.shCTRL_2.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Myoblast.shCTRL_2.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR8351046"
gz1="$(_compress_if_exists 'SRR8351046_1' || true)"
gz2="$(_compress_if_exists 'SRR8351046_2' || true)"
gzs="$(_compress_if_exists 'SRR8351046' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Myoblast.shDDX6_1.R1.fastq.gz"
fi

fasterq-dump --split-files "SRR8351047"
gz1="$(_compress_if_exists 'SRR8351047_1' || true)"
gz2="$(_compress_if_exists 'SRR8351047_2' || true)"
gzs="$(_compress_if_exists 'SRR8351047' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Myoblast.shDDX6_2.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Myoblast.shDDX6_2.R2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351040/SRR8351040"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351041/SRR8351041"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351042/SRR8351042"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351043/SRR8351043"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351044/SRR8351044"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351045/SRR8351045"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351046/SRR8351046"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351047/SRR8351047"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351044/SRR8351044"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351045/SRR8351045"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351046/SRR8351046"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351047/SRR8351047"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351047/SRR8351047"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351040/SRR8351040"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351041/SRR8351041"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351042/SRR8351042"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351043/SRR8351043"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351040/SRR8351040"

echo "Download complete. Files saved to $OUTDIR"