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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE157nnn/GSE157072/suppl/GSE157072_Processed_D55_LCMV_RNAseq.txt.gz" -O "GSE157072_Processed_D55_LCMV_RNAseq.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 "SRR12540684"
gz1="$(_compress_if_exists 'SRR12540684_1' || true)"
gz2="$(_compress_if_exists 'SRR12540684_2' || true)"
gzs="$(_compress_if_exists 'SRR12540684' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "000008565169_G09.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "000008565169_G09.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540685"
gz1="$(_compress_if_exists 'SRR12540685_1' || true)"
gz2="$(_compress_if_exists 'SRR12540685_2' || true)"
gzs="$(_compress_if_exists 'SRR12540685' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "000008565169_H09.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "000008565169_H09.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540686"
gz1="$(_compress_if_exists 'SRR12540686_1' || true)"
gz2="$(_compress_if_exists 'SRR12540686_2' || true)"
gzs="$(_compress_if_exists 'SRR12540686' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "000008565169_A10.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "000008565169_A10.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540687"
gz1="$(_compress_if_exists 'SRR12540687_1' || true)"
gz2="$(_compress_if_exists 'SRR12540687_2' || true)"
gzs="$(_compress_if_exists 'SRR12540687' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "000008565169_B10.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "000008565169_B10.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540688"
gz1="$(_compress_if_exists 'SRR12540688_1' || true)"
gz2="$(_compress_if_exists 'SRR12540688_2' || true)"
gzs="$(_compress_if_exists 'SRR12540688' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "000008565169_C10.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "000008565169_C10.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540689"
gz1="$(_compress_if_exists 'SRR12540689_1' || true)"
gz2="$(_compress_if_exists 'SRR12540689_2' || true)"
gzs="$(_compress_if_exists 'SRR12540689' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "000008565169_D10.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "000008565169_D10.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540690"
gz1="$(_compress_if_exists 'SRR12540690_1' || true)"
gz2="$(_compress_if_exists 'SRR12540690_2' || true)"
gzs="$(_compress_if_exists 'SRR12540690' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "000008565169_G10.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "000008565169_G10.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540691"
gz1="$(_compress_if_exists 'SRR12540691_1' || true)"
gz2="$(_compress_if_exists 'SRR12540691_2' || true)"
gzs="$(_compress_if_exists 'SRR12540691' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "000008565169_H10.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "000008565169_H10.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540692"
gz1="$(_compress_if_exists 'SRR12540692_1' || true)"
gz2="$(_compress_if_exists 'SRR12540692_2' || true)"
gzs="$(_compress_if_exists 'SRR12540692' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "000008565169_E10.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "000008565169_E10.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540693"
gz1="$(_compress_if_exists 'SRR12540693_1' || true)"
gz2="$(_compress_if_exists 'SRR12540693_2' || true)"
gzs="$(_compress_if_exists 'SRR12540693' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "000008565169_F10.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "000008565169_F10.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540694"
gz1="$(_compress_if_exists 'SRR12540694_1' || true)"
gz2="$(_compress_if_exists 'SRR12540694_2' || true)"
gzs="$(_compress_if_exists 'SRR12540694' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SSF1856_F04.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SSF1856_F04.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540695"
gz1="$(_compress_if_exists 'SRR12540695_1' || true)"
gz2="$(_compress_if_exists 'SRR12540695_2' || true)"
gzs="$(_compress_if_exists 'SRR12540695' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SSF1857_F04.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SSF1857_F04.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540696"
gz1="$(_compress_if_exists 'SRR12540696_1' || true)"
gz2="$(_compress_if_exists 'SRR12540696_2' || true)"
gzs="$(_compress_if_exists 'SRR12540696' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SSF1856_E04.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SSF1856_E04.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540697"
gz1="$(_compress_if_exists 'SRR12540697_1' || true)"
gz2="$(_compress_if_exists 'SRR12540697_2' || true)"
gzs="$(_compress_if_exists 'SRR12540697' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SSF1857_E04.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SSF1857_E04.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540698"
gz1="$(_compress_if_exists 'SRR12540698_1' || true)"
gz2="$(_compress_if_exists 'SRR12540698_2' || true)"
gzs="$(_compress_if_exists 'SRR12540698' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SSF1856_D04.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SSF1856_D04.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12540699"
gz1="$(_compress_if_exists 'SRR12540699_1' || true)"
gz2="$(_compress_if_exists 'SRR12540699_2' || true)"
gzs="$(_compress_if_exists 'SRR12540699' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SSF1857_D04.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SSF1857_D04.R2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540684/SRR12540684"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540685/SRR12540685"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540686/SRR12540686"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540687/SRR12540687"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos9/sra-pub-zq-924/SRR012/12540/SRR12540688/SRR12540688.lite.1"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540689/SRR12540689"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540690/SRR12540690"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540691/SRR12540691"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540692/SRR12540692"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos9/sra-pub-zq-924/SRR012/12540/SRR12540693/SRR12540693.lite.1"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540694/SRR12540694"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos9/sra-pub-zq-922/SRR012/12540/SRR12540695/SRR12540695.lite.1"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540696/SRR12540696"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540697/SRR12540697"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540698/SRR12540698"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540699/SRR12540699"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540686/SRR12540686"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540687/SRR12540687"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540689/SRR12540689"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540692/SRR12540692"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540684/SRR12540684"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540690/SRR12540690"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540685/SRR12540685"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540691/SRR12540691"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos9/sra-pub-zq-924/SRR012/12540/SRR12540688/SRR12540688.lite.1"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos9/sra-pub-zq-924/SRR012/12540/SRR12540693/SRR12540693.lite.1"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos9/sra-pub-zq-922/SRR012/12540/SRR12540695/SRR12540695.lite.1"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540698/SRR12540698"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540696/SRR12540696"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540694/SRR12540694"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540699/SRR12540699"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12540697/SRR12540697"

echo "Download complete. Files saved to $OUTDIR"