Repository: sourmash-bio/sourmash (language: python)

Issue:
fix rust `Signature` API for minhashes method.
see https://github.com/dib-lab/sourmash/pull/1159#issuecomment-671987255 and follow-on.
fix rust `Signature` API for minhashes method.
see https://github.com/dib-lab/sourmash/pull/1159#issuecomment-671987255 and follow-on.


Discussion / hints:
worked on exposing this error more cleanly --

in `test_sourmash_sketch.py::test_dna_defaults()`, I added the following code at the end:
```
siglist = factory()
sig = siglist[0]
```

then:

```
sig.minhash
```
yields
```
E       sourmash.exceptions.Panic: sourmash panicked: thread 'unnamed' panicked with 'not implemented' at src/core/src/ffi/signature.rs:172
```

---

and
```
minhashes = sig.minhashes()
```
causes a segmentation fault.


For whoever ends up implementing this: rename the `.minhashes` method to `.sketches` instead =]
worked on exposing this error more cleanly --

in `test_sourmash_sketch.py::test_dna_defaults()`, I added the following code at the end:
```
siglist = factory()
sig = siglist[0]
```

then:

```
sig.minhash
```
yields
```
E       sourmash.exceptions.Panic: sourmash panicked: thread 'unnamed' panicked with 'not implemented' at src/core/src/ffi/signature.rs:172
```

---

and
```
minhashes = sig.minhashes()
```
causes a segmentation fault.


For whoever ends up implementing this: rename the `.minhashes` method to `.sketches` instead =]

Fix patch:
diff --git a/src/core/src/ffi/signature.rs b/src/core/src/ffi/signature.rs
index 9ffd5549ff..825e091f4d 100644
--- a/src/core/src/ffi/signature.rs
+++ b/src/core/src/ffi/signature.rs
@@ -4,6 +4,8 @@ use std::io;
 use std::os::raw::c_char;
 use std::slice;
 
+use crate::errors::SourmashError;
+
 use crate::encodings::HashFunctions;
 use crate::signature::Signature;
 use crate::sketch::Sketch;
@@ -165,11 +167,16 @@ ffi_fn! {
 unsafe fn signature_first_mh(ptr: *const SourmashSignature) -> Result<*mut SourmashKmerMinHash> {
     let sig = SourmashSignature::as_rust(ptr);
 
-    if let Some(Sketch::MinHash(mh)) = sig.signatures.get(0) {
-        Ok(SourmashKmerMinHash::from_rust(mh.clone()))
-    } else {
-        // TODO: need to select the correct one
-        unimplemented!()
+    match sig.signatures.get(0) {
+        Some(Sketch::MinHash(mh)) => {
+            Ok(SourmashKmerMinHash::from_rust(mh.clone()))
+        },
+        Some(Sketch::LargeMinHash(mh_btree)) => {
+            Ok(SourmashKmerMinHash::from_rust(mh_btree.into()))
+        },
+        _ => Err(SourmashError::Internal {
+            message: "found unsupported sketch type".to_string()
+        }),
     }
 }
 }
diff --git a/src/core/src/sketch/minhash.rs b/src/core/src/sketch/minhash.rs
index 41c24a27ce..71332d394b 100644
--- a/src/core/src/sketch/minhash.rs
+++ b/src/core/src/sketch/minhash.rs
@@ -1617,6 +1617,30 @@ impl From<KmerMinHashBTree> for KmerMinHash {
     }
 }
 
+impl From<&KmerMinHashBTree> for KmerMinHash {
+    fn from(other: &KmerMinHashBTree) -> KmerMinHash {
+        let mut new_mh = KmerMinHash::new(
+            other.scaled(),
+            other.ksize() as u32,
+            other.hash_function(),
+            other.seed(),
+            other.track_abundance(),
+            other.num(),
+        );
+
+        let mins = other.mins.iter().copied().collect();
+        let abunds = other
+            .abunds
+            .as_ref()
+            .map(|abunds| abunds.values().cloned().collect());
+
+        new_mh.mins = mins;
+        new_mh.abunds = abunds;
+
+        new_mh
+    }
+}
+
 impl From<KmerMinHash> for KmerMinHashBTree {
     fn from(other: KmerMinHash) -> KmerMinHashBTree {
         let mut new_mh = KmerMinHashBTree::new(


Test patch:
diff --git a/tests/test_sourmash_sketch.py b/tests/test_sourmash_sketch.py
index 1c36be9c62..fd299a82ed 100644
--- a/tests/test_sourmash_sketch.py
+++ b/tests/test_sourmash_sketch.py
@@ -213,6 +213,10 @@ def test_dna_defaults():
     assert not params.hp
     assert not params.protein
 
+    siglist = factory()
+    sig = siglist[0]
+    sig.minhash
+
 
 def test_dna_override_1():
     factory = _signatures_for_sketch_factory(['k=21,scaled=2000,abund'],