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

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

# --- GEO supplementary files ---
wget -nc ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE145nnn/GSE145984/suppl/GSE145984_circRNA_expression_CPM.txt.gz -O GSE145984_circRNA_expression_CPM.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 SRR11186531
gz1="$(_compress_if_exists SRR11186531_1 || true)"
gz2="$(_compress_if_exists SRR11186531_2 || true)"
gzs="$(_compress_if_exists SRR11186531 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" HepG2_KHSRP_1_S7_L005_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" HepG2_KHSRP_1_S7_L005_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR11186532
gz1="$(_compress_if_exists SRR11186532_1 || true)"
gz2="$(_compress_if_exists SRR11186532_2 || true)"
gzs="$(_compress_if_exists SRR11186532 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" HepG2_KHSRP_2_S8_L005_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" HepG2_KHSRP_2_S8_L005_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR11186533
gz1="$(_compress_if_exists SRR11186533_1 || true)"
gz2="$(_compress_if_exists SRR11186533_2 || true)"
gzs="$(_compress_if_exists SRR11186533 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" HepG2_NonTargeting_1_S5_L005_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" HepG2_NonTargeting_1_S5_L005_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR11186534
gz1="$(_compress_if_exists SRR11186534_1 || true)"
gz2="$(_compress_if_exists SRR11186534_2 || true)"
gzs="$(_compress_if_exists SRR11186534 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" HepG2_NonTargeting_2_S6_L005_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" HepG2_NonTargeting_2_S6_L005_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR11186535
gz1="$(_compress_if_exists SRR11186535_1 || true)"
gz2="$(_compress_if_exists SRR11186535_2 || true)"
gzs="$(_compress_if_exists SRR11186535 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" K562_KHSRP_1_S3_L005_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" K562_KHSRP_1_S3_L005_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR11186536
gz1="$(_compress_if_exists SRR11186536_1 || true)"
gz2="$(_compress_if_exists SRR11186536_2 || true)"
gzs="$(_compress_if_exists SRR11186536 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" K562_KHSRP_2_S4_L005_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" K562_KHSRP_2_S4_L005_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR11186537
gz1="$(_compress_if_exists SRR11186537_1 || true)"
gz2="$(_compress_if_exists SRR11186537_2 || true)"
gzs="$(_compress_if_exists SRR11186537 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" K562_NonTargeting_1_S1_L005_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" K562_NonTargeting_1_S1_L005_R2_001.fastq.gz
fi

fasterq-dump --split-files SRR11186538
gz1="$(_compress_if_exists SRR11186538_1 || true)"
gz2="$(_compress_if_exists SRR11186538_2 || true)"
gzs="$(_compress_if_exists SRR11186538 || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" K562_NonTargeting_2_S2_L005_R1_001.fastq.gz
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" K562_NonTargeting_2_S2_L005_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/SRR11186531/SRR11186531
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186532/SRR11186532
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186533/SRR11186533
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186534/SRR11186534
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186535/SRR11186535
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186536/SRR11186536
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186537/SRR11186537
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186538/SRR11186538

# --- Additional data files ---
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186531/SRR11186531
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186532/SRR11186532
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186533/SRR11186533
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186534/SRR11186534
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186535/SRR11186535
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186536/SRR11186536
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186537/SRR11186537
curl -L -O -C - https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR11186538/SRR11186538

echo "Download complete. Files saved to $OUTDIR"