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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE51nnn/GSE51684/suppl/GSE51684_expression_table.txt.gz" -O "GSE51684_expression_table.txt.gz"

# --- 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 "SRR1017149"
gz1="$(_compress_if_exists 'SRR1017149_1' || true)"
gz2="$(_compress_if_exists 'SRR1017149_2' || true)"
gzs="$(_compress_if_exists 'SRR1017149' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "ALS14.fq.gz"
fi

fasterq-dump --split-files "SRR1017150"
gz1="$(_compress_if_exists 'SRR1017150_1' || true)"
gz2="$(_compress_if_exists 'SRR1017150_2' || true)"
gzs="$(_compress_if_exists 'SRR1017150' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "ALS20.fq.gz"
fi

fasterq-dump --split-files "SRR1017151"
gz1="$(_compress_if_exists 'SRR1017151_1' || true)"
gz2="$(_compress_if_exists 'SRR1017151_2' || true)"
gzs="$(_compress_if_exists 'SRR1017151' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "ALS25.fq.gz"
fi

fasterq-dump --split-files "SRR1017152"
gz1="$(_compress_if_exists 'SRR1017152_1' || true)"
gz2="$(_compress_if_exists 'SRR1017152_2' || true)"
gzs="$(_compress_if_exists 'SRR1017152' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "ALS2.fq.gz"
fi

fasterq-dump --split-files "SRR1017153"
gz1="$(_compress_if_exists 'SRR1017153_1' || true)"
gz2="$(_compress_if_exists 'SRR1017153_2' || true)"
gzs="$(_compress_if_exists 'SRR1017153' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "C9-1-Ctrl.fq.gz"
fi

fasterq-dump --split-files "SRR1017154"
gz1="$(_compress_if_exists 'SRR1017154_1' || true)"
gz2="$(_compress_if_exists 'SRR1017154_2' || true)"
gzs="$(_compress_if_exists 'SRR1017154' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "C9-1-Mock.fq.gz"
fi

fasterq-dump --split-files "SRR1017155"
gz1="$(_compress_if_exists 'SRR1017155_1' || true)"
gz2="$(_compress_if_exists 'SRR1017155_2' || true)"
gzs="$(_compress_if_exists 'SRR1017155' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "C9-1-Tx.fq.gz"
fi

fasterq-dump --split-files "SRR1017156"
gz1="$(_compress_if_exists 'SRR1017156_1' || true)"
gz2="$(_compress_if_exists 'SRR1017156_2' || true)"
gzs="$(_compress_if_exists 'SRR1017156' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "C9-2-Ctrl.fq.gz"
fi

fasterq-dump --split-files "SRR1017157"
gz1="$(_compress_if_exists 'SRR1017157_1' || true)"
gz2="$(_compress_if_exists 'SRR1017157_2' || true)"
gzs="$(_compress_if_exists 'SRR1017157' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "C9-2-Mock.fq.gz"
fi

fasterq-dump --split-files "SRR1017158"
gz1="$(_compress_if_exists 'SRR1017158_1' || true)"
gz2="$(_compress_if_exists 'SRR1017158_2' || true)"
gzs="$(_compress_if_exists 'SRR1017158' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "C9-2-Tx.fq.gz"
fi

fasterq-dump --split-files "SRR1017159"
gz1="$(_compress_if_exists 'SRR1017159_1' || true)"
gz2="$(_compress_if_exists 'SRR1017159_2' || true)"
gzs="$(_compress_if_exists 'SRR1017159' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "C9-3-Ctrl.fq.gz"
fi

fasterq-dump --split-files "SRR1017160"
gz1="$(_compress_if_exists 'SRR1017160_1' || true)"
gz2="$(_compress_if_exists 'SRR1017160_2' || true)"
gzs="$(_compress_if_exists 'SRR1017160' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "C9-3-Mock.fq.gz"
fi

fasterq-dump --split-files "SRR1017161"
gz1="$(_compress_if_exists 'SRR1017161_1' || true)"
gz2="$(_compress_if_exists 'SRR1017161_2' || true)"
gzs="$(_compress_if_exists 'SRR1017161' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "C9-3-Tx.fq.gz"
fi

fasterq-dump --split-files "SRR1017162"
gz1="$(_compress_if_exists 'SRR1017162_1' || true)"
gz2="$(_compress_if_exists 'SRR1017162_2' || true)"
gzs="$(_compress_if_exists 'SRR1017162' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "C9-4-Ctrl.fq.gz"
fi

fasterq-dump --split-files "SRR1017163"
gz1="$(_compress_if_exists 'SRR1017163_1' || true)"
gz2="$(_compress_if_exists 'SRR1017163_2' || true)"
gzs="$(_compress_if_exists 'SRR1017163' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "C9-4-Mock.fq.gz"
fi

fasterq-dump --split-files "SRR1017164"
gz1="$(_compress_if_exists 'SRR1017164_1' || true)"
gz2="$(_compress_if_exists 'SRR1017164_2' || true)"
gzs="$(_compress_if_exists 'SRR1017164' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "C9-4-Tx.fq.gz"
fi

fasterq-dump --split-files "SRR1017165"
gz1="$(_compress_if_exists 'SRR1017165_1' || true)"
gz2="$(_compress_if_exists 'SRR1017165_2' || true)"
gzs="$(_compress_if_exists 'SRR1017165' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "Kin1ALS17-Ctrl.fq.gz"
fi

fasterq-dump --split-files "SRR1017166"
gz1="$(_compress_if_exists 'SRR1017166_1' || true)"
gz2="$(_compress_if_exists 'SRR1017166_2' || true)"
gzs="$(_compress_if_exists 'SRR1017166' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "Kin1ALS17-Tx.fq.gz"
fi

fasterq-dump --split-files "SRR1017167"
gz1="$(_compress_if_exists 'SRR1017167_1' || true)"
gz2="$(_compress_if_exists 'SRR1017167_2' || true)"
gzs="$(_compress_if_exists 'SRR1017167' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "Kin1ALS6-Ctrl.fq.gz"
fi

fasterq-dump --split-files "SRR1017168"
gz1="$(_compress_if_exists 'SRR1017168_1' || true)"
gz2="$(_compress_if_exists 'SRR1017168_2' || true)"
gzs="$(_compress_if_exists 'SRR1017168' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "Kin1ALS6-Tx.fq.gz"
fi

fasterq-dump --split-files "SRR1017169"
gz1="$(_compress_if_exists 'SRR1017169_1' || true)"
gz2="$(_compress_if_exists 'SRR1017169_2' || true)"
gzs="$(_compress_if_exists 'SRR1017169' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "Kin2ALS6-Ctrl.fastq.gz"
fi

fasterq-dump --split-files "SRR1017170"
gz1="$(_compress_if_exists 'SRR1017170_1' || true)"
gz2="$(_compress_if_exists 'SRR1017170_2' || true)"
gzs="$(_compress_if_exists 'SRR1017170' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "Kin2ALS6-Tx.fq.gz"
fi

fasterq-dump --split-files "SRR1017171"
gz1="$(_compress_if_exists 'SRR1017171_1' || true)"
gz2="$(_compress_if_exists 'SRR1017171_2' || true)"
gzs="$(_compress_if_exists 'SRR1017171' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "Kin4ALS6-Ctrl.fq.gz"
fi

fasterq-dump --split-files "SRR1017172"
gz1="$(_compress_if_exists 'SRR1017172_1' || true)"
gz2="$(_compress_if_exists 'SRR1017172_2' || true)"
gzs="$(_compress_if_exists 'SRR1017172' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "Kin4ALS6-Tx.fq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017149/SRR1017149"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017150/SRR1017150"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017151/SRR1017151"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017152/SRR1017152"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017153/SRR1017153"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017154/SRR1017154"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017155/SRR1017155"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017156/SRR1017156"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017157/SRR1017157"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017158/SRR1017158"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017159/SRR1017159"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017160/SRR1017160"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017161/SRR1017161"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017162/SRR1017162"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017163/SRR1017163"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017164/SRR1017164"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017165/SRR1017165"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017166/SRR1017166"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017167/SRR1017167"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017168/SRR1017168"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017169/SRR1017169"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017170/SRR1017170"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017171/SRR1017171"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017172/SRR1017172"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017149/SRR1017149"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017150/SRR1017150"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017151/SRR1017151"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017152/SRR1017152"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017153/SRR1017153"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017154/SRR1017154"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017155/SRR1017155"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017156/SRR1017156"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017157/SRR1017157"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017158/SRR1017158"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017159/SRR1017159"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017160/SRR1017160"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017161/SRR1017161"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017162/SRR1017162"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017163/SRR1017163"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017164/SRR1017164"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017165/SRR1017165"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017166/SRR1017166"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017167/SRR1017167"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017168/SRR1017168"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017169/SRR1017169"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017170/SRR1017170"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017171/SRR1017171"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017172/SRR1017172"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017149/SRR1017149"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017151/SRR1017151"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017157/SRR1017157"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017158/SRR1017158"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017159/SRR1017159"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1017160/SRR1017160"

echo "Download complete. Files saved to $OUTDIR"