Skip to content

Picking n-th spectra from a mgf file #103

Answered by mobiusklein
hsogaard asked this question in Q&A
Discussion options

You must be logged in to vote

mgf.write takes an Iterable over spectra, not a single spectrum as its first argument. Without seeing an error message, I have to guess this is the problem you've encountered. You can rewrite this two ways to make it work:

file = mgf.read(filePath)
nSpectra = len(file)

for i in range(nSpectra):
    if i % 10 == 0:
        mgf.write([file[i]], outputFile)

Or

file = mgf.read(filePath)
nSpectra = len(file)
mgf.write((file[i] for i in range(nSpectra) if i % 10), outputFile)

The first solves the problem by calling write and passing a list of spectra of length 1 for every spectrum you want to write.
The second solves the problem by converting your for-loop to a generator expression and passing…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@hsogaard
Comment options

Answer selected by hsogaard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants