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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE210nnn/GSE210263/suppl/GSE210263_RAW.tar" -O "GSE210263_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 "SRR20738139"
gz1="$(_compress_if_exists 'SRR20738139_1' || true)"
gz2="$(_compress_if_exists 'SRR20738139_2' || true)"
gzs="$(_compress_if_exists 'SRR20738139' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mdmPsoas_replicate5_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mdmPsoas_replicate5_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR20738140"
gz1="$(_compress_if_exists 'SRR20738140_1' || true)"
gz2="$(_compress_if_exists 'SRR20738140_2' || true)"
gzs="$(_compress_if_exists 'SRR20738140' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mdmSoleus_replicate3_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mdmSoleus_replicate3_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR20738141"
gz1="$(_compress_if_exists 'SRR20738141_1' || true)"
gz2="$(_compress_if_exists 'SRR20738141_2' || true)"
gzs="$(_compress_if_exists 'SRR20738141' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mdmPsoas_replicate4_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mdmPsoas_replicate4_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR20738142"
gz1="$(_compress_if_exists 'SRR20738142_1' || true)"
gz2="$(_compress_if_exists 'SRR20738142_2' || true)"
gzs="$(_compress_if_exists 'SRR20738142' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mdmEDL_replicate5_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mdmEDL_replicate5_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR20738143"
gz1="$(_compress_if_exists 'SRR20738143_1' || true)"
gz2="$(_compress_if_exists 'SRR20738143_2' || true)"
gzs="$(_compress_if_exists 'SRR20738143' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mdmEDL_replicate4_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mdmEDL_replicate4_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR20738144"
gz1="$(_compress_if_exists 'SRR20738144_1' || true)"
gz2="$(_compress_if_exists 'SRR20738144_2' || true)"
gzs="$(_compress_if_exists 'SRR20738144' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mdmSoleus_replicate2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mdmSoleus_replicate2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR20738145"
gz1="$(_compress_if_exists 'SRR20738145_1' || true)"
gz2="$(_compress_if_exists 'SRR20738145_2' || true)"
gzs="$(_compress_if_exists 'SRR20738145' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mdmPsoas_replicate3_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mdmPsoas_replicate3_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR20738146"
gz1="$(_compress_if_exists 'SRR20738146_1' || true)"
gz2="$(_compress_if_exists 'SRR20738146_2' || true)"
gzs="$(_compress_if_exists 'SRR20738146' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mdmPsoas_replicate1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mdmPsoas_replicate1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR20738147"
gz1="$(_compress_if_exists 'SRR20738147_1' || true)"
gz2="$(_compress_if_exists 'SRR20738147_2' || true)"
gzs="$(_compress_if_exists 'SRR20738147' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mdmEDL_replicate3_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mdmEDL_replicate3_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR20738148"
gz1="$(_compress_if_exists 'SRR20738148_1' || true)"
gz2="$(_compress_if_exists 'SRR20738148_2' || true)"
gzs="$(_compress_if_exists 'SRR20738148' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mdmPsoas_replicate2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mdmPsoas_replicate2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR20738149"
gz1="$(_compress_if_exists 'SRR20738149_1' || true)"
gz2="$(_compress_if_exists 'SRR20738149_2' || true)"
gzs="$(_compress_if_exists 'SRR20738149' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mdmSoleus_replicate1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mdmSoleus_replicate1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR20738150"
gz1="$(_compress_if_exists 'SRR20738150_1' || true)"
gz2="$(_compress_if_exists 'SRR20738150_2' || true)"
gzs="$(_compress_if_exists 'SRR20738150' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mdmEDL_replicate2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mdmEDL_replicate2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR20738151"
gz1="$(_compress_if_exists 'SRR20738151_1' || true)"
gz2="$(_compress_if_exists 'SRR20738151_2' || true)"
gzs="$(_compress_if_exists 'SRR20738151' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "mdmEDL_replicate1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "mdmEDL_replicate1_R2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738139/SRR20738139"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738140/SRR20738140"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738141/SRR20738141"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738142/SRR20738142"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738143/SRR20738143"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738144/SRR20738144"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738145/SRR20738145"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738146/SRR20738146"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738147/SRR20738147"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738148/SRR20738148"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738149/SRR20738149"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738150/SRR20738150"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738151/SRR20738151"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738151/SRR20738151"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738150/SRR20738150"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738147/SRR20738147"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738143/SRR20738143"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738142/SRR20738142"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738146/SRR20738146"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738148/SRR20738148"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738145/SRR20738145"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738141/SRR20738141"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738139/SRR20738139"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738149/SRR20738149"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738144/SRR20738144"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR20738140/SRR20738140"

echo "Download complete. Files saved to $OUTDIR"