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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE147nnn/GSE147263/suppl/GSE147263_Processed_Tumor_RNASeq.txt.gz" -O "GSE147263_Processed_Tumor_RNASeq.txt.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 "SRR11357341"
gz1="$(_compress_if_exists 'SRR11357341_1' || true)"
gz2="$(_compress_if_exists 'SRR11357341_2' || true)"
gzs="$(_compress_if_exists 'SRR11357341' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SK-3L4G_G10.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SK-3L4G_G10.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11357342"
gz1="$(_compress_if_exists 'SRR11357342_1' || true)"
gz2="$(_compress_if_exists 'SRR11357342_2' || true)"
gzs="$(_compress_if_exists 'SRR11357342' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SK-3L4G_G11.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SK-3L4G_G11.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11357343"
gz1="$(_compress_if_exists 'SRR11357343_1' || true)"
gz2="$(_compress_if_exists 'SRR11357343_2' || true)"
gzs="$(_compress_if_exists 'SRR11357343' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SK-3L4G_G8.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SK-3L4G_G8.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11357344"
gz1="$(_compress_if_exists 'SRR11357344_1' || true)"
gz2="$(_compress_if_exists 'SRR11357344_2' || true)"
gzs="$(_compress_if_exists 'SRR11357344' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SK-3L4G_G9.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SK-3L4G_G9.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11357345"
gz1="$(_compress_if_exists 'SRR11357345_1' || true)"
gz2="$(_compress_if_exists 'SRR11357345_2' || true)"
gzs="$(_compress_if_exists 'SRR11357345' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SK-3L4G_G12.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SK-3L4G_G12.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11357346"
gz1="$(_compress_if_exists 'SRR11357346_1' || true)"
gz2="$(_compress_if_exists 'SRR11357346_2' || true)"
gzs="$(_compress_if_exists 'SRR11357346' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SK-3L4G_H1.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SK-3L4G_H1.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11357347"
gz1="$(_compress_if_exists 'SRR11357347_1' || true)"
gz2="$(_compress_if_exists 'SRR11357347_2' || true)"
gzs="$(_compress_if_exists 'SRR11357347' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SK-3L4G_G4.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SK-3L4G_G4.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11357348"
gz1="$(_compress_if_exists 'SRR11357348_1' || true)"
gz2="$(_compress_if_exists 'SRR11357348_2' || true)"
gzs="$(_compress_if_exists 'SRR11357348' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SK-3L4G_G3.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SK-3L4G_G3.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11357349"
gz1="$(_compress_if_exists 'SRR11357349_1' || true)"
gz2="$(_compress_if_exists 'SRR11357349_2' || true)"
gzs="$(_compress_if_exists 'SRR11357349' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SK-3L4G_G2.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SK-3L4G_G2.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11357350"
gz1="$(_compress_if_exists 'SRR11357350_1' || true)"
gz2="$(_compress_if_exists 'SRR11357350_2' || true)"
gzs="$(_compress_if_exists 'SRR11357350' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SK-3L4G_G5.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SK-3L4G_G5.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11357351"
gz1="$(_compress_if_exists 'SRR11357351_1' || true)"
gz2="$(_compress_if_exists 'SRR11357351_2' || true)"
gzs="$(_compress_if_exists 'SRR11357351' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SK-3L4G_G6.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SK-3L4G_G6.R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11357352"
gz1="$(_compress_if_exists 'SRR11357352_1' || true)"
gz2="$(_compress_if_exists 'SRR11357352_2' || true)"
gzs="$(_compress_if_exists 'SRR11357352' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "SK-3L4G_G7.R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "SK-3L4G_G7.R2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357341/SRR11357341"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357342/SRR11357342"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357343/SRR11357343"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357344/SRR11357344"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357345/SRR11357345"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357346/SRR11357346"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357347/SRR11357347"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357348/SRR11357348"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357349/SRR11357349"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357350/SRR11357350"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357351/SRR11357351"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357352/SRR11357352"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357341/SRR11357341"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357342/SRR11357342"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357345/SRR11357345"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357349/SRR11357349"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357348/SRR11357348"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357347/SRR11357347"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357350/SRR11357350"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357351/SRR11357351"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357352/SRR11357352"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357343/SRR11357343"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357344/SRR11357344"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11357346/SRR11357346"

echo "Download complete. Files saved to $OUTDIR"