Skip to contents

Compares every pair of sketches from smash_aggregate(), computing the same similarities as sourmash compare, in R, from the hashes. sourmash itself is not needed; smash_read_compare() reads the output of sourmash compare instead, if you have run it.

Usage

smash_compare(data, sample = "sample_id", keep = NULL)

Arguments

data

A data frame from smash_aggregate(), with one sketch per sample.

sample

Column identifying the sample. Defaults to "sample_id".

keep

Columns of data describing the samples to carry into the result, each twice, with suffixes _1 and _2 (e.g. host_subject_id_1, host_subject_id_2). Defaults to sample_type, species, sample_source, and host_subject_id and collection_date where present.

Value

A tibble with one row per ordered pair of samples (both sample_1–sample_2 and sample_2–sample_1, but not a sample with itself), with columns sample_1, sample_2, n_hashes_1, n_hashes_2, n_shared, the similarities above, and the keep columns. The sketch parameters are in its "sketch" attribute.

Details

For two sketches \(A\) and \(B\) (sets of hashes), with \(|A \cap B|\) hashes in common (n_shared):

jaccard

\(|A \cap B| / |A \cup B|\), the Jaccard similarity (sourmash compare --ignore-abundance). Symmetric.

containment

\(|A \cap B| / |A|\): the fraction of sample_1's hashes found in sample_2 (how much of sample_1 is contained in sample_2). Directional: an isolate is contained in a metagenome it came from, but not the reverse. sourmash compare --containment reports the same values, with sample_2 as the row and sample_1 as the column.

max_containment, avg_containment

the larger and the mean of the two containments (--max-containment, --avg-containment). Symmetric.

angular_similarity

for sketches with abundances, \(1 - 2 \theta / \pi\), where \(\theta\) is the angle between the two sketches' abundance vectors: what sourmash compare reports for sketches with abundances. NA otherwise.

jaccard_ani, containment_ani, max_containment_ani, avg_containment_ani

average nucleotide identity (ANI) estimated from the Jaccard similarity, \((2J / (1 + J))^{1/k}\), or from a containment \(C\), \(C^{1/k}\), as sourmash compare --ani estimates them (point estimates; 0 when no hashes are shared). Use containment ANI for isolates of different genome sizes or for incomplete genomes, and for an isolate against a metagenome.

All sketches must have the same k, seed and molecule, and one sketch per sample (filter smash_aggregate()'s result first). Sketches at different scaled values are downsampled to the largest, as sourmash does. Hashes in common are counted with a sparse matrix, so thousands of isolate sketches can be compared at once; large metagenome sketches are slower, and benefit from min_abundance and downsample in smash_aggregate().

See also

smash_compare_plot() to plot it, smash_compare_matrix() for a samples x samples matrix, and smash_read_compare() to read sourmash compare output instead.

Examples

sketches <- smash_aggregate(smash_example("signatures"),
                            metadata = smash_example("runs"),
                            k = 31, min_abundance = 2)
cmp <- smash_compare(sketches)
cmp
#> # A tibble: 306 × 24
#>    sample_1    sample_2    n_hashes_1 n_hashes_2 n_shared  jaccard containment
#>    <chr>       <chr>            <int>      <int>    <dbl>    <dbl>       <dbl>
#>  1 SRR41520862 SRR41520863      15752      10090     1423 0.0583      0.0903  
#>  2 SRR41520862 SRR41520865      15752      13620     2077 0.0761      0.132   
#>  3 SRR41520862 SRR41520867      15752      14592     1579 0.0549      0.100   
#>  4 SRR41520862 SRR41520868      15752      12638     5720 0.252       0.363   
#>  5 SRR41520862 SRR41520869      15752      12422     1260 0.0468      0.0800  
#>  6 SRR41520862 SRR41520870      15752       4485      553 0.0281      0.0351  
#>  7 SRR41520862 SRR41520873      15752       1806       10 0.000570    0.000635
#>  8 SRR41520862 SRR41520874      15752       1579       28 0.00162     0.00178 
#>  9 SRR41520862 SRR41520876      15752      15637     1575 0.0528      0.1000  
#> 10 SRR41520862 SRR41520878      15752       7538     1129 0.0509      0.0717  
#> # ℹ 296 more rows
#> # ℹ 17 more variables: max_containment <dbl>, avg_containment <dbl>,
#> #   angular_similarity <dbl>, jaccard_ani <dbl>, containment_ani <dbl>,
#> #   max_containment_ani <dbl>, avg_containment_ani <dbl>, sample_type_1 <chr>,
#> #   sample_type_2 <chr>, species_1 <chr>, species_2 <chr>,
#> #   sample_source_1 <chr>, sample_source_2 <chr>, host_subject_id_1 <chr>,
#> #   host_subject_id_2 <chr>, collection_date_1 <date>, …

# the most similar pairs of samples from different subjects
different <- cmp[cmp$host_subject_id_1 != cmp$host_subject_id_2, ]
head(different[order(-different$jaccard),
               c("sample_1", "sample_2", "jaccard", "angular_similarity")])
#> # A tibble: 6 × 4
#>   sample_1    sample_2    jaccard angular_similarity
#>   <chr>       <chr>         <dbl>              <dbl>
#> 1 SRR41520873 SRR41520919   0.173              0.811
#> 2 SRR41520919 SRR41520873   0.173              0.811
#> 3 SRR41520874 SRR41520897   0.138              0.788
#> 4 SRR41520897 SRR41520874   0.138              0.788
#> 5 SRR41520897 SRR41520919   0.135              0.715
#> 6 SRR41520919 SRR41520897   0.135              0.715