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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE86nnn/GSE86227/suppl/GSE86227_mature_counts_hg19.csv.gz" -O "GSE86227_mature_counts_hg19.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE86nnn/GSE86227/suppl/GSE86227_mature_counts_mm10.csv.gz" -O "GSE86227_mature_counts_mm10.csv.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 "SRR4175377"
gz1="$(_compress_if_exists 'SRR4175377_1' || true)"
gz2="$(_compress_if_exists 'SRR4175377_2' || true)"
gzs="$(_compress_if_exists 'SRR4175377' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "B7P80-A1_S2_L001_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR4175378"
gz1="$(_compress_if_exists 'SRR4175378_1' || true)"
gz2="$(_compress_if_exists 'SRR4175378_2' || true)"
gzs="$(_compress_if_exists 'SRR4175378' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "B7P80-A2_S9_L001_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR4175379"
gz1="$(_compress_if_exists 'SRR4175379_1' || true)"
gz2="$(_compress_if_exists 'SRR4175379_2' || true)"
gzs="$(_compress_if_exists 'SRR4175379' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "B7P80-A3_S11_L001_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR4175380"
gz1="$(_compress_if_exists 'SRR4175380_1' || true)"
gz2="$(_compress_if_exists 'SRR4175380_2' || true)"
gzs="$(_compress_if_exists 'SRR4175380' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "B7P80-D1_S12_L001_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR4175381"
gz1="$(_compress_if_exists 'SRR4175381_1' || true)"
gz2="$(_compress_if_exists 'SRR4175381_2' || true)"
gzs="$(_compress_if_exists 'SRR4175381' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "B7P80-D2_S13_L001_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR4175382"
gz1="$(_compress_if_exists 'SRR4175382_1' || true)"
gz2="$(_compress_if_exists 'SRR4175382_2' || true)"
gzs="$(_compress_if_exists 'SRR4175382' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "B7P80-D3_S8_L001_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR4175383"
gz1="$(_compress_if_exists 'SRR4175383_1' || true)"
gz2="$(_compress_if_exists 'SRR4175383_2' || true)"
gzs="$(_compress_if_exists 'SRR4175383' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "B7P80-C-1_S4_L001_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR4175384"
gz1="$(_compress_if_exists 'SRR4175384_1' || true)"
gz2="$(_compress_if_exists 'SRR4175384_2' || true)"
gzs="$(_compress_if_exists 'SRR4175384' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "B7P80-C-2_S5_L001_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR4175385"
gz1="$(_compress_if_exists 'SRR4175385_1' || true)"
gz2="$(_compress_if_exists 'SRR4175385_2' || true)"
gzs="$(_compress_if_exists 'SRR4175385' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "B7P80-C-3_S6_L001_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR4175386"
gz1="$(_compress_if_exists 'SRR4175386_1' || true)"
gz2="$(_compress_if_exists 'SRR4175386_2' || true)"
gzs="$(_compress_if_exists 'SRR4175386' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "B7P80-501-1_S7_L001_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR4175387"
gz1="$(_compress_if_exists 'SRR4175387_1' || true)"
gz2="$(_compress_if_exists 'SRR4175387_2' || true)"
gzs="$(_compress_if_exists 'SRR4175387' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "B7P80-501-2_S1_L001_R1_001.fastq.gz"
fi

fasterq-dump --split-files "SRR4175388"
gz1="$(_compress_if_exists 'SRR4175388_1' || true)"
gz2="$(_compress_if_exists 'SRR4175388_2' || true)"
gzs="$(_compress_if_exists 'SRR4175388' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "B7P80-501-3_S3_L001_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/SRR4175377/SRR4175377"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175378/SRR4175378"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175379/SRR4175379"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175380/SRR4175380"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175381/SRR4175381"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175382/SRR4175382"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175383/SRR4175383"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175384/SRR4175384"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175385/SRR4175385"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175386/SRR4175386"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175387/SRR4175387"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175388/SRR4175388"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175386/SRR4175386"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175387/SRR4175387"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175388/SRR4175388"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175377/SRR4175377"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175378/SRR4175378"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175379/SRR4175379"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175383/SRR4175383"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175384/SRR4175384"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175385/SRR4175385"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175380/SRR4175380"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175381/SRR4175381"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR4175382/SRR4175382"

echo "Download complete. Files saved to $OUTDIR"