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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE201nnn/GSE201891/suppl/GSE201891_CellType_table_DM_forGEO.csv.gz" -O "GSE201891_CellType_table_DM_forGEO.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE201nnn/GSE201891/suppl/GSE201891_CellType_table_MECP2_forGEO.csv.gz" -O "GSE201891_CellType_table_MECP2_forGEO.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE201nnn/GSE201891/suppl/GSE201891_DM_Ctrl_barcode_qc_clustering_CellType.csv.gz" -O "GSE201891_DM_Ctrl_barcode_qc_clustering_CellType.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE201nnn/GSE201891/suppl/GSE201891_DM_Ctrl_gene_features.csv.gz" -O "GSE201891_DM_Ctrl_gene_features.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE201nnn/GSE201891/suppl/GSE201891_MECP2_barcode_qc_clustering_CellType.csv.gz" -O "GSE201891_MECP2_barcode_qc_clustering_CellType.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE201nnn/GSE201891/suppl/GSE201891_MECP2_gene_features.csv.gz" -O "GSE201891_MECP2_gene_features.csv.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE201nnn/GSE201891/suppl/GSE201891_RAW.tar" -O "GSE201891_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 "SRR18972861"
gz1="$(_compress_if_exists 'SRR18972861_1' || true)"
gz2="$(_compress_if_exists 'SRR18972861_2' || true)"
gzs="$(_compress_if_exists 'SRR18972861' || true)"

fasterq-dump --split-files "SRR18972862"
gz1="$(_compress_if_exists 'SRR18972862_1' || true)"
gz2="$(_compress_if_exists 'SRR18972862_2' || true)"
gzs="$(_compress_if_exists 'SRR18972862' || true)"

fasterq-dump --split-files "SRR18972863"
gz1="$(_compress_if_exists 'SRR18972863_1' || true)"
gz2="$(_compress_if_exists 'SRR18972863_2' || true)"
gzs="$(_compress_if_exists 'SRR18972863' || true)"

fasterq-dump --split-files "SRR18972864"
gz1="$(_compress_if_exists 'SRR18972864_1' || true)"
gz2="$(_compress_if_exists 'SRR18972864_2' || true)"
gzs="$(_compress_if_exists 'SRR18972864' || true)"

fasterq-dump --split-files "SRR18972865"
gz1="$(_compress_if_exists 'SRR18972865_1' || true)"
gz2="$(_compress_if_exists 'SRR18972865_2' || true)"
gzs="$(_compress_if_exists 'SRR18972865' || true)"


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-src-2.s3.amazonaws.com/SRR18972861/MECP2_WT_possorted_genome_bam.bam.1"
curl -L -O -C - "https://sra-pub-src-2.s3.amazonaws.com/SRR18972862/MECP2_KO_possorted_genome_bam.bam.1"
curl -L -O -C - "https://sra-pub-src-1.s3.amazonaws.com/SRR18972863/DM600_6M_possorted_genome_bam.bam.1"
curl -L -O -C - "https://sra-pub-src-2.s3.amazonaws.com/SRR18972864/DM1200_6M_possorted_genome_bam.bam.1"
curl -L -O -C - "https://sra-pub-src-1.s3.amazonaws.com/SRR18972865/Control_6M_possorted_genome_bam.bam.1"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-src-1.s3.amazonaws.com/SRR18972865/Control_6M_possorted_genome_bam.bam.1"
curl -L -O -C - "https://sra-pub-src-2.s3.amazonaws.com/SRR18972864/DM1200_6M_possorted_genome_bam.bam.1"
curl -L -O -C - "https://sra-pub-src-1.s3.amazonaws.com/SRR18972863/DM600_6M_possorted_genome_bam.bam.1"
curl -L -O -C - "https://sra-pub-src-2.s3.amazonaws.com/SRR18972862/MECP2_KO_possorted_genome_bam.bam.1"
curl -L -O -C - "https://sra-pub-src-2.s3.amazonaws.com/SRR18972861/MECP2_WT_possorted_genome_bam.bam.1"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos8/sra-pub-zq-818/SRR018/18972/SRR18972861/SRR18972861.lite.1"

echo "Download complete. Files saved to $OUTDIR"