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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE98nnn/GSE98869/suppl/GSE98869_diffexp.tsv.gz" -O "GSE98869_diffexp.tsv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE98nnn/GSE98869/suppl/GSE98869_editing_calls.tsv.gz" -O "GSE98869_editing_calls.tsv.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 "SRR5534733"
gz1="$(_compress_if_exists 'SRR5534733_1' || true)"
gz2="$(_compress_if_exists 'SRR5534733_2' || true)"
gzs="$(_compress_if_exists 'SRR5534733' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "GSF973-Hundley-SarahD-1_S1_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR5534734"
gz1="$(_compress_if_exists 'SRR5534734_1' || true)"
gz2="$(_compress_if_exists 'SRR5534734_2' || true)"
gzs="$(_compress_if_exists 'SRR5534734' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "GSF973-Hundley-SarahD-2_S2_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR5534735"
gz1="$(_compress_if_exists 'SRR5534735_1' || true)"
gz2="$(_compress_if_exists 'SRR5534735_2' || true)"
gzs="$(_compress_if_exists 'SRR5534735' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "GSF973-Hundley-SarahD-3_S3_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR5534736"
gz1="$(_compress_if_exists 'SRR5534736_1' || true)"
gz2="$(_compress_if_exists 'SRR5534736_2' || true)"
gzs="$(_compress_if_exists 'SRR5534736' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "GSF973-Hundley-SarahD-5_S5_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR5534737"
gz1="$(_compress_if_exists 'SRR5534737_1' || true)"
gz2="$(_compress_if_exists 'SRR5534737_2' || true)"
gzs="$(_compress_if_exists 'SRR5534737' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "GSF973-Hundley-SarahD-6_S6_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR5534738"
gz1="$(_compress_if_exists 'SRR5534738_1' || true)"
gz2="$(_compress_if_exists 'SRR5534738_2' || true)"
gzs="$(_compress_if_exists 'SRR5534738' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "GSF973-Hundley-SarahD-7_S7_R1_001.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5534733/SRR5534733"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5534734/SRR5534734"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5534735/SRR5534735"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos5/sra-pub-zq-14/SRR005/534/SRR5534736.sralite.1"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5534737/SRR5534737"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5534738/SRR5534738"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5534733/SRR5534733"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5534734/SRR5534734"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5534737/SRR5534737"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5534738/SRR5534738"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5534735/SRR5534735"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos5/sra-pub-zq-14/SRR005/534/SRR5534736.sralite.1"

echo "Download complete. Files saved to $OUTDIR"