Skip to content

Instantly share code, notes, and snippets.

@dansondergaard
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save dansondergaard/5f17ab7296b8c9fffac2 to your computer and use it in GitHub Desktop.

Select an option

Save dansondergaard/5f17ab7296b8c9fffac2 to your computer and use it in GitHub Desktop.
import xml.etree.ElementTree as et
import base64
import array
def parse_file(filename_or_obj):
tree = et.parse(filename_or_obj)
for spectrum in tree.findall('.//spectrum'):
mz_elm = spectrum.find('mzArrayBinary').find('data')
intensity_elm = spectrum.find('intenArrayBinary').find('data')
precursor_mz = float(spectrum.find('.//precursorList/precursor/ionSelection/cvParam[@name="selected ion m/z"]').attrib['value'])
precursor_charge = float(spectrum.find('.//precursorList/precursor/ionSelection/cvParam[@name="charge state"]').attrib['value'])
mz = array.array('d', base64.b64decode(mz_elm.text))
intensity = array.array('d', base64.b64decode(intensity_elm.text))
yield precursor_mz, precursor_charge, mz, intensity
if __name__ == '__main__':
import sys
for precursor_mass, precursor_charge, mz, intensity in parse_file(sys.argv[1]):
print "BEGIN IONS"
print "PEPMASS={}".format(precursor_mass)
print "CHARGE={}".format(precursor_charge)
for x, y in zip(mz, intensity):
print x, y
print "END IONS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment