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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE55nnn/GSE55887/suppl/GSE55887_RAW.tar" -O "GSE55887_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 "SRR1191627"
gz1="$(_compress_if_exists 'SRR1191627_1' || true)"
gz2="$(_compress_if_exists 'SRR1191627_2' || true)"
gzs="$(_compress_if_exists 'SRR1191627' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "sgRNA1_Replicate1.fastq.gz"
fi

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

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

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

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

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

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

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

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

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

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

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

fasterq-dump --split-files "SRR1191639"
gz1="$(_compress_if_exists 'SRR1191639_1' || true)"
gz2="$(_compress_if_exists 'SRR1191639_2' || true)"
gzs="$(_compress_if_exists 'SRR1191639' || true)"

fasterq-dump --split-files "SRR1191640"
gz1="$(_compress_if_exists 'SRR1191640_1' || true)"
gz2="$(_compress_if_exists 'SRR1191640_2' || true)"
gzs="$(_compress_if_exists 'SRR1191640' || true)"


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191627/SRR1191627"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191628/SRR1191628"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191629/SRR1191629"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191630/SRR1191630"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191631/SRR1191631"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191632/SRR1191632"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191633/SRR1191633"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191634/SRR1191634"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191635/SRR1191635"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191636/SRR1191636"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191637/SRR1191637"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191638/SRR1191638"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191639/SRR1191639"
curl -L -O -C - "https://sra-pub-src-1.s3.amazonaws.com/SRR1191640/WTCas9only.bam.1"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191638/SRR1191638"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191627/SRR1191627"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191629/SRR1191629"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191628/SRR1191628"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191633/SRR1191633"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191635/SRR1191635"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191636/SRR1191636"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191637/SRR1191637"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191630/SRR1191630"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191631/SRR1191631"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191632/SRR1191632"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191634/SRR1191634"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR1191639/SRR1191639"
curl -L -O -C - "https://sra-pub-src-1.s3.amazonaws.com/SRR1191640/WTCas9only.bam.1"

echo "Download complete. Files saved to $OUTDIR"