-
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.
- Loading branch information
1 parent
4d4cd80
commit 79e577c
Showing
5 changed files
with
162 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::{io, path, env}; | ||
use std::time; | ||
|
||
#[cfg(not(feature = "async"))] | ||
compile_error!("This example requires the async feature"); | ||
|
||
use tokio; | ||
use tokio::fs; | ||
|
||
use mzdata::io::mzml; | ||
use mzdata::io::prelude::*; | ||
|
||
async fn load_file<P: Into<path::PathBuf> + Clone>(path: P) -> io::Result<mzml::AsyncMzMLReader<fs::File>> { | ||
let fh = fs::File::open(path.into()).await?; | ||
let mut reader = mzml::AsyncMzMLReader::new(fh).await; | ||
reader.read_index_from_end().await.expect("Failed to read index from the file"); | ||
Ok(reader) | ||
} | ||
|
||
|
||
async fn scan_file( | ||
reader: &mut mzml::AsyncMzMLReader<fs::File>, | ||
) { | ||
let start = time::Instant::now(); | ||
let n = reader.len(); | ||
let mut i = 0; | ||
while let Some(scan) = reader.get_spectrum_by_index(i).await { | ||
if i % 10000 == 0 { | ||
println!( | ||
"\tScan {}: {}|{} ({} seconds)", | ||
i, | ||
scan.id(), | ||
scan.index(), | ||
(time::Instant::now() - start).as_secs_f64(), | ||
); | ||
} | ||
i += 1; | ||
if i == n { | ||
break; | ||
} | ||
} | ||
let end = time::Instant::now(); | ||
println!("Loaded in {} spectra {} seconds", i, (end - start).as_secs_f64()); | ||
} | ||
|
||
|
||
#[tokio::main(flavor = "multi_thread", worker_threads = 10)] | ||
async fn main() -> io::Result<()> { | ||
let path = path::PathBuf::from( | ||
env::args() | ||
.skip(1) | ||
.next() | ||
.expect("Please pass an MS data file path"), | ||
); | ||
if let Some(ext) = path.extension() { | ||
if ext.to_string_lossy().to_lowercase() == "mzml" { | ||
let mut reader = load_file(path).await?; | ||
scan_file(&mut reader).await; | ||
} else { | ||
panic!("Could not infer the file format") | ||
} | ||
} else { | ||
let mut reader = load_file(path).await?; | ||
scan_file(&mut reader).await; | ||
}; | ||
Ok(()) | ||
} |
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,91 @@ | ||
use std::{io, path, env, fs}; | ||
use std::time; | ||
|
||
use rayon::prelude::*; | ||
|
||
use mzdata::io::{mzml, mzmlb}; | ||
use mzdata::io::prelude::*; | ||
use mzdata::spectrum::MultiLayerSpectrum; | ||
use mzpeaks::{CentroidPeak, DeconvolutedPeak}; | ||
|
||
|
||
fn load_file<P: Into<path::PathBuf> + Clone>(path: P) -> io::Result<mzml::MzMLReader<fs::File>> { | ||
let reader = mzml::MzMLReader::open_path(path)?; | ||
Ok(reader) | ||
} | ||
|
||
#[cfg(feature = "mzmlb")] | ||
fn load_mzmlb_file<P: Into<path::PathBuf> + Clone>(path: P) -> io::Result<mzmlb::MzMLbReader> { | ||
let reader = mzmlb::MzMLbReader::open_path(&path.into())?; | ||
let blosc_threads = match std::env::var("BLOSC_NUM_THREADS") { | ||
Ok(val) => { | ||
match val.parse() { | ||
Ok(nt) => nt, | ||
Err(e) => { | ||
eprintln!("Failed to parse BLOSC_NUM_THREADS env var: {}", e); | ||
4 | ||
}, | ||
} | ||
}, | ||
Err(_) => 4, | ||
}; | ||
mzmlb::MzMLbReader::set_blosc_nthreads(blosc_threads); | ||
Ok(reader) | ||
} | ||
|
||
fn scan_file< | ||
R: MZFileReader< | ||
CentroidPeak, | ||
DeconvolutedPeak, | ||
MultiLayerSpectrum<CentroidPeak, DeconvolutedPeak> | ||
> + Iterator<Item=MultiLayerSpectrum<CentroidPeak, DeconvolutedPeak>> + Send, | ||
>( | ||
reader: &mut R, | ||
) { | ||
let start = time::Instant::now(); | ||
reader.enumerate().par_bridge().for_each(|(i, scan)| { | ||
if i % 10000 == 0 { | ||
println!( | ||
"\tScan {}: {}|{} ({} seconds)", | ||
i, | ||
scan.id(), | ||
scan.index(), | ||
(time::Instant::now() - start).as_secs_f64(), | ||
); | ||
} | ||
}); | ||
let end = time::Instant::now(); | ||
println!("Loaded in {} seconds", (end - start).as_secs_f64()); | ||
} | ||
|
||
|
||
fn main() -> io::Result<()> { | ||
let path = path::PathBuf::from( | ||
env::args() | ||
.skip(1) | ||
.next() | ||
.expect("Please pass an MS data file path"), | ||
); | ||
if let Some(ext) = path.extension() { | ||
if ext.to_string_lossy().to_lowercase() == "mzmlb" { | ||
#[cfg(feature = "mzmlb")] | ||
{ | ||
let mut reader = load_mzmlb_file(path)?; | ||
scan_file(&mut reader) | ||
} | ||
#[cfg(not(feature = "mzmlb"))] | ||
{ | ||
panic!("Cannot read mzMLb file. Recompile enabling the `mzmlb` feature") | ||
} | ||
} else if ext.to_string_lossy().to_lowercase() == "mzml" { | ||
let mut reader = load_file(path)?; | ||
scan_file(&mut reader) | ||
} else { | ||
panic!("Could not infer the file format") | ||
} | ||
} else { | ||
let mut reader = load_file(path)?; | ||
scan_file(&mut reader) | ||
}; | ||
Ok(()) | ||
} |
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