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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE147nnn/GSE147127/suppl/GSE147127_RAW.tar" -O "GSE147127_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 "SRR11336245"
gz1="$(_compress_if_exists 'SRR11336245_1' || true)"
gz2="$(_compress_if_exists 'SRR11336245_2' || true)"
gzs="$(_compress_if_exists 'SRR11336245' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10X_Millay_Aging_20190326_3v3mm_S3_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10X_Millay_Aging_20190326_3v3mm_S3_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11336246"
gz1="$(_compress_if_exists 'SRR11336246_1' || true)"
gz2="$(_compress_if_exists 'SRR11336246_2' || true)"
gzs="$(_compress_if_exists 'SRR11336246' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10X_Millay_Aging_20190326_3v3mm_S3_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10X_Millay_Aging_20190326_3v3mm_S3_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11336247"
gz1="$(_compress_if_exists 'SRR11336247_1' || true)"
gz2="$(_compress_if_exists 'SRR11336247_2' || true)"
gzs="$(_compress_if_exists 'SRR11336247' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10X-Millay-WT-Muscle-20190131-3v3_S10_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10X-Millay-WT-Muscle-20190131-3v3_S10_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11336248"
gz1="$(_compress_if_exists 'SRR11336248_1' || true)"
gz2="$(_compress_if_exists 'SRR11336248_2' || true)"
gzs="$(_compress_if_exists 'SRR11336248' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10X-Millay-WT-Muscle-20190131-3v3_S10_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10X-Millay-WT-Muscle-20190131-3v3_S10_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11336249"
gz1="$(_compress_if_exists 'SRR11336249_1' || true)"
gz2="$(_compress_if_exists 'SRR11336249_2' || true)"
gzs="$(_compress_if_exists 'SRR11336249' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10X-Millay-30-Month-20190812-3v3mm_S6_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10X-Millay-30-Month-20190812-3v3mm_S6_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11336250"
gz1="$(_compress_if_exists 'SRR11336250_1' || true)"
gz2="$(_compress_if_exists 'SRR11336250_2' || true)"
gzs="$(_compress_if_exists 'SRR11336250' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10X-Millay-30-Month-20190812-3v3mm_S6_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10X-Millay-30-Month-20190812-3v3mm_S6_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11336251"
gz1="$(_compress_if_exists 'SRR11336251_1' || true)"
gz2="$(_compress_if_exists 'SRR11336251_2' || true)"
gzs="$(_compress_if_exists 'SRR11336251' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10X-Millay-P10-20191120-3v3mm_S1_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10X-Millay-P10-20191120-3v3mm_S1_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11336252"
gz1="$(_compress_if_exists 'SRR11336252_1' || true)"
gz2="$(_compress_if_exists 'SRR11336252_2' || true)"
gzs="$(_compress_if_exists 'SRR11336252' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10X-Millay-P10-20191120-3v3mm_S1_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10X-Millay-P10-20191120-3v3mm_S1_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11336253"
gz1="$(_compress_if_exists 'SRR11336253_1' || true)"
gz2="$(_compress_if_exists 'SRR11336253_2' || true)"
gzs="$(_compress_if_exists 'SRR11336253' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10X-Millay-P21-20190513-3v3mm_S10_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10X-Millay-P21-20190513-3v3mm_S10_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11336254"
gz1="$(_compress_if_exists 'SRR11336254_1' || true)"
gz2="$(_compress_if_exists 'SRR11336254_2' || true)"
gzs="$(_compress_if_exists 'SRR11336254' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10X-Millay-P21-20190513-3v3mm_S10_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10X-Millay-P21-20190513-3v3mm_S10_L002_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11336255"
gz1="$(_compress_if_exists 'SRR11336255_1' || true)"
gz2="$(_compress_if_exists 'SRR11336255_2' || true)"
gzs="$(_compress_if_exists 'SRR11336255' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10X-Millay-Soleus-20190826-3v3mm_S6_L001_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10X-Millay-Soleus-20190826-3v3mm_S6_L001_R2_001.fastq.gz"
fi

fasterq-dump --split-files "SRR11336256"
gz1="$(_compress_if_exists 'SRR11336256_1' || true)"
gz2="$(_compress_if_exists 'SRR11336256_2' || true)"
gzs="$(_compress_if_exists 'SRR11336256' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "10X-Millay-Soleus-20190826-3v3mm_S6_L002_R1_001.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "10X-Millay-Soleus-20190826-3v3mm_S6_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/SRR11336245/SRR11336245"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336246/SRR11336246"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336247/SRR11336247"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336248/SRR11336248"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336249/SRR11336249"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336250/SRR11336250"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336251/SRR11336251"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336252/SRR11336252"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336253/SRR11336253"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336254/SRR11336254"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336255/SRR11336255"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336256/SRR11336256"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336249/SRR11336249"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336250/SRR11336250"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336245/SRR11336245"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336246/SRR11336246"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336251/SRR11336251"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336252/SRR11336252"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336253/SRR11336253"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336254/SRR11336254"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336255/SRR11336255"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336256/SRR11336256"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336247/SRR11336247"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11336248/SRR11336248"

echo "Download complete. Files saved to $OUTDIR"