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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE76nnn/GSE76220/suppl/GSE76220_ALS_LCM_MPR_gene_expression.txt.gz" -O "GSE76220_ALS_LCM_MPR_gene_expression.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE76nnn/GSE76220/suppl/GSE76220_ALS_LCM_RPKM.txt.gz" -O "GSE76220_ALS_LCM_RPKM.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 "SRR3038253"
gz1="$(_compress_if_exists 'SRR3038253_1' || true)"
gz2="$(_compress_if_exists 'SRR3038253_2' || true)"
gzs="$(_compress_if_exists 'SRR3038253' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "10C.fastq.gz"
fi

fasterq-dump --split-files "SRR3038254"
gz1="$(_compress_if_exists 'SRR3038254_1' || true)"
gz2="$(_compress_if_exists 'SRR3038254_2' || true)"
gzs="$(_compress_if_exists 'SRR3038254' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "65C.fastq.gz"
fi

fasterq-dump --split-files "SRR3038255"
gz1="$(_compress_if_exists 'SRR3038255_1' || true)"
gz2="$(_compress_if_exists 'SRR3038255_2' || true)"
gzs="$(_compress_if_exists 'SRR3038255' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "78C.fastq.gz"
fi

fasterq-dump --split-files "SRR3038256"
gz1="$(_compress_if_exists 'SRR3038256_1' || true)"
gz2="$(_compress_if_exists 'SRR3038256_2' || true)"
gzs="$(_compress_if_exists 'SRR3038256' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "39C.fastq.gz"
fi

fasterq-dump --split-files "SRR3038257"
gz1="$(_compress_if_exists 'SRR3038257_1' || true)"
gz2="$(_compress_if_exists 'SRR3038257_2' || true)"
gzs="$(_compress_if_exists 'SRR3038257' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "67C.fastq.gz"
fi

fasterq-dump --split-files "SRR3038258"
gz1="$(_compress_if_exists 'SRR3038258_1' || true)"
gz2="$(_compress_if_exists 'SRR3038258_2' || true)"
gzs="$(_compress_if_exists 'SRR3038258' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "76C.fastq.gz"
fi

fasterq-dump --split-files "SRR3038259"
gz1="$(_compress_if_exists 'SRR3038259_1' || true)"
gz2="$(_compress_if_exists 'SRR3038259_2' || true)"
gzs="$(_compress_if_exists 'SRR3038259' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "44C.fastq.gz"
fi

fasterq-dump --split-files "SRR3038260"
gz1="$(_compress_if_exists 'SRR3038260_1' || true)"
gz2="$(_compress_if_exists 'SRR3038260_2' || true)"
gzs="$(_compress_if_exists 'SRR3038260' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "88C.fastq.gz"
fi

fasterq-dump --split-files "SRR3038261"
gz1="$(_compress_if_exists 'SRR3038261_1' || true)"
gz2="$(_compress_if_exists 'SRR3038261_2' || true)"
gzs="$(_compress_if_exists 'SRR3038261' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "60A.fastq.gz"
fi

fasterq-dump --split-files "SRR3038262"
gz1="$(_compress_if_exists 'SRR3038262_1' || true)"
gz2="$(_compress_if_exists 'SRR3038262_2' || true)"
gzs="$(_compress_if_exists 'SRR3038262' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "62A.fastq.gz"
fi

fasterq-dump --split-files "SRR3038263"
gz1="$(_compress_if_exists 'SRR3038263_1' || true)"
gz2="$(_compress_if_exists 'SRR3038263_2' || true)"
gzs="$(_compress_if_exists 'SRR3038263' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "63A.fastq.gz"
fi

fasterq-dump --split-files "SRR3038264"
gz1="$(_compress_if_exists 'SRR3038264_1' || true)"
gz2="$(_compress_if_exists 'SRR3038264_2' || true)"
gzs="$(_compress_if_exists 'SRR3038264' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "84A.fastq.gz"
fi

fasterq-dump --split-files "SRR3038265"
gz1="$(_compress_if_exists 'SRR3038265_1' || true)"
gz2="$(_compress_if_exists 'SRR3038265_2' || true)"
gzs="$(_compress_if_exists 'SRR3038265' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "89A.fastq.gz"
fi

fasterq-dump --split-files "SRR3038266"
gz1="$(_compress_if_exists 'SRR3038266_1' || true)"
gz2="$(_compress_if_exists 'SRR3038266_2' || true)"
gzs="$(_compress_if_exists 'SRR3038266' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "21A.fastq.gz"
fi

fasterq-dump --split-files "SRR3038267"
gz1="$(_compress_if_exists 'SRR3038267_1' || true)"
gz2="$(_compress_if_exists 'SRR3038267_2' || true)"
gzs="$(_compress_if_exists 'SRR3038267' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "34A.fastq.gz"
fi

fasterq-dump --split-files "SRR3038268"
gz1="$(_compress_if_exists 'SRR3038268_1' || true)"
gz2="$(_compress_if_exists 'SRR3038268_2' || true)"
gzs="$(_compress_if_exists 'SRR3038268' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "79A.fastq.gz"
fi

fasterq-dump --split-files "SRR3038269"
gz1="$(_compress_if_exists 'SRR3038269_1' || true)"
gz2="$(_compress_if_exists 'SRR3038269_2' || true)"
gzs="$(_compress_if_exists 'SRR3038269' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "82A.fastq.gz"
fi

fasterq-dump --split-files "SRR3038270"
gz1="$(_compress_if_exists 'SRR3038270_1' || true)"
gz2="$(_compress_if_exists 'SRR3038270_2' || true)"
gzs="$(_compress_if_exists 'SRR3038270' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "16A.fastq.gz"
fi

fasterq-dump --split-files "SRR3038271"
gz1="$(_compress_if_exists 'SRR3038271_1' || true)"
gz2="$(_compress_if_exists 'SRR3038271_2' || true)"
gzs="$(_compress_if_exists 'SRR3038271' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "27A.fastq.gz"
fi

fasterq-dump --split-files "SRR3038272"
gz1="$(_compress_if_exists 'SRR3038272_1' || true)"
gz2="$(_compress_if_exists 'SRR3038272_2' || true)"
gzs="$(_compress_if_exists 'SRR3038272' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "48A.fastq.gz"
fi

fasterq-dump --split-files "SRR3038273"
gz1="$(_compress_if_exists 'SRR3038273_1' || true)"
gz2="$(_compress_if_exists 'SRR3038273_2' || true)"
gzs="$(_compress_if_exists 'SRR3038273' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "85A.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038253/SRR3038253"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038254/SRR3038254"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038255/SRR3038255"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038256/SRR3038256"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038257/SRR3038257"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038258/SRR3038258"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038259/SRR3038259"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038260/SRR3038260"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038261/SRR3038261"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038262/SRR3038262"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038263/SRR3038263"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038264/SRR3038264"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038265/SRR3038265"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038266/SRR3038266"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038267/SRR3038267"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038268/SRR3038268"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038269/SRR3038269"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038270/SRR3038270"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038271/SRR3038271"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038272/SRR3038272"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038273/SRR3038273"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038253/SRR3038253"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038270/SRR3038270"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038266/SRR3038266"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038271/SRR3038271"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038267/SRR3038267"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038256/SRR3038256"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038259/SRR3038259"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038272/SRR3038272"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038261/SRR3038261"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038262/SRR3038262"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038263/SRR3038263"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038254/SRR3038254"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038257/SRR3038257"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038258/SRR3038258"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038255/SRR3038255"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038268/SRR3038268"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038269/SRR3038269"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038264/SRR3038264"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038273/SRR3038273"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038260/SRR3038260"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038265/SRR3038265"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038260/SRR3038260"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038263/SRR3038263"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038264/SRR3038264"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR3038265/SRR3038265"

echo "Download complete. Files saved to $OUTDIR"