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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE195nnn/GSE195494/suppl/GSE195494_RiboSTAMP_norm_epkm.csv.gz" -O "GSE195494_RiboSTAMP_norm_epkm.csv.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 "SRR17771528"
gz1="$(_compress_if_exists 'SRR17771528_1' || true)"
gz2="$(_compress_if_exists 'SRR17771528_2' || true)"
gzs="$(_compress_if_exists 'SRR17771528' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "IP_PABPC_RPS2STAMP_Rep3_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "IP_PABPC_RPS2STAMP_Rep3_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771529"
gz1="$(_compress_if_exists 'SRR17771529_1' || true)"
gz2="$(_compress_if_exists 'SRR17771529_2' || true)"
gzs="$(_compress_if_exists 'SRR17771529' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "IP_PABPC_RPS2STAMP_Rep2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "IP_PABPC_RPS2STAMP_Rep2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771530"
gz1="$(_compress_if_exists 'SRR17771530_1' || true)"
gz2="$(_compress_if_exists 'SRR17771530_2' || true)"
gzs="$(_compress_if_exists 'SRR17771530' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "IP_PABPC_RPS2STAMP_Rep1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "IP_PABPC_RPS2STAMP_Rep1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771531"
gz1="$(_compress_if_exists 'SRR17771531_1' || true)"
gz2="$(_compress_if_exists 'SRR17771531_2' || true)"
gzs="$(_compress_if_exists 'SRR17771531' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "IP_PABPN_RPS2STAMP_Rep3_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "IP_PABPN_RPS2STAMP_Rep3_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771532"
gz1="$(_compress_if_exists 'SRR17771532_1' || true)"
gz2="$(_compress_if_exists 'SRR17771532_2' || true)"
gzs="$(_compress_if_exists 'SRR17771532' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "IP_PABPN_RPS2STAMP_Rep1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "IP_PABPN_RPS2STAMP_Rep1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771533"
gz1="$(_compress_if_exists 'SRR17771533_1' || true)"
gz2="$(_compress_if_exists 'SRR17771533_2' || true)"
gzs="$(_compress_if_exists 'SRR17771533' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Input_RPS2STAMP_Rep3_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Input_RPS2STAMP_Rep3_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771534"
gz1="$(_compress_if_exists 'SRR17771534_1' || true)"
gz2="$(_compress_if_exists 'SRR17771534_2' || true)"
gzs="$(_compress_if_exists 'SRR17771534' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Input_RPS2STAMP_Rep2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Input_RPS2STAMP_Rep2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771535"
gz1="$(_compress_if_exists 'SRR17771535_1' || true)"
gz2="$(_compress_if_exists 'SRR17771535_2' || true)"
gzs="$(_compress_if_exists 'SRR17771535' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "IP_PABPN_RPS2STAMP_Rep2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "IP_PABPN_RPS2STAMP_Rep2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771536"
gz1="$(_compress_if_exists 'SRR17771536_1' || true)"
gz2="$(_compress_if_exists 'SRR17771536_2' || true)"
gzs="$(_compress_if_exists 'SRR17771536' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Input_RPS2STAMP_Rep1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Input_RPS2STAMP_Rep1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771537"
gz1="$(_compress_if_exists 'SRR17771537_1' || true)"
gz2="$(_compress_if_exists 'SRR17771537_2' || true)"
gzs="$(_compress_if_exists 'SRR17771537' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "IP_PABPC_CntrlSTAMP_Rep2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "IP_PABPC_CntrlSTAMP_Rep2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771538"
gz1="$(_compress_if_exists 'SRR17771538_1' || true)"
gz2="$(_compress_if_exists 'SRR17771538_2' || true)"
gzs="$(_compress_if_exists 'SRR17771538' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "IP_PABPC_CntrlSTAMP_Rep1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "IP_PABPC_CntrlSTAMP_Rep1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771539"
gz1="$(_compress_if_exists 'SRR17771539_1' || true)"
gz2="$(_compress_if_exists 'SRR17771539_2' || true)"
gzs="$(_compress_if_exists 'SRR17771539' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "IP_PABPN_CntrlSTAMP_Rep3_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "IP_PABPN_CntrlSTAMP_Rep3_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771540"
gz1="$(_compress_if_exists 'SRR17771540_1' || true)"
gz2="$(_compress_if_exists 'SRR17771540_2' || true)"
gzs="$(_compress_if_exists 'SRR17771540' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "IP_PABPN_CntrlSTAMP_Rep2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "IP_PABPN_CntrlSTAMP_Rep2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771541"
gz1="$(_compress_if_exists 'SRR17771541_1' || true)"
gz2="$(_compress_if_exists 'SRR17771541_2' || true)"
gzs="$(_compress_if_exists 'SRR17771541' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "IP_PABPN_CntrlSTAMP_Rep1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "IP_PABPN_CntrlSTAMP_Rep1_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771542"
gz1="$(_compress_if_exists 'SRR17771542_1' || true)"
gz2="$(_compress_if_exists 'SRR17771542_2' || true)"
gzs="$(_compress_if_exists 'SRR17771542' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Input_CntrlSTAMP_Rep3_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Input_CntrlSTAMP_Rep3_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771543"
gz1="$(_compress_if_exists 'SRR17771543_1' || true)"
gz2="$(_compress_if_exists 'SRR17771543_2' || true)"
gzs="$(_compress_if_exists 'SRR17771543' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Input_CntrlSTAMP_Rep2_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Input_CntrlSTAMP_Rep2_R2.fastq.gz"
fi

fasterq-dump --split-files "SRR17771544"
gz1="$(_compress_if_exists 'SRR17771544_1' || true)"
gz2="$(_compress_if_exists 'SRR17771544_2' || true)"
gzs="$(_compress_if_exists 'SRR17771544' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Input_CntrlSTAMP_Rep1_R1.fastq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Input_CntrlSTAMP_Rep1_R2.fastq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771528/SRR17771528"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771529/SRR17771529"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771530/SRR17771530"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771531/SRR17771531"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771532/SRR17771532"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771533/SRR17771533"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771534/SRR17771534"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771535/SRR17771535"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771536/SRR17771536"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771537/SRR17771537"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771538/SRR17771538"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771539/SRR17771539"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771540/SRR17771540"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771541/SRR17771541"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771542/SRR17771542"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771543/SRR17771543"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771544/SRR17771544"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771544/SRR17771544"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771543/SRR17771543"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771542/SRR17771542"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771536/SRR17771536"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771534/SRR17771534"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771533/SRR17771533"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771538/SRR17771538"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771537/SRR17771537"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771530/SRR17771530"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771529/SRR17771529"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771528/SRR17771528"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771541/SRR17771541"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771540/SRR17771540"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771539/SRR17771539"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771532/SRR17771532"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771535/SRR17771535"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR17771531/SRR17771531"

echo "Download complete. Files saved to $OUTDIR"