Skip to contents

Downloads each SRA run in `runs`, sketches it with `sourmash sketch dna`, and deletes the reads before moving on, so that only one run's reads are ever stored locally. `runs` is typically the output of [get_bioproject_runs()], or a filtered version of it.

Usage

smash_runs(
  runs,
  k = 31,
  scaled = 1000,
  method = "abund",
  seed = 42,
  outdir = file.path("data", "smash"),
  working_file = "working_data.fastq.gz",
  threads = 6,
  quiet = FALSE,
  overwrite = FALSE,
  sra_env = "sra_env",
  smash_env = "smash_env"
)

Arguments

runs

A data frame with a `run_accession` column (SRR, ERR or DRR accessions), such as the output of [get_bioproject_runs()].

k

k-mer size(s) for the sketch, e.g. `31` or `c(21, 31, 51)`.

scaled

Scaled value(s) for the FracMinHash sketch, e.g. `1000` or `c(1000, 100)`.

method

`"abund"` to track k-mer abundance, `"noabund"` not to, or `c("abund", "noabund")` for both. Defaults to `"abund"`.

seed

Hash seed for the sketch. Only a single seed is allowed.

outdir

Directory for the signature files. Created if needed.

working_file

Path of the temporary reads file. If it ends in `.gz` (the default), the reads are compressed; otherwise (e.g. `"working_data.fastq"`), they are written as plain FASTQ, which is faster but takes several times the disk space.

threads

Number of threads for `fasterq-dump` and `pigz`.

quiet

If `TRUE`, hide the console output of `prefetch`, `fasterq-dump` and `sourmash`. Defaults to `FALSE`.

overwrite

If `TRUE`, sketch runs whose signature file already exists again. If `FALSE` (the default), skip them.

sra_env

Conda environment with the SRA Toolkit (see [setup_conda_sra()]).

smash_env

Conda environment with sourmash (see [setup_conda_sourmash()]).

Value

A tibble with one row per run and combination of `k`, `scaled` and `method`, invisibly, with columns `run_accession`, `k`, `scaled`, `method`, `signature` (path to the signature file, `NA` if it failed), `status` (`"sketched"`, `"skipped"` or `"failed"`) and `message` (why it failed, otherwise `NA`).

Details

The runs are processed in order with [purrr::pmap()]. For each run:

  1. The run is downloaded with `prefetch –max-size u` (in `sra_env`) into a folder next to `working_file` (`working_data_sra` for the default `working_file`). Its reads are then extracted with `fasterq-dump –split-spot –skip-technical –stdout` into a single file, `working_file`. `–split-spot` writes both reads of a paired-end spot to the same file; `–skip-technical` leaves out technical reads such as barcodes. If `working_file` ends in `.gz` (the default), the reads are compressed as they are extracted, with `pigz -1` on `threads` threads, or with `gzip -1` if pigz is not installed in `sra_env` (see [setup_conda_sra()]); otherwise they are written as plain FASTQ. The folder, which also holds `fasterq-dump`'s temporary files, is deleted once the reads are extracted. `working_file` is overwritten for each run.

  2. `working_file` is sketched with `sourmash sketch dna -p k=<k>,scaled=<scaled>,<method>,seed=<seed>` (in `smash_env`). The signature is named after the run accession.

  3. Each combination of `k`, `scaled` and `method` gets its own signature file, `<outdir>/<run_accession>_k<k>_scale<scaled>_seed<seed>_<method>.sig`, e.g. `data/smash/SRR123456_k31_scale1000_seed42_abund.sig`. So `k = c(21, 31)` with `scaled = c(1000, 100)` gives four files per run. With several combinations, the reads are still sketched in a single pass, with one `-p` per combination; the sketches are then split into their files with sourmash's Python API. Because the file name records the sketch parameters, sketches of the same run with different parameters can sit side by side in `outdir`.

When all runs are done, `working_file` is deleted. It is also deleted if the function stops early, for example on an error or interrupt.

While a run's reads are extracted, the disk holds the downloaded run, `fasterq-dump`'s temporary files (several times the size of the download) and `working_file`, so put `working_file` where there is room for all three; on WSL2, keep it in the Linux file system (e.g. under `~`) rather than on a Windows drive under `/mnt/`, which is much slower. A plain `working_file` (e.g. `"working_data.fastq"`) skips compression, which is faster but makes `working_file` several times larger.

`prefetch`, `fasterq-dump` and `sourmash` print progress to the console as they run. With `quiet = TRUE` that output is hidden; if a step fails, its last lines are included in the failure message. The one-line progress messages from `smash_runs()` itself are shown either way, and can be hidden with [suppressMessages()]. They name each step, for example:


[3/120] SRR123456: downloading (prefetch) ...
[3/120] SRR123456: extracting and compressing reads (fasterq-dump) ...
[3/120] SRR123456: sketching ...

A run that fails to download or sketch is reported with a warning and recorded in the result; the remaining runs are still processed. Each failure is also reported straight away, with a `[3/120] SRR...: FAILED: <reason>` message, since R shows warnings only once the whole call has finished; the reason is kept in the result's `message` column. Signature files that already exist (for the same run and parameters) are skipped unless `overwrite = TRUE`, so an interrupted job can be restarted with the same call. A run is downloaded only if at least one of its signature files still needs sketching, and then only the missing combinations are sketched.

Only finished outputs are skipped. While an output is being made, a hidden, empty marker file sits beside it (e.g. `.SRR123456_k31_scale1000_seed42_abund.sig.incomplete`), removed once the output is written. If R is interrupted (e.g. with Esc or Ctrl-C), the unfinished outputs and their markers are removed before it stops; if R is killed or the computer crashes, the marker stays, and the next call makes that output again instead of skipping it.

See also

[get_bioproject_runs()] to list the runs in a BioProject, and [smash_local()] for local paired-end reads.

Examples

if (FALSE) { # \dontrun{
runs <- get_bioproject_runs("PRJNA123456")
smash_runs(runs[runs$library_strategy == "WGS", ])

# several k-mer sizes, without abundance: one file per run and k, e.g.
# data/smash/SRR123456_k21_scale1000_seed42_noabund.sig
smash_runs(runs, k = c(21, 31, 51), method = "noabund")

# 2 k-mer sizes x 2 scaled values x 2 methods = 8 files per run
smash_runs(runs, k = c(21, 31), scaled = c(1000, 100),
           method = c("abund", "noabund"))
} # }