Skip to content

Instantly share code, notes, and snippets.

@SJShaw
Created May 28, 2024 14:14
Show Gist options
  • Save SJShaw/5010d19067555b9fb526bc3ad558f4d2 to your computer and use it in GitHub Desktop.
Save SJShaw/5010d19067555b9fb526bc3ad558f4d2 to your computer and use it in GitHub Desktop.
Convert newer OpenMS feature tables to a format accepted by older GNPS
#!/usr/bin/env python3
import argparse
import sys
def convert_lines(lines):
new_lines = []
for line in lines:
# check the modifications are necessary in the first place
if line.startswith("#CONSENSUS") and "row ID" not in line:
return lines
if line.lstrip("#").startswith("CONSENSUS"):
parts = line.split("\t")
line = "\t".join(parts[:7] + parts[8:])
new_lines.append(line)
return new_lines
def main(args):
lines = convert_lines(args.input.read().splitlines())
try:
for line in lines:
args.output.write(f"{line}\n")
except BrokenPipeError:
return 0
return 0
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=(
"Removes the 'row ID' column from OpenMS 3.0 feature qualification tables "
"so that version 28 of GNPS (the webtool version at time of writing) doesn't crash. "
"This bug is fixed in GNPS 30, so the conversion isn't necessary"
),
)
parser.add_argument("input", type=argparse.FileType("r+"), help="The file to convert")
parser.add_argument('-o', '--output-file', dest="output", type=argparse.FileType("w"),
default=sys.stdout, help="A file to write the output to, otherwise it is printed to terminal")
main(parser.parse_args())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment