Example #1
0
def integrate_matching_fragments(tables, transitions, delta_mz):
    """extracts mass transitions defined in transition table 'transitions'
    with column names 'name', 'precursor', 'fragment', 'rtmin', 'rtmax'.

    Detecting the peaks does not use a peak detector as centWave, but uses mz
    and rt ranges to fit a EMG model to the underlying raw peaks m/z traces.
    This avoids cumbersome parameter optimization of a peak detector and
    returns all peaks irrespective of filtering according to some heuristic
    criterion for peak quality.
    """

    table = ms.mergeTables(tables)
    # To shorten the name in commands we introduce abbreviations
    trans = transitions
    t = table

    # table has columns
    #
    #    precursor  fragment  peakmap  mzmin  mzmax
    #
    # and transitions:
    #
    #   name  precursor  fragment  rtmin  rtmax

    # find candidates defined in parameter table transitions
    identified = t.join(trans,
                        trans.precursor.approxEqual(t.precursor, delta_mz)\
                        & trans.fragment.approxEqual(t.fragment, delta_mz))

    # identified has columns:

    #   precursor  fragment  peakman  mzmin  mzmax  name__0  precursor__0
    #   ...  fragment__0  rtmin__0 rtmax__0

    identified.renameColumns(name__0="name",
                             rtmin__0="rtmin",
                             rtmax__0="rtmax")

    identified = identified.extractColumns("name", "precursor", "fragment",
                                           "mzmin", "mzmax", "rtmin", "rtmax",
                                           "peakmap")

    # now we have columns mzmin, mzmax, rtmin, rtmax and peakmap which we
    # need for fitting EMG model with ms.integrate:
    identified = ms.integrate(identified, "emg_exact", msLevel=2)
    return identified.splitBy("name")
Example #2
0
def integrate_matching_fragments(tables, transitions, delta_mz):
    """extracts mass transitions defined in transition table 'transitions'
    with column names 'name', 'precursor', 'fragment', 'rtmin', 'rtmax'.

    Detecting the peaks does not use a peak detector as centWave, but uses mz
    and rt ranges to fit a EMG model to the underlying raw peaks m/z traces.
    This avoids cumbersome parameter optimization of a peak detector and
    returns all peaks irrespective of filtering according to some heuristic
    criterion for peak quality.
    """

    table = ms.mergeTables(tables)
    # To shorten the name in commands we introduce abbreviations
    trans = transitions
    t = table

    # table has columns
    #
    #    precursor  fragment  peakmap  mzmin  mzmax
    #
    # and transitions:
    #
    #   name  precursor  fragment  rtmin  rtmax

    # find candidates defined in parameter table transitions
    identified = t.join(trans,
                        trans.precursor.approxEqual(t.precursor, delta_mz)\
                        & trans.fragment.approxEqual(t.fragment, delta_mz))

    # identified has columns:

    #   precursor  fragment  peakman  mzmin  mzmax  name__0  precursor__0
    #   ...  fragment__0  rtmin__0 rtmax__0

    identified.renameColumns(name__0="name",
                             rtmin__0="rtmin",
                             rtmax__0="rtmax")

    identified = identified.extractColumns("name", "precursor", "fragment",
                                           "mzmin", "mzmax", "rtmin", "rtmax",
                                           "peakmap")

    # now we have columns mzmin, mzmax, rtmin, rtmax and peakmap which we
    # need for fitting EMG model with ms.integrate:
    identified = ms.integrate(identified, "emg_exact", msLevel=2)
    return identified.splitBy("name")
Example #3
0
def split_srm_peakmap_to_tables(peakmap, n_digits=2):
    """ Processes a srm/mrm peakmap.
    The result is a list of tables with chromatographic peaks of MS2 data. The
    peaks are integrated over the full time range of the individual MS2
    peakmaps.

    n_digits is the precision of the precursor m/z values.

    Detecting the peaks does not use a peak detector as centWave, but uses
    mz ranges to fit a EMG model to the underlying raw peaks m/z traces. This avoids
    cumbersome parameter optimization of a peak detector and returns all peaks
    irrespective of filtering according to some heuristic criterion for peak
    quality.
    """

    result = []
    ms2_maps = peakmap.splitLevelN(2, n_digits)

    # half resolution according to n_digits:
    delta_mz = 0.5 * (0.1)**n_digits

    for pre_mz, ms2_map in ms2_maps:

        # get unique m/z values in level 2 map in ascending order:
        ions = sorted(set(mz for mz, I in ms2_map.msNPeaks(2)))

        # build a  table with 'number of ions' rows
        table = ms.toTable("precursor", [pre_mz] * len(ions))
        table.addColumn("fragment_ion", ions)

        # Set rt range for later integration. We do no specific peak detection
        # here but use the full time range:
        rtmin, rtmax = ms2_map.rtRange()
        table.addColumn("rtmin", rtmin)
        table.addColumn("rtmax", rtmax)
        table.addColumn("mzmin", table.fragment_ion - delta_mz)
        table.addColumn("mzmax", table.fragment_ion + delta_mz)
        table.addColumn("peakmap", ms2_map)

        # Now rtmin/rtmax, mzmin/mzmax and peakmap columns are created. These
        # are mandatory for fitting and integrating peaks with ms.integrate:
        table = ms.integrate(table, "emg_exact")
        result.append(table)

    return result
Example #4
0
def split_srm_peakmap_to_tables(peakmap, n_digits=2):
    """ Processes a srm/mrm peakmap.
    The result is a list of tables with chromatographic peaks of MS2 data. The
    peaks are integrated over the full time range of the individual MS2
    peakmaps.

    n_digits is the precision of the precursor m/z values.

    Detecting the peaks does not use a peak detector as centWave, but uses
    mz ranges to fit a EMG model to the underlying raw peaks m/z traces. This avoids
    cumbersome parameter optimization of a peak detector and returns all peaks
    irrespective of filtering according to some heuristic criterion for peak
    quality.
    """

    result = []
    ms2_maps = peakmap.splitLevelN(2, n_digits)

    # half resolution according to n_digits:
    delta_mz = 0.5 * (0.1)**n_digits

    for pre_mz, ms2_map in ms2_maps:

        # get unique m/z values in level 2 map in ascending order:
        ions = sorted(set(mz for mz, I in ms2_map.msNPeaks(2)))

        # build a  table with 'number of ions' rows
        table = ms.toTable("precursor", [pre_mz] * len(ions))
        table.addColumn("fragment_ion", ions)

        # Set rt range for later integration. We do no specific peak detection
        # here but use the full time range:
        rtmin, rtmax = ms2_map.rtRange()
        table.addColumn("rtmin", rtmin)
        table.addColumn("rtmax", rtmax)
        table.addColumn("mzmin", table.fragment_ion - delta_mz)
        table.addColumn("mzmax", table.fragment_ion + delta_mz)
        table.addColumn("peakmap", ms2_map)

        # Now rtmin/rtmax, mzmin/mzmax and peakmap columns are created. These
        # are mandatory for fitting and integrating peaks with ms.integrate:
        table = ms.integrate(table, "emg_exact")
        result.append(table)

    return result
Example #5
0
def integrate_fragments(measured_transitions):
    """
    Fit a EMG model to each fragment. we use the full rt range as rt limits.
    As this model fits against the peak with maximal intensity, this approach
    is feasible for exploration.
    """
    result = []
    for table in measured_transitions:
        # column peakmap has one unique value, take this to get values
        # for rtmin and rtmax:
        rtmin, rtmax = table.peakmap.uniqueValue().rtRange()
        table.addColumn("rtmin", rtmin)
        table.addColumn("rtmax", rtmax)

        # Now table has columns rtmin/rtmax, mzmin/mzmax and peakmap. These are
        # mandatory for fitting EMG model with ms.integrate:
        table = ms.integrate(table, "emg_exact", msLevel=2)
        result.append(table)

    return result
Example #6
0
def integrate_fragments(measured_transitions):
    """
    Fit a EMG model to each fragment. we use the full rt range as rt limits.
    As this model fits against the peak with maximal intensity, this approach
    is feasible for exploration.
    """
    result=[]
    for table in measured_transitions:
        # column peakmap has one unique value, take this to get values
        # for rtmin and rtmax:
        rtmin, rtmax = table.peakmap.uniqueValue().rtRange()
        table.addColumn("rtmin", rtmin)
        table.addColumn("rtmax", rtmax)

        # Now table has columns rtmin/rtmax, mzmin/mzmax and peakmap. These are
        # mandatory for fitting EMG model with ms.integrate:
        table = ms.integrate(table, "emg_exact", msLevel=2)
        result.append(table)

    return result
Example #7
0
def testActions():

    t = buildTable()
    n = len(t)
    recorder = RecordingObject()
    model = TableModel(t, recorder)
    t_orig = t.copy()

    action = DeleteRowAction(model, 0)
    action.do()
    assert len(model.table) == len(t_orig) - 1
    assert model.table.rows[0] == t_orig.rows[1]
    action.undo()
    assert len(model.table) == len(t_orig)
    assert model.table.rows[0] == t_orig.rows[0]

    action = CloneRowAction(model, 0)
    action.do()
    assert model.table.rows[0] == t_orig.rows[0]
    assert model.table.rows[1] == t_orig.rows[0]
    assert model.table.rows[2] == t_orig.rows[1]
    assert len(model.table) == n + 1
    action.undo()
    assert len(model.table) == n
    assert model.table.rows[0] == t_orig.rows[0]
    assert model.table.rows[1] == t_orig.rows[1]

    action = SortTableAction(model, 0, 0, Qt.AscendingOrder)
    action.do()
    assert model.table.mz.values == [None, 1.0, 2.0]
    action.undo()
    assert model.table.mz.values == t_orig.mz.values

    action = SortTableAction(model, 0, 0, Qt.DescendingOrder)
    action.do()
    assert model.table.mz.values == [2.0, 1.0, None]
    action.undo()
    assert model.table.mz.values == t_orig.mz.values

    class Index(object):
        def row(self):
            return 0

        def column(self):
            return 0

    action = ChangeValueAction(model, Index(), 0, 3.0)
    action.do()
    assert model.table.rows[0][0] == 3.0
    action.undo()
    assert model.table.rows[0][0] == 1.0

    t = buildTable()
    from libms.DataStructures.MSTypes import *
    import numpy
    peak = numpy.array(((1.0, 100.0), ))
    specs = [Spectrum(peak, rt, 1, "+") for rt in range(9, 15)]
    pm = PeakMap(specs)

    t.replaceColumn("peakmap", pm)

    model.table = ms.integrate(t, "no_integration")

    action = IntegrateAction(model, 0, "", "trapez", 0, 100)
    action.do()
    assert model.table.area.values[0] == 500.0
    action.undo()

    assert model.table.area.values[0] == None
Example #8
0
def testIntegration():

    # test with and without unicode:
    ft = ms.loadTable(u"data/features.table")
    # an invalid row should not stop integration, but result
    # in None values for ms.integrate generated columns
    ftr = ms.integrate(ft, "trapez")
    assert len(ftr) == len(ft)
    assert "area" in ftr.getColNames()
    assert "rmse" in ftr.getColNames()
    assert ftr.area.values[0] > 0, ftr.area.values[0]
    assert ftr.rmse.values[0] >= 0, ftr.rmse.values[0]
    assert ftr.params.values[0] is not None
    assert ftr.method.values[0] is not None

    ft.setValue(ft.rows[0], "mzmin", None)

    ft._addColumnWithoutNameCheck("mzmin__0", ft.mzmin)
    ft._addColumnWithoutNameCheck("mzmax__0", ft.mzmax)
    ft._addColumnWithoutNameCheck("rtmin__0", ft.rtmin)
    ft._addColumnWithoutNameCheck("rtmax__0", ft.rtmax)
    ft._addColumnWithoutNameCheck("peakmap__0", ft.peakmap)

    ft.addColumn("mzminX", ft.mzmin)
    ft.addColumn("mzmaxX", ft.mzmax)
    ft.addColumn("rtminX", ft.rtmin)
    ft.addColumn("rtmaxX", ft.rtmax)
    ft.addColumn("peakmapX", ft.peakmap)

    ftr = ms.integrate(ft, "trapez")
    ftr.info()
    assert len(ftr) == len(ft)
    assert "area" in ftr.getColNames()
    assert "rmse" in ftr.getColNames()
    assert "area__0" in ftr.getColNames()
    assert "rmse__0" in ftr.getColNames()
    assert "areaX" in ftr.getColNames()
    assert "rmseX" in ftr.getColNames()

    assert ftr.area.values[0] is None
    assert ftr.rmse.values[0] is None
    assert ftr.params.values[0] is None
    assert ftr.method.values[0] is not None

    assert ftr.area.values[1] >= 0
    assert ftr.rmse.values[1] >= 0
    assert ftr.params.values[1] is not None
    assert ftr.method.values[1] is not None

    assert ftr.area__0.values[0] is None
    assert ftr.rmse__0.values[0] is None
    assert ftr.params__0.values[0] is None
    assert ftr.method__0.values[0] is not None

    assert ftr.params__0.values[1] is not None
    assert ftr.method__0.values[1] is not None
    assert ftr.area__0.values[1] >= 0
    assert ftr.rmse__0.values[1] >= 0

    assert ftr.areaX.values[0] is None
    assert ftr.rmseX.values[0] is None
    assert ftr.paramsX.values[0] is None
    assert ftr.methodX.values[0] is not None

    assert ftr.paramsX.values[1] is not None
    assert ftr.methodX.values[1] is not None
    assert ftr.areaX.values[1] >= 0
    assert ftr.rmseX.values[1] >= 0
Example #9
0
def testActions():

    t = buildTable()
    n = len(t)
    recorder = RecordingObject()
    model = TableModel(t, recorder)
    t_orig = t.copy()


    action = DeleteRowAction(model, 0)
    action.do()
    assert len(model.table) == len(t_orig)-1
    assert model.table.rows[0] == t_orig.rows[1]
    action.undo()
    assert len(model.table) == len(t_orig)
    assert model.table.rows[0] == t_orig.rows[0]


    action = CloneRowAction(model, 0)
    action.do()
    assert model.table.rows[0] == t_orig.rows[0]
    assert model.table.rows[1] == t_orig.rows[0]
    assert model.table.rows[2] == t_orig.rows[1]
    assert len(model.table) == n+1
    action.undo()
    assert len(model.table) == n
    assert model.table.rows[0] == t_orig.rows[0]
    assert model.table.rows[1] == t_orig.rows[1]


    action = SortTableAction(model, 0, 0, Qt.AscendingOrder)
    action.do()
    assert model.table.mz.values == [ None, 1.0, 2.0]
    action.undo()
    assert model.table.mz.values == t_orig.mz.values


    action = SortTableAction(model, 0, 0, Qt.DescendingOrder)
    action.do()
    assert model.table.mz.values == [ 2.0, 1.0, None]
    action.undo()
    assert model.table.mz.values == t_orig.mz.values

    class Index(object):
        def row(self):
            return 0
        def column(self):
            return 0

    action = ChangeValueAction(model, Index(), 0, 3.0)
    action.do()
    assert model.table.rows[0][0] == 3.0
    action.undo()
    assert model.table.rows[0][0] == 1.0

    t= buildTable()
    from libms.DataStructures.MSTypes import *
    import numpy
    peak = numpy.array(((1.0, 100.0),))
    specs = [Spectrum(peak, rt, 1, "+") for rt in range(9, 15)]
    pm = PeakMap(specs)

    t.replaceColumn("peakmap", pm)


    model.table = ms.integrate(t, "no_integration")

    action = IntegrateAction(model, 0, "", "trapez", 0, 100)
    action.do()
    assert model.table.area.values[0] == 500.0
    action.undo()

    assert model.table.area.values[0] == None
Example #10
0
#encoding: utf-8
import sys

sys.path.insert(0, "..")

import ms

table = ms.loadTable("features.table")

reinttab = ms.integrate(table, "std")
reinttab = ms.integrate(reinttab, "std")

reinttab.store("integrated_features.table", True)
Example #11
0
#encoding: utf-8
import sys
sys.path.insert(0, "..")

import ms

table = ms.loadTable("features.table")

reinttab = ms.integrate(table, "std")
reinttab = ms.integrate(reinttab, "std")

reinttab.store("integrated_features.table", True)
Example #12
0
def testIntegration():

    # test with and without unicode:
    ft = ms.loadTable(u"data/features.table")
    # an invalid row should not stop integration, but result
    # in None values for ms.integrate generated columns
    ftr = ms.integrate(ft, "trapez")
    assert len(ftr) == len(ft)
    assert "area" in ftr.getColNames()
    assert "rmse" in ftr.getColNames()
    assert ftr.area.values[0] > 0, ftr.area.values[0]
    assert ftr.rmse.values[0] >= 0, ftr.rmse.values[0]
    assert ftr.params.values[0] is not None
    assert ftr.method.values[0] is not None

    ft.setValue(ft.rows[0], "mzmin", None)

    ft._addColumnWithoutNameCheck("mzmin__0", ft.mzmin)
    ft._addColumnWithoutNameCheck("mzmax__0", ft.mzmax)
    ft._addColumnWithoutNameCheck("rtmin__0", ft.rtmin)
    ft._addColumnWithoutNameCheck("rtmax__0", ft.rtmax)
    ft._addColumnWithoutNameCheck("peakmap__0", ft.peakmap)

    ft.addColumn("mzminX", ft.mzmin)
    ft.addColumn("mzmaxX", ft.mzmax)
    ft.addColumn("rtminX", ft.rtmin)
    ft.addColumn("rtmaxX", ft.rtmax)
    ft.addColumn("peakmapX", ft.peakmap)

    ftr = ms.integrate(ft, "trapez")
    ftr.info()
    assert len(ftr) == len(ft)
    assert "area" in ftr.getColNames()
    assert "rmse" in ftr.getColNames()
    assert "area__0" in ftr.getColNames()
    assert "rmse__0" in ftr.getColNames()
    assert "areaX" in ftr.getColNames()
    assert "rmseX" in ftr.getColNames()

    assert ftr.area.values[0] is None
    assert ftr.rmse.values[0] is None
    assert ftr.params.values[0] is None
    assert ftr.method.values[0] is not None

    assert ftr.area.values[1] >= 0
    assert ftr.rmse.values[1] >= 0
    assert ftr.params.values[1] is not None
    assert ftr.method.values[1] is not  None

    assert ftr.area__0.values[0] is None
    assert ftr.rmse__0.values[0] is None
    assert ftr.params__0.values[0] is None
    assert ftr.method__0.values[0] is not None

    assert ftr.params__0.values[1] is not None
    assert ftr.method__0.values[1] is not None
    assert ftr.area__0.values[1] >= 0
    assert ftr.rmse__0.values[1] >= 0

    assert ftr.areaX.values[0] is None
    assert ftr.rmseX.values[0] is None
    assert ftr.paramsX.values[0] is None
    assert ftr.methodX.values[0] is not None

    assert ftr.paramsX.values[1] is not None
    assert ftr.methodX.values[1] is not None
    assert ftr.areaX.values[1] >= 0
    assert ftr.rmseX.values[1] >= 0