Compare samples
Once you have two or more .snipesig signatures, you can combine and compare them with set operations: keep what they share, pool them together, or subtract one from another. This guide walks the four operations — intersect, union, sum, and difference — from both the snipe CLI and the import snipe Python API.
You will learn:
- What each set operation means for edgemers, not just plain k-mers.
- The two CLI shapes:
intersect(sample vs. reference) and the repeatable-i/--inputoperations. - The matching Python operators (
&,|,+,-) and methods (sig.intersect/union/sum/difference). - How to measure similarity with
jaccardandcontainment.
The four operations at a glance
Section titled “The four operations at a glance”Each operation has a CLI subcommand, a Python operator, and a Python method. They fall into two families:
- Additive ops (
union,sum) combine signatures and require both operands to share the same backing — both edgemer, or both K1-only. - Filter ops (
intersect,difference) subset the left signature; the result follows the left operand’s backing.
| Operation | CLI | Python operator | Python method | What it keeps |
|---|---|---|---|---|
| Intersect | snipe intersect |
a & b |
a.intersect(b) |
Edgemers present in both |
| Union | snipe union |
a | b |
a.union(b) |
Every edgemer, max abundance (max-merge) |
| Sum | snipe sum |
a + b |
a.sum(b) |
Every edgemer, abundances added |
| Difference | snipe difference |
a - b |
a.difference(b) |
Left minus right |
The union vs. sum distinction matters for abundance: union is a max-merge (each edgemer keeps the larger of the two abundances), while sum adds the abundances — pooling depth across signatures.
-
Intersect: what two signatures share
Section titled “Intersect: what two signatures share”snipe intersectcompares a sample against a reference and keeps only the edgemers present in both. Unlike the other set operations, it takes exactly two named inputs —--sampleand--reference— not a repeatable list:Terminal window snipe intersect --sample sampleA.snipesig --reference sampleB.snipesig -o shared.snipesigThe output
shared.snipesigis the shared content. Because intersect is a filter op, the result follows the left (sample) signature’s backing: intersecting an edgemer sample keeps edgemer structure.In Python, use the
&operator or theintersectmethod:import snipea = snipe.load("sampleA.snipesig")b = snipe.load("sampleB.snipesig")shared = a & b # operator formshared = a.intersect(b) # method form — identical resultshared.save("shared.snipesig")Pass
inplace=Truetoa.intersect(b, inplace=True)to mutateaand returnNoneinstead of allocating a new signature. -
Union and sum: combine many signatures
Section titled “Union and sum: combine many signatures”union,sum, anddifferenceall use the repeatable-i/--inputflag and need at least two inputs. Repeat the flag once per signature:Terminal window snipe union -i run1.snipesig -i run2.snipesig -i run3.snipesig -o merged.snipesigunionis a max-merge: the merged signature carries every edgemer seen in any input, each at its maximum observed abundance. To add abundances instead — pooling coverage across technical replicates of the same sample — usesum:Terminal window snipe sum -i rep1.snipesig -i rep2.snipesig -o pooled.snipesigBoth are additive ops, so every input must share the same backing. In Python, the operators fold naturally, and there are module-level helpers for a list:
import snipesigs = snipe.load_many("runs/*.snipesig")merged = snipe.union(sigs) # left-fold | over the listpooled = snipe.sum(sigs) # left-fold + over the list# equivalently, two at a time:merged = sigs[0] | sigs[1]pooled = sigs[0] + sigs[1]snipe.unionandsnipe.sumleft-fold|/+across the list — the same semantics as the CLI, where the first input is the base. -
Difference: subtract one signature from another
Section titled “Difference: subtract one signature from another”snipe differencecomputes the first input minus the rest — the edgemers unique to the first signature after removing everything in the others:Terminal window snipe difference -i sampleA.snipesig -i contaminant.snipesig -o cleaned.snipesigBy default, difference removes an edgemer entirely when it appears in a later input. The
--weightedflag changes this to subtract abundances instead of removing edgemers completely — useful when you want to deplete depth rather than drop content outright:Terminal window snipe difference --weighted -i sampleA.snipesig -i background.snipesig -o depleted.snipesigDifference is a filter op, so the result follows the first (left) signature’s backing. In Python:
import snipea = snipe.load("sampleA.snipesig")b = snipe.load("contaminant.snipesig")cleaned = a - b # operator formcleaned = a.difference(b) # method form# fold over a whole list (first minus the rest):cleaned = snipe.difference([a, b, c]) -
Measure similarity without writing a file
Section titled “Measure similarity without writing a file”Set operations produce new signatures. When you only want a number describing how alike two samples are, the Python API exposes two scalar comparisons directly on a signature:
import snipea = snipe.load("sampleA.snipesig")b = snipe.load("sampleB.snipesig")a.jaccard(b) # |A ∩ B| / |A ∪ B| — symmetric overlapa.containment(b) # |A ∩ B| / |A| — how much of A is in BBoth operate over the K1-hash sets.
jaccardis symmetric;containmentis directional — how much ofais contained inb. Two empty signatures give0.0.
- Intersect (
&,snipe intersect --sample/--reference) keeps shared edgemers — a filter op that follows the left backing. - Union (
|,snipe union -i …) max-merges; sum (+,snipe sum -i …) adds abundances. Both are additive and need matching backings and at least two inputs. - Difference (
-,snipe difference -i …) is first-minus-the-rest;--weightedsubtracts abundances instead of dropping edgemers. - For a similarity score rather than a new signature, use
sig.jaccardorsig.containment.
- Edgemers → — the k-mer-pair idea these operations preserve.
snipe intersectreference → — every flag and default, generated from the CLI.- Python API reference → — the full
Signaturesurface, includinginplaceand backing rules. - QC a sequencing run → — turn a compared signature into quality metrics.