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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE143nnn/GSE143437/suppl/GSE143437_DeMicheli_MuSCatlas_metadata.txt.gz" -O "GSE143437_DeMicheli_MuSCatlas_metadata.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE143nnn/GSE143437/suppl/GSE143437_DeMicheli_MuSCatlas_normalizeddata.txt.gz" -O "GSE143437_DeMicheli_MuSCatlas_normalizeddata.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE143nnn/GSE143437/suppl/GSE143437_DeMicheli_MuSCatlas_rawdata.txt.gz" -O "GSE143437_DeMicheli_MuSCatlas_rawdata.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 "SRR10870296"
gz1="$(_compress_if_exists 'SRR10870296_1' || true)"
gz2="$(_compress_if_exists 'SRR10870296_2' || true)"
gzs="$(_compress_if_exists 'SRR10870296' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample_77904_HCNLTBGX7_D0_A_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample_77904_HCNLTBGX7_D0_A_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR10870297"
gz1="$(_compress_if_exists 'SRR10870297_1' || true)"
gz2="$(_compress_if_exists 'SRR10870297_2' || true)"
gzs="$(_compress_if_exists 'SRR10870297' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample_77905_HCNLTBGX7_D0_B_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample_77905_HCNLTBGX7_D0_B_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR10870298"
gz1="$(_compress_if_exists 'SRR10870298_1' || true)"
gz2="$(_compress_if_exists 'SRR10870298_2' || true)"
gzs="$(_compress_if_exists 'SRR10870298' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample_87381_HH3Y5BGX9_D0_Cv3_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample_87381_HH3Y5BGX9_D0_Cv3_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR10870299"
gz1="$(_compress_if_exists 'SRR10870299_1' || true)"
gz2="$(_compress_if_exists 'SRR10870299_2' || true)"
gzs="$(_compress_if_exists 'SRR10870299' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample_79500_HGLYTBGX7_D2_C_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample_79500_HGLYTBGX7_D2_C_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR10870300"
gz1="$(_compress_if_exists 'SRR10870300_1' || true)"
gz2="$(_compress_if_exists 'SRR10870300_2' || true)"
gzs="$(_compress_if_exists 'SRR10870300' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample_84097_HMYLYBGX7_D2_D_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample_84097_HMYLYBGX7_D2_D_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR10870301"
gz1="$(_compress_if_exists 'SRR10870301_1' || true)"
gz2="$(_compress_if_exists 'SRR10870301_2' || true)"
gzs="$(_compress_if_exists 'SRR10870301' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample_79493_HV5VGBGX7_D5_A_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample_79493_HV5VGBGX7_D5_A_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR10870302"
gz1="$(_compress_if_exists 'SRR10870302_1' || true)"
gz2="$(_compress_if_exists 'SRR10870302_2' || true)"
gzs="$(_compress_if_exists 'SRR10870302' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample_79499_HGLYTBGX7_D5_B_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample_79499_HGLYTBGX7_D5_B_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR10870303"
gz1="$(_compress_if_exists 'SRR10870303_1' || true)"
gz2="$(_compress_if_exists 'SRR10870303_2' || true)"
gzs="$(_compress_if_exists 'SRR10870303' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample_79495_HV5G3BGX7_D5_C_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample_79495_HV5G3BGX7_D5_C_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR10870304"
gz1="$(_compress_if_exists 'SRR10870304_1' || true)"
gz2="$(_compress_if_exists 'SRR10870304_2' || true)"
gzs="$(_compress_if_exists 'SRR10870304' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample_79496_HV5G3BGX7_D7_C_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample_79496_HV5G3BGX7_D7_C_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR10870305"
gz1="$(_compress_if_exists 'SRR10870305_1' || true)"
gz2="$(_compress_if_exists 'SRR10870305_2' || true)"
gzs="$(_compress_if_exists 'SRR10870305' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample_84098_HMYLYBGX7_D7_D_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample_84098_HMYLYBGX7_D7_D_R2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870296/SRR10870296"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870297/SRR10870297"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870298/SRR10870298"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870299/SRR10870299"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870300/SRR10870300"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870301/SRR10870301"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870302/SRR10870302"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870303/SRR10870303"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870304/SRR10870304"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870305/SRR10870305"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870296/SRR10870296"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870297/SRR10870297"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870301/SRR10870301"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870303/SRR10870303"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870304/SRR10870304"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870302/SRR10870302"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870299/SRR10870299"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870300/SRR10870300"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870305/SRR10870305"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR10870298/SRR10870298"

echo "Download complete. Files saved to $OUTDIR"