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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE107nnn/GSE107867/suppl/GSE107867_fragileX_gene_expression.txt.gz" -O "GSE107867_fragileX_gene_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 "SRR6363414"
gz1="$(_compress_if_exists 'SRR6363414_1' || true)"
gz2="$(_compress_if_exists 'SRR6363414_2' || true)"
gzs="$(_compress_if_exists 'SRR6363414' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "fragileX_sample_5319.1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "fragileX_sample_5319.2.fastq.gz"
fi

fasterq-dump --split-files "SRR6363415"
gz1="$(_compress_if_exists 'SRR6363415_1' || true)"
gz2="$(_compress_if_exists 'SRR6363415_2' || true)"
gzs="$(_compress_if_exists 'SRR6363415' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "fragileX_sample_5746.1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "fragileX_sample_5746.2.fastq.gz"
fi

fasterq-dump --split-files "SRR6363416"
gz1="$(_compress_if_exists 'SRR6363416_1' || true)"
gz2="$(_compress_if_exists 'SRR6363416_2' || true)"
gzs="$(_compress_if_exists 'SRR6363416' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "carrier_sample_5212.1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "carrier_sample_5212.2.fastq.gz"
fi

fasterq-dump --split-files "SRR6363417"
gz1="$(_compress_if_exists 'SRR6363417_1' || true)"
gz2="$(_compress_if_exists 'SRR6363417_2' || true)"
gzs="$(_compress_if_exists 'SRR6363417' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "carrier_sample_5529.1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "carrier_sample_5529.2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6363414/SRR6363414"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6363415/SRR6363415"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6363416/SRR6363416"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6363417/SRR6363417"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6363416/SRR6363416"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6363417/SRR6363417"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6363414/SRR6363414"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6363415/SRR6363415"

echo "Download complete. Files saved to $OUTDIR"