Reading and subsampling MGF file #49
-
|
Hello, I would like to read a Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
You could do almost exactly what you have there, just wrap each individual spectrum in a list or tuple: with mgf.MGF(input_mgf_fp) as reader:
for spectrum in reader:
params = spectrum.get('params')
if params['feature_id'] in feature_ids:
mgf.write([spectrum], output_fp)The first parameter of Another option is to use a generator to abstract away the filtering logic: def producer(reader, feature_ids):
for spectrum in reader:
params = spectrum.get('params')
if params['feature_id'] in feature_ids:
yield spectrum
with mgf.MGF(input_mgf_fp) as reader:
spectrum_iterator = producer(reader, feature_ids)
mgf.write(spectrum_iterator, output_fp)This approach is usually more generalizable, though it does make it a little bit harder to follow because the filtering logic is no longer inline at the place where it's used. |
Beta Was this translation helpful? Give feedback.
You could do almost exactly what you have there, just wrap each individual spectrum in a list or tuple:
The first parameter of
mgf.writeis aniterableof spectra, so in order to match its expectations, we need to wrap single spectrumdictobjects in something that satisfies the expectation thatnext(iter(obj)) -> dict. This happens to work becausemgf.writedoesn't close the output stream and doesn't automatically append a file header unless you tell it to.Another option is to use a generato…