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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE153nnn/GSE153279/suppl/GSE153279_042420_HA-AE_cutandrun_diffbind-deseq2_q0.001.csv.gz" -O "GSE153279_042420_HA-AE_cutandrun_diffbind-deseq2_q0.001.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE153nnn/GSE153279/suppl/GSE153279_042420_HA_AE_Diffbind-peakinfo_q0.001.csv.gz" -O "GSE153279_042420_HA_AE_Diffbind-peakinfo_q0.001.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE153nnn/GSE153279/suppl/GSE153279_CD34_HA_merged_noNeg_nomodel_extsize300_q0.001_peaks.txt.gz" -O "GSE153279_CD34_HA_merged_noNeg_nomodel_extsize300_q0.001_peaks.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE153nnn/GSE153279/suppl/GSE153279_New_ZnF_q0.001_peaks.txt.gz" -O "GSE153279_New_ZnF_q0.001_peaks.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE153nnn/GSE153279/suppl/GSE153279_RAW.tar" -O "GSE153279_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 "SRR12092005"
gz1="$(_compress_if_exists 'SRR12092005_1' || true)"
gz2="$(_compress_if_exists 'SRR12092005_2' || true)"
gzs="$(_compress_if_exists 'SRR12092005' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "4617-MB-1_S71_L005_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "4617-MB-1_S71_L005_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR12092006"
gz1="$(_compress_if_exists 'SRR12092006_1' || true)"
gz2="$(_compress_if_exists 'SRR12092006_2' || true)"
gzs="$(_compress_if_exists 'SRR12092006' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "4617-MB-2_S72_L005_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "4617-MB-2_S72_L005_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR12092007"
gz1="$(_compress_if_exists 'SRR12092007_1' || true)"
gz2="$(_compress_if_exists 'SRR12092007_2' || true)"
gzs="$(_compress_if_exists 'SRR12092007' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "4617-MB-3_S73_L005_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "4617-MB-3_S73_L005_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR12092008"
gz1="$(_compress_if_exists 'SRR12092008_1' || true)"
gz2="$(_compress_if_exists 'SRR12092008_2' || true)"
gzs="$(_compress_if_exists 'SRR12092008' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "4617-MB-4_S74_L005_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "4617-MB-4_S74_L005_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR12092009"
gz1="$(_compress_if_exists 'SRR12092009_1' || true)"
gz2="$(_compress_if_exists 'SRR12092009_2' || true)"
gzs="$(_compress_if_exists 'SRR12092009' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "3856-CS-34-CCAAGGTT-AGGATAGC_S286_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "3856-CS-34-CCAAGGTT-AGGATAGC_S286_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR12092010"
gz1="$(_compress_if_exists 'SRR12092010_1' || true)"
gz2="$(_compress_if_exists 'SRR12092010_2' || true)"
gzs="$(_compress_if_exists 'SRR12092010' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "3691-CS-1-TTACCGAC-CGAATACG_S1_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "3691-CS-1-TTACCGAC-CGAATACG_S1_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR12092011"
gz1="$(_compress_if_exists 'SRR12092011_1' || true)"
gz2="$(_compress_if_exists 'SRR12092011_2' || true)"
gzs="$(_compress_if_exists 'SRR12092011' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "3691-CS-7-TAGGAGCT-GCCTTAAC_S5_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "3691-CS-7-TAGGAGCT-GCCTTAAC_S5_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR12092012"
gz1="$(_compress_if_exists 'SRR12092012_1' || true)"
gz2="$(_compress_if_exists 'SRR12092012_2' || true)"
gzs="$(_compress_if_exists 'SRR12092012' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "4699-KS-3_S3_L005_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "4699-KS-3_S3_L005_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR12092013"
gz1="$(_compress_if_exists 'SRR12092013_1' || true)"
gz2="$(_compress_if_exists 'SRR12092013_2' || true)"
gzs="$(_compress_if_exists 'SRR12092013' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "4699-KS-4_S4_L005_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "4699-KS-4_S4_L005_R2_001.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092005/SRR12092005"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092006/SRR12092006"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092007/SRR12092007"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092008/SRR12092008"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092009/SRR12092009"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092010/SRR12092010"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092011/SRR12092011"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092012/SRR12092012"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092013/SRR12092013"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092010/SRR12092010"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092011/SRR12092011"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092009/SRR12092009"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092005/SRR12092005"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092006/SRR12092006"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092007/SRR12092007"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092008/SRR12092008"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092012/SRR12092012"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR12092013/SRR12092013"

echo "Download complete. Files saved to $OUTDIR"