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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE126nnn/GSE126337/suppl/GSE126337_MKamedaRNASeq_ReadCounts_updatedAnnot_hg38.txt.gz" -O "GSE126337_MKamedaRNASeq_ReadCounts_updatedAnnot_hg38.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE126nnn/GSE126337/suppl/GSE126337_RAW.tar" -O "GSE126337_RAW.tar"

# --- 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 "SRR8555180"
gz1="$(_compress_if_exists 'SRR8555180_1' || true)"
gz2="$(_compress_if_exists 'SRR8555180_2' || true)"
gzs="$(_compress_if_exists 'SRR8555180' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MB002-03-5-1_S16_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "MB002-03-5-1_S16_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR8555181"
gz1="$(_compress_if_exists 'SRR8555181_1' || true)"
gz2="$(_compress_if_exists 'SRR8555181_2' || true)"
gzs="$(_compress_if_exists 'SRR8555181' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MB002-03-5-2_S17_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "MB002-03-5-2_S17_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR8555182"
gz1="$(_compress_if_exists 'SRR8555182_1' || true)"
gz2="$(_compress_if_exists 'SRR8555182_2' || true)"
gzs="$(_compress_if_exists 'SRR8555182' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MB002-03-5-3_S18_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "MB002-03-5-3_S18_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR8555183"
gz1="$(_compress_if_exists 'SRR8555183_1' || true)"
gz2="$(_compress_if_exists 'SRR8555183_2' || true)"
gzs="$(_compress_if_exists 'SRR8555183' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MB002-shGFP-1_S19_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "MB002-shGFP-1_S19_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR8555184"
gz1="$(_compress_if_exists 'SRR8555184_1' || true)"
gz2="$(_compress_if_exists 'SRR8555184_2' || true)"
gzs="$(_compress_if_exists 'SRR8555184' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MB002-shGFP-2_S20_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "MB002-shGFP-2_S20_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR8555185"
gz1="$(_compress_if_exists 'SRR8555185_1' || true)"
gz2="$(_compress_if_exists 'SRR8555185_2' || true)"
gzs="$(_compress_if_exists 'SRR8555185' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MB002-shGFP-3_S21_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "MB002-shGFP-3_S21_R2_001.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8555180/SRR8555180"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8555181/SRR8555181"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8555182/SRR8555182"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8555183/SRR8555183"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8555184/SRR8555184"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8555185/SRR8555185"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8555180/SRR8555180"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8555181/SRR8555181"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8555182/SRR8555182"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8555183/SRR8555183"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8555184/SRR8555184"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8555185/SRR8555185"

echo "Download complete. Files saved to $OUTDIR"