Created
May 23, 2025 12:52
-
-
Save MatteoLacki/79eed7852beb0e3b30d8d24d043e1a52 to your computer and use it in GitHub Desktop.
Adding inverse ion mobility information from an MGF to an MZML MS Convert spit out but without ion mobility.... stupid mzml format.
This file contains hidden or 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 re | |
import sys | |
def iter_iims( | |
input_mgf: str, | |
iim_pattern: str = r"IonMobility:(\d+\.\d+)", | |
): | |
iim_pattern = re.compile(iim_pattern) | |
with open(input_mgf, "r") as infile: | |
for line in infile: | |
matches = iim_pattern.findall(line) | |
for match in matches: | |
yield float(match) | |
def make_new_mzml( | |
ion_mobility_values: str, | |
input_mzml: str, | |
output_mzml: str, | |
pattern=re.compile( | |
r'<cvParam cvRef="MS" accession="MS:1000016" name="scan start time" ' | |
r'value="\d+\.\d+" unitCvRef="UO" unitAccession="UO:0000010" unitName="second"\s*/>' | |
), | |
line_for_iim: str = ' <cvParam cvRef="MS" accession="MS:1002815" name="inverse reduced ion mobility" value="{iim}" unitCvRef="MS" unitAccession="MS:1002814" unitName="volt-second per square centimeter"/>\n', | |
): | |
i = 0 | |
with open(input_mzml, "r") as infile, open(output_mzml, "w") as outfile: | |
for line in infile: | |
line = line.rstrip("\n") | |
outfile.write(line + "\n") | |
if pattern.fullmatch(line.strip()): | |
iim = ion_mobility_values[i] | |
outfile.write(line_for_iim.format(iim=iim)) | |
i += 1 | |
assert i == len( | |
ion_mobility_values | |
), "Number of IIMs to fill did not match the number of spectra." | |
if __name__ == "__main__": | |
input_mgf, input_mzml, output_mzml = sys.argv[1:] | |
## test | |
# input_mgf = "F17835_mgf_example_5spectra.mgf.txt" | |
# input_mzml = "F17835_mzML_example_5spectra.mzML.txt" | |
# output_mzml = "output.xml" | |
ion_mobility_values = list(iter_iims(input_mgf)) | |
make_new_mzml(ion_mobility_values, input_mzml, output_mzml) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment