Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MRG: add kmers_and_hashes method to get canonical kmers + hashes #40

Merged
merged 18 commits into from
Sep 26, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use anyhow::{anyhow, Result};
use log::debug;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use sourmash::encodings::HashFunctions;
use sourmash::encodings::{revcomp, HashFunctions};
use sourmash::signature::SeqToHashes;

// Set version variable
Expand Down Expand Up @@ -387,6 +387,52 @@ impl KmerCountTable {
self.counts.insert(hashval, count);
Ok(())
}

pub fn kmers_and_hashes(
&self,
seq: String,
allow_bad_kmers: bool,
) -> PyResult<Vec<(String, u64)>> {
let seqb = seq.as_bytes();
let mut hasher = SeqToHashes::new(
seqb,
self.ksize.into(),
allow_bad_kmers,
false,
HashFunctions::Murmur64Dna,
42,
);

let ksize = self.ksize as usize;
let end: usize = seq.len() - ksize + 1;

let mut v: Vec<(String, u64)> = vec![];
for start in 0..end {
let substr = &seq[start..start + ksize];
ctb marked this conversation as resolved.
Show resolved Hide resolved
let hashval = hasher.next().unwrap().unwrap();
v.push((substr.to_string(), hashval));
ctb marked this conversation as resolved.
Show resolved Hide resolved
}
/*
let seqb_rc = revcomp(&seqb);
let seq_rc = String::from_utf8(seqb_rc.clone()).unwrap();

let mut hasher = SeqToHashes::new(
&seqb_rc,
self.ksize.into(),
allow_bad_kmers,
false,
HashFunctions::Murmur64Dna,
42,
);
for start in 0..end {
let substr = &seq_rc[start..start + ksize];
let hashval = hasher.next().unwrap().unwrap();
v.push((substr.to_string(), hashval));
}
*/

Ok(v)
ctb marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Iterator implementation for KmerCountTable
Expand Down
Loading