-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix up infer_from_stream on compressed streams
- Loading branch information
1 parent
daae3d4
commit c213497
Showing
9 changed files
with
140 additions
and
24 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,45 @@ | ||
/*! | ||
* Demo how to read an mzML file that is compressed | ||
*/ | ||
use std::env; | ||
use std::fs; | ||
use std::io; | ||
use std::process::exit; | ||
use std::time::Instant; | ||
|
||
use mzdata::io::PreBufferedStream; | ||
use mzdata::io::{MzMLReader, RestartableGzDecoder}; | ||
use mzdata::prelude::*; | ||
|
||
fn main() -> io::Result<()> { | ||
let input = env::args().skip(1).next().unwrap_or_else(|| { | ||
eprintln!("Please provide a file path or '-' for STDIN"); | ||
exit(1) | ||
}); | ||
let start = Instant::now(); | ||
let groups = if input == "-" { | ||
let stream = RestartableGzDecoder::new(io::BufReader::new(PreBufferedStream::new(io::stdin())?)); | ||
let reader = MzMLReader::new(stream); | ||
let groups: Vec<_> = reader.into_groups().collect(); | ||
groups | ||
} else { | ||
let stream = RestartableGzDecoder::new(io::BufReader::new(fs::File::open(input)?)); | ||
let reader = MzMLReader::new(stream); | ||
let groups: Vec<_> = reader.into_groups().collect(); | ||
groups | ||
}; | ||
let spectra: Vec<_> = groups | ||
.iter() | ||
.flat_map(|g| g.precursor.iter().chain(g.products.iter())) | ||
.collect(); | ||
let end = Instant::now(); | ||
eprintln!( | ||
"Read {} groups with {} spectra in {:0.3?}", | ||
groups.len(), | ||
spectra.len(), | ||
end - start | ||
); | ||
assert!(spectra.len() > 0); | ||
|
||
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 |
---|---|---|
@@ -1,33 +1,54 @@ | ||
use std::io; | ||
/*! | ||
* Demo how to read a data file from STDIN, and then batch collect its spectra | ||
*/ | ||
use std::io::{self, Seek}; | ||
use std::time::Instant; | ||
|
||
use mzdata::{MzMLReader, MGFReader}; | ||
use mzdata::io::{PreBufferedStream, ScanSource, infer_from_stream, MassSpectrometryFormat}; | ||
use mzdata::io::{ | ||
infer_from_stream, MassSpectrometryFormat, PreBufferedStream, RestartableGzDecoder, ScanSource, | ||
}; | ||
use mzdata::{MGFReader, MzMLReader}; | ||
|
||
fn main() -> io::Result<()> { | ||
let start = Instant::now(); | ||
let stream = io::stdin(); | ||
let mut stream = PreBufferedStream::new(stream)?; | ||
let (fmt, compressed) = infer_from_stream(&mut stream)?; | ||
if compressed { | ||
panic!("Compression not supported!") | ||
} | ||
stream.seek(io::SeekFrom::Start(0))?; | ||
let groups: Vec<_> = match fmt { | ||
MassSpectrometryFormat::MGF => { | ||
MGFReader::new(stream).into_groups().collect() | ||
}, | ||
if compressed { | ||
MGFReader::new(RestartableGzDecoder::new(io::BufReader::new(stream))) | ||
.into_groups() | ||
.collect() | ||
} else { | ||
MGFReader::new(stream).into_groups().collect() | ||
} | ||
} | ||
MassSpectrometryFormat::MzML => { | ||
MzMLReader::new(stream).into_groups().collect() | ||
}, | ||
_ => { | ||
panic!("Cannot identify file format") | ||
if compressed { | ||
MzMLReader::new(RestartableGzDecoder::new(io::BufReader::new(stream))) | ||
.into_groups() | ||
.collect() | ||
} else { | ||
MzMLReader::new(stream).into_groups().collect() | ||
} | ||
} | ||
x => { | ||
panic!("Cannot identify file format ({:?})", x) | ||
} | ||
}; | ||
let spectra: Vec<_> = groups.iter().flat_map(|g| { | ||
g.precursor.iter().chain(g.products.iter()) | ||
}).collect(); | ||
let spectra: Vec<_> = groups | ||
.iter() | ||
.flat_map(|g| g.precursor.iter().chain(g.products.iter())) | ||
.collect(); | ||
let end = Instant::now(); | ||
eprintln!("Read {} groups with {} spectra in {:0.3?}", groups.len(), spectra.len(), end - start); | ||
eprintln!( | ||
"Read {} groups with {} spectra in {:0.3?}", | ||
groups.len(), | ||
spectra.len(), | ||
end - start | ||
); | ||
assert!(spectra.len() > 0); | ||
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,27 @@ | ||
/*! | ||
* Demo the minimum code needed to infer the input file format from a path | ||
* or STDIN using `infer_format`, `infer_from_stream` and | ||
*/ | ||
use std::process::exit; | ||
use std::env; | ||
use std::io; | ||
|
||
use mzdata::io::{infer_format, infer_from_stream, PreBufferedStream}; | ||
|
||
|
||
|
||
fn main() -> io::Result<()> { | ||
let input = env::args().skip(1).next().unwrap_or_else(|| { | ||
eprintln!("Please provide a file path or '-' for STDIN"); | ||
exit(1) | ||
}); | ||
|
||
let (inferred, gzipped) = if input == "-" { | ||
let mut stream = PreBufferedStream::new(io::stdin())?; | ||
infer_from_stream(&mut stream)? | ||
} else { | ||
infer_format(input)? | ||
}; | ||
println!("{:?} (gzip: {})", inferred, gzipped); | ||
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
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
Binary file not shown.