Reads sourmash signatures (one .sig file per sample and setting, as
written by sra2lab::smash_runs(), smash_stream() and smash_local(),
or any other sourmash signature files) into one tibble with a row per
sketch. Each row holds the sketch's parameters, its full list of hashes
(and their abundances), what was sketched (a cultured isolate or a
metagenome, the sample's source and, for an isolate, its species) and the
sample's metadata.
Usage
smash_aggregate(
signatures,
metadata = NULL,
metadata_id = "run_accession",
k = NULL,
scaled = NULL,
seed = NULL,
method = NULL,
min_abundance = 1,
downsample = NULL,
sample_type = NULL,
species = "organism",
source = "isolation_source",
sample_pattern = NULL,
pattern = "\\.(sig|sig\\.gz|zip)$"
)Arguments
- signatures
The sourmash signatures: a directory (all files in it matching
patternare read), or a character vector of signature files.- metadata
Optional sample metadata: a data frame, or the path to a CSV or TSV file, with one row per sample. The run table from
sra2lab::get_bioproject_runs()works directly, for isolate (Microbe) and metagenome BioProjects alike.- metadata_id
Column of
metadataholding the sample IDs that matchsample_id. Defaults to"run_accession", as in an sra2lab run table; forsra2lab::smash_local()signatures, use the specimen ID column.- k, scaled, seed
Optional k-mer size(s), scaled value(s) and seed(s) to keep, for signatures made at several settings.
NULL(the default) keeps all.- method
Optional
"abund"or"noabund", to keep only sketches with or without k-mer abundances.NULLkeeps both.- min_abundance
Drop hashes seen fewer than this many times, in sketches with abundances. Defaults to 1 (keep all). See Details.
- downsample
Optional scaled value to downsample every sketch to. It must be at least each sketch's own
scaled.- sample_type
NULL(the default) to work out whether each sample is an isolate or a metagenome from the metadata; a column ofmetadatasaying so; or"isolate"or"metagenome"for all samples. See Details.- species
Column of
metadatawith an isolate's species. Defaults to"organism".- source
Column of
metadatawith the sample's source. Defaults to"isolation_source".- sample_pattern
Optional regular expression (Perl-compatible) that extracts the sample ID from each signature's file path. Files where it does not match get
sample_idNA, with a warning.- pattern
Regular expression for the signature files when
signaturesis a directory. Defaults to files ending in.sig,.sig.gzor.zip.
Value
A tibble with one row per sketch, holding sample_id,
sample_type, species, sample_source, the sketch parameters k,
scaled, seed, method and molecule, n_hashes (after
min_abundance and downsample), n_hashes_sketch (before), est_bp
(n_hashes x scaled, an estimate of the number of distinct k-mers,
which for an isolate approximates its genome size), the list columns
hashes and abundances, the signature's md5sum, name and
filename (the sequence file it was made from), sig_file, and the
metadata columns.
Details
The steps are:
Read. Every signature file is read in R (no sourmash needed):
.sigand.sig.gzJSON files, and sourmash.zipcollections. A file can hold several sketches (e.g. k = 21, 31 and 51); each becomes a row. Only FracMinHash (scaled) sketches are read;numsketches are skipped with a warning.Sketch parameters.
k(the k-mer size),scaled,seed,molecule("DNA", or a protein alphabet),method("abund"if the sketch tracks k-mer abundance, otherwise"noabund") andmd5sumare taken from the sketch itself, not from the file name.Hashes.
hashesis a list column holding each sketch's hashes (the hashed k-mers it kept), as decimal text: they are unsigned 64-bit integers, too large for R's numbers to hold exactly.abundancesholds the matching k-mer counts for"abund"sketches (NULLfor"noabund"). Usesmash_hashes()for one row per sketch and hash, e.g. to assign hashes to strains (at k = 51) or species (at k = 31) later, andsmash_compare()to compare sketches.Low-abundance k-mers. Sketches of sequencing reads (rather than assemblies) include k-mers made by sequencing errors, which are seen only once.
min_abundance = 2drops hashes seen fewer than 2 times (in"abund"sketches), which removes most of them; it makes isolates from the same strain look much more alike.n_hashes_sketchkeeps the number of hashes before this filter.Downsampling.
downsamplekeeps only the hashes a sketch at a largerscaledwould have kept, assourmash sig downsampledoes, e.g. to compare sketches made at differentscaledvalues, or to make large metagenome sketches smaller.Sample IDs. For files named as sra2lab names them,
<id>_k<k>_scale<scaled>_seed<seed>_<method>.sig,sample_idis<id>(the run accession, or the specimen ID for local reads). Otherwise it is the signature's name (set bysourmash sketch --name), or failing that the file name without its extension. Givesample_pattern, a regular expression, to extract it from the file path instead.Metadata. With
metadata, sample metadata is left-joined bysample_id=metadata[[metadata_id]]. Samples without a metadata row are kept (withNAs) and reported with a warning.What was sketched. Three columns describe the sample:
sample_type"isolate"(a cultured microbe) or"metagenome". Withsample_type = NULL(the default) it is worked out from the metadata: the BioSample package (BioSampleModelin an sra2lab run table:"Microbe, viral or environmental"for an isolate,"Metagenome or environmental"for a metagenome), elselibrary_source("GENOMIC"or"METAGENOMIC"), else anorganismending in "metagenome". Give a column name to use your own, or"isolate"or"metagenome"for all samples.speciesfor isolates, the organism (the
speciescolumn,"organism"by default, which for a Microbe BioSample is the isolate's species, e.g. "Klebsiella pneumoniae");NAfor metagenomes, whoseorganismis a metagenome type such as "human gut metagenome".sample_sourcethe sample's source (the
sourcecolumn,"isolation_source"by default): the specimen a metagenome was sequenced from, or the specimen an isolate was cultured from.
See also
smash_hashes() for one row per hash, smash_compare() to
compare sketches, and smash_timeline() and smash_heatmap(), which
plot the result.
Examples
sketches <- smash_aggregate(smash_example("signatures"),
metadata = smash_example("runs"))
#> 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 × 48
#> sample_id sample_type species sample_source k scaled seed method
#> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <chr>
#> 1 SRR41520862 metagenome NA Stool Swab 31 10000 42 abund
#> 2 SRR41520862 metagenome NA Stool Swab 51 10000 42 abund
#> 3 SRR41520863 metagenome NA Stool Swab 31 10000 42 abund
#> 4 SRR41520863 metagenome NA Stool Swab 51 10000 42 abund
#> 5 SRR41520865 metagenome NA Perirectal Swab 31 10000 42 abund
#> 6 SRR41520865 metagenome NA Perirectal Swab 51 10000 42 abund
#> 7 SRR41520867 metagenome NA Stool Swab 31 10000 42 abund
#> 8 SRR41520867 metagenome NA Stool Swab 51 10000 42 abund
#> 9 SRR41520868 metagenome NA Stool Swab 31 10000 42 abund
#> 10 SRR41520868 metagenome NA Stool Swab 51 10000 42 abund
#> # ℹ 26 more rows
#> # ℹ 40 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>, run_alias <chr>, spots <dbl>, bases <dbl>,
#> # size <dbl>, published <dttm>, experiment_accession <chr>,
#> # experiment_title <chr>, library_name <chr>, library_strategy <chr>,
#> # library_source <chr>, library_selection <chr>, library_layout <chr>, …
# one sketch setting, without k-mers seen only once
sketches <- smash_aggregate(smash_example("signatures"),
metadata = smash_example("runs"),
k = 51, min_abundance = 2)
sketches[, c("sample_id", "sample_type", "sample_source", "k",
"n_hashes_sketch", "n_hashes")]
#> # A tibble: 18 × 6
#> sample_id sample_type sample_source k n_hashes_sketch n_hashes
#> <chr> <chr> <chr> <dbl> <int> <int>
#> 1 SRR41520862 metagenome Stool Swab 51 14045 14045
#> 2 SRR41520863 metagenome Stool Swab 51 9248 9248
#> 3 SRR41520865 metagenome Perirectal Swab 51 12301 12301
#> 4 SRR41520867 metagenome Stool Swab 51 13111 13111
#> 5 SRR41520868 metagenome Stool Swab 51 11214 11214
#> 6 SRR41520869 metagenome Perirectal Swab 51 11235 11235
#> 7 SRR41520870 metagenome Perirectal Swab 51 4020 4020
#> 8 SRR41520873 metagenome Groin Swab 51 1687 1687
#> 9 SRR41520874 metagenome Groin Swab 51 1440 1440
#> 10 SRR41520876 metagenome Stool Swab 51 14266 14266
#> 11 SRR41520878 metagenome Perirectal Swab 51 6659 6659
#> 12 SRR41520879 metagenome Stool Swab 51 9816 9816
#> 13 SRR41520882 metagenome Perirectal Swab 51 8268 8268
#> 14 SRR41520884 metagenome Stool Swab 51 2821 2821
#> 15 SRR41520894 metagenome Stool Swab 51 2410 2410
#> 16 SRR41520897 metagenome Axilla Swab 51 1383 1383
#> 17 SRR41520902 metagenome Stool Swab 51 22544 22544
#> 18 SRR41520919 metagenome Axilla Swab 51 2017 2017
# isolates from a Microbe BioProject: species and source are joined
isolates <- smash_aggregate(smash_example("isolates"),
metadata = smash_example("isolate_runs"),
k = 51)
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
if (FALSE) { # \dontrun{
# local reads, with sample metadata keyed by specimen ID
sketches <- smash_aggregate("data/smash", metadata = "tabs/specimens.csv",
metadata_id = "specimen_id",
sample_type = "isolate", species = "maldi_id")
} # }