From 2819307a768469ed8d6bd15f8e17889edfcf81b6 Mon Sep 17 00:00:00 2001 From: Luiz Irber Date: Wed, 19 Oct 2022 17:54:46 -0700 Subject: [PATCH] more picklist --- include/sourmash.h | 12 ++++++++++++ src/core/src/ffi/index/mod.rs | 23 +++++++++++++++++++++++ src/core/src/ffi/picklist.rs | 15 ++++++++++++++- src/core/src/index/mod.rs | 10 ++++++++++ src/core/src/picklist.rs | 14 +++++++++++++- src/sourmash/picklist.py | 8 ++++---- 6 files changed, 76 insertions(+), 6 deletions(-) diff --git a/include/sourmash.h b/include/sourmash.h index ef08b7d473..4a7c9bd235 100644 --- a/include/sourmash.h +++ b/include/sourmash.h @@ -16,6 +16,12 @@ enum HashFunctions { }; typedef uint32_t HashFunctions; +enum PickStyle { + PICK_STYLE_INCLUDE = 1, + PICK_STYLE_EXCLUDE = 2, +}; +typedef uint32_t PickStyle; + enum SourmashErrorCode { SOURMASH_ERROR_CODE_NO_ERROR = 0, SOURMASH_ERROR_CODE_PANIC = 1, @@ -371,6 +377,8 @@ void picklist_set_column_name(SourmashPicklist *ptr, const char *prop_ptr, uintp void picklist_set_pickfile(SourmashPicklist *ptr, const char *prop_ptr, uintptr_t insize); +void picklist_set_pickstyle(SourmashPicklist *ptr, PickStyle pickstyle); + void revindex_free(SourmashRevIndex *ptr); const SourmashSearchResult *const *revindex_gather(const SourmashRevIndex *ptr, @@ -428,6 +436,8 @@ SourmashSelection *selection_new(void); uint32_t selection_num(const SourmashSelection *ptr); +const SourmashPicklist *selection_picklist(const SourmashSelection *ptr); + uint32_t selection_scaled(const SourmashSelection *ptr); void selection_set_abund(SourmashSelection *ptr, bool new_abund); @@ -440,6 +450,8 @@ void selection_set_moltype(SourmashSelection *ptr, HashFunctions new_moltype); void selection_set_num(SourmashSelection *ptr, uint32_t new_num); +void selection_set_picklist(SourmashSelection *ptr, SourmashPicklist *new_picklist); + void selection_set_scaled(SourmashSelection *ptr, uint32_t new_scaled); void signature_add_protein(SourmashSignature *ptr, const char *sequence); diff --git a/src/core/src/ffi/index/mod.rs b/src/core/src/ffi/index/mod.rs index 6ef1c26f76..516a3eafc7 100644 --- a/src/core/src/ffi/index/mod.rs +++ b/src/core/src/ffi/index/mod.rs @@ -5,6 +5,7 @@ use crate::index::{Selection, SigStore}; use crate::signature::Signature; +use crate::ffi::picklist::SourmashPicklist; use crate::ffi::signature::SourmashSignature; use crate::ffi::utils::{ForeignObject, SourmashStr}; @@ -154,6 +155,28 @@ pub unsafe extern "C" fn selection_set_moltype( sel.set_moltype(new_moltype); } +#[no_mangle] +pub unsafe extern "C" fn selection_picklist( + ptr: *const SourmashSelection, +) -> *const SourmashPicklist { + let sel = SourmashSelection::as_rust(ptr); + if let Some(picklist) = sel.picklist() { + SourmashPicklist::from_rust(picklist) + } else { + todo!("empty picklist case not supported yet") + } +} + +#[no_mangle] +pub unsafe extern "C" fn selection_set_picklist( + ptr: *mut SourmashSelection, + new_picklist: *mut SourmashPicklist, +) { + let sel = SourmashSelection::as_rust_mut(ptr); + let pick = SourmashPicklist::into_rust(new_picklist); + sel.set_picklist(*pick); +} + //================================================================ // pub struct SignatureIterator { diff --git a/src/core/src/ffi/picklist.rs b/src/core/src/ffi/picklist.rs index 5a2f9cc63b..c7bea755ae 100644 --- a/src/core/src/ffi/picklist.rs +++ b/src/core/src/ffi/picklist.rs @@ -1,7 +1,7 @@ use std::os::raw::c_char; use std::slice; -use crate::picklist::Picklist; +use crate::picklist::{PickStyle, Picklist}; use crate::ffi::utils::ForeignObject; @@ -74,3 +74,16 @@ unsafe fn picklist_set_column_name( Ok(()) } } + +ffi_fn! { +unsafe fn picklist_set_pickstyle( + ptr: *mut SourmashPicklist, + pickstyle: PickStyle, +) -> Result<()> { + let pl = SourmashPicklist::as_rust_mut(ptr); + + pl.set_pickstyle(pickstyle); + + Ok(()) +} +} diff --git a/src/core/src/index/mod.rs b/src/core/src/index/mod.rs index 07058a473d..b2bcd54d7c 100644 --- a/src/core/src/index/mod.rs +++ b/src/core/src/index/mod.rs @@ -18,6 +18,7 @@ use typed_builder::TypedBuilder; use crate::encodings::HashFunctions; use crate::errors::ReadDataError; use crate::index::search::{search_minhashes, search_minhashes_containment}; +use crate::picklist::Picklist; use crate::prelude::*; use crate::signature::SigsTrait; use crate::sketch::Sketch; @@ -32,6 +33,7 @@ pub struct Selection { scaled: Option, containment: Option, moltype: Option, + picklist: Option, } impl Selection { @@ -82,6 +84,14 @@ impl Selection { pub fn set_moltype(&mut self, value: HashFunctions) { self.moltype = Some(value); } + + pub fn picklist(&self) -> Option { + self.picklist.clone() + } + + pub fn set_picklist(&mut self, value: Picklist) { + self.picklist = Some(value); + } } pub trait Index<'a> { diff --git a/src/core/src/picklist.rs b/src/core/src/picklist.rs index 0b77cb4457..8b3be7e70c 100644 --- a/src/core/src/picklist.rs +++ b/src/core/src/picklist.rs @@ -1,7 +1,7 @@ use getset::{CopyGetters, Getters, Setters}; use typed_builder::TypedBuilder; -#[derive(Default, TypedBuilder, CopyGetters, Getters, Setters)] +#[derive(Default, TypedBuilder, CopyGetters, Getters, Setters, Clone)] pub struct Picklist { #[getset(get = "pub", set = "pub")] #[builder(default = "".into())] @@ -14,4 +14,16 @@ pub struct Picklist { #[getset(get = "pub", set = "pub")] #[builder(default = "".into())] column_name: String, + + #[getset(get = "pub", set = "pub")] + #[builder] + pickstyle: PickStyle, +} + +#[derive(Default, Clone)] +#[repr(u32)] +pub enum PickStyle { + #[default] + Include = 1, + Exclude = 2, } diff --git a/src/sourmash/picklist.py b/src/sourmash/picklist.py index b2764ffce7..af15df0990 100644 --- a/src/sourmash/picklist.py +++ b/src/sourmash/picklist.py @@ -258,10 +258,10 @@ def _as_rust(self): ptr = lib.picklist_new() - rustcall(lib.picklist_set_coltype, ptr, self.coltype) - rustcall(lib.picklist_set_pickfile, ptr, self.pickfile) - rustcall(lib.picklist_set_column_name, ptr, self.column_name) - rustcall(lib.picklist_set_pickstyle, ptr, self.pickstyle) + rustcall(lib.picklist_set_coltype, ptr, self.coltype.encode('utf-8'), len(self.coltype)) + rustcall(lib.picklist_set_pickfile, ptr, self.pickfile.encode('utf-8'), len(self.pickfile)) + rustcall(lib.picklist_set_column_name, ptr, self.column_name.encode('utf-8'), len(self.column_name)) + rustcall(lib.picklist_set_pickstyle, ptr, self.pickstyle.value) #self.preprocess_fn = preprocess[coltype] #self.pickset = None