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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE77nnn/GSE77704/suppl/GSE77704_RAW.tar" -O "GSE77704_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 "SRR3153311"
gz1="$(_compress_if_exists 'SRR3153311_1' || true)"
gz2="$(_compress_if_exists 'SRR3153311_2' || true)"
gzs="$(_compress_if_exists 'SRR3153311' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "NPC0_ctrl.fastq.gz"
fi

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153311/SRR3153311"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153312/SRR3153312"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153313/SRR3153313"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153314/SRR3153314"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153315/SRR3153315"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153316/SRR3153316"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153317/SRR3153317"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153318/SRR3153318"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153319/SRR3153319"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153320/SRR3153320"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153321/SRR3153321"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153322/SRR3153322"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153323/SRR3153323"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153324/SRR3153324"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153325/SRR3153325"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153326/SRR3153326"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153327/SRR3153327"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153328/SRR3153328"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153311/SRR3153311"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153312/SRR3153312"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153313/SRR3153313"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153314/SRR3153314"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153315/SRR3153315"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153316/SRR3153316"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153317/SRR3153317"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153318/SRR3153318"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153319/SRR3153319"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153320/SRR3153320"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153321/SRR3153321"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153322/SRR3153322"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153323/SRR3153323"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153324/SRR3153324"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153325/SRR3153325"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153326/SRR3153326"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153327/SRR3153327"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153328/SRR3153328"

echo "Download complete. Files saved to $OUTDIR"