tfmindi.pp.calculate_motif_similarity#
- tfmindi.pp.calculate_motif_similarity(seqlets, known_motifs, chunk_size=None, n_nearest=None, threshold=None, reference=None, n_motifs_per_reference_cluster=None, **kwargs)#
Calculate TomTom similarity and convert to log-space for clustering.
- Parameters:
seqlets (
list[ndarray]) – List of seqlet contribution matrices, each with shape (4, length)known_motifs (
list[ndarray] |dict[tuple[str,str],ndarray]) – List of known motif PPM matrices, each with shape (4, length) or a dictionary of motif PPMs, each with shape (4, length)chunk_size (
int|None(default:None)) – If provided, process seqlets in chunks of this size to manage memory usage. If None, process all seqlets at once (original behavior).n_nearest (
int|None(default:None)) – If provided, only keep the n most similar motifs for each seqlet. This creates naturally sparse matrices and reduces memory usage. If None, computes similarities to all motifs (with optional thresholding).threshold (
float|None(default:None)) – Similarity threshold for sparsity when n_nearest is None. Values below threshold are clipped to zero. Default 0.05. Ignored when n_nearest is specified.reference (
MotifCollectionData|None(default:None)) – Motif collection to project each seqlet into while its complete similarity profile is still in memory. Supplying it makesn_nearestfree of consequences: the projection thattfmindi.tl.predict_tf_family_seqlets()needs is built from the unpruned profile of every chunk, so only the pruned matrix is ever accumulated. Requiresknown_motifsto be a dict, since the reference motifs are matched by name.n_motifs_per_reference_cluster (
int|str|None(default:None)) – Reference budget selecting which PCA embedding to project onto. Required withreference, and must match what is later passed totfmindi.tl.predict_tf_family_seqlets().**kwargs – Additional arguments for memelite’s TomTom
- Return type:
- Returns:
Sparse log-transformed similarity array with shape (n_seqlets, n_motifs). When n_nearest is used, only the top-k similarities per seqlet are stored. When threshold is used, values below threshold are clipped to zero.
With
reference, a tuple of that array and the reference projection, shape (n_seqlets, n_PCs). Store the projection atadata.obsm[f"X_ref_proj_{n_motifs_per_reference_cluster}"]; it is what letstfmindi.tl.predict_tf_family_seqlets()annotate from a pruned matrix.- Raises:
ValueError – If
referenceis given withoutn_motifs_per_reference_cluster, with aknown_motifslist rather than a dict, or with reference motifs absent from it.
Examples
>>> _, seqlet_matrices = tfmindi.pp.extract_seqlets(contrib, oh) >>> # Memory-efficient: only keep top 50 similarities per seqlet >>> similarity_matrix = calculate_motif_similarity(seqlet_matrices, known_motifs, n_nearest=50) >>> print(similarity_matrix.shape) (1250, 3989) >>> # Traditional approach with thresholding >>> similarity_matrix = calculate_motif_similarity(seqlet_matrices, known_motifs, threshold=0.1) >>> # For large datasets, use chunking with n_nearest >>> similarity_matrix = calculate_motif_similarity(seqlet_matrices, known_motifs, chunk_size=10000, n_nearest=50) >>> # Pruned storage without pruned annotation: the projection sees every full profile >>> sim, proj = calculate_motif_similarity( ... seqlet_matrices, motifs, n_nearest=100, reference=collection, n_motifs_per_reference_cluster=20 ... ) >>> adata.obsm["X_ref_proj_20"] = proj