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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE77nnn/GSE77702/suppl/GSE77702_RAW.tar" -O "GSE77702_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 "SRR3153251"
gz1="$(_compress_if_exists 'SRR3153251_1' || true)"
gz2="$(_compress_if_exists 'SRR3153251_2' || true)"
gzs="$(_compress_if_exists 'SRR3153251' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "FUS_shRNA_1.fastq.gz"
fi

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153251/SRR3153251"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153252/SRR3153252"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153253/SRR3153253"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153254/SRR3153254"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153255/SRR3153255"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153256/SRR3153256"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153257/SRR3153257"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153258/SRR3153258"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153259/SRR3153259"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153260/SRR3153260"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153261/SRR3153261"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153262/SRR3153262"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153263/SRR3153263"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153264/SRR3153264"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153265/SRR3153265"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153266/SRR3153266"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153261/SRR3153261"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153262/SRR3153262"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153263/SRR3153263"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153264/SRR3153264"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153265/SRR3153265"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153266/SRR3153266"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153251/SRR3153251"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153252/SRR3153252"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153253/SRR3153253"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153254/SRR3153254"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153255/SRR3153255"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153256/SRR3153256"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153257/SRR3153257"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153258/SRR3153258"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153259/SRR3153259"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3153260/SRR3153260"

echo "Download complete. Files saved to $OUTDIR"