Skip to contents

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 pattern are 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 metadata holding the sample IDs that match sample_id. Defaults to "run_accession", as in an sra2lab run table; for sra2lab::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. NULL keeps 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 of metadata saying so; or "isolate" or "metagenome" for all samples. See Details.

species

Column of metadata with an isolate's species. Defaults to "organism".

source

Column of metadata with 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_id NA, with a warning.

pattern

Regular expression for the signature files when signatures is a directory. Defaults to files ending in .sig, .sig.gz or .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:

  1. Read. Every signature file is read in R (no sourmash needed): .sig and .sig.gz JSON files, and sourmash .zip collections. A file can hold several sketches (e.g. k = 21, 31 and 51); each becomes a row. Only FracMinHash (scaled) sketches are read; num sketches are skipped with a warning.

  2. 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") and md5sum are taken from the sketch itself, not from the file name.

  3. Hashes. hashes is 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. abundances holds the matching k-mer counts for "abund" sketches (NULL for "noabund"). Use smash_hashes() for one row per sketch and hash, e.g. to assign hashes to strains (at k = 51) or species (at k = 31) later, and smash_compare() to compare sketches.

  4. 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 = 2 drops 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_sketch keeps the number of hashes before this filter.

  5. Downsampling. downsample keeps only the hashes a sketch at a larger scaled would have kept, as sourmash sig downsample does, e.g. to compare sketches made at different scaled values, or to make large metagenome sketches smaller.

  6. Sample IDs. For files named as sra2lab names them, <id>_k<k>_scale<scaled>_seed<seed>_<method>.sig, sample_id is <id> (the run accession, or the specimen ID for local reads). Otherwise it is the signature's name (set by sourmash sketch --name), or failing that the file name without its extension. Give sample_pattern, a regular expression, to extract it from the file path instead.

  7. Metadata. With metadata, sample metadata is left-joined by sample_id = metadata[[metadata_id]]. Samples without a metadata row are kept (with NAs) and reported with a warning.

  8. What was sketched. Three columns describe the sample:

    sample_type

    "isolate" (a cultured microbe) or "metagenome". With sample_type = NULL (the default) it is worked out from the metadata: the BioSample package (BioSampleModel in an sra2lab run table: "Microbe, viral or environmental" for an isolate, "Metagenome or environmental" for a metagenome), else library_source ("GENOMIC" or "METAGENOMIC"), else an organism ending in "metagenome". Give a column name to use your own, or "isolate" or "metagenome" for all samples.

    species

    for isolates, the organism (the species column, "organism" by default, which for a Microbe BioSample is the isolate's species, e.g. "Klebsiella pneumoniae"); NA for metagenomes, whose organism is a metagenome type such as "human gut metagenome".

    sample_source

    the sample's source (the source column, "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")
} # }