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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE146nnn/GSE146916/suppl/GSE146916_DESeq2res_WT_BKO.csv.gz" -O "GSE146916_DESeq2res_WT_BKO.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE146nnn/GSE146916/suppl/GSE146916_DESeq2res_WT_CKO.csv.gz" -O "GSE146916_DESeq2res_WT_CKO.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE146nnn/GSE146916/suppl/GSE146916_RAW.tar" -O "GSE146916_RAW.tar"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE146nnn/GSE146916/suppl/GSE146916_tpm.csv.gz" -O "GSE146916_tpm.csv.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 "SRR11300801"
gz1="$(_compress_if_exists 'SRR11300801_1' || true)"
gz2="$(_compress_if_exists 'SRR11300801_2' || true)"
gzs="$(_compress_if_exists 'SRR11300801' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT_RNA_1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WT_RNA_1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11300802"
gz1="$(_compress_if_exists 'SRR11300802_1' || true)"
gz2="$(_compress_if_exists 'SRR11300802_2' || true)"
gzs="$(_compress_if_exists 'SRR11300802' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT_RNA_2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WT_RNA_2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11300803"
gz1="$(_compress_if_exists 'SRR11300803_1' || true)"
gz2="$(_compress_if_exists 'SRR11300803_2' || true)"
gzs="$(_compress_if_exists 'SRR11300803' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT_RNA_3_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WT_RNA_3_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11300804"
gz1="$(_compress_if_exists 'SRR11300804_1' || true)"
gz2="$(_compress_if_exists 'SRR11300804_2' || true)"
gzs="$(_compress_if_exists 'SRR11300804' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BKO_RNA_1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BKO_RNA_1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11300805"
gz1="$(_compress_if_exists 'SRR11300805_1' || true)"
gz2="$(_compress_if_exists 'SRR11300805_2' || true)"
gzs="$(_compress_if_exists 'SRR11300805' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BKO_RNA_2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BKO_RNA_2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11300806"
gz1="$(_compress_if_exists 'SRR11300806_1' || true)"
gz2="$(_compress_if_exists 'SRR11300806_2' || true)"
gzs="$(_compress_if_exists 'SRR11300806' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "BKO_RNA_3_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "BKO_RNA_3_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11300807"
gz1="$(_compress_if_exists 'SRR11300807_1' || true)"
gz2="$(_compress_if_exists 'SRR11300807_2' || true)"
gzs="$(_compress_if_exists 'SRR11300807' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CKO_RNA_1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CKO_RNA_1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11300808"
gz1="$(_compress_if_exists 'SRR11300808_1' || true)"
gz2="$(_compress_if_exists 'SRR11300808_2' || true)"
gzs="$(_compress_if_exists 'SRR11300808' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CKO_RNA_2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CKO_RNA_2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR11300809"
gz1="$(_compress_if_exists 'SRR11300809_1' || true)"
gz2="$(_compress_if_exists 'SRR11300809_2' || true)"
gzs="$(_compress_if_exists 'SRR11300809' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "CKO_RNA_3_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "CKO_RNA_3_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR12424181"
gz1="$(_compress_if_exists 'SRR12424181_1' || true)"
gz2="$(_compress_if_exists 'SRR12424181_2' || true)"
gzs="$(_compress_if_exists 'SRR12424181' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "WT_THRAP3_ChIPseq_rep1.fq.gz"
fi

fasterq-dump --split-files "SRR12424182"
gz1="$(_compress_if_exists 'SRR12424182_1' || true)"
gz2="$(_compress_if_exists 'SRR12424182_2' || true)"
gzs="$(_compress_if_exists 'SRR12424182' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "WT_THRAP3_ChIPseq_rep2.fq.gz"
fi

fasterq-dump --split-files "SRR12424183"
gz1="$(_compress_if_exists 'SRR12424183_1' || true)"
gz2="$(_compress_if_exists 'SRR12424183_2' || true)"
gzs="$(_compress_if_exists 'SRR12424183' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "WT_THRAP3_ChIPseq_Input.fq.gz"
fi

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

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

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

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

fasterq-dump --split-files "SRR12424188"
gz1="$(_compress_if_exists 'SRR12424188_1' || true)"
gz2="$(_compress_if_exists 'SRR12424188_2' || true)"
gzs="$(_compress_if_exists 'SRR12424188' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "BKO_THRAP3_ChIPseq_rep1.fq.gz"
fi

fasterq-dump --split-files "SRR12424189"
gz1="$(_compress_if_exists 'SRR12424189_1' || true)"
gz2="$(_compress_if_exists 'SRR12424189_2' || true)"
gzs="$(_compress_if_exists 'SRR12424189' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "BKO_THRAP3_ChIPseq_rep2.fq.gz"
fi

fasterq-dump --split-files "SRR12424190"
gz1="$(_compress_if_exists 'SRR12424190_1' || true)"
gz2="$(_compress_if_exists 'SRR12424190_2' || true)"
gzs="$(_compress_if_exists 'SRR12424190' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "BKO_THRAP3_ChIPseq_Input.fq.gz"
fi

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

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

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

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


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300801/SRR11300801"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300802/SRR11300802"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300803/SRR11300803"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300804/SRR11300804"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300805/SRR11300805"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300806/SRR11300806"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300807/SRR11300807"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300808/SRR11300808"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300809/SRR11300809"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424181/SRR12424181"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424182/SRR12424182"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424183/SRR12424183"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424184/SRR12424184"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424185/SRR12424185"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424186/SRR12424186"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424187/SRR12424187"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424188/SRR12424188"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424189/SRR12424189"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424190/SRR12424190"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424191/SRR12424191"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424192/SRR12424192"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424193/SRR12424193"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424194/SRR12424194"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424190/SRR12424190"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424188/SRR12424188"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424189/SRR12424189"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424183/SRR12424183"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424181/SRR12424181"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424182/SRR12424182"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424193/SRR12424193"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424194/SRR12424194"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424191/SRR12424191"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424192/SRR12424192"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424186/SRR12424186"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424187/SRR12424187"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424184/SRR12424184"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12424185/SRR12424185"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300804/SRR11300804"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300805/SRR11300805"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300806/SRR11300806"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300807/SRR11300807"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300808/SRR11300808"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300809/SRR11300809"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300801/SRR11300801"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300802/SRR11300802"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11300803/SRR11300803"

echo "Download complete. Files saved to $OUTDIR"