-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Brought
Sequences
over, IndexedDataContainer
no longer has lifeti…
…me, more. - Renamed multiple things for accuracy, consistency. - Removed generic lifetime in `IndexedDataContainer` trait, which greatly simplifies things. - New `Operation` enum for bedtools map-like operations. - `float_compute()` function for doing operations (this will likely change) - Brough sequences stuff over. - `apply_over_join()` and `apply_into_vec()` (both names likely to change). - New sequences doc and test.
- Loading branch information
Showing
24 changed files
with
1,609 additions
and
45 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
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//! Implementations of various operations on data. | ||
//! | ||
use num_traits::{Float, ToPrimitive}; | ||
use std::iter::Sum; | ||
|
||
/// Calculate the median. | ||
/// | ||
/// This will clone and turn `numbers` into a `Vec`. | ||
pub fn median<F: Float + Ord + Sum>(numbers: &[F]) -> F { | ||
let mut numbers = numbers.to_vec(); | ||
numbers.sort_unstable(); | ||
let mid = numbers.len() / 2; | ||
if numbers.len() % 2 == 0 { | ||
(numbers[mid - 1] + numbers[mid]) / F::from(2.0).unwrap() | ||
} else { | ||
numbers[mid] | ||
} | ||
} | ||
|
||
/// The (subset of) standard `bedtools map` operations. | ||
pub enum Operation { | ||
Sum, | ||
Min, | ||
Max, | ||
Mean, | ||
Median, | ||
Collapse, | ||
} | ||
|
||
pub enum OperationResult<T> | ||
where | ||
T: Float, | ||
{ | ||
Float(T), | ||
String(String), | ||
} | ||
|
||
pub fn float_compute<T>(operation: Operation, data: &[T]) -> Option<OperationResult<T>> | ||
where | ||
T: Float + Sum<T> + ToPrimitive + Ord + Clone + ToString, | ||
{ | ||
match operation { | ||
Operation::Sum => { | ||
let sum: T = data.iter().cloned().sum(); | ||
Some(OperationResult::Float(sum)) | ||
} | ||
Operation::Min => data.iter().cloned().min().map(OperationResult::Float), | ||
Operation::Max => data.iter().cloned().max().map(OperationResult::Float), | ||
Operation::Mean => { | ||
if data.is_empty() { | ||
None | ||
} else { | ||
let sum: T = data.iter().cloned().sum(); | ||
let mean = sum / T::from(data.len()).unwrap(); | ||
Some(OperationResult::Float(mean)) | ||
} | ||
} | ||
Operation::Median => Some(OperationResult::Float(median(data))), | ||
Operation::Collapse => { | ||
let collapsed = data | ||
.iter() | ||
.map(|num| num.to_string()) | ||
.collect::<Vec<_>>() | ||
.join(", "); | ||
Some(OperationResult::String(collapsed)) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.