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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE90nnn/GSE90650/suppl/GSE90650_RAW.tar" -O "GSE90650_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 "SRR5060656"
gz1="$(_compress_if_exists 'SRR5060656_1' || true)"
gz2="$(_compress_if_exists 'SRR5060656_2' || true)"
gzs="$(_compress_if_exists 'SRR5060656' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "siGFP-1.fastq.gz"
fi

fasterq-dump --split-files "SRR5060657"
gz1="$(_compress_if_exists 'SRR5060657_1' || true)"
gz2="$(_compress_if_exists 'SRR5060657_2' || true)"
gzs="$(_compress_if_exists 'SRR5060657' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "siGFP-2.fastq.gz"
fi

fasterq-dump --split-files "SRR5060658"
gz1="$(_compress_if_exists 'SRR5060658_1' || true)"
gz2="$(_compress_if_exists 'SRR5060658_2' || true)"
gzs="$(_compress_if_exists 'SRR5060658' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "siNEAT1-1.fastq.gz"
fi

fasterq-dump --split-files "SRR5060659"
gz1="$(_compress_if_exists 'SRR5060659_1' || true)"
gz2="$(_compress_if_exists 'SRR5060659_2' || true)"
gzs="$(_compress_if_exists 'SRR5060659' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "siNEAT1-2.fastq.gz"
fi

fasterq-dump --split-files "SRR5060660"
gz1="$(_compress_if_exists 'SRR5060660_1' || true)"
gz2="$(_compress_if_exists 'SRR5060660_2' || true)"
gzs="$(_compress_if_exists 'SRR5060660' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "siNEAT1_V2-1.fastq.gz"
fi

fasterq-dump --split-files "SRR5060661"
gz1="$(_compress_if_exists 'SRR5060661_1' || true)"
gz2="$(_compress_if_exists 'SRR5060661_2' || true)"
gzs="$(_compress_if_exists 'SRR5060661' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "siNEAT1_V2-2.fastq.gz"
fi

fasterq-dump --split-files "SRR5060662"
gz1="$(_compress_if_exists 'SRR5060662_1' || true)"
gz2="$(_compress_if_exists 'SRR5060662_2' || true)"
gzs="$(_compress_if_exists 'SRR5060662' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "siNONO-1.fastq.gz"
fi

fasterq-dump --split-files "SRR5060663"
gz1="$(_compress_if_exists 'SRR5060663_1' || true)"
gz2="$(_compress_if_exists 'SRR5060663_2' || true)"
gzs="$(_compress_if_exists 'SRR5060663' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "siNONO-2.fastq.gz"
fi

fasterq-dump --split-files "SRR5060664"
gz1="$(_compress_if_exists 'SRR5060664_1' || true)"
gz2="$(_compress_if_exists 'SRR5060664_2' || true)"
gzs="$(_compress_if_exists 'SRR5060664' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "siPSF-1.fastq.gz"
fi

fasterq-dump --split-files "SRR5060665"
gz1="$(_compress_if_exists 'SRR5060665_1' || true)"
gz2="$(_compress_if_exists 'SRR5060665_2' || true)"
gzs="$(_compress_if_exists 'SRR5060665' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "siPSF-2.fastq.gz"
fi

fasterq-dump --split-files "SRR5060666"
gz1="$(_compress_if_exists 'SRR5060666_1' || true)"
gz2="$(_compress_if_exists 'SRR5060666_2' || true)"
gzs="$(_compress_if_exists 'SRR5060666' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "siPSPC1-1.fastq.gz"
fi

fasterq-dump --split-files "SRR5060667"
gz1="$(_compress_if_exists 'SRR5060667_1' || true)"
gz2="$(_compress_if_exists 'SRR5060667_2' || true)"
gzs="$(_compress_if_exists 'SRR5060667' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "siPSPC1-2.fastq.gz"
fi

fasterq-dump --split-files "SRR5060668"
gz1="$(_compress_if_exists 'SRR5060668_1' || true)"
gz2="$(_compress_if_exists 'SRR5060668_2' || true)"
gzs="$(_compress_if_exists 'SRR5060668' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NONO-1.fastq.gz"
fi

fasterq-dump --split-files "SRR5060669"
gz1="$(_compress_if_exists 'SRR5060669_1' || true)"
gz2="$(_compress_if_exists 'SRR5060669_2' || true)"
gzs="$(_compress_if_exists 'SRR5060669' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NONO-2.fastq.gz"
fi

fasterq-dump --split-files "SRR5060670"
gz1="$(_compress_if_exists 'SRR5060670_1' || true)"
gz2="$(_compress_if_exists 'SRR5060670_2' || true)"
gzs="$(_compress_if_exists 'SRR5060670' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "PSF-1.fastq.gz"
fi

fasterq-dump --split-files "SRR5060671"
gz1="$(_compress_if_exists 'SRR5060671_1' || true)"
gz2="$(_compress_if_exists 'SRR5060671_2' || true)"
gzs="$(_compress_if_exists 'SRR5060671' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "PSF-2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060656/SRR5060656"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060657/SRR5060657"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060658/SRR5060658"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060659/SRR5060659"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060660/SRR5060660"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060661/SRR5060661"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060662/SRR5060662"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060663/SRR5060663"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060664/SRR5060664"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060665/SRR5060665"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060666/SRR5060666"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060667/SRR5060667"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060668/SRR5060668"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060669/SRR5060669"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060670/SRR5060670"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060671/SRR5060671"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060656/SRR5060656"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060657/SRR5060657"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060658/SRR5060658"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060659/SRR5060659"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060660/SRR5060660"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060661/SRR5060661"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060662/SRR5060662"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060663/SRR5060663"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060664/SRR5060664"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060665/SRR5060665"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060666/SRR5060666"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060667/SRR5060667"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060668/SRR5060668"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060669/SRR5060669"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060670/SRR5060670"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR5060671/SRR5060671"

echo "Download complete. Files saved to $OUTDIR"