Skip to content

Instantly share code, notes, and snippets.

@bschiffthaler
Created April 13, 2021 10:41
Show Gist options
  • Save bschiffthaler/319b3affad5c6f68e878531b553199af to your computer and use it in GitHub Desktop.
Save bschiffthaler/319b3affad5c6f68e878531b553199af to your computer and use it in GitHub Desktop.
Download FASTQ files from the EBI SRA mirror
#!/usr/bin/env bash
get_sra() {
local __FASTQ="$1"
local __OUT="$PWD"
local __DOWNLOAD_THREADS=1
# Allow downloading with multiple threads
if [[ ! -z "${DOWNLOAD_THREADS+x}" ]]; then
__DOWNLOAD_THREADS=${DOWNLOAD_THREADS}
fi
# Allow optional output dir
if [[ $# -eq 2 ]]; then
__OUT="$2"
fi
# Figure out the location on the EBI server
local __PREFIX="ftp://ftp.sra.ebi.ac.uk/vol1/fastq"
local __ACCESSION=""
__ACCESSION=$(echo "${__FASTQ}" | tr '.' '_' | cut -d '_' -f 1)
local __DIR1="${__ACCESSION:0:6}"
local __A_LEN=${#__ACCESSION}
local __DIR2=""
if [[ ${__A_LEN} -eq 9 ]]; then
__DIR2=""
elif [[ ${__A_LEN} -eq 10 ]]; then
__DIR2="00${__ACCESSION:9:1}"
elif [[ ${__A_LEN} -eq 11 ]]; then
__DIR2="0${__ACCESSION:9:2}"
else
__DIR2="${__ACCESSION:9:3}"
fi
local __URL="${__PREFIX}/${__DIR1}/${__DIR2}/${__ACCESSION}/${__FASTQ}"
# Check if we are PE or SE
local __STATUS
__STATUS=$(wget --spider "${__URL}_2.fastq.gz" 2>&1 | grep exists || true)
# Set appropriate variables and download
if [[ ! -z "${__STATUS}" ]]; then
PAIRED=1
FIRST="${__OUT}/${__FASTQ}_1.fastq.gz"
SECOND="${__OUT}/${__FASTQ}_2.fastq.gz"
printf "%s\n%s\n" "${__URL}_1.fastq.gz" "${__URL}_2.fastq.gz" |
xargs -I {} -P 2 wget --quiet -P "${__OUT}" {}
else
PAIRED=0
FIRST="${OUT}/${FASTQ}.fastq.gz"
SECOND=""
wget --quiet -P "${OUT}" "${URL}.fastq.gz"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment