bittremieux / spectrum_utils

Python package for efficient mass spectrometry data processing and visualization
https://spectrum-utils.readthedocs.io/
Apache License 2.0
140 stars 20 forks source link

Butterfly Plot #13

Closed BenSamy2020 closed 3 years ago

BenSamy2020 commented 3 years ago

Greetings,

I am a new user to your tool and also and newbie in python. Sorry for the trouble.

I am currently, trying to mirror two fragment ion spectrum for the same peptide (same modification, charge and RT) from two different .mgf files. Based on my low level knowledge in python coding language, what I can understanding from your sample script written for mirror plotting (script provided at the bottom for your reference) ------> The script will only enable mirror of fragment ion fragment to theoretical ion fragment profile of my peptide of interest. If my understanding is correct, by any chance will you be able to provide me with a script utilizing your tool to mirror two same fragment ion fragment from two different .mgf files?

Lastly, I would like to thank you in advance for your assistance with my query.

Regards, Ben

import matplotlib.pyplot as plt import spectrum_utils.plot as sup import spectrum_utils.spectrum as sus from pyteomics import mgf

spectra = [] for spectrum_dict in mgf.read('spectra.mgf'): if 'DLTDYLMK' in spectrum_dict['params']['title']: identifier = spectrum_dict['params']['title'] precursor_mz = spectrum_dict['params']['pepmass'][0] precursor_charge = spectrum_dict['params']['charge'][0] mz = spectrum_dict['m/z array'] intensity = spectrum_dict['intensity array'] retention_time = float(spectrum_dict['params']['rtinseconds']) peptide = 'DLTDYLMK' modifications = {6: 15.994915}

    # Create the MS/MS spectrum.
    spectra.append(sus.MsmsSpectrum(identifier, precursor_mz,
                                    precursor_charge, mz, intensity,
                                    retention_time=retention_time,
                                    peptide=peptide,
                                    modifications=modifications)
                   .filter_intensity(0.01, 50)
                   .scale_intensity('root')
                   .annotate_peptide_fragments(0.5, 'Da', ion_types='aby'))

fig, ax = plt.subplots(figsize=(12, 6)) spectrum_top, spectrum_bottom = spectra sup.mirror(spectrum_top, spectrum_bottom, ax=ax) plt.show() plt.close()

bittremieux commented 3 years ago

Hi Ben, indeed, the annotate_peptide_fragments function and subsequent plotting will color fragments that match the theoretical fragments of the peptide. If I understand your question correctly, instead you want to highlight peaks that match between both spectra? That isn't directly supported, but you should still be able to do it with some "hacking".

To highlight matching peaks you first need to determine those peaks. You could do that like this:

fragment_mz_tolerance = 0.05
peak_matches = []
j = 0
for i in range(len(spectrum1.mz)):
    while j < len(spectrum2.mz) and spectrum2.mz[j] + fragment_mz_tolerance < spectrum1.mz[i]:
        j += 1
    if abs(spectrum1.mz[i] - spectrum2.mz[j]) < fragment_mz_tolerance:
        peak_matches.append((i, j))

Next, you need to annotate the matching peaks:

import spectrum_utils.spectrum as sus

spectrum1.annotation = np.full_like(spectrum1.mz, None, object)
spectrum2.annotation = np.full_like(spectrum2.mz, None, object)
for i, j in peak_matches:
    fragment_annotation = sus.PeptideFragmentAnnotation(1, spectrum1.mz[i], 'z', 0)
    fragment_annotation.ion_type = 'match'
    spectrum1.annotation[peak_match[0]] = spectrum2.annotation[peak_match[1]] = \
        fragment_annotation

Finally, choose a color for the matching peaks and plot the spectra:

import spectrum_utils.plot as sup

sup.colors['match'] = 'red'

fig, ax = plt.subplots(figsize=(12, 6))
sup.mirror(spectrum1, spectrum2, {'color_ions': True, 'annotate_ions': False}, ax=ax)
plt.show()
plt.close()

Please let me know if this works for you.

Note: I just wrote the code snippets here without testing them, so maybe some minor fixes are needed to actually run it.

BenSamy2020 commented 3 years ago

Greetings,

Sorry for the late response. Your suggestions works!