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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE203nnn/GSE203091/suppl/GSE203091_RAW.tar" -O "GSE203091_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 "SRR19221346"
gz1="$(_compress_if_exists 'SRR19221346_1' || true)"
gz2="$(_compress_if_exists 'SRR19221346_2' || true)"
gzs="$(_compress_if_exists 'SRR19221346' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NT_MP_iso.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NT_MP_iso.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR19221347"
gz1="$(_compress_if_exists 'SRR19221347_1' || true)"
gz2="$(_compress_if_exists 'SRR19221347_2' || true)"
gzs="$(_compress_if_exists 'SRR19221347' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NT_Malat1_iso.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NT_Malat1_iso.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR19221348"
gz1="$(_compress_if_exists 'SRR19221348_1' || true)"
gz2="$(_compress_if_exists 'SRR19221348_2' || true)"
gzs="$(_compress_if_exists 'SRR19221348' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NT_TE_iso.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NT_TE_iso.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR19221349"
gz1="$(_compress_if_exists 'SRR19221349_1' || true)"
gz2="$(_compress_if_exists 'SRR19221349_2' || true)"
gzs="$(_compress_if_exists 'SRR19221349' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Malat1_TE_iso.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Malat1_TE_iso.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR19221350"
gz1="$(_compress_if_exists 'SRR19221350_1' || true)"
gz2="$(_compress_if_exists 'SRR19221350_2' || true)"
gzs="$(_compress_if_exists 'SRR19221350' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NT_MP_2.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NT_MP_2.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR19221351"
gz1="$(_compress_if_exists 'SRR19221351_1' || true)"
gz2="$(_compress_if_exists 'SRR19221351_2' || true)"
gzs="$(_compress_if_exists 'SRR19221351' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NT_MP_1.R1.fastq.gz"
fi

fasterq-dump --split-files "SRR19221352"
gz1="$(_compress_if_exists 'SRR19221352_1' || true)"
gz2="$(_compress_if_exists 'SRR19221352_2' || true)"
gzs="$(_compress_if_exists 'SRR19221352' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Malat1_MP_2.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Malat1_MP_2.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR19221353"
gz1="$(_compress_if_exists 'SRR19221353_1' || true)"
gz2="$(_compress_if_exists 'SRR19221353_2' || true)"
gzs="$(_compress_if_exists 'SRR19221353' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Malat1_MP_1.R1.fastq.gz"
fi

fasterq-dump --split-files "SRR19221354"
gz1="$(_compress_if_exists 'SRR19221354_1' || true)"
gz2="$(_compress_if_exists 'SRR19221354_2' || true)"
gzs="$(_compress_if_exists 'SRR19221354' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NT_TE_2.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "NT_TE_2.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR19221355"
gz1="$(_compress_if_exists 'SRR19221355_1' || true)"
gz2="$(_compress_if_exists 'SRR19221355_2' || true)"
gzs="$(_compress_if_exists 'SRR19221355' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "NT_TE_1.R1.fastq.gz"
fi

fasterq-dump --split-files "SRR19221356"
gz1="$(_compress_if_exists 'SRR19221356_1' || true)"
gz2="$(_compress_if_exists 'SRR19221356_2' || true)"
gzs="$(_compress_if_exists 'SRR19221356' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Malat1_TE_2.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Malat1_TE_2.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR19221357"
gz1="$(_compress_if_exists 'SRR19221357_1' || true)"
gz2="$(_compress_if_exists 'SRR19221357_2' || true)"
gzs="$(_compress_if_exists 'SRR19221357' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Malat1_TE_1.R1.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221346/SRR19221346"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221347/SRR19221347"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221348/SRR19221348"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221349/SRR19221349"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221350/SRR19221350"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221351/SRR19221351"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221352/SRR19221352"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221353/SRR19221353"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221354/SRR19221354"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221355/SRR19221355"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221356/SRR19221356"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221357/SRR19221357"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221353/SRR19221353"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221352/SRR19221352"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221357/SRR19221357"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221356/SRR19221356"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221349/SRR19221349"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221347/SRR19221347"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221351/SRR19221351"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221350/SRR19221350"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221346/SRR19221346"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221355/SRR19221355"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221354/SRR19221354"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR19221348/SRR19221348"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos4/sra-pub-zq-1/SRR019/19221/SRR19221352/SRR19221352.lite.1"

echo "Download complete. Files saved to $OUTDIR"