Пример #1
0
def getModelDefaults(default_model):
    params = pms.Param()
    params.setValue("type", pms.DataValue(default_model), "Type of model")
    model_types = [ "linear", "b_spline", "interpolated"]
    if default_model not in model_types:
        model_types.insert(0, default_model)
    params.setValidStrings("type", model_types)

    model_params = pms.Param()

    pms.TransformationModelLinear.getDefaultParameters(model_params)
    params.insert("linear:", model_params)
    params.setSectionDescription("linear", "Parameters for 'linear' model")

    pms.TransformationModelBSpline.getDefaultParameters(model_params)
    params.insert("b_spline:", model_params)
    params.setSectionDescription("b_spline", "Parameters for 'b_spline' model")

    pms.TransformationModelInterpolated.getDefaultParameters(model_params)
    entry = model_params.getEntry("interpolation_type")
    interpolation_types = entry.valid_strings
    if "polynomial" in interpolation_types:
        interpolation_types.remove("polynomial")
    model_params.setValidStrings("interpolation_type", interpolation_types)

    params.insert("interpolated:", model_params)
    params.setSectionDescription("interpolated", "Parameters for 'interpolated' model")
    return params
Пример #2
0
def parseCTDCommandLine(argv, model, openms_param):
    # Configure CTDOpt to use OpenMS style on the command line.
    directives = parse_cl_directives(argv,
                                     input_ctd='ini',
                                     write_tool_ctd='write_ini',
                                     prefix='-')

    if directives[
            "write_tool_ctd"] is not None:  # triggered if -write_ini was provided on CML
        # if called with -write_ini write CTD
        model.write_ctd(directives["write_tool_ctd"])
        exit(0)
    elif directives["input_ctd"] is not None:  # read ctd/ini file
        model = CTDModel(from_file=directives["input_ctd"])
        #        print(model.get_defaults())

        param = pms.Param()
        fh = pms.ParamXMLFile()
        fh.load(directives["input_ctd"], param)
        openms_param.update(param, True)
        return model.get_defaults(), openms_param

    else:  # only command line options provided
        temp = tempfile.NamedTemporaryFile(
            suffix='ini')  # makes sure we get a writable file
        tmp_name = temp.name
        temp.close()  # removes the file

        model.write_ctd(tmp_name)
        param = pms.Param()
        fh = pms.ParamXMLFile()
        fh.load(tmp_name, param)
        openms_param.update(param)
        os.remove(tmp_name)
        return model.parse_cl_args(argv), openms_param
Пример #3
0
    def build_params():
        mtd_params = pyopenms.MassTraceDetection().getDefaults()
        mtd_params.remove("chrom_peak_snr")
        mtd_params.remove("noise_threshold_int")

        epdet_params = pyopenms.ElutionPeakDetection().getDefaults()
        epdet_params.remove("noise_threshold_int")
        epdet_params.remove("chrom_peak_snr")
        epdet_params.remove("chrom_fwhm")

        common_params = pyopenms.Param()
        common_params.setValue(
            "noise_threshold_int", 10.0,
            "Intensity threshold below which peaks are regarded as noise.")
        common_params.setValue(
            "chrom_peak_snr", 3.0,
            "Minimum signal-to-noise a mass trace should have")
        common_params.setValue(
            "chrom_fwhm", 5.0,
            "Expected chromatographic peak width (in seconds).")

        ffm_params = pyopenms.FeatureFindingMetabo().getDefaults()
        ffm_params.remove("chrom_fwhm")

        combined_params = pyopenms.Param()
        combined_params.insert("common_", common_params)
        combined_params.insert("mtd_", mtd_params)
        combined_params.insert("epdet_", epdet_params)
        combined_params.insert("ffm_", ffm_params)

        return combined_params
Пример #4
0
def getDefaultParameters():
    model_param = getModelDefaults("linear")
    algo_param = pms.MapAlignmentAlgorithmPoseClustering().getParameters()
    default = pms.Param()
    default.insert("model:", model_param)
    default.insert("algorithm:", algo_param)
    return default
Пример #5
0
def testFeatureFinderAlgorithmPicked():
    """
    @tests:
     FeatureFinderAlgorithmPicked.__init__
     FeatureFinderAlgorithmPicked.getDefaults
     FeatureFinderAlgorithmPicked.getName
     FeatureFinderAlgorithmPicked.getParameters
     FeatureFinderAlgorithmPicked.getProductName
     FeatureFinderAlgorithmPicked.setName
     FeatureFinderAlgorithmPicked.setParameters
    """
    ff = pyopenms.FeatureFinderAlgorithmPicked()
    p = ff.getDefaults()
    _testParam(p)

    _testParam(ff.getParameters())

    assert ff.getName() == "FeatureFinderAlgorithm"
    assert pyopenms.FeatureFinderAlgorithmPicked.getProductName(
    ) == "centroided"

    ff.setParameters(pyopenms.Param())

    ff.setName("test")
    assert ff.getName() == "test"
Пример #6
0
def algorithm(chromatograms, targeted):
    # Create empty files as input and finally as output
    empty_swath = pyopenms.MSExperiment()
    trafo = pyopenms.TransformationDescription()
    output = pyopenms.FeatureMap()

    # set up featurefinder and run
    featurefinder = pyopenms.MRMFeatureFinderScoring()
    # set the correct rt use values
    scoring_params = pyopenms.MRMFeatureFinderScoring().getDefaults()
    scoring_params.setValue("Scores:use_rt_score", 'false', '')
    featurefinder.setParameters(scoring_params)
    featurefinder.pickExperiment(chromatograms, output, targeted, trafo,
                                 empty_swath)

    # get the pairs
    pairs = []
    simple_find_best_feature(output, pairs, targeted)
    pairs_corrected = pyopenms.MRMRTNormalizer().rm_outliers(pairs, 0.95, 0.6)
    pairs_corrected = [list(p) for p in pairs_corrected]

    # // store transformation, using a linear model as default
    trafo_out = pyopenms.TransformationDescription()
    trafo_out.setDataPoints(pairs_corrected)
    model_params = pyopenms.Param()
    model_params.setValue("symmetric_regression", 'false', '')
    model_type = "linear"
    trafo_out.fitModel(model_type, model_params)
    return trafo_out
Пример #7
0
    def __init__(self):
        self.__dict__ = Elements.__we_are_all_one  # shared state

        if not hasattr(self, "rows"):
            #path = os.path.dirname(os.path.abspath(__file__))
            path = pyopenms.File.find("CHEMISTRY/Elements.xml", [])
            param = pyopenms.Param()
            ph = pyopenms.ParamXMLFile()
            ph.load(path, param)

            data = NestedBunchDict()
            for k, value in param.asDict().items():
                fields = k.split(":")
                element = fields[1]
                kind = fields[2]
                if kind in ["Name", "Symbol", "AtomicNumber"]:
                    data[element][kind] = value
                if kind == "Isotopes":
                    massnumber = int(fields[3])
                    kind = fields[4]
                    data[element]["Isotopes"][massnumber][kind] = value

            colNames = [
                "number", "symbol", "name", "massnumber", "mass", "abundance"
            ]
            colTypes = [int, str, str, int, float, float]
            colFormats = ["%d", "%s", "%s", "%d", "%.10f", "%.3f"]

            rows = []
            for props in data.values():
                row0 = [props.AtomicNumber, props.Symbol, props.Name]
                for k, isoprop in props.Isotopes.items():
                    row = row0 + [
                        k, isoprop.AtomicMass,
                        isoprop.RelativeAbundance / 100.0
                    ]
                    rows.append(row)

            super(Elements, self).__init__(colNames,
                                           colTypes,
                                           colFormats,
                                           rows,
                                           title="Elements")
            self.sortBy("number")

            self.massDict = dict()
            for row in self.rows:
                sym = self.getValue(row, "symbol")
                massnum = self.getValue(row, "massnumber")
                mass = self.getValue(row, "mass")
                self.massDict[sym, massnum] = mass
            syms = set(s for s, _ in self.massDict.keys())
            for sym in syms:
                minmass = min(mass for (s, mass) in self.massDict.keys()
                              if s == sym)
                self.massDict[sym, None] = self.massDict[sym, minmass]

        # borg pattern is sh*t for columns which hold a reference to the
        # table which might change, but the columns stay the same !!!
        self.resetInternals()
Пример #8
0
def main(options):
    out = options.outfile
    chromat_in = options.infile
    traml_in = options.traml_in
    trafo_in = options.trafo_in

    pp = pyopenms.MRMTransitionGroupPicker()


    metabolomics = False
    # this is an important weight for RT-deviation -- the larger the value, the less importance will be given to exact RT matches
    # for proteomics data it tends to be a good idea to set it to the length of
    # the RT space (e.g. for 100 second RT space, set it to 100)
    rt_normalization_factor = 100.0

    pp_params = pp.getDefaults();
    pp_params.setValue("PeakPickerMRM:remove_overlapping_peaks", options.remove_overlapping_peaks, '')
    pp_params.setValue("PeakPickerMRM:method", options.method, '')
    if (metabolomics):
        # Need to change those for metabolomics and very short peaks!
        pp_params.setValue("PeakPickerMRM:signal_to_noise", 0.01, '')
        pp_params.setValue("PeakPickerMRM:peak_width", 0.1, '')
        pp_params.setValue("PeakPickerMRM:gauss_width", 0.1, '')
        pp_params.setValue("resample_boundary", 0.05, '')
        pp_params.setValue("compute_peak_quality", "true", '')
    pp.setParameters(pp_params)

    scorer = pyopenms.MRMFeatureFinderScoring()
    scoring_params = scorer.getDefaults();
    # Only report the top 5 features
    scoring_params.setValue("stop_report_after_feature", 5, '')
    scoring_params.setValue("rt_normalization_factor", rt_normalization_factor, '')
    scorer.setParameters(scoring_params);

    chromatograms = pyopenms.MSExperiment()
    fh = pyopenms.FileHandler()
    fh.loadExperiment(chromat_in, chromatograms)
    targeted = pyopenms.TargetedExperiment();
    tramlfile = pyopenms.TraMLFile();
    tramlfile.load(traml_in, targeted);

    trafoxml = pyopenms.TransformationXMLFile()
    trafo = pyopenms.TransformationDescription()
    if trafo_in is not None:
        model_params = pyopenms.Param()
        model_params.setValue("symmetric_regression", "false", "", [])
        model_type = "linear"
        trafoxml.load(trafo_in, trafo, True)
        trafo.fitModel(model_type, model_params);


    light_targeted = pyopenms.LightTargetedExperiment();
    pyopenms.OpenSwathDataAccessHelper().convertTargetedExp(targeted, light_targeted)
    output = algorithm(chromatograms, light_targeted, pp, scorer, trafo)

    pyopenms.FeatureXMLFile().store(out, output);
Пример #9
0
def updateDefaults(args, defaults):
    if args.ini:
        param = pms.Param()
        fh = pms.ParamXMLFile()
        fh.load(args.ini, param)
        defaults.update(param)
    elif args.dict_ini:
        with open(args.dict_ini, "r") as fp:
            try:
                dd = eval(fp.read())
            except:
                raise Exception("could not parse %s" % args.dict_ini)
        defaults.update(dd)
Пример #10
0
def testTransformationModels():
    """
    @tests:
     TransformationModelBSpline.getDefaultParameters
     TransformationModelBSpline.getParameters
     TransformationModelInterpolated.getDefaultParameters
     TransformationModelInterpolated.getParameters
     TransformationModelLinear.getDefaultParameters
     TransformationModelLinear.getParameters
    """
    for clz in [
            pyopenms.TransformationModelBSpline,
            pyopenms.TransformationModelLinear,
            pyopenms.TransformationModelInterpolated
    ]:
        mod = clz()
        p = pyopenms.Param()
        clz.getDefaultParameters(p)
Пример #11
0
def testFeatureFinder():
    """
    @tests:
     FeatureFinder.__init__
     FeatureFinder.endProgress
     FeatureFinder.getLogType
     FeatureFinder.getParameters
     FeatureFinder.run
     FeatureFinder.setLogType
     FeatureFinder.setProgress
     FeatureFinder.startProgress
    """
    ff = pyopenms.FeatureFinder()
    name = pyopenms.FeatureFinderAlgorithmPicked.getProductName()
    ff.run(name, pyopenms.MSExperiment(), pyopenms.FeatureMap(),
           pyopenms.Param(), pyopenms.FeatureMap())

    _testProgressLogger(ff)

    p = ff.getParameters(name)
    _testParam(p)
Пример #12
0
def _computeTransformation(algo, refMap, fm, numBreakpoints):
    # be careful: alignFeatureMaps modifies second arg,
    # so you MUST NOT put the arg as [] into this
    # function ! in this case you have no access to the calculated
    # transformations.
    import pyopenms
    is_v2 = pyopenms.__version__.startswith("2.0.")
    # ts = []
    # index is 1-based, so 1 refers to refMap when calling
    # alignFeatureMaps below:
    algo.setReference(refMap)
    trafo = pyopenms.TransformationDescription()
    if (refMap == fm):
        trafo.fitModel("identity")
    else:
        algo.align(fm, trafo)
        model_params = pyopenms.Param()
        if is_v2:
            model_params.setValue("num_nodes", numBreakpoints, "", [])
            model_params.setValue("wavelength", 0.0, "", [])
            model_params.setValue("boundary_condition", 2, "", [])
            model_params.setValue("extrapolate", "bspline", "", [])
        else:
            pyopenms.TransformationModelBSpline.getDefaultParameters(
                model_params)
            model_params.setValue("num_breakpoints", numBreakpoints, "", [])
        trafo.fitModel("b_spline", model_params)

        # from here on used:
        # trafo.getDataPoints
        # trafo.apply
        lowess = False
        if lowess:
            dp = trafo.getDataPoints()
            x, y = zip(*dp)
            smoother = None  # smoother_lowess(y, x, frat, iterations)
            trafo = LowessTrafoHolder(smoother, dp)
    return trafo
Пример #13
0
def _computeTransformation(algo, refMap, fm, numBreakpoints):
    # be careful: alignFeatureMaps modifies second arg,
    # so you MUST NOT put the arg as [] into this
    # function ! in this case you have no access to the calculated
    # transformations.
    import pyopenms
    #ts = []
    # index is 1-based, so 1 refers to refMap when calling
    # alignFeatureMaps below:
    algo.setReference(refMap)
    trafo = pyopenms.TransformationDescription()
    if (refMap == fm):
        trafo.fitModel("identity")
    else:
        algo.align(fm, trafo)
        model_params = pyopenms.Param()
        pyopenms.TransformationModelBSpline.getDefaultParameters(model_params)

        model_params.setValue("num_breakpoints", numBreakpoints, "", [])
        trafo.fitModel("b_spline", model_params)
        trafo.getModelParameters(model_params)

    return trafo
Пример #14
0
def main():

    parser = argparse.ArgumentParser(description="PeakPickerHiRes")
    parser.add_argument("-in",
                        action="append",
                        type=str,
                        dest="in_",
                        metavar="input_file",
                        )

    parser.add_argument("-seeds",
                        action="store",
                        type=str,
                        metavar="seeds_file",
                        )

    parser.add_argument("-out",
                        action="append",
                        type=str,
                        metavar="output_file",
                        )

    parser.add_argument("-trafo_out",
                        action="append",
                        type=str,
                        metavar="output_file",
                        )

    parser.add_argument("-ini",
                        action="store",
                        type=str,
                        metavar="ini_file",
                        )

    parser.add_argument("-dict_ini",
                        action="store",
                        type=str,
                        metavar="python_dict_ini_file",
                        )

    parser.add_argument("-write_ini",
                        action="store",
                        type=str,
                        metavar="ini_file",
                        )

    parser.add_argument("-write_dict_ini",
                        action="store",
                        type=str,
                        metavar="python_dict_ini_file",
                        )

    parser.add_argument("-reference:file",
                        action="store",
                        type=str,
                        metavar="reference_file",
                        dest="reference_file",
                        )
    parser.add_argument("-reference:index",
                        action="store",
                        type=int,
                        metavar="reference_index",
                        dest="reference_index",
                        )

    args = parser.parse_args()

    def collect(args):
        return [f.strip() for arg in args or [] for f in arg.split(",")]

    in_files = collect(args.in_)
    out_files = collect(args.out)
    trafo_out_files = collect(args.trafo_out)

    run_mode = args.in_ is not None and args.out is not None\
                and (args.ini is not None or args.dict_ini is not None)
    write_mode = args.write_ini is not None or args.write_dict_ini is not None
    ok = run_mode or write_mode
    if not ok:
        parser.error("either specify -in, -(trafo_)out and -(dict)ini for running "
                     "the peakpicker\nor -write(dict)ini for creating std "
                     "ini file")

    defaults = getDefaultParameters()

    if args.write_dict_ini or args.write_ini:
        if args.write_dict_ini:
            with open(args.write_dict_ini, "w") as fp:
                pprint.pprint(defaults.asDict(), stream=fp)
        if args.write_ini:
            defaults.store(args.write_ini)

    else:
        if args.ini:
            param = pms.Param()
            param.load(args.ini)
            defaults.update(param, False, False)
        elif args.dict_ini:
            with open(args.dict_ini, "r") as fp:
                try:
                    dd = eval(fp.read())
                except:
                    raise Exception("could not parse %s" % args.dict_ini)
            defaults.updateFrom(dd)

        if not out_files and not trafo_out_files:
            parser.error("need -out or -trafo_out files")

        if out_files and len(out_files) != len(in_files):
            parser.error("need as many -out files as -in files")
        if trafo_out_files and len(trafo_out_files) != len(in_files):
            parser.error("need as many -trafo_out files as -in files")

        if args.reference_index is not None and args.reference_file is not None:
            parser.error("can only handle either reference:index or reference:file")

        if args.reference_index is not None:
            if args.reference_index <0 or args.reference_index >= len(in_files):
                parser.error("reference:index invalid")

        in_types = set(pms.FileHandler.getType(in_) for in_ in in_files)

        if in_types <= set((pms.Type.MZML, pms.Type.MZXML, pms.Type.MZDATA)):
            pass
        elif in_types == set((pms.Type.FEATUREXML,)):
            pass
        else:
            parser.error("different kinds of input files")

        align(in_files, out_files, trafo_out_files, args.reference_index or 0,
                args.reference_file or "", defaults)
Пример #15
0
def _testParam(p):
    """
    @tests:
     Param.__init__
     Param.addTag
     Param.addTags
     Param.asDict
     Param.clearTags
     Param.copy
     Param.exists
     Param.getDescription
     Param.getEntry
     Param.getSectionDescription
     Param.getTags
     Param.getValue
     Param.hasTag
     Param.insert
     Param.load
     Param.setMaxFloat
     Param.setMaxInt
     Param.setMinFloat
     Param.setMinInt
     Param.setSectionDescription
     Param.setValidStrings
     Param.setValue
     Param.size
     Param.store
     Param.update
     Param.updateFrom
     Param.__eq__
     Param.__ge__
     Param.__gt__
     Param.__le__
     Param.__lt__
     Param.__ne__
     ParamEntry.__init__
     ParamEntry.description
     ParamEntry.isValid
     ParamEntry.max_float
     ParamEntry.max_int
     ParamEntry.min_float
     ParamEntry.min_int
     ParamEntry.name
     ParamEntry.tags
     ParamEntry.valid_strings
     ParamEntry.value
     ParamEntry.__eq__
     ParamEntry.__ge__
     ParamEntry.__gt__
     ParamEntry.__le__
     ParamEntry.__lt__
     ParamEntry.__ne__
    """

    assert p == p

    dd = p.asDict()
    assert len(dd) == p.size()
    assert isinstance(dd, dict)

    keys = dd.keys()
    for k in keys:
        value = p.getValue(k)
        desc = p.getDescription(k)
        tags = p.getTags(k)
        p.setValue(k, value, desc, tags)
        p.setValue(k, value, desc)
        assert p.exists(k)
        f = k.split(":")[0]
        p.setSectionDescription(f, k)
        assert p.getSectionDescription(f) == k

    assert not p.exists("asdflkj01231321321v")
    p.addTag(k, "a")
    p.addTags(k, ["b", "c"])
    assert sorted(p.getTags(k)) == ["a", "b", "c"]
    p.clearTags(k)
    assert p.getTags(k) == []

    pn = pyopenms.Param()
    pn.insert("master:", p)
    assert pn.exists("master:" + k)

    p1 = pn.copy("master:", True)
    assert p1 == p

    p1.update(p, True, False)

    p.setValidStrings
    p.setMinFloat
    p.setMaxFloat
    p.setMinInt
    p.setMaxInt
    p.store("test.ini")
    p1 = pyopenms.Param()
    p1.load("test.ini")
    assert p == p1

    e1 = p1.getEntry(k)
    for f in [
            "name", "description", "value", "tags", "valid_strings",
            "min_float", "max_float", "min_int", "max_int"
    ]:
        assert getattr(e1, f) is not None

    assert e1 == e1
Пример #16
0
def main():

    parser = argparse.ArgumentParser(description="PeakPickerHiRes")
    parser.add_argument(
        "-in",
        action="store",
        type=str,
        dest="in_",
        metavar="input_file",
    )

    parser.add_argument(
        "-out",
        action="store",
        type=str,
        metavar="output_file",
    )

    parser.add_argument(
        "-ini",
        action="store",
        type=str,
        metavar="ini_file",
    )

    parser.add_argument(
        "-dict_ini",
        action="store",
        type=str,
        metavar="python_dict_ini_file",
    )

    parser.add_argument(
        "-write_ini",
        action="store",
        type=str,
        metavar="ini_file",
    )

    parser.add_argument(
        "-write_dict_ini",
        action="store",
        type=str,
        metavar="python_dict_ini_file",
    )

    args = parser.parse_args()

    run_mode = args.in_ is not None and args.out is not None\
                and (args.ini is not None or args.dict_ini is not None)
    write_mode = args.write_ini is not None or args.write_dict_ini is not None
    ok = run_mode or write_mode
    if not ok:
        parser.error("either specify -in, -out and -(dict)ini for running "
                     "the peakpicker\nor -write(dict)ini for creating std "
                     "ini file")

    defaults = pms.PeakPickerHiRes().getDefaults()
    if args.write_dict_ini or args.write_ini:
        if args.write_dict_ini:
            with open(args.write_dict_ini, "w") as fp:
                pprint.pprint(defaults.asDict(), stream=fp)
        if args.write_ini:
            defaults.store(args.write_ini)

    else:
        if args.ini:
            param = pms.Param()
            param.load(args.ini)
            defaults.update(param, False, False)
        elif args.dict_ini:
            with open(args.dict_ini, "r") as fp:
                try:
                    dd = eval(fp.read())
                except:
                    raise Exception("could not parse %s" % args.dict_ini)
            defaults.updateFrom(dd)

        fh = pms.MzXMLFile()
        fh.setLogType(pms.LogType.CMD)
        input_map = pms.MSExperiment()
        fh.load(args.in_, input_map)

        run_peak_picker(input_map, defaults, args.out)