Esempio n. 1
0
def loadPeakMap(path=None):
    """ loads mzXML, mzML and mzData files

        If *path* is missing, a dialog for file selection is opened
        instead.
    """

    # local import in order to keep namespaces clean
    import os.path
    import sys
    from pyopenms import MSExperiment, FileHandler
    from ..core.data_types import PeakMap

    path = _prepare_path(path, extensions=["mzML", "mzXML", "mzData"])
    if path is None:
        return None

    # open-ms returns empty peakmap if file not exists, so we
    # check ourselves:
    if not os.path.exists(path):
        raise Exception("file %s does not exist" % path)
    if not os.path.isfile(path):
        raise Exception("path %s is not a file" % path)

    experiment = MSExperiment()
    fh = FileHandler()
    if sys.platform == "win32":
        path = path.replace("/", "\\")  # needed for network shares
    fh.loadExperiment(path, experiment)

    return PeakMap.fromMSExperiment(experiment)
Esempio n. 2
0
def loadPeakMap(path=None):
    """ loads mzXML, mzML and mzData files

        If *path* is missing, a dialog for file selection is opened
        instead.
    """

    # local import in order to keep namespaces clean
    import ms
    import os.path
    import sys
    from   pyopenms import MSExperiment, FileHandler
    from   libms.DataStructures import PeakMap

    if isinstance(path, unicode):
        path = path.encode(sys.getfilesystemencoding())
    elif path is None:
        path = ms.askForSingleFile(extensions="mzML mzXML mzData".split())
        if path is None:
            return None

    # open-ms returns empty peakmap if file not exists, so we
    # check ourselves:
    if not os.path.exists(path):
        raise Exception("file %s does not exist" % path)
    if not os.path.isfile(path):
        raise Exception("path %s is not a file" % path)

    experiment = MSExperiment()
    fh  = FileHandler()
    if sys.platform == "win32":
        path = path.replace("/","\\") # needed for network shares
    fh.loadExperiment(path, experiment)

    return PeakMap.fromMSExperiment(experiment)
Esempio n. 3
0
def storePeakMap(pm, path=None):
    """ Stores peakmap *pm* in mzXML, mzML or mzData format.
        The used format depends on the file extension given
        in *path*. If no *path* is given, a dialog for
        choosing an output file name is opened.
    """

    # local import in order to keep namespaces clean
    import ms
    import sys
    from pyopenms import FileHandler

    if isinstance(path, unicode):
        path = path.encode(sys.getfilesystemencoding())
    elif path is None:
        path = ms.askForSave(extensions="mzML mzXML mzData".split())
        if path is None:
            return None

    if sys.platform == "win32":
        path = path.replace("/","\\") # needed for network shares

    experiment = pm.toMSExperiment()
    fh = FileHandler()
    fh.storeExperiment(path, experiment)
Esempio n. 4
0
def storePeakMap(pm, path=None):
    """ Stores peakmap *pm* in mzXML, mzML or mzData format.
        The used format depends on the file extension given
        in *path*. If no *path* is given, a dialog for
        choosing an output file name is opened.
    """

    # local import in order to keep namespaces clean
    import ms
    import sys
    from pyopenms import FileHandler

    if isinstance(path, unicode):
        path = path.encode(sys.getfilesystemencoding())
    elif path is None:
        path = ms.askForSave(extensions="mzML mzXML mzData".split())
        if path is None:
            return None

    if sys.platform == "win32":
        path = path.replace("/", "\\")  # needed for network shares

    experiment = pm.toMSExperiment()
    fh = FileHandler()
    fh.storeExperiment(path, experiment)
Esempio n. 5
0
def read_ms1_experiment(filepath):
    source_experiment = MSExperiment()
    file_handler = FileHandler()
    # bytes is required by `loadExperiment()` called below
    typed_fp = filepath if isinstance(filepath, bytes) else filepath.encode()
    file_handler.loadExperiment(typed_fp, source_experiment)

    ms1_experiment = MSExperiment()
    for spectrum in source_experiment:
        if spectrum.getMSLevel() == 1:
            ms1_experiment.addSpectrum(spectrum)
    return ms1_experiment
Esempio n. 6
0
    def read_ms1_experiment(filepath):
        # pylint: disable=import-outside-toplevel,no-name-in-module,import-error
        from pyopenms import FileHandler, MSExperiment

        source_experiment = MSExperiment()
        file_handler = FileHandler()
        # bytes is required by `loadExperiment()` called below
        typed_fp = filepath if isinstance(filepath,
                                          bytes) else filepath.encode()
        file_handler.loadExperiment(typed_fp, source_experiment)

        ms1_experiment = MSExperiment()
        for spectrum in source_experiment:
            if spectrum.getMSLevel() == 1:
                ms1_experiment.addSpectrum(spectrum)
        return ms1_experiment
Esempio n. 7
0
def storePeakMap(pm, path=None):
    """ Stores peakmap *pm* in mzXML, mzML or mzData format.
        The used format depends on the file extension given
        in *path*. If no *path* is given, a dialog for
        choosing an output file name is opened.
    """

    # local import in order to keep namespaces clean
    import sys
    from pyopenms import FileHandler

    path = _prepare_path(path, extensions=["mzML", "mzXML", "mzData"], store=False)
    if path is None:
        return None

    if sys.platform == "win32":
        path = path.replace("/", "\\")  # needed for network shares

    experiment = pm.toMSExperiment()
    fh = FileHandler()
    fh.storeExperiment(path, experiment)
Esempio n. 8
0
    def test001(self):
        exp = MSExperiment()
        basename = "SHORT_MS2_FILE.mzData"
        here = os.path.dirname(os.path.abspath(__file__))
        FileHandler().loadExperiment(os.path.join(here, "data", basename), exp)
        assert exp.size() > 0

        pc = Precursor()
        pc.setMZ(1.0)
        pc.setIntensity(100)
        s0 = exp[0]
        s0.setPrecursors([pc])
        s0.setMSLevel(2)
        spec = Spectrum.fromMSSpectrum(s0)
        settings = InstrumentSettings()
        settings.setPolarity(IonSource.Polarity.POSITIVE)
        s0.setInstrumentSettings(settings)

        self.compare_specs(spec, s0)

        specneu = Spectrum.fromMSSpectrum(spec.toMSSpectrum())

        self.compare_specs(specneu, s0)

        pm = PeakMap.fromMSExperiment(exp)
        assert os.path.basename(pm.meta["source"]) == basename

        rtmin, rtmax = pm.rtRange(None)
        ms1s = pm.msNPeaks(1, rtmin, rtmax)
        assert ms1s.shape == (1797, 2), ms1s.shape

        ms1s2 = pm.msNPeaks(1, rtmax=rtmax)
        assert np.all(ms1s == ms1s2)

        ms1s3 = pm.msNPeaks(1, rtmin=0)
        assert np.all(ms1s == ms1s3)

        spec = pm.spectra[0]
        assert len(list(spec)) == len(spec)  # calls iter

        allrts = pm.allRts()
        assert allrts[0] <= pm.rtRange()[0]
        assert allrts[-1] >= pm.rtRange()[1]
        assert len(allrts) == 41, len(allrts)

        level1 = pm.levelNSpecs(1, 1)
        level2 = pm.levelNSpecs(2, 2)
        level12 = pm.levelNSpecs(1, 2)
        assert len(level1) > 0
        assert len(level2) > 0
        assert len(level1) + len(level2) == len(level12) == len(pm)
        assert level1[0].msLevel == 1
        assert level2[0].msLevel == 2

        # use default arg: nmax = nmin if not provided:
        level1 = pm.levelNSpecs(1)
        level2 = pm.levelNSpecs(2)
        assert len(level1) > 0
        assert len(level2) > 0
        assert len(level1) + len(level2) == len(level12) == len(pm)
        assert level1[0].msLevel == 1
        assert level2[0].msLevel == 2

        lone = pm.levelOneRts()
        assert len(lone) == len(level1)

        self.compare_exp(pm, exp, basename)
        pm2 = PeakMap.fromMSExperiment(pm.toMSExperiment())
        self.compare_exp(pm2, exp, basename)

        pm2 = pm.extract(rtmin=rtmin + .000001)
        assert len(pm2) == len(pm) - 1
        pm2 = pm2.extract(rtmax=rtmax - 0.000001)
        assert len(pm2) == len(pm) - 2

        mzmin, mzmax = pm.mzRange(2)

        assert mzmin < 250
        assert mzmax > 860

        mzmin, mzmax = pm.mzRange(1)

        assert mzmin >= 700
        assert mzmax <= 1050

        pm2 = pm.extract(rtmin + 0.00001, mzmin=300)

        mzmin2, mzmax2 = pm2.mzRange()
        assert mzmin2 >= 300
        assert mzmax2 == mzmax

        pm2 = pm.extract(rtmin=rtmin + 0.000001, mzmin=300, mzmax=1000)
        mzmin2, mzmax2 = pm2.mzRange()
        assert mzmin2 >= 300
        assert mzmax2 <= 1000

        with pytest.raises(Exception):
            pm.spectra[0].peaksInRange()

        pp1 = pm.spectra[0].peaksInRange(mzmax=10000)
        pp2 = pm.spectra[0].peaksInRange(mzmin=0)
        assert np.all(pp1 == pp2)

        specs0 = list(pm.spectra)
        specs1 = pm.specsInRange(0, 99999)
        specs2 = pm.specsInRange(0, specs0[0].rt)
        specs3 = pm.specsInRange(specs0[-1].rt, 999999)

        assert specs0 == specs1
        assert specs2 == [specs0[0]]
        assert specs3 == [specs0[-1]]

        pm.spectra[0].polarity = "+"
        pm.spectra[1].polarity = "-"

        pm = PeakMap(pm.spectra)
        mz = pm.representingMzPeak(0, 99999, 0, 99999)
        assert abs(mz - 831.86538) < 0.0001
Esempio n. 9
0
    def process(self, peakMap):
        assert isinstance(peakMap, PeakMap)
        if len(peakMap) == 0:
            raise Exception("empty peakmap")

        temp_peakmap = _get_temp_peakmap(self.config.get("msLevel"), peakMap)
        minRt = peakMap.spectra[0].rt
        # matched filter  does not like rt <= 0, so we shift that rt starts
        # with 1.0: we have to undo this shift later when parsing the output of
        # xcms
        shift = minRt - 1.0
        peakMap.shiftRt(-shift)

        td = tempfile.mkdtemp(prefix="emzed_r_script_matched_filter_")

        temp_input = os.path.join(td, "input.mzData")
        temp_output = os.path.join(td, "output.csv")

        # needed for network shares:
        if sys.platform == "win32":
            temp_input = temp_input.replace("/", "\\")

        FileHandler().storeExperiment(temp_input, peakMap.toMSExperiment())

        dd = self.config.copy()
        dd["temp_input"] = temp_input
        dd["temp_output"] = temp_output
        dd["index"] = str(dd["index"]).upper()

        script = """
                 library(xcms)
                 xs <- xcmsSet(%(temp_input)r, method="matchedFilter",
                                 fwhm = %(fwhm)f, sigma = %(sigma)f,
                                 max = %(max_)d,
                                 snthresh = %(snthresh)f,
                                 step = %(step)f, steps=%(steps)d,
                                 mzdiff = %(mzdiff)f,
                                 index = %(index)s,
                                 sleep=0
                                 )
                 peaks <- data.frame(xs@peaks)
                 """ % dd

        del dd["temp_input"]
        del dd["temp_output"]

        table = execute(script).get_df_as_table("peaks",
                                                types=dict(mz=float,
                                                           mzmin=float,
                                                           mzmax=float,
                                                           rt=float,
                                                           rtmin=float,
                                                           ftmax=float,
                                                           into=float,
                                                           intf=float,
                                                           maxo=float,
                                                           maxf=float,
                                                           i=int,
                                                           sn=float,
                                                           sample=int),
                                                formats=dict(
                                                    mz="%10.5f",
                                                    mzmin="%10.5f",
                                                    mzmax="%10.5f",
                                                    rt=formatSeconds,
                                                    rtmin=formatSeconds,
                                                    rtmax=formatSeconds,
                                                    into="%.2e",
                                                    intf="%.2e",
                                                    maxo="%.2e",
                                                    maxf="%.2e",
                                                    sn="%.2e"))

        # parse csv and
        table.addConstantColumn("matchedfilter_config", dd, dict, None)
        table.meta["generator"] = "xcms.matchedfilter"
        decorate(table, temp_peakmap)
        # undo shiftRt done above:
        table.rtmin += shift
        table.rtmax += shift
        table.rt += shift
        return table
Esempio n. 10
0
    def process(self, peakMap):
        assert isinstance(peakMap, PeakMap)
        if len(peakMap) == 0:
            raise Exception("empty peakmap")

        temp_peakmap = _get_temp_peakmap(self.config.get("msLevel"), peakMap)

        td = tempfile.mkdtemp(prefix="emzed_r_script_centwave_")

        temp_input = os.path.join(td, "input.mzData")
        temp_output = os.path.join(td, "output.csv")

        # needed for network shares:
        if sys.platform == "win32":
            temp_input = temp_input.replace("/", "\\")

        FileHandler().storeExperiment(temp_input,
                                      temp_peakmap.toMSExperiment())

        dd = self.config.copy()
        dd["temp_input"] = temp_input
        dd["temp_output"] = temp_output
        dd["fitgauss"] = str(dd["fitgauss"]).upper()
        dd["verbose_columns"] = str(dd["verbose_columns"]).upper()

        script = """
                 library(xcms)
                 xs <- xcmsSet(%(temp_input)r, method="centWave",
                                     ppm=%(ppm)d,
                                     peakwidth=c%(peakwidth)r,
                                     prefilter=c%(prefilter)r,
                                     snthresh = %(snthresh)f,
                                     integrate= %(integrate)d,
                                     mzdiff   = %(mzdiff)f,
                                     noise    = %(noise)f,
                                     fitgauss = %(fitgauss)s,
                                     verbose.columns = %(verbose_columns)s,
                                     mzCenterFun = %(mzCenterFun)r
                                 )
                 print(xs@peaks)
                 peaks <- data.frame(xs@peaks)
                 """ % dd

        del dd["temp_input"]
        del dd["temp_output"]

        table = execute(script).get_df_as_table("peaks",
                                                types=dict(mz=float,
                                                           mzmin=float,
                                                           mzmax=float,
                                                           rt=float,
                                                           rtmin=float,
                                                           ftmax=float,
                                                           into=float,
                                                           intb=float,
                                                           maxo=float,
                                                           sn=float,
                                                           sample=int),
                                                formats=dict(
                                                    mz="%10.5f",
                                                    mzmin="%10.5f",
                                                    mzmax="%10.5f",
                                                    rt=formatSeconds,
                                                    rtmin=formatSeconds,
                                                    rtmax=formatSeconds,
                                                    into="%.2e",
                                                    intb="%.2e",
                                                    maxo="%.2e",
                                                    sn="%.2e",
                                                    peakmap="%s"))

        table.addConstantColumn("centwave_config", dd, dict, None)
        table.meta["generator"] = "xcms.centwave"
        decorate(table, temp_peakmap)
        return table