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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE171nnn/GSE171553/suppl/GSE171553_RPS3A_eCLIP_peaks.tsv.gz" -O "GSE171553_RPS3A_eCLIP_peaks.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 "SRR14150910"
gz1="$(_compress_if_exists 'SRR14150910_1' || true)"
gz2="$(_compress_if_exists 'SRR14150910_2' || true)"
gzs="$(_compress_if_exists 'SRR14150910' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "RPS3A_rep1_input_S5_L008_R1_001.fastq.gz"
fi

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

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

fasterq-dump --split-files "SRR14150913"
gz1="$(_compress_if_exists 'SRR14150913_1' || true)"
gz2="$(_compress_if_exists 'SRR14150913_2' || true)"
gzs="$(_compress_if_exists 'SRR14150913' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "RPS3A_rep2_IP_S16_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/SRR14150910/SRR14150910"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR14150911/SRR14150911"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR14150912/SRR14150912"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR14150913/SRR14150913"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR14150910/SRR14150910"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR14150911/SRR14150911"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR14150912/SRR14150912"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR14150913/SRR14150913"

echo "Download complete. Files saved to $OUTDIR"