The *_local() functions (smash_local(),
sylph_local(), funprofiler_local(),
rgi_local() and multiprocess_local()) process
one specimen at a time. sylph and RGI can use several processors for
each specimen (threads), but sourmash sketch
and funcprofiler use about one each, so
smash_local() and funprofiler_local() leave
most of a modern computer idle.
You can process several specimens at once without anything new in
sra2lab: split the specimens into groups, and run the same
*_local() call on each group in its own R session. This
vignette shows how, with furrr.
Before you start
Install furrr, which also installs future:
install.packages("furrr")furrr’s future_map() works like
purrr::map(), but runs each element in a separate R
session. A plain purrr::map() over the groups would run
them one after another in the same session, which is no faster than a
single call.
The R sessions furrr starts with plan(multisession) are
new R processes, not forks of the current one. So, unlike
multiprocess_local(parallel = TRUE), this works in Positron
and RStudio as well as from a terminal.
The recipe, with smash_local()
library(dplyr)
library(purrr)
library(furrr)
library(sra2lab)
plan(multisession, workers = 4)
# absolute paths, so that every R session finds the same folders
local_files <- normalizePath("fastq")
smash_dir <- file.path(getwd(), "data", "smash")
# deal the specimens into 4 groups, like cards
specimen_list <- specimens |>
mutate(group = (row_number() - 1) %% 4) |>
group_split(group, .keep = FALSE)
res <- future_map(specimen_list, \(x) sra2lab::smash_local(
x, local_files,
k = 31, scaled = 1000, method = "abund", seed = 42,
outdir = smash_dir,
quiet = TRUE
)) |>
list_rbind()
plan(sequential) # close the R sessionsspecimens is the usual table with a
specimen_id column. Each group runs in its own R session,
and list_rbind() joins the four results into one table,
with the same columns as a single smash_local() call.
Dealing the specimens out in turn (%% 4), rather than in
four blocks (dplyr::ntile()), tends to give each group a
similar mix of large and small specimens, so the groups finish at about
the same time.
In a test on a 12-core Mac with 8 small synthetic specimens (150,000
read pairs each), smash_local() took 15 seconds on its own
and 5 seconds in 4 groups, and funprofiler_local() (below)
took 70 seconds on its own and 23 seconds in 4 groups. The signatures
and KO profiles were identical either way. Real specimens take longer,
but the saving is similar while the computer has free processors and the
disk keeps up.
Across the *_local() functions
Groups of different specimens never write the same output file, so
they can share outdir. What else each function needs:
| Function | In parallel groups |
|---|---|
smash_local() |
Nothing else: use it as above. |
funprofiler_local() |
Give each group its own working_file. |
sylph_local() |
Split threads between the groups, and check
memory. |
rgi_local() |
Split threads between the groups, and check
memory. |
multiprocess_local() |
Its own working_file for each group, split
threads, and keep parallel = FALSE. |
funprofiler_local(): one working file per group
funprofiler_local() joins each specimen’s R1 and R2 into
working_file before profiling it. With the default, every
group would use the same working_data.fastq.gz, and the
groups would overwrite each other’s reads: in a test with 4 groups, 7 of
8 specimens failed. Name the working file after the group instead:
plan(multisession, workers = 4)
res <- future_map(specimen_list, \(x) sra2lab::funprofiler_local(
x, local_files,
database = normalizePath("../KOs_sketched_scaled_1000.sig.zip"),
outdir = file.path(getwd(), "data", "funprofiler"),
working_file = file.path(getwd(),
paste0("working_", x$specimen_id[1], ".fastq.gz")),
quiet = TRUE
)) |>
list_rbind()
plan(sequential)Each group’s working file is named after its first specimen and deleted when the group finishes.
sylph_local() and rgi_local(): share the
processors and memory
sylph and RGI already use threads processors per
specimen, so running groups in parallel helps less, and only if the
processors are shared out: with 4 groups on an 8-core computer, use
threads = 2. Each group also loads the tool’s database into
memory separately (sylph’s database, or RGI’s CARD index), so 4 groups
need about 4 times the memory of one. If memory is short, use fewer
groups, or run these functions as a single call.
plan(multisession, workers = 4)
res <- future_map(specimen_list, \(x) sra2lab::sylph_local(
x, local_files,
database = normalizePath("../gtdb-r232-c200-dbv2.syl2db"),
outdir = file.path(getwd(), "data", "sylph"),
threads = 2,
quiet = TRUE
)) |>
list_rbind()
plan(sequential)rgi_local() is the same, with
card_db = normalizePath("../localDB").
multiprocess_local()
multiprocess_local() runs each pathway as its own
*_local() call would, so the same rules apply: its own
working_file for each group if process_path
includes "funprofiler", threads split between
the groups, and enough memory for every group’s databases. Keep its own
parallel = FALSE: the groups already run in parallel, and
running each group’s pathways in parallel as well would multiply the
processors and memory needed again.
Good to know
-
How many groups.
parallelly::availableCores()(installed with future) reports the processors available. Leave one or two for the rest of the computer, and remember thatworkerstimesthreadsis what the tools use together. -
Messages. Each group’s progress messages, and the
tools’ output, are shown only when that whole group has finished, all
mixed together, so use
quiet = TRUE. A specimen that fails is still recorded as"failed"in the combined result, with the reason in itsmessagecolumn. - Restarting. As with a single call, outputs that already exist are skipped, so the same code can be run again to make only the missing ones. If you stop a parallel job, the R sessions may take a moment to stop; an output left half-written is marked as unfinished, and is made again by the next run rather than skipped.
-
SRA runs. This vignette is about local reads. The
*_runs()functions spend most of their time downloading, which more groups at once do not speed up much, and each group would need its ownworking_fileand the disk space for a run’s download and reads.