Skip to contents

`multiprocess_runs()` and `multiprocess_local()` combine [smash_runs()], [sylph_runs()], [funprofiler_runs()] and [rgi_runs()] (or [smash_local()], [sylph_local()], [funprofiler_local()] and [rgi_local()]) so that each sample's reads are downloaded or found once, and then processed by every pathway named in `process_path` before moving on to the next sample:

`"smash"`

sourmash signatures, as from [smash_runs()]: `<smash_outdir>/<id>_k<k>_scale<scaled>_seed<seed>_<method>.sig`.

`"sylph"`

sylph taxonomic profiles, as from [sylph_runs()]: `<sylph_outdir>/<id>_profiling.tsv`.

`"funprofiler"`

KEGG Orthology profiles, as from [funprofiler_runs()]: `<funprofiler_outdir>/<id>_k<k>_scale<scaled>_ko_profiles.csv`.

`"rgi"`

antibiotic resistance gene read mapping, as from [rgi_runs()]: `<rgi_outdir>/<id>_<aligner>[_wildcard][_other_models].gene_mapping_data.txt`, with RGI's other outputs alongside.

where `<id>` is the run accession (`multiprocess_runs()`) or the specimen ID (`multiprocess_local()`). The pathways run in the order given in `process_path`, and each works exactly as in its single-pathway function, with the same outputs, so outputs made by either can be skipped by the other.

Usage

multiprocess_runs(
  runs,
  process_path = c("smash", "sylph", "funprofiler"),
  smash_k = 31,
  smash_scaled = 1000,
  smash_method = "abund",
  seed = 42,
  sylph_database = "../gtdb-r232-c200-dbv2.syl2db",
  funprofiler_database = "../KOs_sketched_scaled_1000.sig.zip",
  funprofiler_k = 11,
  funprofiler_scaled = 1000,
  threshold_bp = 1000,
  rgi_card_db = "../localDB",
  rgi_aligner = "kma",
  rgi_include_wildcard = FALSE,
  rgi_include_other_models = FALSE,
  rgi_keep_bam = FALSE,
  smash_outdir = file.path("data", "smash"),
  sylph_outdir = file.path("data", "sylph"),
  funprofiler_outdir = file.path("data", "funprofiler"),
  rgi_outdir = file.path("data", "rgi"),
  working_file = "working_data.fastq.gz",
  threads = 6,
  parallel = FALSE,
  quiet = FALSE,
  overwrite = FALSE,
  sra_env = "sra_env",
  smash_env = "smash_env",
  sylph_env = "sylph_env",
  funprofiler_env = "funprofiler_env",
  rgi_env = "rgi_env"
)

multiprocess_local(
  specimens,
  local_files,
  process_path = c("smash", "sylph", "funprofiler"),
  smash_k = 31,
  smash_scaled = 1000,
  smash_method = "abund",
  seed = 42,
  sylph_database = "../gtdb-r232-c200-dbv2.syl2db",
  funprofiler_database = "../KOs_sketched_scaled_1000.sig.zip",
  funprofiler_k = 11,
  funprofiler_scaled = 1000,
  threshold_bp = 1000,
  rgi_card_db = "../localDB",
  rgi_aligner = "kma",
  rgi_include_wildcard = FALSE,
  rgi_include_other_models = FALSE,
  rgi_keep_bam = FALSE,
  smash_outdir = file.path("data", "smash"),
  sylph_outdir = file.path("data", "sylph"),
  funprofiler_outdir = file.path("data", "funprofiler"),
  rgi_outdir = file.path("data", "rgi"),
  working_file = "working_data.fastq.gz",
  threads = 6,
  parallel = FALSE,
  quiet = FALSE,
  overwrite = FALSE,
  smash_env = "smash_env",
  sylph_env = "sylph_env",
  funprofiler_env = "funprofiler_env",
  rgi_env = "rgi_env"
)

Arguments

runs

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

process_path

The pathways to run: one or more of `"smash"`, `"sylph"`, `"funprofiler"` and `"rgi"`. Defaults to `"smash"`, `"sylph"` and `"funprofiler"`; add `"rgi"` to map reads to CARD as well.

smash_k, smash_scaled, smash_method, seed

The `k`, `scaled`, `method` and `seed` of [smash_runs()].

sylph_database

The `database` of [sylph_runs()].

funprofiler_database, funprofiler_k, funprofiler_scaled, threshold_bp

The `database`, `k`, `scaled` and `threshold_bp` of [funprofiler_runs()].

rgi_card_db, rgi_aligner, rgi_include_wildcard, rgi_include_other_models, rgi_keep_bam

The `card_db`, `aligner`, `include_wildcard`, `include_other_models` and `keep_bam` of [rgi_runs()].

smash_outdir, sylph_outdir, funprofiler_outdir, rgi_outdir

Output directories for each pathway, with the same defaults as the single-pathway functions.

working_file

Path of the temporary reads file: the downloaded reads for `multiprocess_runs()`, or the joined R1 and R2 for the funprofiler pathway of `multiprocess_local()`. For `multiprocess_runs()`, a name ending in `.gz` (the default) compresses the reads, and a plain name (e.g. `"working_data.fastq"`) skips compression, which is faster but takes several times the disk space.

threads

Number of threads for `fasterq-dump`, `pigz`, `sylph sketch`, `sylph profile` and `rgi bwt` (for each of them, so with `parallel = TRUE`, sylph and RGI together use up to twice as many).

parallel

If `TRUE`, run each sample's pathways at the same time (see Details). Defaults to `FALSE`, which runs them one after another. Use `TRUE` from `Rscript` in a terminal, not from an IDE such as Positron or RStudio, where forking fails or is unreliable.

quiet

If `TRUE`, hide the tools' console output. Defaults to `FALSE`.

overwrite

If `TRUE`, make outputs that already exist 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()]).

sylph_env

Conda environment with sylph (see [setup_conda_sylph()]).

funprofiler_env

Conda environment with fmh-funprofiler (see [setup_conda_funprofiler()]).

rgi_env

Conda environment with RGI (see [setup_conda_rgi()]).

specimens

A data frame with a `specimen_id` column. The IDs are used in the output file names, so they must be unique and must not contain `/` or `\`. Other columns are ignored.

local_files

Directory holding the specimens' paired FASTQ files.

Value

A tibble with one row per sample and output file, invisibly, with columns `run_accession` (`multiprocess_runs()`) or `specimen_id` (`multiprocess_local()`), `process` (`"smash"`, `"sylph"`, `"funprofiler"` or `"rgi"`), `k`, `scaled`, `method` and `aligner` (the parameters of that output, `NA` where they do not apply), `output` (path to the output file, `NA` if it failed; for rgi, the `gene_mapping_data.txt` file), `status` (`"sketched"`, `"profiled"`, `"mapped"`, `"skipped"` or `"failed"`) and `message` (why it failed, otherwise `NA`).

Details

For SRA runs, each run is downloaded once with `prefetch` and its reads extracted into `working_file` with `fasterq-dump` (as in [smash_runs()], compressed if `working_file` ends in `.gz`), which every pathway then reads; it is deleted at the end. For local reads, R1 and R2 are found as in [smash_local()]; the smash, sylph and rgi pathways read the pair directly, and the funprofiler pathway joins them into `working_file` first.

Arguments that mean different things for different tools are prefixed with the pathway's name: `smash_k` and `smash_scaled` are DNA k-mer sizes and scaled values for sourmash, while `funprofiler_k` and `funprofiler_scaled` are protein k-mer sizes and scaled values for `funcprofiler`, and the `rgi_*` arguments are those of [rgi_runs()]. Arguments for pathways not in `process_path` are ignored, and only the conda environments and databases of the chosen pathways need to exist. Every chosen pathway's arguments, environments and databases are checked before any reads are downloaded.

Outputs that already exist are skipped unless `overwrite = TRUE`, and a sample is downloaded only if at least one of its outputs still needs to be made. Outputs left unfinished by an interrupted, killed or crashed R session are made again, not skipped (see [smash_runs()]); an interrupt keeps the outputs of the pathways that had already finished for the sample. If one pathway fails for a sample (for example, sylph), its outputs are recorded as `"failed"` with a warning, but the other pathways' outputs for that sample are still made. If the download (or, for local reads, finding the read pair) fails, all of that sample's outputs fail.

With `parallel = TRUE`, a sample's pathways run at the same time rather than one after another, each in its own forked R process (with [parallel::mcparallel()]), all reading the same reads; the next sample starts once all of them have finished. This can make each sample several times faster, but the tools then compete for the computer's processors and memory: each tool still uses `threads` threads, and the memory needed is that of all the chosen tools together (for example, sylph's database and RGI's CARD index at once), so consider a smaller `threads`, and use `parallel = FALSE` if memory is short. A pathway that fails, or whose process is killed (for example, for running out of memory), is recorded as `"failed"` without stopping the others. An interrupt (Esc or Ctrl-C) stops all of the sample's pathways and the tools they are running; the outputs of pathways that had already finished are kept, and the rest are made by the next call. Progress messages are labelled with the pathway (for example, `[1/8] SRR123 [sylph]`), but the tools' console output is interleaved, so `quiet = TRUE` makes the console easier to read. Forking is not available on Windows, where `parallel = TRUE` gives a warning and runs the pathways one after another. The outputs are the same either way.

Some IDEs do not allow forking, or do not support it well. Positron refuses it ("Can't fork the R session in Positron"), and RStudio advises against it. In those, `parallel = TRUE` can fail every sample, each with that error as its `message`, after its reads have been downloaded. So with `parallel = TRUE`, run the call in a script from a terminal, outside the IDE, for example with `Rscript run_pathways.R` (or with `nohup Rscript run_pathways.R > run_pathways.log 2>&1 &` for a long job that should keep going after the terminal is closed). In an IDE's console, use `parallel = FALSE`.

See also

[smash_runs()], [sylph_runs()], [funprofiler_runs()], [rgi_runs()] and [smash_local()] for the single pathways.

Examples

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

# sourmash signatures and sylph profiles from one download per run
res <- multiprocess_runs(wgs, process_path = c("smash", "sylph"))

# the default three, with two k-mer sizes for sourmash
res <- multiprocess_runs(wgs, smash_k = c(21, 31))

# all four, including RGI (CARD data loaded in ../localDB)
res <- multiprocess_runs(
  wgs, process_path = c("smash", "sylph", "funprofiler", "rgi")
)

# all four at the same time for each run, with fewer threads per tool
res <- multiprocess_runs(
  wgs, process_path = c("smash", "sylph", "funprofiler", "rgi"),
  parallel = TRUE, threads = 4, quiet = TRUE
)

# the same for local read pairs
specimens <- tibble::tibble(specimen_id = c("S-01", "S-02"))
res <- multiprocess_local(specimens, "fastq")
} # }