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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE123nnn/GSE123881/suppl/GSE123881_healthy_Ileum.txt.gz" -O "GSE123881_healthy_Ileum.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE123nnn/GSE123881/suppl/GSE123881_healthy_colon.txt.gz" -O "GSE123881_healthy_colon.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 "SRR8320977"
gz1="$(_compress_if_exists 'SRR8320977_1' || true)"
gz2="$(_compress_if_exists 'SRR8320977_2' || true)"
gzs="$(_compress_if_exists 'SRR8320977' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NA01_S1_L008_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NA01_S1_L008_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR8320978"
gz1="$(_compress_if_exists 'SRR8320978_1' || true)"
gz2="$(_compress_if_exists 'SRR8320978_2' || true)"
gzs="$(_compress_if_exists 'SRR8320978' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NA03_S3_L008_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NA03_S3_L008_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR8320979"
gz1="$(_compress_if_exists 'SRR8320979_1' || true)"
gz2="$(_compress_if_exists 'SRR8320979_2' || true)"
gzs="$(_compress_if_exists 'SRR8320979' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NA02_S2_L008_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NA02_S2_L008_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR8320980"
gz1="$(_compress_if_exists 'SRR8320980_1' || true)"
gz2="$(_compress_if_exists 'SRR8320980_2' || true)"
gzs="$(_compress_if_exists 'SRR8320980' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NA04_S4_L008_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NA04_S4_L008_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR8320981"
gz1="$(_compress_if_exists 'SRR8320981_1' || true)"
gz2="$(_compress_if_exists 'SRR8320981_2' || true)"
gzs="$(_compress_if_exists 'SRR8320981' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NA05_S5_L008_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NA05_S5_L008_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR8320982"
gz1="$(_compress_if_exists 'SRR8320982_1' || true)"
gz2="$(_compress_if_exists 'SRR8320982_2' || true)"
gzs="$(_compress_if_exists 'SRR8320982' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NA07_S7_L008_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NA07_S7_L008_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR8320983"
gz1="$(_compress_if_exists 'SRR8320983_1' || true)"
gz2="$(_compress_if_exists 'SRR8320983_2' || true)"
gzs="$(_compress_if_exists 'SRR8320983' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NA06_S6_L008_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NA06_S6_L008_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR8320984"
gz1="$(_compress_if_exists 'SRR8320984_1' || true)"
gz2="$(_compress_if_exists 'SRR8320984_2' || true)"
gzs="$(_compress_if_exists 'SRR8320984' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NA08_S8_L008_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NA08_S8_L008_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/SRR8320977/SRR8320977"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320978/SRR8320978"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320979/SRR8320979"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320980/SRR8320980"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320981/SRR8320981"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320982/SRR8320982"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320983/SRR8320983"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320984/SRR8320984"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320977/SRR8320977"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320979/SRR8320979"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320978/SRR8320978"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320980/SRR8320980"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320981/SRR8320981"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320983/SRR8320983"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320982/SRR8320982"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR8320984/SRR8320984"

echo "Download complete. Files saved to $OUTDIR"