Skip to contents

A sourmash signature is a sketch of a genome or metagenome: a sample of the hashed k-mers in its sequence, kept so that sketches of different samples can be compared quickly. sra2lab writes one signature per sample and sketch setting. smash_aggregate() reads all of a project’s signatures into one tibble, with one row per sketch, and adds what the signatures do not know: what was sketched (a cultured isolate, with its species, or a metagenome), where it came from, and the rest of the sample’s metadata. Every other smash2lab function starts from that tibble.

Taxonomic or functional profiles (sylph2lab, funprofiler2lab) say what is in a sample. A sketch does not: its value is in comparison with other sketches, so smash2lab keeps every hash and the parameters needed to compare them.

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.

Read the signatures

Point smash_aggregate() at the directory of signatures. It reads every .sig, .sig.gz and .zip file in it, in R:

library(smash2lab)

sig_dir <- smash_example("signatures")   # in a real analysis: "data/smash"
head(list.files(sig_dir))
#> [1] "SRR41520862_k31_scale10000_seed42_abund.sig.gz"
#> [2] "SRR41520862_k51_scale10000_seed42_abund.sig.gz"
#> [3] "SRR41520863_k31_scale10000_seed42_abund.sig.gz"
#> [4] "SRR41520863_k51_scale10000_seed42_abund.sig.gz"
#> [5] "SRR41520865_k31_scale10000_seed42_abund.sig.gz"
#> [6] "SRR41520865_k51_scale10000_seed42_abund.sig.gz"

sketches <- smash_aggregate(sig_dir)
#> The signatures hold sketches at more than one setting (k = 31, scaled = 10000, seed = 42, abund; k = 51, scaled = 10000, seed = 42, abund). Choose one with `k`, `scaled`, `seed` and `method` before comparing or plotting.
sketches
#> # A tibble: 36 × 18
#>    sample_id   sample_type species sample_source     k scaled  seed method
#>    <chr>       <chr>       <chr>   <chr>         <dbl>  <dbl> <dbl> <chr> 
#>  1 SRR41520862 NA          NA      NA               31  10000    42 abund 
#>  2 SRR41520862 NA          NA      NA               51  10000    42 abund 
#>  3 SRR41520863 NA          NA      NA               31  10000    42 abund 
#>  4 SRR41520863 NA          NA      NA               51  10000    42 abund 
#>  5 SRR41520865 NA          NA      NA               31  10000    42 abund 
#>  6 SRR41520865 NA          NA      NA               51  10000    42 abund 
#>  7 SRR41520867 NA          NA      NA               31  10000    42 abund 
#>  8 SRR41520867 NA          NA      NA               51  10000    42 abund 
#>  9 SRR41520868 NA          NA      NA               31  10000    42 abund 
#> 10 SRR41520868 NA          NA      NA               51  10000    42 abund 
#> # ℹ 26 more rows
#> # ℹ 10 more variables: molecule <chr>, n_hashes <int>, n_hashes_sketch <int>,
#> #   est_bp <dbl>, hashes <list>, abundances <list>, md5sum <chr>, name <chr>,
#> #   filename <chr>, sig_file <chr>

Each row is one sketch. The example runs were sketched at two k-mer sizes, so each run has two rows, and smash_aggregate() says so: compare and plot sketches of one setting at a time (see below).

Column Meaning
sample_id the sample, from the file name (the run accession for sra2lab::smash_runs()), else the signature’s name
sample_type, species, sample_source what was sketched; see Join sample metadata
k, scaled, seed the k-mer size, the scaled value (about 1 in scaled k-mers is kept) and the hash seed, read from the sketch itself
method "abund" if the sketch counts how often each k-mer was seen, "noabund" if not
molecule "DNA" for DNA sketches (or a protein alphabet)
n_hashes the number of hashes in the sketch (after min_abundance and downsample)
n_hashes_sketch the number in the signature file, before those filters
est_bp n_hashes x scaled: an estimate of the number of distinct k-mers sketched; for an isolate’s genome, about its length
hashes, abundances list columns: the sketch’s hashes and their k-mer counts
md5sum, name, filename, sig_file the signature’s checksum, name, the sequence file it was made from, and the signature file

The full hashes

hashes holds every hash in every sketch. They are 64-bit unsigned integers, larger than R’s numbers can hold exactly, so they are kept as text, digit for digit as in the signature file:

sketches$hashes[[1]][1:5]
#> [1] "7504968134"   "432562212700" "453066401984" "461023125421" "463641985922"

smash_hashes() unnests them into one row per sketch and hash, which is the form to join to other tables of hashes, for example to label the hashes that belong to a species (at k = 31) or a strain (at k = 51) once they have been assigned from reference genomes or isolates:

hashes <- smash_hashes(sketches[sketches$k == 51, ])
hashes
#> # A tibble: 148,485 × 7
#>    sample_id       k scaled  seed method hash          abundance
#>    <chr>       <dbl>  <dbl> <dbl> <chr>  <chr>             <dbl>
#>  1 SRR41520862    51  10000    42 abund  196397122172         11
#>  2 SRR41520862    51  10000    42 abund  334189847742          2
#>  3 SRR41520862    51  10000    42 abund  975247846319          2
#>  4 SRR41520862    51  10000    42 abund  975620253002          3
#>  5 SRR41520862    51  10000    42 abund  1201699242587        14
#>  6 SRR41520862    51  10000    42 abund  1293671068588         2
#>  7 SRR41520862    51  10000    42 abund  1392706356360         7
#>  8 SRR41520862    51  10000    42 abund  1459017142733         2
#>  9 SRR41520862    51  10000    42 abund  1506483291264         2
#> 10 SRR41520862    51  10000    42 abund  1599676576985        25
#> # ℹ 148,475 more rows

keep adds columns of the sketches, such as the subject or source once metadata has been joined (below), e.g. keep = "host_subject_id".

vignette("smash-heatmap") uses this to follow hashes across samples.

Choose a sketch setting

k, scaled, seed and method keep only matching sketches:

k31 <- smash_aggregate(sig_dir, k = 31)
table(k31$k)
#> 
#> 31 
#> 18

Which k-mer size? Two genomes of the same species typically share about 95% average nucleotide identity (ANI) or more, and k = 31 measures similarity well at that level; isolates of the same strain share 99.5% ANI or more, which k = 51 resolves better, because a longer k-mer is more likely to span a difference. Smaller scaled values (e.g. 100 rather than 1000) keep more hashes, making similarity estimates more precise at the cost of larger sketches; to resolve strains, sketch isolates with a small scaled (see vignette("smash-compare")).

Drop k-mers from sequencing errors

A sketch of sequencing reads (rather than of an assembled genome) holds the k-mers of sequencing errors too. An error makes up to k new k-mers that are almost never seen twice, so most hashes seen only once are noise: in deeply sequenced isolates, many times more than the genome’s real k-mers. They make every sample look unique, and two isolates of the same strain look much less alike than they are. For sketches with abundances, min_abundance = 2 drops them:

filtered <- smash_aggregate(sig_dir, k = 31, min_abundance = 3)
summary(filtered$n_hashes / filtered$n_hashes_sketch)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  0.4638  0.6276  0.6944  0.6728  0.7334  0.7768

(The example sketches already had k-mers seen only once removed, so this removes those seen twice.) The effect is largest for isolates, sequenced deeply enough that each real k-mer of the genome is seen tens to hundreds of times. Before filtering, the sketches of the isolate example data estimated 25 to 55 Mb of sequence for genomes of 3 to 7 Mb; with min_abundance = 2 that fell to 5 to 11 Mb, and with min_abundance = 5 to 2.9 to 7.3 Mb, the genomes themselves, keeping only 6% to 16% of the hashes. (The isolate sketches shipped with smash2lab were filtered this way, at 5, before they were downsampled.) For isolate reads at moderate depth, min_abundance = 2 to 5 works well; check est_bp against the species’ genome size. In metagenomes, a rare organism’s real k-mers can also be seen only once, so filtering trades sensitivity to rare organisms for less noise. Sketches made without abundances ("noabund") cannot be filtered this way; sketch with abundances (the default in sra2lab) to keep the choice.

Downsample

downsample keeps only the hashes a sketch made at a larger scaled would have kept, exactly as sourmash sig downsample does. Use it to compare sketches made at different scaled values, or to make large metagenome sketches smaller and faster to compare:

small <- smash_aggregate(sig_dir, k = 31, downsample = 50000)
data.frame(sample_id = small$sample_id[1:4],
           scaled_10000 = k31$n_hashes[1:4],
           scaled_50000 = small$n_hashes[1:4])
#>     sample_id scaled_10000 scaled_50000
#> 1 SRR41520862        15752         3137
#> 2 SRR41520863        10090         2014
#> 3 SRR41520865        13620         2700
#> 4 SRR41520867        14592         2853

Join sample metadata

Give the run table (or its path) as metadata. It is joined by matching sample_id to the table’s run_accession column:

sketches <- smash_aggregate(
  sig_dir,
  metadata = smash_example("runs"),   # "data/PRJNA1556220_runs.csv"
  k = 31
)

sketches[, c("sample_id", "sample_type", "species", "sample_source",
             "host_subject_id", "collection_date")]
#> # A tibble: 18 × 6
#>    sample_id   sample_type species sample_source host_subject_id collection_date
#>    <chr>       <chr>       <chr>   <chr>         <chr>           <date>         
#>  1 SRR41520862 metagenome  NA      Stool Swab    GOL_019         2026-04-16     
#>  2 SRR41520863 metagenome  NA      Stool Swab    GOL_017         2026-04-03     
#>  3 SRR41520865 metagenome  NA      Perirectal S… GOL_017         2026-03-24     
#>  4 SRR41520867 metagenome  NA      Stool Swab    GOL_015         2026-05-12     
#>  5 SRR41520868 metagenome  NA      Stool Swab    GOL_019         2026-05-09     
#>  6 SRR41520869 metagenome  NA      Perirectal S… GOL_016         2026-04-09     
#>  7 SRR41520870 metagenome  NA      Perirectal S… GOL_016         2026-04-03     
#>  8 SRR41520873 metagenome  NA      Groin Swab    GOL_017         2026-03-24     
#>  9 SRR41520874 metagenome  NA      Groin Swab    GOL_034         2026-08-18     
#> 10 SRR41520876 metagenome  NA      Stool Swab    GOL_015         2026-05-17     
#> 11 SRR41520878 metagenome  NA      Perirectal S… GOL_017         2026-04-03     
#> 12 SRR41520879 metagenome  NA      Stool Swab    GOL_017         2026-03-27     
#> 13 SRR41520882 metagenome  NA      Perirectal S… GOL_017         2026-03-27     
#> 14 SRR41520884 metagenome  NA      Stool Swab    GOL_003         2025-09-16     
#> 15 SRR41520894 metagenome  NA      Stool Swab    GOL_018         2026-03-02     
#> 16 SRR41520897 metagenome  NA      Axilla Swab   GOL_015         2026-05-11     
#> 17 SRR41520902 metagenome  NA      Stool Swab    GOL_003         2025-09-09     
#> 18 SRR41520919 metagenome  NA      Axilla Swab   GOL_016         2026-03-30

Three columns describe what was sketched:

  • sample_type is "isolate" or "metagenome". It is worked out from the metadata: the BioSample package (BioSampleModel: "Microbe, viral or environmental" or "Metagenome or environmental"), else the run’s library_source ("GENOMIC" or "METAGENOMIC"), else an organism ending in “metagenome”. Give sample_type = "isolate" or "metagenome" to set it for all samples, or the name of a column of your own.
  • species is the isolate’s species: the organism of a Microbe BioSample (or another column, with species = "maldi_id", say). It is NA for metagenomes, whose organism is a metagenome type.
  • sample_source is the sample’s source, from isolation_source (or source = "..."): the specimen a metagenome was sequenced from, or the specimen an isolate was cultured from.

Here every sample is a metagenome. For an isolate BioProject, the same call gives each isolate’s species. A made-up example, with a metagenome and two isolates, as a run table that mixed the two BioSample packages would describe them:

meta <- data.frame(
  run_accession = c("SRR41520865", "SRR41520873", "SRR41520882"),
  BioSampleModel = c("Metagenome or environmental", "Microbe", "Microbe"),
  organism = c("human gut metagenome", "Klebsiella pneumoniae",
               "Escherichia coli"),
  isolation_source = c("Perirectal Swab", "Groin Swab", "Perirectal Swab")
)
mixed <- suppressWarnings(smash_aggregate(
  smash_example("signatures"), metadata = meta, k = 31
))
mixed[mixed$sample_id %in% meta$run_accession,
      c("sample_id", "sample_type", "species", "sample_source")]
#> # A tibble: 3 × 4
#>   sample_id   sample_type species               sample_source  
#>   <chr>       <chr>       <chr>                 <chr>          
#> 1 SRR41520865 metagenome  NA                    Perirectal Swab
#> 2 SRR41520873 isolate     Klebsiella pneumoniae Groin Swab     
#> 3 SRR41520882 isolate     Escherichia coli      Perirectal Swab

(These runs are really metagenomes; only the metadata is made up. The other example runs have no row in meta, hence the warning, suppressed here.)

An isolate BioProject

The isolate example data come from a real Microbe BioProject. Its run table, from sra2lab::get_bioproject_runs(), is used as it is:

isolates <- smash_aggregate(
  smash_example("isolates"),              # in a real analysis: "data/smash"
  metadata = smash_example("isolate_runs"),  # "data/PRJNA1570337_runs.csv"
  k = 51
)
isolates[, c("sample_id", "sample_type", "species", "sample_source",
             "host_subject_id", "collection_date")]
#> # A tibble: 30 × 6
#>    sample_id   sample_type species sample_source host_subject_id collection_date
#>    <chr>       <chr>       <chr>   <chr>         <chr>           <date>         
#>  1 SRR42080112 isolate     Pseudo… environmenta… P007            2024-11-08     
#>  2 SRR42080113 isolate     Pseudo… Stool Swab    P007            2025-02-17     
#>  3 SRR42080114 isolate     Entero… Stool Swab    P011            2025-03-10     
#>  4 SRR42080127 isolate     Klebsi… environmenta… P005            2024-10-20     
#>  5 SRR42080143 isolate     Pseudo… environmenta… P008            2024-12-08     
#>  6 SRR42080165 isolate     Staphy… environmenta… P027            2025-10-14     
#>  7 SRR42080179 isolate     Escher… Stool Swab    P005            2024-10-18     
#>  8 SRR42080180 isolate     Staphy… throat swab   P008            2024-12-06     
#>  9 SRR42080197 isolate     Entero… environmenta… P011            2025-02-28     
#> 10 SRR42080207 isolate     Entero… Bacterial Ce… P011            2025-02-20     
#> # ℹ 20 more rows
table(isolates$species, isolates$host_subject_id)
#>                         
#>                          P005 P007 P008 P011 P027
#>   Enterococcus faecium      0    8    0    5    0
#>   Escherichia coli          1    0    0    0    0
#>   Klebsiella aerogenes      4    0    0    0    0
#>   Pseudomonas aeruginosa    0    3    1    0    0
#>   Staphylococcus aureus     0    0    4    0    4

Every sketch is an "isolate" (the BioSample package is Microbe), with its species from organism and its source from isolation_source. This run table also has a column of its own named sample_type (an SRA attribute, here "missing"); like any metadata column whose name smash2lab uses, it is kept as sample_type_metadata.

For an isolate, est_bp estimates the size of its genome. The median for each species is close to its known genome size (about 3 Mb for Enterococcus faecium and Staphylococcus aureus, 5 Mb for Escherichia coli and Klebsiella aerogenes, 7 Mb for Pseudomonas aeruginosa):

round(tapply(isolates$est_bp, isolates$species, median) / 1e6, 2)
#>   Enterococcus faecium       Escherichia coli   Klebsiella aerogenes 
#>                   3.15                   5.03                   5.48 
#> Pseudomonas aeruginosa  Staphylococcus aureus 
#>                   6.96                   3.03

which shows the sketches are of single genomes, cleaned of sequencing errors (above).

A few more things to know:

  • For sra2lab::smash_local() signatures, or your own metadata, name the ID column with metadata_id, e.g. metadata_id = "specimen_id".
  • The metadata must have one row per sample; duplicated IDs are an error.
  • Samples with no metadata row are kept, with NA metadata, and listed in a warning, so a typo in an ID does not silently drop data.
  • Metadata columns that share a name with a smash2lab column get the suffix _metadata.

Sample IDs from other file names

Signatures named otherwise than by sra2lab get the signature’s name (set with sourmash sketch --name) as their ID, or failing that the file name. To take the ID from elsewhere in the path, pass a regular expression with sample_pattern:

sketches <- smash_aggregate(
  Sys.glob("results/*/sketch.sig.gz"),
  sample_pattern = "(?<=results/)[^/]+"
)

Save the result

The hashes and abundances list columns do not fit a CSV file; save the tibble as an .rds file, and the long table of hashes as CSV if you need it elsewhere (the hashes stay exact, as text):

saveRDS(sketches, "data/smash_sketches_k31.rds")
readr::write_csv(smash_hashes(sketches), "data/smash_hashes_k31.csv.gz")

Next steps