Skip to content

Commit ee929a1

Browse files
committed
round of all floats to 2 decimal places
1 parent c1a3c6e commit ee929a1

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

src/bamcoverage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub fn r_bamcoverage(
208208
(fields[0].to_string(), Value {
209209
start: fields[1].parse::<u32>().unwrap(),
210210
end: fields[2].parse::<u32>().unwrap(),
211-
value: fields[3].parse::<f32>().unwrap() * sf
211+
value: (fields[3].parse::<f32>().unwrap() * sf * 100.0).round() / 100.0,
212212
})
213213
)
214214
}

src/calc.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,33 +134,39 @@ pub fn calc_ratio(
134134
"log2" => {
135135
let num: f32 = (cov1 * *sf1) + *pseudocount1;
136136
let den: f32 = (cov2 * *sf2) + *pseudocount2;
137-
return (num / den).log2();
137+
let fcov: f32 = (num / den).log2();
138+
return (fcov * 100.0).round() / 100.0;
138139
}
139140
"ratio" => {
140141
let num: f32 = (cov1 * *sf1) + *pseudocount1;
141142
let den: f32 = (cov2 * *sf2) + *pseudocount2;
142-
return num / den;
143+
let fcov: f32 = num / den;
144+
return (fcov * 100.0).round() / 100.0;
143145
}
144146
"reciprocal_ratio" => {
145147
let num: f32 = (cov1 * *sf1) + *pseudocount1;
146148
let den: f32 = (cov2 * *sf2) + *pseudocount2;
147149
let ratio: f32 = num / den;
148150
if ratio >= 1.0 {
149-
return den / num;
151+
let fcov: f32 = den / num;
152+
return (fcov * 100.0).round() / 100.0;
150153
} else {
151-
return -num / den;
154+
let fcov: f32 = -num / den;
155+
return (fcov * 100.0).round() / 100.0;
152156
}
153157
}
154158
"subtract" => {
155159
let num: f32 = (cov1 * *sf1) + *pseudocount1;
156160
let den: f32 = (cov2 * *sf2) + *pseudocount2;
157-
return num - den;
161+
let fcov: f32 = num - den;
162+
return (fcov * 100.0).round() / 100.0;
158163
}
159164
_ => {
160165
// No operation is never allowed (on the py arg level, so just default to log2)
161166
let num: f32 = (cov1 * *sf1) + *pseudocount1;
162167
let den: f32 = (cov2 * *sf2) + *pseudocount2;
163-
return (num / den).log2();
168+
let fcov: f32 = (num / den).log2();
169+
return (fcov * 100.0).round() / 100.0;
164170
}
165171
}
166172
}

src/filehandler.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,12 @@ pub fn write_matrix(
645645
region.strand, // Strand field persisted from bedfile
646646
);
647647
writerow.push_str(
648-
&row.iter().map(|x| (scale_regions.scale * x).to_string()).collect::<Vec<String>>().join("\t")
648+
&row
649+
.iter()
650+
.map(
651+
|x| ((scale_regions.scale * x * 100.0).round() / 100.0).to_string()
652+
)
653+
.collect::<Vec<String>>().join("\t")
649654
);
650655
writerow.push_str("\n");
651656
encoder.write_all(writerow.as_bytes()).unwrap();

src/multibamsummary.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,10 @@ pub fn r_mbams(
240240
.map(|x| x.split('\t').collect())
241241
.map(|x: Vec<&str> | ( x[0].to_string(), x[1].to_string(), x[2].to_string(), x[3].parse::<f32>().unwrap() ) )
242242
.collect();
243-
let counts = lines.par_iter().map(|x| x.3).collect::<Vec<_>>();
243+
let counts = lines
244+
.par_iter()
245+
.map(|x| (x.3 * 100.0).round() / 100.0)
246+
.collect::<Vec<_>>();
244247
let regions: (String, String, String) = (lines[0].0.clone(), lines[0].1.clone(), lines[0].2.clone());
245248
_matvec.push(counts);
246249
_regions.push(regions);

0 commit comments

Comments
 (0)