Before comparing sketches, it helps to see how the cohort was
sampled: how many subjects, how many samples each gave, from which
sources, and how far apart; and, when cultured isolates were sequenced
too, which organisms were grown from whom, and when.
smash_timeline() draws one row per subject and one point
per sample, at the day it was collected, coloured by the sample’s source
or by the isolate’s species, with isolates and metagenomes drawn as
different shapes.
Files you need
smash2lab works on files you already have: sourmash signatures (usually made with sra2lab) and the samples’ metadata. Unlike taxonomic or functional profiles, sketches need no reference database to read, combine or compare. A typical analysis directory looks like this:
~/projects/
└── PRJNA1556220/ <- analysis directory (working directory)
└── data/
├── PRJNA1556220_runs.csv <- run metadata
└── smash/ <- one .sig per run and sketch setting
├── SRR41520862_k31_scale1000_seed42_abund.sig
├── SRR41520862_k51_scale1000_seed42_abund.sig
└── ...
The signatures come from
sra2lab::smash_runs(), smash_stream(),
smash_local() or multiprocess_runs(), which
run sourmash sketch dna in sra2lab’s smash_env
conda environment (made by
sra2lab::setup_conda_sourmash()). They write one
<id>_k<k>_scale<scaled>_seed<seed>_<method>.sig
per sample and setting:
runs <- sra2lab::get_bioproject_runs("PRJNA1556220")
write.csv(runs, "data/PRJNA1556220_runs.csv", row.names = FALSE)
# k = 31 for species-level comparisons, k = 51 for strains
sra2lab::smash_runs(runs, k = c(31, 51), scaled = 1000, method = "abund")Any other sourmash signature files work too (.sig,
.sig.gz or .zip collections, e.g. from
sourmash sketch run by hand), as long as they hold
FracMinHash (scaled) sketches. smash2lab reads them in R;
sourmash is needed only to make them, and optionally to compare them
(see vignette("smash-compare")).
Run metadata: isolates and metagenomes.
sra2lab::get_bioproject_runs() returns one row per SRA run,
with the BioSample attributes the submitters gave. Its columns depend on
the BioSample package the BioProject used, the two packages that lv2sra prepares submissions
for:
| BioSample package | BioSampleModel |
What was sequenced | Key attributes |
|---|---|---|---|
| Metagenome or environmental | Metagenome or environmental |
a specimen’s whole microbial community |
organism (a metagenome type, e.g. “human gut
metagenome”), isolation_source,
host_subject_id, collection_date
|
| Microbe | Microbe, viral or environmental |
a cultured isolate |
organism (the isolate’s species, e.g. “Klebsiella
pneumoniae”), isolate or strain,
isolation_source (the specimen it was cultured from),
host_subject_id, collection_date
|
smash_aggregate() uses these to record, for every
sketch, whether it is of an isolate or a metagenome, its source, and for
an isolate its species. For local reads sketched with
sra2lab::smash_local(), use your own specimen table
instead, with one row per specimen ID.
The example data
smash2lab ships sourmash signatures of 18 runs from BioProject PRJNA1556220,
“Microbiome changes in the peri-heart transplant setting”: skin, stool
and perirectal swab metagenomes from 7 subjects, five of whom have two
to six samples. They are the same runs as the example data of sylph2lab, funprofiler2lab and
rgi2lab. The runs were
sketched with sra2lab::smash_stream() at k = 31 and k = 51
(scaled = 1000, seed = 42, with abundances). To keep the package small,
the shipped sketches keep only k-mers seen at least twice and are
downsampled to scaled = 10000; that is, they are what
smash_aggregate(min_abundance = 2, downsample = 10000)
would make of the originals, and are named for their parameters (e.g.
SRR41520862_k31_scale10000_seed42_abund.sig.gz). The
package also ships the run table for all 67 runs of the BioProject.
smash_example() gives their paths, which stand in for
data/smash and data/PRJNA1556220_runs.csv
below.
The isolate example data
smash2lab also ships sourmash signatures of 30 cultured bacterial
isolates from BioProject PRJNA1570337,
“Bacterial isolates from human subjects with a history of MDRO infection
and their healthcare environment”, and the run table of all 239 of its
runs, all Microbe BioSamples. The 30 isolates are of six species, from
five subjects: from each subject’s throat, stool and perirectal swabs,
from environmental swabs linked to the subject
(environmental swab), and, for four subjects, the isolate
recorded with the source Bacterial Cell Iso, collected
before the others. They were sketched with
sra2lab::smash_stream() at k = 31 and k = 51, with scaled =
100 to resolve strains (seed = 42, with abundances); the shipped
sketches are filtered and downsampled as described in
vignette("smash-aggregate").
smash_example("isolates") and
smash_example("isolate_runs") give their paths.
Some examples also use small made-up tables, marked as such, to show isolates and metagenomes side by side.
The timeline needs only sample metadata: the subject, the collection
date and (optionally) the source, sample type and species of each
sample. It is the same function as
sylph2lab::sylph_timeline() and
funprofiler2lab::funprofiler_timeline(), with the isolate
options added, so cohorts analysed with several tools are drawn the same
way.
The sketched samples
Combine the sketches with the run table, as in
vignette("smash-aggregate"):
library(smash2lab)
sketches <- smash_aggregate(smash_example("signatures"),
metadata = smash_example("runs"), k = 31)and plot their timeline, coloured by source. The defaults match
sra2lab run tables: subjects in host_subject_id, dates in
collection_date, and samples in sample_id:
smash_timeline(sketches, site = "sample_source")
Each subject’s row is labelled with its number of samples, subjects
are ordered by their first sample, and by default the x axis is days
since that subject’s first sample (see The x
axis for the alternatives). If you keep sketches at several k-mer
sizes, a sample has a row for each, but smash_timeline()
reduces data to one row per sample first, so each sample is
drawn once. Five of the seven subjects have repeat samples: GOL_017, for
example, gave a groin and a perirectal swab on day 0, and stool and
perirectal swabs on days 3 and 10, while GOL_019’s two stool swabs were
more than three weeks apart. All of these samples are metagenomes
(circles); smash_timeline() uses the
sample_type column made by smash_aggregate()
to tell isolates from metagenomes (below).
The whole cohort
smash_timeline() takes any data frame with sample,
subject and date columns, so it can also show every run in the
BioProject, sketched or not, straight from the run table. Name the
sample column:
runs <- read.csv(smash_example("runs")) # "data/PRJNA1556220_runs.csv"
smash_timeline(runs, sample = "run_accession", site = "isolation_source")
Samples taken from the same subject on the same day (here, often several swab types at one visit) are spread out vertically so that none is hidden.
Isolates and metagenomes
Studies of the transmission of multidrug-resistant organisms often
sequence both kinds of sample: metagenomes of a subject’s specimens, and
the isolates cultured from those specimens or from the subject’s
surroundings. The example BioProject holds metagenomes only, so here is
a made-up cohort of the same shape, with isolates cultured from some of
the sketched specimens, and from two environmental swabs, as an isolate
(Microbe) BioProject’s run table and smash_aggregate()
would describe them:
specimens <- unique(sketches[, c("sample_id", "host_subject_id",
"collection_date", "sample_source")])
specimens$sample_type <- "metagenome"
specimens$species <- NA
# made-up isolates: cultured from some of the specimens above, and from the
# rooms of two subjects
isolates <- data.frame(
sample_id = paste0("ISO", 1:9),
host_subject_id = c("GOL_017", "GOL_017", "GOL_017", "GOL_016", "GOL_016",
"GOL_003", "GOL_015", "GOL_015", "GOL_019"),
collection_date = c("2026-03-24", "2026-03-27", "2026-04-03", "2026-04-03",
"2026-04-09", "2025-09-09", "2026-05-12", "2026-05-17",
"2026-05-09"),
sample_source = c("Perirectal Swab", "Stool Swab", "Bed rail",
"Perirectal Swab", "Perirectal Swab", "Stool Swab",
"Stool Swab", "Sink drain", "Stool Swab"),
sample_type = "isolate",
species = c("Klebsiella pneumoniae", "Klebsiella pneumoniae",
"Klebsiella pneumoniae", "Enterococcus faecium",
"Escherichia coli", "Enterococcus faecium",
"Escherichia coli", "Escherichia coli", "Enterococcus faecium")
)
cohort <- rbind(specimens, isolates)With a sample_type column, isolates are triangles and
metagenomes circles. color_var = "species" colours the
isolates by species, with metagenomes in grey, so you can follow each
organism through a subject’s samples:
smash_timeline(cohort, color_var = "species")
Here a Klebsiella pneumoniae was cultured from GOL_017’s
perirectal swab on day 0, stool on day 3 and, on day 10, from a bed rail
in the subject’s room: a sequence worth checking for a shared strain
with vignette("smash-compare"). GOL_015’s Escherichia
coli, from a stool swab and five days later from the sink drain, is
another. Colour by source instead to see what each isolate was cultured
from (the shapes still tell isolates from metagenomes):
smash_timeline(cohort, site = "sample_source")
facet_var = "species" gives each species a panel of its
own, with the metagenomes in a last panel, so each organism’s subjects
and sources are easy to read, one species at a time:
smash_timeline(cohort, site = "sample_source", facet_var = "species")
The sample_type and species columns are
those of smash_aggregate()’s result; name other columns
with sample_type = and species =. For a run
table of isolates straight from
sra2lab::get_bioproject_runs(), the species is the
organism column (species = "organism").
A real isolate cohort
The isolate example data are 30 isolates of six species from five
subjects, cultured from the subjects’ throat, stool and perirectal swabs
and from environmental swabs, as smash_aggregate() reads
them with the BioProject’s run table:
isolates <- smash_aggregate(smash_example("isolates"),
metadata = smash_example("isolate_runs"), k = 51)All are isolates, so all are triangles; colour them by species to see which organisms were grown from whom, and when:
smash_timeline(isolates, color_var = "species", sample_noun = "isolates")
For four subjects, the first isolate
(Bacterial Cell Iso) came three to eight days before the
rest. P007 then gave Enterococcus faecium and Pseudomonas
aeruginosa over ten days in November, and both again three months
later. Split by species, and coloured by source, each organism’s sources
are easy to read:
smash_timeline(isolates, site = "sample_source", facet_var = "species",
sample_noun = "isolates")
Whether isolates of a species from one subject, or from a subject and
the environment, are the same strain is the question of
vignette("smash-compare").
The x axis
x_axis sets where day 0 is, and so what the timeline
emphasises:
x_axis |
Day 0 | Shows |
|---|---|---|
"subject-day" (default) |
each subject’s first sample | the spacing of each subject’s samples, aligned at the start |
"study-day" |
the first sample in the data, across all subjects | when each subject entered, and their follow-up, on one study clock |
"calendar-day" |
(calendar dates) | when samples were taken, e.g. to line up with seasons, or with other subjects’ isolates in the same unit |
With "study-day", each subject’s row starts on the day
they entered the study, counted from the cohort’s first sample:
smash_timeline(sketches, site = "sample_source", x_axis = "study-day")
"calendar-day" shows the same layout with dates. For
transmission questions, a calendar axis lines up isolates from different
subjects (and their rooms) collected at the same time. Here, without
site, each subject gets its own colour and there is no
legend:
smash_timeline(runs, sample = "run_accession", x_axis = "calendar-day")
Colours by subject feature
Besides "site" and "species",
color_var can be a subject feature: anything with
one value per subject, such as treatment arm, outcome or age, given as a
column of data (color_var = "treatment_arm")
or as a vector with one value per row of data:
n_samples <- tapply(sketches$sample_id, sketches$host_subject_id,
function(x) length(unique(x)))
repeat_sampled <- names(n_samples)[n_samples > 1]
smash_timeline(
sketches,
color_var = ifelse(sketches$host_subject_id %in% repeat_sampled,
"Repeat samples", "Single sample"),
color_label = "Follow-up"
)
A subject feature must have the same value in all of a subject’s
rows; otherwise smash_timeline() stops and names the
subjects where it varies. Numeric features, such as age, get a
continuous colour scale.
Panels
facet_var splits the timeline into panels, stacked in
one column with one panel per value. It takes the same options as
color_var: "site", "species", a
subject-level column, or a vector with one value per row. Each panel
shows only its own subjects. With facet_var = "site", a
subject sampled at several sites has a row in each site’s panel:
follow_up <- ifelse(sketches$host_subject_id %in% repeat_sampled,
"Repeat samples", "Single sample")
smash_timeline(sketches, site = "sample_source", facet_var = "site",
color_var = follow_up, color_label = "Follow-up")
Other options
sample_noun changes the word in the labels,
e.g. sample_noun = "cultures", and
show_counts = FALSE drops the counts. palette
takes any viridis option ("turbo", the default,
"viridis", "magma", …).
Dates can be Dates, or text in YYYY-MM-DD,
YYYY/MM/DD or MM/DD/YYYY form. SRA allows
collection dates such as "missing", "2025" or
"2025-09"; samples with such dates cannot be placed on a
day, so they are left out with a warning.