-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parallelize k-mer frequency computation
- Loading branch information
Showing
8 changed files
with
72 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
[package] | ||
name = "rnasamba" | ||
version = "0.2.0" | ||
version = "0.2.1" | ||
authors = ["Antonio Camargo <[email protected]>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
name = "rnasamba" | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
itertools = "0.8.1" | ||
ndarray = "0.13.0" | ||
numpy = "0.7.0" | ||
rayon = "1.2.0" | ||
|
||
[dependencies.pyo3] | ||
version = "0.8.1" | ||
features = ["extension-module"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,68 @@ | ||
use pyo3::prelude::*; | ||
use pyo3::wrap_pyfunction; | ||
use itertools::iproduct; | ||
use ndarray::Array2; | ||
use numpy::{convert::ToPyArray, PyArray2}; | ||
use pyo3::{prelude::*, wrap_pyfunction}; | ||
use rayon::prelude::*; | ||
use std::collections::HashMap; | ||
|
||
#[pyfunction] | ||
fn count_kmers(sequence: &str, k: usize) -> PyResult<HashMap<&str, u16>> { | ||
fn sequence_kmer_counts(sequence: &str, k: usize) -> HashMap<&str, u16> { | ||
let mut counts = HashMap::new(); | ||
let n_kmers = sequence.len() - k + 1; | ||
for i in 0..n_kmers { | ||
let kmer = &sequence[i..i + k]; | ||
*counts.entry(kmer).or_insert(0) += 1; | ||
} | ||
Ok(counts) | ||
counts | ||
} | ||
|
||
fn kmer_generator(alphabet: String, k: usize) -> Vec<String> { | ||
match k { | ||
0 => vec![], | ||
1 => alphabet.chars().map(|c| c.to_string()).collect(), | ||
2 => iproduct!(alphabet.chars(), alphabet.chars()) | ||
.map(|(a, b)| format!("{}{}", a, b)) | ||
.collect(), | ||
_ => iproduct!(kmer_generator(alphabet.clone(), k - 1), alphabet.chars()) | ||
.map(|(a, b)| format!("{}{}", a, b)) | ||
.collect(), | ||
} | ||
} | ||
|
||
fn sequence_kmer_frequencies(sequence: &str) -> Vec<f32> { | ||
let mut kmer_frequency_array: Vec<f32> = Vec::new(); | ||
for k in 2..5 { | ||
let sequence_total_kmers = sequence.len() - k + 1; | ||
let sequence_kmer_count = sequence_kmer_counts(sequence, k); | ||
for kmer in kmer_generator(String::from("ATCG"), k).into_iter() { | ||
let kmer_count: u16; | ||
match sequence_kmer_count.get(&kmer[..]) { | ||
Some(n) => kmer_count = *n, | ||
_ => kmer_count = 0, | ||
}; | ||
let kmer_frequency = kmer_count as f32 / sequence_total_kmers as f32; | ||
kmer_frequency_array.push(kmer_frequency); | ||
} | ||
} | ||
kmer_frequency_array | ||
} | ||
|
||
#[pyfunction] | ||
fn kmer_frequencies_array(sequences: Vec<(&str, &str)>) -> Py<PyArray2<f32>> { | ||
Array2::from_shape_vec( | ||
(sequences.len(), 336), | ||
sequences | ||
.par_iter() | ||
.map(|sequence| sequence_kmer_frequencies(sequence.0)) | ||
.flatten() | ||
.collect(), | ||
) | ||
.unwrap() | ||
.to_pyarray(Python::acquire_gil().python()) | ||
.to_owned() | ||
} | ||
|
||
#[pymodule] | ||
fn kmer(_py: Python<'_>, m: &PyModule) -> PyResult<()> { | ||
m.add_wrapped(wrap_pyfunction!(count_kmers))?; | ||
m.add_wrapped(wrap_pyfunction!(kmer_frequencies_array))?; | ||
Ok(()) | ||
} |