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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE124nnn/GSE124071/suppl/GSE124071_RAW.tar" -O "GSE124071_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 "SRR8351048"
gz1="$(_compress_if_exists 'SRR8351048_1' || true)"
gz2="$(_compress_if_exists 'SRR8351048_2' || true)"
gzs="$(_compress_if_exists 'SRR8351048' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "iPSC.CTRL.1.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "iPSC.CTRL.1.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR8351049"
gz1="$(_compress_if_exists 'SRR8351049_1' || true)"
gz2="$(_compress_if_exists 'SRR8351049_2' || true)"
gzs="$(_compress_if_exists 'SRR8351049' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "iPSC.CTRL.2.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "iPSC.CTRL.2.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR8351050"
gz1="$(_compress_if_exists 'SRR8351050_1' || true)"
gz2="$(_compress_if_exists 'SRR8351050_2' || true)"
gzs="$(_compress_if_exists 'SRR8351050' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "iPSC.DDX6.1.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "iPSC.DDX6.1.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR8351051"
gz1="$(_compress_if_exists 'SRR8351051_1' || true)"
gz2="$(_compress_if_exists 'SRR8351051_2' || true)"
gzs="$(_compress_if_exists 'SRR8351051' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "iPSC.DDX6.2.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "iPSC.DDX6.2.R2.fastq.gz"
fi

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

fasterq-dump --split-files "SRR8351053"
gz1="$(_compress_if_exists 'SRR8351053_1' || true)"
gz2="$(_compress_if_exists 'SRR8351053_2' || true)"
gzs="$(_compress_if_exists 'SRR8351053' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Myoblast.CTRL.2.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Myoblast.CTRL.2.R2.fastq.gz"
fi

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

fasterq-dump --split-files "SRR8351055"
gz1="$(_compress_if_exists 'SRR8351055_1' || true)"
gz2="$(_compress_if_exists 'SRR8351055_2' || true)"
gzs="$(_compress_if_exists 'SRR8351055' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Myoblast.DDX6.2.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Myoblast.DDX6.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/SRR8351048/SRR8351048"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351049/SRR8351049"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351050/SRR8351050"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351051/SRR8351051"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351052/SRR8351052"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351053/SRR8351053"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351054/SRR8351054"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351055/SRR8351055"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351048/SRR8351048"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351049/SRR8351049"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351050/SRR8351050"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351051/SRR8351051"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351052/SRR8351052"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351052/SRR8351052"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351053/SRR8351053"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351054/SRR8351054"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351055/SRR8351055"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8351055/SRR8351055"

echo "Download complete. Files saved to $OUTDIR"