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

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

# --- GEO supplementary files ---
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE47nnn/GSE47626/suppl/GSE47626_miRNA.txt.gz" -O "GSE47626_miRNA.txt.gz"
wget -nc "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE47nnn/GSE47626/suppl/GSE47626_rpkm.txt.gz" -O "GSE47626_rpkm.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 "SRR873616"
gz1="$(_compress_if_exists 'SRR873616_1' || true)"
gz2="$(_compress_if_exists 'SRR873616_2' || true)"
gzs="$(_compress_if_exists 'SRR873616' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample1.hADRC.1.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample1.hADRC.1.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873617"
gz1="$(_compress_if_exists 'SRR873617_1' || true)"
gz2="$(_compress_if_exists 'SRR873617_2' || true)"
gzs="$(_compress_if_exists 'SRR873617' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample2.hADRC.2.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample2.hADRC.2.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873618"
gz1="$(_compress_if_exists 'SRR873618_1' || true)"
gz2="$(_compress_if_exists 'SRR873618_2' || true)"
gzs="$(_compress_if_exists 'SRR873618' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample3.hWT33.1.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample3.hWT33.1.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873619"
gz1="$(_compress_if_exists 'SRR873619_1' || true)"
gz2="$(_compress_if_exists 'SRR873619_2' || true)"
gzs="$(_compress_if_exists 'SRR873619' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample4.hWT33.2.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample4.hWT33.2.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873620"
gz1="$(_compress_if_exists 'SRR873620_1' || true)"
gz2="$(_compress_if_exists 'SRR873620_2' || true)"
gzs="$(_compress_if_exists 'SRR873620' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample5.hWT9.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample5.hWT9.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873621"
gz1="$(_compress_if_exists 'SRR873621_1' || true)"
gz2="$(_compress_if_exists 'SRR873621_2' || true)"
gzs="$(_compress_if_exists 'SRR873621' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample6.hWT126.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample6.hWT126.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873622"
gz1="$(_compress_if_exists 'SRR873622_1' || true)"
gz2="$(_compress_if_exists 'SRR873622_2' || true)"
gzs="$(_compress_if_exists 'SRR873622' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample7.cCL5.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample7.cCL5.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873623"
gz1="$(_compress_if_exists 'SRR873623_1' || true)"
gz2="$(_compress_if_exists 'SRR873623_2' || true)"
gzs="$(_compress_if_exists 'SRR873623' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample8.c018B.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample8.c018B.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873624"
gz1="$(_compress_if_exists 'SRR873624_1' || true)"
gz2="$(_compress_if_exists 'SRR873624_2' || true)"
gzs="$(_compress_if_exists 'SRR873624' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample9.c029.1.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample9.c029.1.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873625"
gz1="$(_compress_if_exists 'SRR873625_1' || true)"
gz2="$(_compress_if_exists 'SRR873625_2' || true)"
gzs="$(_compress_if_exists 'SRR873625' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample10.c029.2.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample10.c029.2.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873626"
gz1="$(_compress_if_exists 'SRR873626_1' || true)"
gz2="$(_compress_if_exists 'SRR873626_2' || true)"
gzs="$(_compress_if_exists 'SRR873626' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample11.b86.1.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample11.b86.1.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873627"
gz1="$(_compress_if_exists 'SRR873627_1' || true)"
gz2="$(_compress_if_exists 'SRR873627_2' || true)"
gzs="$(_compress_if_exists 'SRR873627' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample12.b86.2.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample12.b86.2.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873628"
gz1="$(_compress_if_exists 'SRR873628_1' || true)"
gz2="$(_compress_if_exists 'SRR873628_2' || true)"
gzs="$(_compress_if_exists 'SRR873628' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample13.bCL7B.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample13.bCL7B.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873629"
gz1="$(_compress_if_exists 'SRR873629_1' || true)"
gz2="$(_compress_if_exists 'SRR873629_2' || true)"
gzs="$(_compress_if_exists 'SRR873629' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample14.b53A.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample14.b53A.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873630"
gz1="$(_compress_if_exists 'SRR873630_1' || true)"
gz2="$(_compress_if_exists 'SRR873630_2' || true)"
gzs="$(_compress_if_exists 'SRR873630' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample15.HUES6.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample15.HUES6.R2.fq.gz"
fi

fasterq-dump --split-files "SRR873631"
gz1="$(_compress_if_exists 'SRR873631_1' || true)"
gz2="$(_compress_if_exists 'SRR873631_2' || true)"
gzs="$(_compress_if_exists 'SRR873631' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz1" ]; then
  mv -f "$gz1" "Sample16.H1.R1.fq.gz"
fi
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gz2" ]; then
  mv -f "$gz2" "Sample16.H1.R2.fq.gz"
fi

fasterq-dump --split-files "SRR976113"
gz1="$(_compress_if_exists 'SRR976113_1' || true)"
gz2="$(_compress_if_exists 'SRR976113_2' || true)"
gzs="$(_compress_if_exists 'SRR976113' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "Sample17.iPSC1.smRNA.fq.gz"
fi

fasterq-dump --split-files "SRR976114"
gz1="$(_compress_if_exists 'SRR976114_1' || true)"
gz2="$(_compress_if_exists 'SRR976114_2' || true)"
gzs="$(_compress_if_exists 'SRR976114' || true)"
if [ "$RENAME_SRA_TO_ORIGINAL" = "1" ] && [ -n "$gzs" ]; then
  mv -f "$gzs" "Sample18.iPSC2.smRNA.fq.gz"
fi


# Option B: Direct download (larger .sra files)
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873616/SRR873616"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873617/SRR873617"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873618/SRR873618"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873619/SRR873619"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873620/SRR873620"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873621/SRR873621"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873622/SRR873622"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873623/SRR873623"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873624/SRR873624"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873625/SRR873625"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873626/SRR873626"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873627/SRR873627"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873628/SRR873628"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873629/SRR873629"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873630/SRR873630"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873631/SRR873631"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos5/sra-pub-run-32/SRR000/976/SRR976113/SRR976113.2"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos5/sra-pub-run-32/SRR000/976/SRR976114/SRR976114.2"

# --- Additional data files ---
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873625/SRR873625"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873625/SRR873625"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873626/SRR873626"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873627/SRR873627"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873628/SRR873628"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873629/SRR873629"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873630/SRR873630"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873631/SRR873631"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos5/sra-pub-run-32/SRR000/976/SRR976113/SRR976113.2"
curl -L -O -C - "https://sra-downloadb.be-md.ncbi.nlm.nih.gov/sos5/sra-pub-run-32/SRR000/976/SRR976114/SRR976114.2"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873616/SRR873616"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873617/SRR873617"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873618/SRR873618"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873619/SRR873619"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873620/SRR873620"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873621/SRR873621"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873622/SRR873622"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873623/SRR873623"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873624/SRR873624"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873617/SRR873617"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873619/SRR873619"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873620/SRR873620"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873621/SRR873621"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873626/SRR873626"
curl -L -O -C - "https://sra-pub-run-odp.s3.amazonaws.com/sra/SRR873627/SRR873627"

echo "Download complete. Files saved to $OUTDIR"