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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE220nnn/GSE220461/suppl/GSE220461_scRNA-seq_CPM.xlsx" -O "GSE220461_scRNA-seq_CPM.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 "SRR22566923"
gz1="$(_compress_if_exists 'SRR22566923_1' || true)"
gz2="$(_compress_if_exists 'SRR22566923_2' || true)"
gzs="$(_compress_if_exists 'SRR22566923' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CRE_IDT_3_CKDL210009136-1a-AK10638_HG7NTBBXX_L7_1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CRE_IDT_3_CKDL210009136-1a-AK10638_HG7NTBBXX_L7_2.fq.gz"
fi

fasterq-dump --split-files "SRR22566924"
gz1="$(_compress_if_exists 'SRR22566924_1' || true)"
gz2="$(_compress_if_exists 'SRR22566924_2' || true)"
gzs="$(_compress_if_exists 'SRR22566924' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CRE_IDT_1_CKDL210009136-1a-AK6102_HG7NTBBXX_L7_1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CRE_IDT_1_CKDL210009136-1a-AK6102_HG7NTBBXX_L7_2.fq.gz"
fi

fasterq-dump --split-files "SRR22566925"
gz1="$(_compress_if_exists 'SRR22566925_1' || true)"
gz2="$(_compress_if_exists 'SRR22566925_2' || true)"
gzs="$(_compress_if_exists 'SRR22566925' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CRE_10x_2_CKDL210009136-1a-SI_GA_E2_HG7NTBBXX_S2_L007_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CRE_10x_2_CKDL210009136-1a-SI_GA_E2_HG7NTBBXX_S2_L007_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR22566926"
gz1="$(_compress_if_exists 'SRR22566926_1' || true)"
gz2="$(_compress_if_exists 'SRR22566926_2' || true)"
gzs="$(_compress_if_exists 'SRR22566926' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CRE_IDT_2_CKDL210009136-1a-AK6101_HG7NTBBXX_L7_1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CRE_IDT_2_CKDL210009136-1a-AK6101_HG7NTBBXX_L7_2.fq.gz"
fi

fasterq-dump --split-files "SRR22566927"
gz1="$(_compress_if_exists 'SRR22566927_1' || true)"
gz2="$(_compress_if_exists 'SRR22566927_2' || true)"
gzs="$(_compress_if_exists 'SRR22566927' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CRE_IDT_4_CKDL210009136-1a-AK10639_HG7NTBBXX_L7_1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CRE_IDT_4_CKDL210009136-1a-AK10639_HG7NTBBXX_L7_2.fq.gz"
fi

fasterq-dump --split-files "SRR22566928"
gz1="$(_compress_if_exists 'SRR22566928_1' || true)"
gz2="$(_compress_if_exists 'SRR22566928_2' || true)"
gzs="$(_compress_if_exists 'SRR22566928' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "GFP_IDT_3_CKDL210009136-1a-AK6104_HG7NTBBXX_L7_1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "GFP_IDT_3_CKDL210009136-1a-AK6104_HG7NTBBXX_L7_2.fq.gz"
fi

fasterq-dump --split-files "SRR22566929"
gz1="$(_compress_if_exists 'SRR22566929_1' || true)"
gz2="$(_compress_if_exists 'SRR22566929_2' || true)"
gzs="$(_compress_if_exists 'SRR22566929' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "GFP_IDT_1_CKDL210009136-1a-AK10633_HG7NTBBXX_L7_1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "GFP_IDT_1_CKDL210009136-1a-AK10633_HG7NTBBXX_L7_2.fq.gz"
fi

fasterq-dump --split-files "SRR22566930"
gz1="$(_compress_if_exists 'SRR22566930_1' || true)"
gz2="$(_compress_if_exists 'SRR22566930_2' || true)"
gzs="$(_compress_if_exists 'SRR22566930' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "GFP_10x_1_CKDL210009136-1a-SI_GA_E1_HG7NTBBXX_S1_L007_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "GFP_10x_1_CKDL210009136-1a-SI_GA_E1_HG7NTBBXX_S1_L007_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR22566931"
gz1="$(_compress_if_exists 'SRR22566931_1' || true)"
gz2="$(_compress_if_exists 'SRR22566931_2' || true)"
gzs="$(_compress_if_exists 'SRR22566931' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "GFP_IDT_2_CKDL210009136-1a-AK6103_HG7NTBBXX_L7_1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "GFP_IDT_2_CKDL210009136-1a-AK6103_HG7NTBBXX_L7_2.fq.gz"
fi

fasterq-dump --split-files "SRR22566932"
gz1="$(_compress_if_exists 'SRR22566932_1' || true)"
gz2="$(_compress_if_exists 'SRR22566932_2' || true)"
gzs="$(_compress_if_exists 'SRR22566932' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "GFP_IDT_4_CKDL210009136-1a-AK9608_HG7NTBBXX_L7_1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "GFP_IDT_4_CKDL210009136-1a-AK9608_HG7NTBBXX_L7_2.fq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566923/SRR22566923"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566924/SRR22566924"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566925/SRR22566925"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566926/SRR22566926"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566927/SRR22566927"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566928/SRR22566928"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566929/SRR22566929"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566930/SRR22566930"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566931/SRR22566931"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566932/SRR22566932"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566925/SRR22566925"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566924/SRR22566924"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566926/SRR22566926"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566923/SRR22566923"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566927/SRR22566927"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566930/SRR22566930"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566929/SRR22566929"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566931/SRR22566931"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566928/SRR22566928"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR22566932/SRR22566932"

echo "Download complete. Files saved to $OUTDIR"