Following hashes across subjects, sources and time
Source:vignettes/smash-heatmap.Rmd
smash-heatmap.RmdEach hash in a sketch stands for one k-mer of the sequence sketched.
At k = 31 a k-mer is, almost always, specific to one species, and at k =
51 to one strain or a few closely related ones, so a hash seen again in
a subject’s later samples, or in another subject’s, marks DNA that
persisted or was shared. smash_heatmap() draws a hashes x
samples heatmap: one row per hash, one column per sample, with the
columns split by subject, ordered by time and labelled by source.
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 sketches
Combine the k = 51 sketches with the run table, as in
vignette("smash-aggregate"):
library(smash2lab)
sketches <- smash_aggregate(smash_example("signatures"),
metadata = smash_example("runs"), k = 51)
sketches[, c("sample_id", "host_subject_id", "collection_date",
"sample_source", "n_hashes")]
#> # A tibble: 18 × 5
#> sample_id host_subject_id collection_date sample_source n_hashes
#> <chr> <chr> <date> <chr> <int>
#> 1 SRR41520862 GOL_019 2026-04-16 Stool Swab 14045
#> 2 SRR41520863 GOL_017 2026-04-03 Stool Swab 9248
#> 3 SRR41520865 GOL_017 2026-03-24 Perirectal Swab 12301
#> 4 SRR41520867 GOL_015 2026-05-12 Stool Swab 13111
#> 5 SRR41520868 GOL_019 2026-05-09 Stool Swab 11214
#> 6 SRR41520869 GOL_016 2026-04-09 Perirectal Swab 11235
#> 7 SRR41520870 GOL_016 2026-04-03 Perirectal Swab 4020
#> 8 SRR41520873 GOL_017 2026-03-24 Groin Swab 1687
#> 9 SRR41520874 GOL_034 2026-08-18 Groin Swab 1440
#> 10 SRR41520876 GOL_015 2026-05-17 Stool Swab 14266
#> 11 SRR41520878 GOL_017 2026-04-03 Perirectal Swab 6659
#> 12 SRR41520879 GOL_017 2026-03-27 Stool Swab 9816
#> 13 SRR41520882 GOL_017 2026-03-27 Perirectal Swab 8268
#> 14 SRR41520884 GOL_003 2025-09-16 Stool Swab 2821
#> 15 SRR41520894 GOL_018 2026-03-02 Stool Swab 2410
#> 16 SRR41520897 GOL_015 2026-05-11 Axilla Swab 1383
#> 17 SRR41520902 GOL_003 2025-09-09 Stool Swab 22544
#> 18 SRR41520919 GOL_016 2026-03-30 Axilla Swab 2017Shared hashes
By default, smash_heatmap() shows the top_n
(50) hashes found in the most samples, among those in at least two
samples but not in all of them: the hashes that tell samples apart. Name
the subject, time and source columns to lay out the samples:
smash_heatmap(sketches, subject = "host_subject_id",
time = "collection_date", site = "sample_source")
Hashes are ordered by clustering (cluster = "hashes"),
so hashes found in the same samples sit together. Columns are labelled
with the date and the source of each sample; give
sample_label to label them otherwise.
select changes how the hashes are chosen:
"prevalent" ranks all hashes by the number of samples they
are in (including those in every sample), and "abundant" by
their mean k-mer count. value shows k-mer counts instead of
presence; "log10" keeps the scale readable:
smash_heatmap(sketches, subject = "host_subject_id",
time = "collection_date", site = "sample_source",
select = "abundant", value = "log10")
The hashes of a reference sample
reference shows the hashes of one or more samples,
ranked by the number of other samples they are in. With an
isolate as the reference, the heatmap shows which metagenomes hold its
k-mers; here, with a stool swab of GOL_017 (a subject sampled on three
days), it shows which of that sample’s k-mers turn up in the subject’s
other samples, and in other subjects:
ref <- sketches$sample_id[sketches$host_subject_id == "GOL_017" &
sketches$sample_source == "Stool Swab"][1]
ref
#> [1] "SRR41520863"
smash_heatmap(sketches, subject = "host_subject_id",
time = "collection_date", site = "sample_source",
reference = ref, top_n = 40)
Ranked this way, the top hashes are those of the reference found in the most other samples: here each is in 10 to 15 of the other 17, mostly stool and perirectal swabs of several subjects, so they are k-mers of organisms common in the gut rather than of this subject’s own strains. To follow hashes particular to a subject, or to an isolate, choose them yourself.
Hashes you choose
For the questions a study asks, choose the hashes yourself.
hashes takes a vector of hashes, or a data frame with a
hash column and a group column that splits the
rows into panels. Typically the groups come from reference genomes or
isolates: the hashes of each isolate’s genome (a species at k = 31, a
strain at k = 51). As an example, take the hashes
smash_hashes() finds in each of two subjects’ samples and
in no other subject’s:
long <- smash_hashes(sketches, keep = "host_subject_id")
subjects_per_hash <- tapply(long$host_subject_id, long$hash,
function(x) length(unique(x)))
private <- long[long$hash %in% names(subjects_per_hash)[subjects_per_hash == 1] &
long$host_subject_id %in% c("GOL_017", "GOL_019"), ]
# hashes seen in more than one of the subject's samples, 15 per subject
per_sample <- table(private$hash)
private <- private[private$hash %in% names(per_sample)[per_sample > 1], ]
chosen <- do.call(rbind, lapply(split(private, private$host_subject_id),
function(d) head(unique(d[, c("hash", "host_subject_id")]), 15)))
chosen <- data.frame(hash = chosen$hash,
group = paste("Private to", chosen$host_subject_id))
head(chosen)
#> hash group
#> 1 142824986151 Private to GOL_017
#> 2 440409159497 Private to GOL_017
#> 3 1493765244221 Private to GOL_017
#> 4 2549383859309 Private to GOL_017
#> 5 2803473208763 Private to GOL_017
#> 6 3065866222979 Private to GOL_017
smash_heatmap(sketches, subject = "host_subject_id",
time = "collection_date", site = "sample_source",
hashes = chosen, show_hash_labels = FALSE)
These hashes were chosen to be in more than one of the subject’s samples and in no other subject’s; the heatmap shows which samples, and so how long, and at which sites, the subject’s own DNA (at the resolution of 51-mers) persisted. GOL_017 has thousands of such hashes, some in five or all six of the subject’s samples, across stool and perirectal swabs and ten days. In a transmission study, the same plot of an isolate’s strain-level hashes across patients and environmental samples, in time order, shows where that strain was found, and when.
Isolates: the hashes of a strain
For isolates, the same plot shows which isolates share a strain, hash by hash. Take the Enterococcus faecium isolates of the isolate example data, eight from P007 and five from P011, at k = 51:
isolates <- smash_aggregate(smash_example("isolates"),
metadata = smash_example("isolate_runs"), k = 51)
efm <- isolates[isolates$species == "Enterococcus faecium", ]The shared hashes ranked by select are of little help
here: most are in every isolate but one. Instead, label each hash by the
isolates it is found in. P007’s isolates fall in two sets, seven
collected in November and one from a stool swab three months later
(vignette("smash-compare") shows why these are two
strains):
long <- smash_hashes(efm, keep = c("host_subject_id", "collection_date"))
long$set <- ifelse(long$host_subject_id == "P011", "P011",
ifelse(long$collection_date < as.Date("2025-01-01"),
"P007 Nov", "P007 Feb"))
# for each hash: the sets of isolates it is found in, and in how many isolates
found_in <- tapply(long$set, long$hash,
function(x) paste(sort(unique(x)), collapse = " + "))
n_isolates <- table(long$hash)[names(found_in)]
table(found_in)
#> found_in
#> P007 Feb P007 Feb + P007 Nov
#> 285 169
#> P007 Feb + P007 Nov + P011 P007 Feb + P011
#> 2324 226
#> P007 Nov P007 Nov + P011
#> 1008 298
#> P011
#> 805The largest group, more than 2,000 hashes, is in all three sets of isolates, mostly the species’ core genome. Hundreds are in one set only. Choose 15 of each kind, those in the most isolates:
hashes <- data.frame(hash = names(found_in), group = as.vector(found_in),
n = as.vector(n_isolates))
hashes$group[hashes$group == "P007 Feb + P007 Nov + P011"] <- "All"
groups <- c("All", "P007 Nov", "P007 Feb", "P011")
hashes <- hashes[hashes$group %in% groups, ]
hashes <- hashes[order(-hashes$n, hashes$hash), ]
chosen <- do.call(rbind, lapply(groups, function(g)
head(hashes[hashes$group == g, ], 15)))
chosen$group <- factor(chosen$group, groups)
smash_heatmap(efm, subject = "host_subject_id", time = "collection_date",
site = "sample_source", hashes = chosen[, c("hash", "group")],
top_n = Inf, show_hash_labels = FALSE)
The hashes found only in P007’s November isolates are in all seven of
them: the isolate collected first (Bacterial Cell Iso), the
isolates from the subject’s throat, perirectal and stool swabs, and
those from three environmental swabs over the following two weeks. The
February stool isolate lacks them, and has hashes of its own; P011’s
isolates share a set of their own. (top_n = Inf shows every
chosen hash; by default only 50 are.)
Options
-
min_abundancedrops hashes seen fewer times than this in sketches with abundances, before choosing them (seevignette("smash-aggregate")on sequencing errors). -
cluster = "none"keeps hashes in rank order;"samples"or"both"also clusters the samples within each subject. -
top_n = Infshows every chosen hash, which can be slow to draw. -
palettetakes any viridis option.
The result is a ggplot, with the plotted values in its
data element, so it can be changed with + and
saved with ggsave():
p <- smash_heatmap(sketches, subject = "host_subject_id",
time = "collection_date", site = "sample_source")
head(p$data)
ggplot2::ggsave("figs/hash_heatmap_k51.pdf", p, width = 10, height = 7)To compare whole sketches, rather than single hashes, see
vignette("smash-compare").