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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE107nnn/GSE107122/suppl/GSE107122_E155_Combined_All_Cells_DGE.txt.gz" -O "GSE107122_E155_Combined_All_Cells_DGE.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE107nnn/GSE107122/suppl/GSE107122_E155_Combined_Only_Cortical_Cells_DGE.txt.gz" -O "GSE107122_E155_Combined_Only_Cortical_Cells_DGE.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE107nnn/GSE107122/suppl/GSE107122_RAW.tar" -O "GSE107122_RAW.tar"

# --- 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 "SRR6307231"
gz1="$(_compress_if_exists 'SRR6307231_1' || true)"
gz2="$(_compress_if_exists 'SRR6307231_2' || true)"
gzs="$(_compress_if_exists 'SRR6307231' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "E115_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "E115_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR6307232"
gz1="$(_compress_if_exists 'SRR6307232_1' || true)"
gz2="$(_compress_if_exists 'SRR6307232_2' || true)"
gzs="$(_compress_if_exists 'SRR6307232' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "E135_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "E135_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR6307233"
gz1="$(_compress_if_exists 'SRR6307233_1' || true)"
gz2="$(_compress_if_exists 'SRR6307233_2' || true)"
gzs="$(_compress_if_exists 'SRR6307233' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "E155_Batch1_Run1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "E155_Batch1_Run1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR6307234"
gz1="$(_compress_if_exists 'SRR6307234_1' || true)"
gz2="$(_compress_if_exists 'SRR6307234_2' || true)"
gzs="$(_compress_if_exists 'SRR6307234' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "E155_Batch1_Run2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "E155_Batch1_Run2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR6307235"
gz1="$(_compress_if_exists 'SRR6307235_1' || true)"
gz2="$(_compress_if_exists 'SRR6307235_2' || true)"
gzs="$(_compress_if_exists 'SRR6307235' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "E155_Batch2_Run1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "E155_Batch2_Run1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR6307236"
gz1="$(_compress_if_exists 'SRR6307236_1' || true)"
gz2="$(_compress_if_exists 'SRR6307236_2' || true)"
gzs="$(_compress_if_exists 'SRR6307236' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "E155_Batch2_Run2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "E155_Batch2_Run2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR6307237"
gz1="$(_compress_if_exists 'SRR6307237_1' || true)"
gz2="$(_compress_if_exists 'SRR6307237_2' || true)"
gzs="$(_compress_if_exists 'SRR6307237' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "E175_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "E175_R2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6307231/SRR6307231"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6307232/SRR6307232"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6307233/SRR6307233"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6307234/SRR6307234"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6307235/SRR6307235"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6307236/SRR6307236"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6307237/SRR6307237"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6307231/SRR6307231"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6307232/SRR6307232"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6307233/SRR6307233"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6307234/SRR6307234"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6307235/SRR6307235"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6307236/SRR6307236"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR6307237/SRR6307237"

echo "Download complete. Files saved to $OUTDIR"