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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE121nnn/GSE121569/suppl/GSE121569_category_als_vs_control_all.csv.gz" -O "GSE121569_category_als_vs_control_all.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE121nnn/GSE121569/suppl/GSE121569_combined.dexseq.txt.gz" -O "GSE121569_combined.dexseq.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE121nnn/GSE121569/suppl/GSE121569_dexseq-all.csv.gz" -O "GSE121569_dexseq-all.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE121nnn/GSE121569/suppl/GSE121569_raw_counts.csv.gz" -O "GSE121569_raw_counts.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE121nnn/GSE121569/suppl/GSE121569_tpm.csv.gz" -O "GSE121569_tpm.csv.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 "SRR8083864"
gz1="$(_compress_if_exists 'SRR8083864_1' || true)"
gz2="$(_compress_if_exists 'SRR8083864_2' || true)"
gzs="$(_compress_if_exists 'SRR8083864' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_09_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_09_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083865"
gz1="$(_compress_if_exists 'SRR8083865_1' || true)"
gz2="$(_compress_if_exists 'SRR8083865_2' || true)"
gzs="$(_compress_if_exists 'SRR8083865' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_10_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_10_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083866"
gz1="$(_compress_if_exists 'SRR8083866_1' || true)"
gz2="$(_compress_if_exists 'SRR8083866_2' || true)"
gzs="$(_compress_if_exists 'SRR8083866' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_11_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_11_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083867"
gz1="$(_compress_if_exists 'SRR8083867_1' || true)"
gz2="$(_compress_if_exists 'SRR8083867_2' || true)"
gzs="$(_compress_if_exists 'SRR8083867' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_12_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_12_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083868"
gz1="$(_compress_if_exists 'SRR8083868_1' || true)"
gz2="$(_compress_if_exists 'SRR8083868_2' || true)"
gzs="$(_compress_if_exists 'SRR8083868' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_13_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_13_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083869"
gz1="$(_compress_if_exists 'SRR8083869_1' || true)"
gz2="$(_compress_if_exists 'SRR8083869_2' || true)"
gzs="$(_compress_if_exists 'SRR8083869' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_19_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_19_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083870"
gz1="$(_compress_if_exists 'SRR8083870_1' || true)"
gz2="$(_compress_if_exists 'SRR8083870_2' || true)"
gzs="$(_compress_if_exists 'SRR8083870' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_20_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_20_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083871"
gz1="$(_compress_if_exists 'SRR8083871_1' || true)"
gz2="$(_compress_if_exists 'SRR8083871_2' || true)"
gzs="$(_compress_if_exists 'SRR8083871' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_21_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_21_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083872"
gz1="$(_compress_if_exists 'SRR8083872_1' || true)"
gz2="$(_compress_if_exists 'SRR8083872_2' || true)"
gzs="$(_compress_if_exists 'SRR8083872' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_22_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_22_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083873"
gz1="$(_compress_if_exists 'SRR8083873_1' || true)"
gz2="$(_compress_if_exists 'SRR8083873_2' || true)"
gzs="$(_compress_if_exists 'SRR8083873' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_23_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_23_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083874"
gz1="$(_compress_if_exists 'SRR8083874_1' || true)"
gz2="$(_compress_if_exists 'SRR8083874_2' || true)"
gzs="$(_compress_if_exists 'SRR8083874' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_24_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_24_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083875"
gz1="$(_compress_if_exists 'SRR8083875_1' || true)"
gz2="$(_compress_if_exists 'SRR8083875_2' || true)"
gzs="$(_compress_if_exists 'SRR8083875' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_25_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_25_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083876"
gz1="$(_compress_if_exists 'SRR8083876_1' || true)"
gz2="$(_compress_if_exists 'SRR8083876_2' || true)"
gzs="$(_compress_if_exists 'SRR8083876' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_30_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_30_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083877"
gz1="$(_compress_if_exists 'SRR8083877_1' || true)"
gz2="$(_compress_if_exists 'SRR8083877_2' || true)"
gzs="$(_compress_if_exists 'SRR8083877' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_31_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_31_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083878"
gz1="$(_compress_if_exists 'SRR8083878_1' || true)"
gz2="$(_compress_if_exists 'SRR8083878_2' || true)"
gzs="$(_compress_if_exists 'SRR8083878' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_32_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_32_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083879"
gz1="$(_compress_if_exists 'SRR8083879_1' || true)"
gz2="$(_compress_if_exists 'SRR8083879_2' || true)"
gzs="$(_compress_if_exists 'SRR8083879' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_33_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_33_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083880"
gz1="$(_compress_if_exists 'SRR8083880_1' || true)"
gz2="$(_compress_if_exists 'SRR8083880_2' || true)"
gzs="$(_compress_if_exists 'SRR8083880' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_34_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_34_2.fastq.gz"
fi

fasterq-dump --split-files "SRR8083881"
gz1="$(_compress_if_exists 'SRR8083881_1' || true)"
gz2="$(_compress_if_exists 'SRR8083881_2' || true)"
gzs="$(_compress_if_exists 'SRR8083881' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BD_35_1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BD_35_2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083864/SRR8083864"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083865/SRR8083865"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083866/SRR8083866"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083867/SRR8083867"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083868/SRR8083868"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083869/SRR8083869"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083870/SRR8083870"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083871/SRR8083871"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083872/SRR8083872"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083873/SRR8083873"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083874/SRR8083874"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083875/SRR8083875"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083876/SRR8083876"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083877/SRR8083877"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083878/SRR8083878"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083879/SRR8083879"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083880/SRR8083880"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083881/SRR8083881"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083864/SRR8083864"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083865/SRR8083865"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083866/SRR8083866"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083867/SRR8083867"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083868/SRR8083868"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083869/SRR8083869"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083870/SRR8083870"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083871/SRR8083871"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083872/SRR8083872"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083873/SRR8083873"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083874/SRR8083874"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083875/SRR8083875"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083876/SRR8083876"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083877/SRR8083877"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083878/SRR8083878"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083879/SRR8083879"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083880/SRR8083880"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8083881/SRR8083881"

echo "Download complete. Files saved to $OUTDIR"