Skip to content

Commit

Permalink
feat: Implement TryInto to convert Signature and SigStore into KmerMi…
Browse files Browse the repository at this point in the history
…nHash (#3348)

Ref:
https://github.com/sourmash-bio/sourmash_plugin_branchwater/pull/467/files#r1797783380

Implement `TryInto<KmerMinHash>` for Signature and SigStore to avoid
having to clone a (potentially big) minhash sketch.
  • Loading branch information
luizirber authored Oct 13, 2024
1 parent 62f03eb commit ff4eeda
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/core/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ mod test {
use crate::prelude::Select;
use crate::selection::Selection;
use crate::signature::Signature;
use crate::Result;

#[test]
fn sigstore_selection_with_downsample() {
Expand Down
15 changes: 15 additions & 0 deletions src/core/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ pub enum SourmashError {
#[error("sketch needs abundance for this operation")]
NeedsAbundanceTracking,

#[error("Expected a MinHash sketch in this signature")]
NoMinHashFound,

#[error("Empty signature")]
EmptySignature,

#[error("Multiple sketches found, expected one")]
MultipleSketchesFound,

#[error("Invalid hash function: {function:?}")]
InvalidHashFunction { function: String },

Expand Down Expand Up @@ -108,6 +117,9 @@ pub enum SourmashErrorCode {
MismatchNum = 1_07,
NeedsAbundanceTracking = 1_08,
CannotUpsampleScaled = 1_09,
NoMinHashFound = 1_10,
EmptySignature = 1_11,
MultipleSketchesFound = 1_12,
// Input sequence errors
InvalidDNA = 11_01,
InvalidProt = 11_02,
Expand Down Expand Up @@ -147,6 +159,9 @@ impl SourmashErrorCode {
SourmashError::MismatchSeed => SourmashErrorCode::MismatchSeed,
SourmashError::MismatchSignatureType => SourmashErrorCode::MismatchSignatureType,
SourmashError::NonEmptyMinHash { .. } => SourmashErrorCode::NonEmptyMinHash,
SourmashError::NoMinHashFound => SourmashErrorCode::NoMinHashFound,
SourmashError::EmptySignature => SourmashErrorCode::EmptySignature,
SourmashError::MultipleSketchesFound => SourmashErrorCode::MultipleSketchesFound,
SourmashError::InvalidDNA { .. } => SourmashErrorCode::InvalidDNA,
SourmashError::InvalidProt { .. } => SourmashErrorCode::InvalidProt,
SourmashError::InvalidCodonLength { .. } => SourmashErrorCode::InvalidCodonLength,
Expand Down
22 changes: 22 additions & 0 deletions src/core/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,28 @@ impl PartialEq for Signature {
}
}

impl TryInto<KmerMinHash> for Signature {
type Error = Error;

fn try_into(self) -> Result<KmerMinHash, Error> {
match self.signatures.len() {

Check failure on line 894 in src/core/src/signature.rs

View workflow job for this annotation

GitHub Actions / minimum_rust_version

non-exhaustive patterns: `_` not covered
1 => self
.signatures
.into_iter()
.find_map(|sk| {
if let Sketch::MinHash(mh) = sk {
Some(mh)
} else {
None
}
})
.ok_or_else(|| Error::NoMinHashFound),
0 => Err(Error::EmptySignature),
2.. => Err(Error::MultipleSketchesFound),
}
}
}

#[cfg(test)]
mod test {
use std::fs::File;
Expand Down
10 changes: 10 additions & 0 deletions src/core/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use typed_builder::TypedBuilder;
use crate::errors::ReadDataError;
use crate::prelude::*;
use crate::signature::SigsTrait;
use crate::sketch::minhash::KmerMinHash;
use crate::sketch::Sketch;
use crate::{Error, Result};

Expand Down Expand Up @@ -550,6 +551,15 @@ impl From<Signature> for SigStore {
}
}

impl TryInto<KmerMinHash> for SigStore {
type Error = crate::Error;

fn try_into(self) -> std::result::Result<KmerMinHash, Self::Error> {
let sig: Signature = self.into();
sig.try_into()
}
}

impl Comparable<SigStore> for SigStore {
fn similarity(&self, other: &SigStore) -> f64 {
let ng: &Signature = self.data().unwrap();
Expand Down

0 comments on commit ff4eeda

Please sign in to comment.