Created
December 20, 2017 22:58
-
-
Save bobvh/ff88f9e342401ea1ab542066c8fe8e6b to your computer and use it in GitHub Desktop.
Strips all positive polarity scans from an mzXML file.
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
#!/usr/bin/env python3 | |
import sys, os, argparse, re, glob | |
import xml.etree.ElementTree as ET | |
# interpret CLI arguments | |
parser = argparse.ArgumentParser(description='Strips all positive polarity scans from an mzXML file.') | |
parser.add_argument('INPUT', nargs='+', help='all files to be processed (wildcards allowed, e.g. *.mzXML)') | |
parser.add_argument('-n', '--negative', help='strip negative polarity scans instead', action='store_true') | |
parser.add_argument('-v', '--verbose', help='display more information', action='store_true') | |
args = parser.parse_args() | |
for f in args.INPUT: | |
if args.verbose: print('Reading {}...'.format(f)) | |
# parse input file | |
try: | |
xml = ET.parse(f) | |
except OSError: | |
print('Error: could not open {}'.format(f)) | |
raise | |
except ET.ParseError as msg: | |
print('Error: could not parse {} as XML.'.format(f)) | |
if args.verbose: print('Details: {}'.format(msg)) | |
continue | |
# extract data: | |
root = xml.getroot() | |
# find the namespace (changes for different versions): | |
m = re.search('\{(.*)\}', root.tag) | |
ns = {'x': m.group(1) if m else ''} | |
# get started on the run: | |
msRun = root.find('x:msRun',ns) | |
if not args.negative: | |
for scan in msRun.findall("x:scan[@polarity='+']",ns): | |
msRun.remove(scan) | |
else: | |
for scan in msRun.findall("x:scan[@polarity='-']",ns): | |
msRun.remove(scan) | |
# write back out: | |
try: | |
path = '{}_stripped.mzXML'.format(os.path.basename(f)[:-6]) | |
ET.register_namespace('',ns['x']) | |
ET.ElementTree(root).write(path) | |
except OSError: | |
print('Error: could not create output file {}'.format(path)) | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment