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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE54nnn/GSE54968/suppl/GSE54968_gencode.expression.txt.gz" -O "GSE54968_gencode.expression.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 "SRR1168494"
gz1="$(_compress_if_exists 'SRR1168494_1' || true)"
gz2="$(_compress_if_exists 'SRR1168494_2' || true)"
gzs="$(_compress_if_exists 'SRR1168494' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "H1_D0_1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "H1_D0_2.fq.gz"
fi

fasterq-dump --split-files "SRR1168495"
gz1="$(_compress_if_exists 'SRR1168495_1' || true)"
gz2="$(_compress_if_exists 'SRR1168495_2' || true)"
gzs="$(_compress_if_exists 'SRR1168495' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "H1_D2_1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "H1_D2_2.fq.gz"
fi

fasterq-dump --split-files "SRR1168496"
gz1="$(_compress_if_exists 'SRR1168496_1' || true)"
gz2="$(_compress_if_exists 'SRR1168496_2' || true)"
gzs="$(_compress_if_exists 'SRR1168496' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "H1_D4_1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "H1_D4_2.fq.gz"
fi

fasterq-dump --split-files "SRR1168497"
gz1="$(_compress_if_exists 'SRR1168497_1' || true)"
gz2="$(_compress_if_exists 'SRR1168497_2' || true)"
gzs="$(_compress_if_exists 'SRR1168497' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "H1_D8_1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "H1_D8_2.fq.gz"
fi

fasterq-dump --split-files "SRR1168498"
gz1="$(_compress_if_exists 'SRR1168498_1' || true)"
gz2="$(_compress_if_exists 'SRR1168498_2' || true)"
gzs="$(_compress_if_exists 'SRR1168498' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "H_Endo_1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "H_Endo_2.fq.gz"
fi

fasterq-dump --split-files "SRR1168499"
gz1="$(_compress_if_exists 'SRR1168499_1' || true)"
gz2="$(_compress_if_exists 'SRR1168499_2' || true)"
gzs="$(_compress_if_exists 'SRR1168499' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "HUVEC_1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "HUVEC_2.fq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1168494/SRR1168494"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1168495/SRR1168495"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1168496/SRR1168496"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1168497/SRR1168497"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1168498/SRR1168498"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1168499/SRR1168499"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1168494/SRR1168494"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1168495/SRR1168495"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1168496/SRR1168496"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1168497/SRR1168497"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1168498/SRR1168498"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1168499/SRR1168499"

echo "Download complete. Files saved to $OUTDIR"