Similarity & Clustering¶
sourmash-based pairwise sequence similarity, hierarchical clustering,
and cluster assignment. Requires the cluster extra
(pip install "dot-explorer[cluster]"). See
Similarity & Clustering for how to choose a metric.
SketchParams
dataclass
¶
Parameters for building sourmash FracMinHash sketches.
Attributes:
| Name | Type | Description |
|---|---|---|
ksize |
int
|
K-mer size (sourmash default for DNA comparisons is 21). |
scaled |
int
|
FracMinHash scaling factor: roughly one hash is kept per scaled bp. Lower values keep more hashes, improving ANI estimates on similar sequences at the cost of memory. |
track_abundance |
bool
|
Record k-mer abundances (sourmash |
Source code in dot_explorer/similarity.py
compute_sketches(index, names=None, *, group=None, params=None)
¶
Sketch indexed sequences with sourmash FracMinHash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
SequenceIndex or CrossIndex
|
Index holding the sequences (anything with |
required |
names
|
sequence of str
|
Sequences to sketch (default: all names in the index/group). |
None
|
group
|
str
|
For a |
None
|
params
|
SketchParams
|
Sketch parameters (k, scaled, abundance tracking); defaults to
|
None
|
Returns:
| Type | Description |
|---|---|
dict of str to sourmash.MinHash
|
One sketch per sequence name, in input order. |
Source code in dot_explorer/similarity.py
SimilarityMatrix
dataclass
¶
A named pairwise similarity matrix in [0, 1].
Attributes:
| Name | Type | Description |
|---|---|---|
names |
list of str
|
Sequence names; row/column order of values. |
values |
ndarray
|
|
metric |
str
|
One of |
params |
SketchParams
|
The sketch parameters the sketches were built with. |
ci_low, ci_high |
ndarray or None
|
95% confidence bounds; only populated for |
Source code in dot_explorer/similarity.py
Methods:¶
__getitem__(key)
¶
Return the similarity for a (name_a, name_b) pair.
reorder(names)
¶
Return a copy with rows/columns permuted to names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
sequence of str
|
Permutation of :attr: |
required |
Returns:
| Type | Description |
|---|---|
SimilarityMatrix
|
Reordered copy (CI arrays permuted alongside). |
Source code in dot_explorer/similarity.py
to_csv(path)
¶
Write the matrix as CSV with names as header row and column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Output file path. |
required |
Source code in dot_explorer/similarity.py
pairwise_similarity(sketches, *, metric='jaccard', ignore_abundance=False)
¶
Compare sketches all-vs-all with a sourmash similarity metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sketches
|
dict of str to sourmash.MinHash
|
Sketches from :func: |
required |
metric
|
str
|
|
'jaccard'
|
ignore_abundance
|
bool
|
For |
False
|
Returns:
| Type | Description |
|---|---|
SimilarityMatrix
|
Matrix with the diagonal set to 1. |
Raises:
| Type | Description |
|---|---|
ValueError
|
On an unknown metric, fewer than 2 sketches, mismatched sketch
parameters, or |
Source code in dot_explorer/similarity.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
linkage_from_similarity(sim, *, method='average')
¶
Hierarchically cluster a similarity matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sim
|
SimilarityMatrix
|
Pairwise similarities; converted to distances as |
required |
method
|
str
|
Linkage method for :func: |
'average'
|
Returns:
| Type | Description |
|---|---|
ndarray
|
scipy linkage matrix; feed to :meth: |
Source code in dot_explorer/similarity.py
ClusterResult
dataclass
¶
Cluster assignments for a set of sequences.
Attributes:
| Name | Type | Description |
|---|---|---|
assignments |
dict of str to str
|
Maps each sequence name to its cluster name ( |
cutoff |
float
|
The similarity cutoff used (identity cutoff in dual mode). |
mode |
str
|
|
metric |
str
|
The similarity metric the clustering was based on. |
reciprocal |
bool or None
|
Dual mode only: whether coverage had to pass in both directions. |
Source code in dot_explorer/similarity.py
Attributes¶
clusters
property
¶
Return cluster name → member sequence names.
Methods:¶
to_csv(path)
¶
Write contig,cluster rows (with header) to path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Output file path. |
required |
Source code in dot_explorer/similarity.py
assign_clusters(sim, cutoff, *, linkage=None, method='average')
¶
Assign sequences to clusters at a similarity cutoff.
Cuts the hierarchical clustering tree at distance 1 - cutoff, so
members of a cluster have (linkage-aggregated) similarity of at
least cutoff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sim
|
SimilarityMatrix
|
Pairwise similarities. |
required |
cutoff
|
float
|
Similarity threshold in |
required |
linkage
|
ndarray
|
Precomputed linkage from :func: |
None
|
method
|
str
|
Linkage method when linkage is omitted. |
'average'
|
Returns:
| Type | Description |
|---|---|
ClusterResult
|
Assignments keyed by sequence name. |
Source code in dot_explorer/similarity.py
assign_clusters_dual(identity, coverage, *, identity_cutoff=0.8, coverage_cutoff=0.8, reciprocal=True)
¶
Cluster sequences that pass identity AND coverage thresholds.
Two sequences are linked when their identity (e.g. ANI) is at least identity_cutoff and their containment passes coverage_cutoff — in both directions when reciprocal, in at least one otherwise. Clusters are the connected components of that link graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identity
|
SimilarityMatrix
|
Identity estimates (typically |
required |
coverage
|
SimilarityMatrix
|
Coverage analogue (typically the asymmetric |
required |
identity_cutoff
|
float
|
Thresholds in |
0.8
|
coverage_cutoff
|
float
|
Thresholds in |
0.8
|
reciprocal
|
bool
|
Require coverage in both directions (min) instead of either direction (max). |
True
|
Returns:
| Type | Description |
|---|---|
ClusterResult
|
Assignments keyed by sequence name. |