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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE145nnn/GSE145968/suppl/GSE145968_MD7a-MLE.gene_summary.txt.gz" -O "GSE145968_MD7a-MLE.gene_summary.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE145nnn/GSE145968/suppl/GSE145968_RAW.tar" -O "GSE145968_RAW.tar"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE145nnn/GSE145968/suppl/GSE145968_all.count.txt.gz" -O "GSE145968_all.count.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE145nnn/GSE145968/suppl/GSE145968_all.countsummary.txt.gz" -O "GSE145968_all.countsummary.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 "SRR11185549"
gz1="$(_compress_if_exists 'SRR11185549_1' || true)"
gz2="$(_compress_if_exists 'SRR11185549_2' || true)"
gzs="$(_compress_if_exists 'SRR11185549' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MD7a__1_GFP_High_S3_L007_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11185550"
gz1="$(_compress_if_exists 'SRR11185550_1' || true)"
gz2="$(_compress_if_exists 'SRR11185550_2' || true)"
gzs="$(_compress_if_exists 'SRR11185550' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MD7a__2_GFP_High_S7_L007_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11185551"
gz1="$(_compress_if_exists 'SRR11185551_1' || true)"
gz2="$(_compress_if_exists 'SRR11185551_2' || true)"
gzs="$(_compress_if_exists 'SRR11185551' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MD7a__1_GFP_Low_S4_L007_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11185552"
gz1="$(_compress_if_exists 'SRR11185552_1' || true)"
gz2="$(_compress_if_exists 'SRR11185552_2' || true)"
gzs="$(_compress_if_exists 'SRR11185552' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MD7a__2_GFP_Low_S8_L007_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11185553"
gz1="$(_compress_if_exists 'SRR11185553_1' || true)"
gz2="$(_compress_if_exists 'SRR11185553_2' || true)"
gzs="$(_compress_if_exists 'SRR11185553' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MD7a__1_t_end_S2_L007_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11185554"
gz1="$(_compress_if_exists 'SRR11185554_1' || true)"
gz2="$(_compress_if_exists 'SRR11185554_2' || true)"
gzs="$(_compress_if_exists 'SRR11185554' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MD7a__2_t_end_S6_L007_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11185555"
gz1="$(_compress_if_exists 'SRR11185555_1' || true)"
gz2="$(_compress_if_exists 'SRR11185555_2' || true)"
gzs="$(_compress_if_exists 'SRR11185555' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MD7a__1_t_0_S1_L007_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11185556"
gz1="$(_compress_if_exists 'SRR11185556_1' || true)"
gz2="$(_compress_if_exists 'SRR11185556_2' || true)"
gzs="$(_compress_if_exists 'SRR11185556' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "MD7a__2_t_0_S5_L007_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11185557"
gz1="$(_compress_if_exists 'SRR11185557_1' || true)"
gz2="$(_compress_if_exists 'SRR11185557_2' || true)"
gzs="$(_compress_if_exists 'SRR11185557' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CMP_Batch_2_S3_L008_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11185558"
gz1="$(_compress_if_exists 'SRR11185558_1' || true)"
gz2="$(_compress_if_exists 'SRR11185558_2' || true)"
gzs="$(_compress_if_exists 'SRR11185558' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CMP_Batch_4_S4_L008_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11185559"
gz1="$(_compress_if_exists 'SRR11185559_1' || true)"
gz2="$(_compress_if_exists 'SRR11185559_2' || true)"
gzs="$(_compress_if_exists 'SRR11185559' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CMP_Batch_5_S5_L008_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/SRR11185549/SRR11185549"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185550/SRR11185550"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185551/SRR11185551"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185552/SRR11185552"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185553/SRR11185553"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185554/SRR11185554"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185555/SRR11185555"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185556/SRR11185556"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185557/SRR11185557"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185558/SRR11185558"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185559/SRR11185559"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185549/SRR11185549"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185551/SRR11185551"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185555/SRR11185555"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185553/SRR11185553"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185550/SRR11185550"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185552/SRR11185552"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185556/SRR11185556"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185554/SRR11185554"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185557/SRR11185557"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185558/SRR11185558"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11185559/SRR11185559"

echo "Download complete. Files saved to $OUTDIR"