This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to merge mgf files?

I was wondering if there is an easy way to merge mgf (mass-spec) files with Python and Bioconductor. Thanks!!!

proteomics

1 answer

If none of the MGF files have parameters set at the top of the file, merging MGF files is as simple as concatenating two text files.

#!/usr/bin/python
with open("output.mgf", 'w') as fh:
   fh.write(open("a.mgf", 'r').read())
   fh.write("\n")
   fh.write(open("b.mgf", 'r').read())
   fh.write("\n")
   # or a for-loop if you have more than two files

If there are parameters at the top of both files, and they are not the same from file to file, and you don't care about preserving them, you can skip all the lines in each file before the first occurrence of BEGIN IONS. If you do care about preserving the top-level parameters, there isn't a standard way to merge files, in part because the meaning and interpretation of those parameters aren't standardized.

Log in to answer this question.