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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE113nnn/GSE113193/suppl/GSE113193_RAW.tar" -O "GSE113193_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 "SRR7010498"
gz1="$(_compress_if_exists 'SRR7010498_1' || true)"
gz2="$(_compress_if_exists 'SRR7010498_2' || true)"
gzs="$(_compress_if_exists 'SRR7010498' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SRSF9-rep1-input_S4_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SRSF9-rep1-input_S4_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR7010499"
gz1="$(_compress_if_exists 'SRR7010499_1' || true)"
gz2="$(_compress_if_exists 'SRR7010499_2' || true)"
gzs="$(_compress_if_exists 'SRR7010499' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SRSF9-rep2-input_S1_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SRSF9-rep2-input_S1_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR7010500"
gz1="$(_compress_if_exists 'SRR7010500_1' || true)"
gz2="$(_compress_if_exists 'SRR7010500_2' || true)"
gzs="$(_compress_if_exists 'SRR7010500' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "IgG-IP_S3_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "IgG-IP_S3_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR7010501"
gz1="$(_compress_if_exists 'SRR7010501_1' || true)"
gz2="$(_compress_if_exists 'SRR7010501_2' || true)"
gzs="$(_compress_if_exists 'SRR7010501' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SRSF9-rep1-IP_S2_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SRSF9-rep1-IP_S2_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR7010502"
gz1="$(_compress_if_exists 'SRR7010502_1' || true)"
gz2="$(_compress_if_exists 'SRR7010502_2' || true)"
gzs="$(_compress_if_exists 'SRR7010502' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SRSF9-rep2-IP_S5_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SRSF9-rep2-IP_S5_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/SRR7010498/SRR7010498"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7010499/SRR7010499"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7010500/SRR7010500"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7010501/SRR7010501"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7010502/SRR7010502"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7010500/SRR7010500"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7010498/SRR7010498"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7010501/SRR7010501"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7010499/SRR7010499"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR7010502/SRR7010502"

echo "Download complete. Files saved to $OUTDIR"