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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE224nnn/GSE224548/suppl/GSE224548_count_matrix.tsv.gz" -O "GSE224548_count_matrix.tsv.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 "SRR23343566"
gz1="$(_compress_if_exists 'SRR23343566_1' || true)"
gz2="$(_compress_if_exists 'SRR23343566_2' || true)"
gzs="$(_compress_if_exists 'SRR23343566' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "6-090191-shELAVL1-7-Rep3_S6_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "6-090191-shELAVL1-7-Rep3_S6_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR23343567"
gz1="$(_compress_if_exists 'SRR23343567_1' || true)"
gz2="$(_compress_if_exists 'SRR23343567_2' || true)"
gzs="$(_compress_if_exists 'SRR23343567' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "6-090191-shELAVL1-7-Rep3_S6_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "6-090191-shELAVL1-7-Rep3_S6_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR23343568"
gz1="$(_compress_if_exists 'SRR23343568_1' || true)"
gz2="$(_compress_if_exists 'SRR23343568_2' || true)"
gzs="$(_compress_if_exists 'SRR23343568' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "5-090191-shELAVL1-7-Rep2_S5_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "5-090191-shELAVL1-7-Rep2_S5_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR23343569"
gz1="$(_compress_if_exists 'SRR23343569_1' || true)"
gz2="$(_compress_if_exists 'SRR23343569_2' || true)"
gzs="$(_compress_if_exists 'SRR23343569' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "5-090191-shELAVL1-7-Rep2_S5_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "5-090191-shELAVL1-7-Rep2_S5_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR23343570"
gz1="$(_compress_if_exists 'SRR23343570_1' || true)"
gz2="$(_compress_if_exists 'SRR23343570_2' || true)"
gzs="$(_compress_if_exists 'SRR23343570' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "4-090191-shELAVL1-7-Rep1_S4_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "4-090191-shELAVL1-7-Rep1_S4_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR23343571"
gz1="$(_compress_if_exists 'SRR23343571_1' || true)"
gz2="$(_compress_if_exists 'SRR23343571_2' || true)"
gzs="$(_compress_if_exists 'SRR23343571' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "4-090191-shELAVL1-7-Rep1_S4_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "4-090191-shELAVL1-7-Rep1_S4_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR23343572"
gz1="$(_compress_if_exists 'SRR23343572_1' || true)"
gz2="$(_compress_if_exists 'SRR23343572_2' || true)"
gzs="$(_compress_if_exists 'SRR23343572' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "3-090191-shSCR-Rep3_S3_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "3-090191-shSCR-Rep3_S3_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR23343573"
gz1="$(_compress_if_exists 'SRR23343573_1' || true)"
gz2="$(_compress_if_exists 'SRR23343573_2' || true)"
gzs="$(_compress_if_exists 'SRR23343573' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "3-090191-shSCR-Rep3_S3_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "3-090191-shSCR-Rep3_S3_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR23343574"
gz1="$(_compress_if_exists 'SRR23343574_1' || true)"
gz2="$(_compress_if_exists 'SRR23343574_2' || true)"
gzs="$(_compress_if_exists 'SRR23343574' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "2-090191-shSCR-Rep2_S2_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "2-090191-shSCR-Rep2_S2_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR23343575"
gz1="$(_compress_if_exists 'SRR23343575_1' || true)"
gz2="$(_compress_if_exists 'SRR23343575_2' || true)"
gzs="$(_compress_if_exists 'SRR23343575' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "2-090191-shSCR-Rep2_S2_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "2-090191-shSCR-Rep2_S2_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR23343576"
gz1="$(_compress_if_exists 'SRR23343576_1' || true)"
gz2="$(_compress_if_exists 'SRR23343576_2' || true)"
gzs="$(_compress_if_exists 'SRR23343576' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "1-090191-shSCR-Rep1_S1_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "1-090191-shSCR-Rep1_S1_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR23343577"
gz1="$(_compress_if_exists 'SRR23343577_1' || true)"
gz2="$(_compress_if_exists 'SRR23343577_2' || true)"
gzs="$(_compress_if_exists 'SRR23343577' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "1-090191-shSCR-Rep1_S1_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "1-090191-shSCR-Rep1_S1_L002_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/SRR23343566/SRR23343566"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343567/SRR23343567"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343568/SRR23343568"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343569/SRR23343569"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343570/SRR23343570"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343571/SRR23343571"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343572/SRR23343572"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343573/SRR23343573"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343574/SRR23343574"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343575/SRR23343575"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343576/SRR23343576"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343577/SRR23343577"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343576/SRR23343576"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343577/SRR23343577"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343574/SRR23343574"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343575/SRR23343575"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343572/SRR23343572"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343573/SRR23343573"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343570/SRR23343570"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343571/SRR23343571"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343568/SRR23343568"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343569/SRR23343569"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343566/SRR23343566"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR23343567/SRR23343567"

echo "Download complete. Files saved to $OUTDIR"