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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE214nnn/GSE214109/suppl/GSE214109_batch_corrected_combined_raw_counts_dropped_bad_samples.csv.gz" -O "GSE214109_batch_corrected_combined_raw_counts_dropped_bad_samples.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 "SRR21696928"
gz1="$(_compress_if_exists 'SRR21696928_1' || true)"
gz2="$(_compress_if_exists 'SRR21696928_2' || true)"
gzs="$(_compress_if_exists 'SRR21696928' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mHTT_9_S49_L004_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696929"
gz1="$(_compress_if_exists 'SRR21696929_1' || true)"
gz2="$(_compress_if_exists 'SRR21696929_2' || true)"
gzs="$(_compress_if_exists 'SRR21696929' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mHTT_8_S48_L004_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696930"
gz1="$(_compress_if_exists 'SRR21696930_1' || true)"
gz2="$(_compress_if_exists 'SRR21696930_2' || true)"
gzs="$(_compress_if_exists 'SRR21696930' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mHTT_7_S47_L004_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696931"
gz1="$(_compress_if_exists 'SRR21696931_1' || true)"
gz2="$(_compress_if_exists 'SRR21696931_2' || true)"
gzs="$(_compress_if_exists 'SRR21696931' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mHTT_3_S43_L004_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696932"
gz1="$(_compress_if_exists 'SRR21696932_1' || true)"
gz2="$(_compress_if_exists 'SRR21696932_2' || true)"
gzs="$(_compress_if_exists 'SRR21696932' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mHTT_2_S42_L004_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696933"
gz1="$(_compress_if_exists 'SRR21696933_1' || true)"
gz2="$(_compress_if_exists 'SRR21696933_2' || true)"
gzs="$(_compress_if_exists 'SRR21696933' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mHTT_1_S41_L004_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696934"
gz1="$(_compress_if_exists 'SRR21696934_1' || true)"
gz2="$(_compress_if_exists 'SRR21696934_2' || true)"
gzs="$(_compress_if_exists 'SRR21696934' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mHTT_18_S58_L004_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696935"
gz1="$(_compress_if_exists 'SRR21696935_1' || true)"
gz2="$(_compress_if_exists 'SRR21696935_2' || true)"
gzs="$(_compress_if_exists 'SRR21696935' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mHTT_16_S56_L004_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696936"
gz1="$(_compress_if_exists 'SRR21696936_1' || true)"
gz2="$(_compress_if_exists 'SRR21696936_2' || true)"
gzs="$(_compress_if_exists 'SRR21696936' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mHTT_12_S52_L004_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696937"
gz1="$(_compress_if_exists 'SRR21696937_1' || true)"
gz2="$(_compress_if_exists 'SRR21696937_2' || true)"
gzs="$(_compress_if_exists 'SRR21696937' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mHTT_11_S51_L004_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696938"
gz1="$(_compress_if_exists 'SRR21696938_1' || true)"
gz2="$(_compress_if_exists 'SRR21696938_2' || true)"
gzs="$(_compress_if_exists 'SRR21696938' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mHTT_10_S50_L004_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696939"
gz1="$(_compress_if_exists 'SRR21696939_1' || true)"
gz2="$(_compress_if_exists 'SRR21696939_2' || true)"
gzs="$(_compress_if_exists 'SRR21696939' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "TCTGCAAG_AAGGTGAA_385_S1_L002_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696940"
gz1="$(_compress_if_exists 'SRR21696940_1' || true)"
gz2="$(_compress_if_exists 'SRR21696940_2' || true)"
gzs="$(_compress_if_exists 'SRR21696940' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "392_S6_L002_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696941"
gz1="$(_compress_if_exists 'SRR21696941_1' || true)"
gz2="$(_compress_if_exists 'SRR21696941_2' || true)"
gzs="$(_compress_if_exists 'SRR21696941' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "378_S15_L002_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696942"
gz1="$(_compress_if_exists 'SRR21696942_1' || true)"
gz2="$(_compress_if_exists 'SRR21696942_2' || true)"
gzs="$(_compress_if_exists 'SRR21696942' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "351_S13_L002_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696943"
gz1="$(_compress_if_exists 'SRR21696943_1' || true)"
gz2="$(_compress_if_exists 'SRR21696943_2' || true)"
gzs="$(_compress_if_exists 'SRR21696943' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "343_S12_L002_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696944"
gz1="$(_compress_if_exists 'SRR21696944_1' || true)"
gz2="$(_compress_if_exists 'SRR21696944_2' || true)"
gzs="$(_compress_if_exists 'SRR21696944' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "338_S11_L002_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696945"
gz1="$(_compress_if_exists 'SRR21696945_1' || true)"
gz2="$(_compress_if_exists 'SRR21696945_2' || true)"
gzs="$(_compress_if_exists 'SRR21696945' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "291_S9_L002_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi

fasterq-dump --split-files "SRR21696946"
gz1="$(_compress_if_exists 'SRR21696946_1' || true)"
gz2="$(_compress_if_exists 'SRR21696946_2' || true)"
gzs="$(_compress_if_exists 'SRR21696946' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "287_S10_L002_R1_001.fastqTr.sorted.STARUnmapped.out.sorted.STARAligned.out.sorted.bam.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696928/SRR21696928"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696929/SRR21696929"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696930/SRR21696930"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696931/SRR21696931"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696932/SRR21696932"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696933/SRR21696933"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696934/SRR21696934"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696935/SRR21696935"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696936/SRR21696936"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696937/SRR21696937"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696938/SRR21696938"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696939/SRR21696939"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696940/SRR21696940"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696941/SRR21696941"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696942/SRR21696942"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696943/SRR21696943"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696944/SRR21696944"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696945/SRR21696945"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696946/SRR21696946"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696946/SRR21696946"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696945/SRR21696945"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696944/SRR21696944"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696943/SRR21696943"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696942/SRR21696942"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696941/SRR21696941"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696940/SRR21696940"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696938/SRR21696938"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696937/SRR21696937"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696936/SRR21696936"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696935/SRR21696935"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696934/SRR21696934"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696933/SRR21696933"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696932/SRR21696932"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696931/SRR21696931"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696930/SRR21696930"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696929/SRR21696929"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696928/SRR21696928"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21696939/SRR21696939"

echo "Download complete. Files saved to $OUTDIR"