Skip to content

Documentation for MzmlWriter.py

MzmlWriter

This file provides a class for writing mzML output from simulation. For the actual generating of mzML file, the psims library is used.

MzmlWriter

MzmlWriter(analysis_name, scans)

A class to write peak data to mzML file, typically called after running simulation.

Initialises the mzML writer class.

Parameters:

Name Type Description Default
analysis_name

Name of the analysis.

required
scans

dict where key is scan level, value is a list of Scans object for that level.

required
Source code in vimms/MzmlWriter.py
27
28
29
30
31
32
33
34
35
36
def __init__(self, analysis_name, scans):
    """
    Initialises the mzML writer class.

    Args:
        analysis_name: Name of the analysis.
        scans: dict where key is scan level, value is a list of Scans object for that level.
    """
    self.analysis_name = analysis_name
    self.scans = scans

sort_filter

sort_filter(all_scans, min_scan_id)

Filter scans according to certain criteria. Currently it filters by the minimum scan ID, as a workaround to IAPI which produces unwanted scans at low scan IDs.

Parameters:

Name Type Description Default
all_scans

the list of scans to filter

required
min_scan_id

the minimum scan ID to filter

required

Returns: the list of filtered scans

Source code in vimms/MzmlWriter.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
def sort_filter(self, all_scans, min_scan_id):
    """
    Filter scans according to certain criteria. Currently it filters by
    the minimum scan ID, as a workaround to IAPI which produces unwanted scans at
    low scan IDs.

    Args:
        all_scans: the list of scans to filter
        min_scan_id: the minimum scan ID to filter

    Returns: the list of filtered scans

    """

    new_scans = []
    for s in all_scans:
        if s.scan_id >= min_scan_id:
            if s.num_peaks == 0:  # scans can't be empty, but we can't just remove either...
                s.mzs = np.array([70.0])
                s.intensities = np.array([1.0])
                s.num_peaks = 1
            new_scans.append(s)

    new_scans.sort(key=lambda s: s.rt)
    return new_scans

    all_scans = sorted(all_scans, key=lambda x: x.rt)
    all_scans = [x for x in all_scans if x.num_peaks > 0]
    all_scans = list(filter(lambda x: x.scan_id >= min_scan_id, all_scans))

    # FIXME: why do we need to do this???!!
    # add a single peak to empty scans
    # empty = [x for x in all_scans if x.num_peaks == 0]
    # for scan in empty:
    #     scan.mzs = np.array([100.0])
    #     scan.intensities = np.array([1.0])
    #     scan.num_peaks = 1
    return all_scans

write_mzML

write_mzML(out_file)

Write mzMl to output file

Parameters:

Name Type Description Default
out_file

path to mzML file

required

Returns: None

Source code in vimms/MzmlWriter.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def write_mzML(self, out_file):
    """
    Write mzMl to output file

    Args:
        out_file: path to mzML file

    Returns: None

    """
    # if directory doesn't exist, create it
    out_dir = os.path.dirname(out_file)
    create_if_not_exist(out_dir)

    # start writing mzML here
    with PsimsMzMLWriter(open(out_file, "wb")) as writer:
        # add default controlled vocabularies
        writer.controlled_vocabularies()

        # write other fields like sample list, software list, etc.
        self._write_info(writer)

        # open the run
        with writer.run(id=self.analysis_name):
            self._write_spectra(writer, self.scans)

            # open chromatogram list sections
            with writer.chromatogram_list(count=1):
                tic_rts, tic_intensities = self._get_tic_chromatogram(self.scans)
                writer.write_chromatogram(
                    tic_rts,
                    tic_intensities,
                    id="tic",
                    chromatogram_type="total ion current chromatogram",
                    time_unit="second",
                )

    writer.close()