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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE271nnn/GSE271651/suppl/GSE271651_Microglia_RNAseq_DEGs.xlsx" -O "GSE271651_Microglia_RNAseq_DEGs.xlsx"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE271nnn/GSE271651/suppl/GSE271651_Normalized_counts_microglia_RNA_seq.xlsx" -O "GSE271651_Normalized_counts_microglia_RNA_seq.xlsx"

# --- 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 "SRR29727679"
gz1="$(_compress_if_exists 'SRR29727679_1' || true)"
gz2="$(_compress_if_exists 'SRR29727679_2' || true)"
gzs="$(_compress_if_exists 'SRR29727679' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "cKO-LPS-3_M_ZHO2261A9-1_S9_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "cKO-LPS-3_M_ZHO2261A9-1_S9_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727680"
gz1="$(_compress_if_exists 'SRR29727680_1' || true)"
gz2="$(_compress_if_exists 'SRR29727680_2' || true)"
gzs="$(_compress_if_exists 'SRR29727680' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "cKO-LPS-2_M_ZHO2261A8-1_S8_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "cKO-LPS-2_M_ZHO2261A8-1_S8_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727681"
gz1="$(_compress_if_exists 'SRR29727681_1' || true)"
gz2="$(_compress_if_exists 'SRR29727681_2' || true)"
gzs="$(_compress_if_exists 'SRR29727681' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "cKO-LPS-1_M_ZHO2261A7-1_S7_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "cKO-LPS-1_M_ZHO2261A7-1_S7_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727682"
gz1="$(_compress_if_exists 'SRR29727682_1' || true)"
gz2="$(_compress_if_exists 'SRR29727682_2' || true)"
gzs="$(_compress_if_exists 'SRR29727682' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT-LPS-3_M_ZHO2261A6-1_S6_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WT-LPS-3_M_ZHO2261A6-1_S6_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727683"
gz1="$(_compress_if_exists 'SRR29727683_1' || true)"
gz2="$(_compress_if_exists 'SRR29727683_2' || true)"
gzs="$(_compress_if_exists 'SRR29727683' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT-LPS-2_M_ZHO2261A5-1_S5_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WT-LPS-2_M_ZHO2261A5-1_S5_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727684"
gz1="$(_compress_if_exists 'SRR29727684_1' || true)"
gz2="$(_compress_if_exists 'SRR29727684_2' || true)"
gzs="$(_compress_if_exists 'SRR29727684' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT-LPS-1_M_ZHO2261A4-1_S4_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WT-LPS-1_M_ZHO2261A4-1_S4_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727685"
gz1="$(_compress_if_exists 'SRR29727685_1' || true)"
gz2="$(_compress_if_exists 'SRR29727685_2' || true)"
gzs="$(_compress_if_exists 'SRR29727685' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT-3_M_ZHO2261A3-1_S3_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WT-3_M_ZHO2261A3-1_S3_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727686"
gz1="$(_compress_if_exists 'SRR29727686_1' || true)"
gz2="$(_compress_if_exists 'SRR29727686_2' || true)"
gzs="$(_compress_if_exists 'SRR29727686' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT-2_M_ZHO2261A2-1_S2_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WT-2_M_ZHO2261A2-1_S2_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR29727687"
gz1="$(_compress_if_exists 'SRR29727687_1' || true)"
gz2="$(_compress_if_exists 'SRR29727687_2' || true)"
gzs="$(_compress_if_exists 'SRR29727687' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "WT-1_M_ZHO2261A1-1_S1_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "WT-1_M_ZHO2261A1-1_S1_L001_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/SRR29727679/SRR29727679"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727680/SRR29727680"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727681/SRR29727681"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727682/SRR29727682"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727683/SRR29727683"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727684/SRR29727684"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos6/sra-pub-zq-40/SRR029/29727/SRR29727685/SRR29727685.lite.1"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727686/SRR29727686"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727687/SRR29727687"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727681/SRR29727681"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727680/SRR29727680"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727679/SRR29727679"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos6/sra-pub-zq-40/SRR029/29727/SRR29727685/SRR29727685.lite.1"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727687/SRR29727687"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727686/SRR29727686"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727685/SRR29727685"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727684/SRR29727684"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727683/SRR29727683"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR29727682/SRR29727682"

echo "Download complete. Files saved to $OUTDIR"