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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE213nnn/GSE213470/suppl/GSE213470_Metadata4Wenhao_Cl13.csv.gz" -O "GSE213470_Metadata4Wenhao_Cl13.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE213nnn/GSE213470/suppl/GSE213470_UMI4Wenhao_Cl13_full_table.csv.gz" -O "GSE213470_UMI4Wenhao_Cl13_full_table.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 "SRR21594696"
gz1="$(_compress_if_exists 'SRR21594696_1' || true)"
gz2="$(_compress_if_exists 'SRR21594696_2' || true)"
gzs="$(_compress_if_exists 'SRR21594696' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_10X_D5_Arm_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_10X_D5_Arm_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594697"
gz1="$(_compress_if_exists 'SRR21594697_1' || true)"
gz2="$(_compress_if_exists 'SRR21594697_2' || true)"
gzs="$(_compress_if_exists 'SRR21594697' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_10X_D3_Arm_final_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_10X_D3_Arm_final_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594698"
gz1="$(_compress_if_exists 'SRR21594698_1' || true)"
gz2="$(_compress_if_exists 'SRR21594698_2' || true)"
gzs="$(_compress_if_exists 'SRR21594698' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_10X_Div_1_Arm_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_10X_Div_1_Arm_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594699"
gz1="$(_compress_if_exists 'SRR21594699_1' || true)"
gz2="$(_compress_if_exists 'SRR21594699_2' || true)"
gzs="$(_compress_if_exists 'SRR21594699' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_10X_Naive_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_10X_Naive_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594700"
gz1="$(_compress_if_exists 'SRR21594700_1' || true)"
gz2="$(_compress_if_exists 'SRR21594700_2' || true)"
gzs="$(_compress_if_exists 'SRR21594700' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_10X_D60_Cl13_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_10X_D60_Cl13_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594701"
gz1="$(_compress_if_exists 'SRR21594701_1' || true)"
gz2="$(_compress_if_exists 'SRR21594701_2' || true)"
gzs="$(_compress_if_exists 'SRR21594701' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_D34_Cl13_Re-do_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_D34_Cl13_Re-do_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594702"
gz1="$(_compress_if_exists 'SRR21594702_1' || true)"
gz2="$(_compress_if_exists 'SRR21594702_2' || true)"
gzs="$(_compress_if_exists 'SRR21594702' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_D22_Cl13_Re-do_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_D22_Cl13_Re-do_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594703"
gz1="$(_compress_if_exists 'SRR21594703_1' || true)"
gz2="$(_compress_if_exists 'SRR21594703_2' || true)"
gzs="$(_compress_if_exists 'SRR21594703' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_10X_D8_Cl13_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_10X_D8_Cl13_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594704"
gz1="$(_compress_if_exists 'SRR21594704_1' || true)"
gz2="$(_compress_if_exists 'SRR21594704_2' || true)"
gzs="$(_compress_if_exists 'SRR21594704' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_D5_Cl13_Re-Do_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_D5_Cl13_Re-Do_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594705"
gz1="$(_compress_if_exists 'SRR21594705_1' || true)"
gz2="$(_compress_if_exists 'SRR21594705_2' || true)"
gzs="$(_compress_if_exists 'SRR21594705' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_10X_D3_Cl13_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_10X_D3_Cl13_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594706"
gz1="$(_compress_if_exists 'SRR21594706_1' || true)"
gz2="$(_compress_if_exists 'SRR21594706_2' || true)"
gzs="$(_compress_if_exists 'SRR21594706' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_10X_Div_1_Cl13_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_10X_Div_1_Cl13_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594707"
gz1="$(_compress_if_exists 'SRR21594707_1' || true)"
gz2="$(_compress_if_exists 'SRR21594707_2' || true)"
gzs="$(_compress_if_exists 'SRR21594707' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_10X_D60_Arm_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_10X_D60_Arm_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594708"
gz1="$(_compress_if_exists 'SRR21594708_1' || true)"
gz2="$(_compress_if_exists 'SRR21594708_2' || true)"
gzs="$(_compress_if_exists 'SRR21594708' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_10X_D22_Arm_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_10X_D22_Arm_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594709"
gz1="$(_compress_if_exists 'SRR21594709_1' || true)"
gz2="$(_compress_if_exists 'SRR21594709_2' || true)"
gzs="$(_compress_if_exists 'SRR21594709' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_10X_D8_Arm_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_10X_D8_Arm_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594710"
gz1="$(_compress_if_exists 'SRR21594710_1' || true)"
gz2="$(_compress_if_exists 'SRR21594710_2' || true)"
gzs="$(_compress_if_exists 'SRR21594710' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_10X_D7_Arm_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_10X_D7_Arm_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR21594711"
gz1="$(_compress_if_exists 'SRR21594711_1' || true)"
gz2="$(_compress_if_exists 'SRR21594711_2' || true)"
gzs="$(_compress_if_exists 'SRR21594711' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "LQ_10X_D6_Arm_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "LQ_10X_D6_Arm_R2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594696/SRR21594696"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594697/SRR21594697"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594698/SRR21594698"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594699/SRR21594699"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594700/SRR21594700"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594701/SRR21594701"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594702/SRR21594702"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594703/SRR21594703"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594704/SRR21594704"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594705/SRR21594705"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594706/SRR21594706"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594707/SRR21594707"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594708/SRR21594708"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594709/SRR21594709"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594710/SRR21594710"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594711/SRR21594711"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594708/SRR21594708"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594697/SRR21594697"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594705/SRR21594705"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594696/SRR21594696"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594707/SRR21594707"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594700/SRR21594700"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594711/SRR21594711"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594710/SRR21594710"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594709/SRR21594709"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594703/SRR21594703"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594698/SRR21594698"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594706/SRR21594706"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594699/SRR21594699"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594702/SRR21594702"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594701/SRR21594701"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR21594704/SRR21594704"

echo "Download complete. Files saved to $OUTDIR"