Skip to content

Commit

Permalink
Add test for creating bigwig from iterator, and remove leftover name …
Browse files Browse the repository at this point in the history
…from debugging
  • Loading branch information
jackh726 committed Feb 4, 2024
1 parent 053185c commit 2fca0fc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
7 changes: 3 additions & 4 deletions bigtools/src/utils/cli/bigwigtobedgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub async fn write_bg<R: Reopen + SeekableRead + Send + 'static>(
let end = chrom.as_ref().and_then(|_| end);

let chroms: Vec<ChromInfo> = bigwig.chroms().to_vec();
let chrom_files: Vec<io::Result<(_, TempFileBuffer<File>, String)>> = chroms
let chrom_files: Vec<io::Result<(_, TempFileBuffer<File>)>> = chroms
.into_iter()
.filter(|c| chrom.as_ref().map_or(true, |chrom| &c.name == chrom))
.map(|chrom| {
Expand Down Expand Up @@ -191,16 +191,15 @@ pub async fn write_bg<R: Reopen + SeekableRead + Send + 'static>(
}
let start = start.unwrap_or(0);
let end = end.unwrap_or(chrom.length);
let name = chrom.name.clone();
let handle = runtime
.spawn(file_future(bigwig, chrom, writer, start, end))
.map(|f| f.unwrap());
Ok((handle, buf, name))
Ok((handle, buf))
})
.collect::<Vec<_>>();

for res in chrom_files {
let (f, mut buf, name) = res?;
let (f, mut buf) = res?;
buf.switch(out_file);
f.await?;
while !buf.is_real_file_ready() {
Expand Down
39 changes: 38 additions & 1 deletion bigtools/tests/bigwigwrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tempfile;
use bigtools::bed::bedparser::BedParser;
use bigtools::bedchromdata::BedParserStreamingIterator;
use bigtools::utils::chromvalues::ChromValues;
use bigtools::{BigWigRead, BigWigWrite};
use bigtools::{BigWigRead, BigWigWrite, Value};
use tokio::runtime;

#[test]
Expand Down Expand Up @@ -159,3 +159,40 @@ fn test_multi_chrom() -> io::Result<()> {

Ok(())
}

#[test]
fn test_iter() {
let chroms = vec![
"chrY", "chrY", "chrY", "chrY", "chrY", "chrY", "chrY", "chrY", "chrY", "chrY",
];
let starts: Vec<u32> = vec![530, 538, 584, 713, 751, 860, 865, 873, 879, 902];
let ends: Vec<u32> = vec![538, 584, 713, 751, 860, 865, 873, 879, 902, 923];

let iter = chroms
.into_iter()
.zip(starts.into_iter().zip(ends.into_iter()));
let iter = iter.map(|(chrom, (start, end))| {
Ok::<(String, Value), std::io::Error>((
chrom.to_string(),
Value {
start,
end,
value: 0.0,
},
))
});

let vals_iter = BedParserStreamingIterator::new(BedParser::wrap_iter(iter), true);

let chrom_map = HashMap::from([
("chrY".to_string(), 57_227_415)
]);

let runtime = tokio::runtime::Builder::new_current_thread()
.build()
.expect("Unable to create runtime.");

let tempfile = tempfile::NamedTempFile::new().unwrap();
let outb = BigWigWrite::create_file(tempfile.path().to_string_lossy().to_string());
outb.write_singlethreaded(chrom_map, vals_iter, runtime).unwrap();
}

0 comments on commit 2fca0fc

Please sign in to comment.