Skip to content

Commit 15b477f

Browse files
committed
commands/normalize: Convert features lengths to u32
1 parent 2a818c0 commit 15b477f

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

Diff for: src/commands/normalize.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
collections::HashMap,
23
fs::File,
34
io::{self, BufReader, BufWriter, Write},
45
path::{Path, PathBuf},
@@ -9,8 +10,8 @@ use tracing::info;
910

1011
use crate::{
1112
counts,
12-
normalization::{self, calculate_feature_lengths, fpkm, tpm},
13-
read_features,
13+
normalization::{self, fpkm, tpm},
14+
read_features, Feature,
1415
};
1516

1617
#[derive(Debug, Error)]
@@ -57,8 +58,7 @@ where
5758

5859
info!(normalization_method = ?method, "normalizing counts");
5960

60-
let lengths =
61-
calculate_feature_lengths(&features, &names).map_err(NormalizeError::Normalization)?;
61+
let lengths = calculate_feature_lengths(&features, &names).map_err(NormalizeError::Io)?;
6262

6363
let normalized_counts = match method {
6464
normalization::Method::Fpkm => fpkm::normalize(&lengths, &counts),
@@ -84,6 +84,17 @@ where
8484
counts::read(&mut reader).map_err(NormalizeError::ReadCounts)
8585
}
8686

87+
fn calculate_feature_lengths(
88+
features: &HashMap<String, Vec<Feature>>,
89+
names: &[String],
90+
) -> io::Result<Vec<u32>> {
91+
normalization::calculate_feature_lengths(features, names)
92+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?
93+
.into_iter()
94+
.map(|n| u32::try_from(n).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)))
95+
.collect()
96+
}
97+
8798
fn write_normalized_counts<W>(writer: &mut W, names: &[String], values: &[f64]) -> io::Result<()>
8899
where
89100
W: Write,

Diff for: src/normalization/fpkm.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub fn normalize(lengths: &[usize], counts: &[u32]) -> Vec<f64> {
1+
pub fn normalize(lengths: &[u32], counts: &[u32]) -> Vec<f64> {
22
let sum = counts.iter().copied().map(f64::from).sum();
33

44
lengths
@@ -8,9 +8,9 @@ pub fn normalize(lengths: &[usize], counts: &[u32]) -> Vec<f64> {
88
.collect()
99
}
1010

11-
fn calculate_fpkm(count: u32, length: usize, sum: f64) -> f64 {
11+
fn calculate_fpkm(count: u32, length: u32, sum: f64) -> f64 {
1212
assert!(length > 0);
13-
(f64::from(count) * 1e9) / (length as f64 * sum)
13+
(f64::from(count) * 1e9) / (f64::from(length) * sum)
1414
}
1515

1616
#[cfg(test)]

Diff for: src/normalization/tpm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
pub fn normalize(lengths: &[usize], counts: &[u32]) -> Vec<f64> {
1+
pub fn normalize(lengths: &[u32], counts: &[u32]) -> Vec<f64> {
22
let length_normalized_counts: Vec<_> = lengths
33
.iter()
44
.zip(counts)
55
.map(|(length, count)| {
66
assert!(*length > 0);
7-
f64::from(*count) / (*length as f64)
7+
f64::from(*count) / f64::from(*length)
88
})
99
.collect();
1010

0 commit comments

Comments
 (0)