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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE90nnn/GSE90963/suppl/GSE90963_Table_S1-m5C_candidate_sites.xlsx" -O "GSE90963_Table_S1-m5C_candidate_sites.xlsx"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE90nnn/GSE90963/suppl/GSE90963_Table_S3-m1A_candidate_sites.xlsx" -O "GSE90963_Table_S3-m1A_candidate_sites.xlsx"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE90nnn/GSE90963/suppl/GSE90963_Table_S5-PseudoU_candidate_sites.xlsx" -O "GSE90963_Table_S5-PseudoU_candidate_sites.xlsx"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE90nnn/GSE90963/suppl/GSE90963_Table_S8-PseudoU_candidate_site_validation.xlsx" -O "GSE90963_Table_S8-PseudoU_candidate_site_validation.xlsx"

# --- 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 "SRR5058077"
gz1="$(_compress_if_exists 'SRR5058077_1' || true)"
gz2="$(_compress_if_exists 'SRR5058077_2' || true)"
gzs="$(_compress_if_exists 'SRR5058077' || true)"

fasterq-dump --split-files "SRR5058078"
gz1="$(_compress_if_exists 'SRR5058078_1' || true)"
gz2="$(_compress_if_exists 'SRR5058078_2' || true)"
gzs="$(_compress_if_exists 'SRR5058078' || true)"

fasterq-dump --split-files "SRR5058079"
gz1="$(_compress_if_exists 'SRR5058079_1' || true)"
gz2="$(_compress_if_exists 'SRR5058079_2' || true)"
gzs="$(_compress_if_exists 'SRR5058079' || true)"

fasterq-dump --split-files "SRR5058080"
gz1="$(_compress_if_exists 'SRR5058080_1' || true)"
gz2="$(_compress_if_exists 'SRR5058080_2' || true)"
gzs="$(_compress_if_exists 'SRR5058080' || true)"

fasterq-dump --split-files "SRR5058081"
gz1="$(_compress_if_exists 'SRR5058081_1' || true)"
gz2="$(_compress_if_exists 'SRR5058081_2' || true)"
gzs="$(_compress_if_exists 'SRR5058081' || true)"

fasterq-dump --split-files "SRR5058082"
gz1="$(_compress_if_exists 'SRR5058082_1' || true)"
gz2="$(_compress_if_exists 'SRR5058082_2' || true)"
gzs="$(_compress_if_exists 'SRR5058082' || true)"

fasterq-dump --split-files "SRR5058083"
gz1="$(_compress_if_exists 'SRR5058083_1' || true)"
gz2="$(_compress_if_exists 'SRR5058083_2' || true)"
gzs="$(_compress_if_exists 'SRR5058083' || true)"

fasterq-dump --split-files "SRR5058084"
gz1="$(_compress_if_exists 'SRR5058084_1' || true)"
gz2="$(_compress_if_exists 'SRR5058084_2' || true)"
gzs="$(_compress_if_exists 'SRR5058084' || true)"

fasterq-dump --split-files "SRR5058085"
gz1="$(_compress_if_exists 'SRR5058085_1' || true)"
gz2="$(_compress_if_exists 'SRR5058085_2' || true)"
gzs="$(_compress_if_exists 'SRR5058085' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "11737x2_S2_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "11737x2_S2_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR5058086"
gz1="$(_compress_if_exists 'SRR5058086_1' || true)"
gz2="$(_compress_if_exists 'SRR5058086_2' || true)"
gzs="$(_compress_if_exists 'SRR5058086' || true)"

fasterq-dump --split-files "SRR5058087"
gz1="$(_compress_if_exists 'SRR5058087_1' || true)"
gz2="$(_compress_if_exists 'SRR5058087_2' || true)"
gzs="$(_compress_if_exists 'SRR5058087' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "11737x1_S1_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "11737x1_S1_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR5058088"
gz1="$(_compress_if_exists 'SRR5058088_1' || true)"
gz2="$(_compress_if_exists 'SRR5058088_2' || true)"
gzs="$(_compress_if_exists 'SRR5058088' || true)"

fasterq-dump --split-files "SRR5058089"
gz1="$(_compress_if_exists 'SRR5058089_1' || true)"
gz2="$(_compress_if_exists 'SRR5058089_2' || true)"
gzs="$(_compress_if_exists 'SRR5058089' || true)"

fasterq-dump --split-files "SRR5058090"
gz1="$(_compress_if_exists 'SRR5058090_1' || true)"
gz2="$(_compress_if_exists 'SRR5058090_2' || true)"
gzs="$(_compress_if_exists 'SRR5058090' || true)"


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058077/SRR5058077"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058078/SRR5058078"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058079/SRR5058079"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058080/SRR5058080"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058081/SRR5058081"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058082/SRR5058082"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058083/SRR5058083"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058084/SRR5058084"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058085/SRR5058085"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058086/SRR5058086"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058087/SRR5058087"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058088/SRR5058088"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058089/SRR5058089"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058090/SRR5058090"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058087/SRR5058087"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058085/SRR5058085"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058079/SRR5058079"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058082/SRR5058082"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058086/SRR5058086"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058078/SRR5058078"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058077/SRR5058077"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058084/SRR5058084"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058080/SRR5058080"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058081/SRR5058081"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058089/SRR5058089"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058088/SRR5058088"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058090/SRR5058090"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5058083/SRR5058083"

echo "Download complete. Files saved to $OUTDIR"