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

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

# --- GEO supplementary files ---
wget -nc ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE295nnn/GSE295421/suppl/GSE295421_dmd_cad.txt.gz -O GSE295421_dmd_cad.txt.gz
wget -nc ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE295nnn/GSE295421/suppl/GSE295421_dmd_u7.txt.gz -O GSE295421_dmd_u7.txt.gz
wget -nc ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE295nnn/GSE295421/suppl/GSE295421_rab7a_cad.txt.gz -O GSE295421_rab7a_cad.txt.gz
wget -nc ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE295nnn/GSE295421/suppl/GSE295421_rab7a_u7.txt.gz -O GSE295421_rab7a_u7.txt.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 SRR33284744
gz1="$(_compress_if_exists SRR33284744_1 || true)"
gz2="$(_compress_if_exists SRR33284744_2 || true)"
gzs="$(_compress_if_exists SRR33284744 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" RAB7A_U7_rep3_S37_L001_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" RAB7A_U7_rep3_S37_L001_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR33284745
gz1="$(_compress_if_exists SRR33284745_1 || true)"
gz2="$(_compress_if_exists SRR33284745_2 || true)"
gzs="$(_compress_if_exists SRR33284745 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" RAB7A_U7_rep2_S36_L001_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" RAB7A_U7_rep2_S36_L001_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR33284746
gz1="$(_compress_if_exists SRR33284746_1 || true)"
gz2="$(_compress_if_exists SRR33284746_2 || true)"
gzs="$(_compress_if_exists SRR33284746 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" RAB7A_cad_rep2_S35_L001_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" RAB7A_cad_rep2_S35_L001_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR33284747
gz1="$(_compress_if_exists SRR33284747_1 || true)"
gz2="$(_compress_if_exists SRR33284747_2 || true)"
gzs="$(_compress_if_exists SRR33284747 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" RAB7A_cad_rep1_S34_L001_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" RAB7A_cad_rep1_S34_L001_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR33284748
gz1="$(_compress_if_exists SRR33284748_1 || true)"
gz2="$(_compress_if_exists SRR33284748_2 || true)"
gzs="$(_compress_if_exists SRR33284748 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" DMD_U7_rep3_S13_L001_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" DMD_U7_rep3_S13_L001_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR33284749
gz1="$(_compress_if_exists SRR33284749_1 || true)"
gz2="$(_compress_if_exists SRR33284749_2 || true)"
gzs="$(_compress_if_exists SRR33284749 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" DMD_U7_rep2_S12_L001_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" DMD_U7_rep2_S12_L001_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR33284750
gz1="$(_compress_if_exists SRR33284750_1 || true)"
gz2="$(_compress_if_exists SRR33284750_2 || true)"
gzs="$(_compress_if_exists SRR33284750 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" DMD_cad_rep3_S11_L001_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" DMD_cad_rep3_S11_L001_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR33284751
gz1="$(_compress_if_exists SRR33284751_1 || true)"
gz2="$(_compress_if_exists SRR33284751_2 || true)"
gzs="$(_compress_if_exists SRR33284751 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" DMD_cad_rep1_S10_L001_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" DMD_cad_rep1_S10_L001_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR33284752
gz1="$(_compress_if_exists SRR33284752_1 || true)"
gz2="$(_compress_if_exists SRR33284752_2 || true)"
gzs="$(_compress_if_exists SRR33284752 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" pUC19_rep2_S33_L001_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" pUC19_rep2_S33_L001_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR33284753
gz1="$(_compress_if_exists SRR33284753_1 || true)"
gz2="$(_compress_if_exists SRR33284753_2 || true)"
gzs="$(_compress_if_exists SRR33284753 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" pUC19_rep1_S32_L001_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" pUC19_rep1_S32_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/SRR33284744/SRR33284744
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284745/SRR33284745
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284746/SRR33284746
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284747/SRR33284747
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284748/SRR33284748
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284749/SRR33284749
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284750/SRR33284750
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284751/SRR33284751
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284752/SRR33284752
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284753/SRR33284753

# --- Additional data files ---
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284751/SRR33284751
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284750/SRR33284750
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284749/SRR33284749
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284748/SRR33284748
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284753/SRR33284753
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284752/SRR33284752
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284747/SRR33284747
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284746/SRR33284746
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284745/SRR33284745
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR33284744/SRR33284744

echo "Download complete. Files saved to $OUTDIR"