Ejemplo n.º 1
0
    def test_ms1_controller_with_simulated_chems(self):
        logger.info('Testing MS1 controller with simulated chemicals')

        # create some chemical objects
        chems = ChemicalCreator(self.ps, ROI_Sources, hmdb)
        dataset = chems.sample(mz_range, rt_range, min_ms1_intensity, n_chems, self.ms_level)
        self.assertEqual(len(dataset), n_chems)

        # create a simulated mass spec and MS1 controller
        mass_spec = IndependentMassSpectrometer(POSITIVE, dataset, self.ps)
        controller = SimpleMs1Controller()

        # create an environment to run both the mass spec and controller
        env = Environment(mass_spec, controller, min_rt, max_rt, progress_bar=True)

        # set the log level to WARNING so we don't see too many messages when environment is running
        set_log_level_warning()

        # run the simulation
        env.run()

        # set the log level back to DEBUG
        set_log_level_debug()

        # write simulated output to mzML file
        filename = 'ms1_controller_simulated_chems.mzML'
        out_file = os.path.join(out_dir, filename)
        env.write_mzML(out_dir, filename)
        self.assertTrue(os.path.exists(out_file))
        print()
Ejemplo n.º 2
0
    def test_multiple_adducts(self):
        fs = DatabaseFormulaSampler(HMDB)
        ri = UniformRTAndIntensitySampler(min_rt=100, max_rt=101)
        cs = ConstantChromatogramSampler()
        adduct_prior_dict = {POSITIVE: {'M+H': 100, 'M+Na': 100, 'M+K': 100}}
        cm = ChemicalMixtureCreator(fs,
                                    rt_and_intensity_sampler=ri,
                                    chromatogram_sampler=cs,
                                    adduct_prior_dict=adduct_prior_dict,
                                    adduct_proportion_cutoff=0.0)

        n_adducts = len(adduct_prior_dict[POSITIVE])
        n_chems = 5
        dataset = cm.sample(n_chems, 2)

        for c in dataset:
            c.isotopes = [(c.mass, 1, "Mono")]

        # should be 15 peaks or less all the time
        # some adducts might not be sampled if the probability is less than 0.2
        controller = SimpleMs1Controller()
        ms = IndependentMassSpectrometer(POSITIVE, dataset)
        env = Environment(ms, controller, 102, 110, progress_bar=True)
        set_log_level_warning()
        env.run()
        for scan in controller.scans[1]:
            assert len(scan.mzs) <= n_chems * n_adducts
Ejemplo n.º 3
0
    def test_ms1_controller_with_qcbeer_chems(self):
        logger.info('Testing MS1 controller with QC beer chemicals')

        # create a simulated mass spec and MS1 controller
        mass_spec = IndependentMassSpectrometer(POSITIVE, beer_chems, self.ps)
        controller = SimpleMs1Controller()

        # create an environment to run both the mass spec and controller
        env = Environment(mass_spec, controller, min_rt, max_rt, progress_bar=True)

        # set the log level to WARNING so we don't see too many messages when environment is running
        set_log_level_warning()

        # run the simulation
        env.run()

        # set the log level back to DEBUG
        set_log_level_debug()

        # write simulated output to mzML file
        filename = 'ms1_controller_qcbeer_chems.mzML'
        out_file = os.path.join(out_dir, filename)
        env.write_mzML(out_dir, filename)
        self.assertTrue(os.path.exists(out_file))
        print()
Ejemplo n.º 4
0
    def test_peaks_in_range(self):

        min_mz = 100.
        max_mz = 200.

        logger.info('Testing MS1 controller with narrow m/z range')

        # create a simulated mass spec and MS1 controller
        mass_spec = IndependentMassSpectrometer(POSITIVE, BEER_CHEMS)
        params = AdvancedParams()
        params.default_ms1_scan_window = (min_mz, max_mz)
        controller = SimpleMs1Controller(params=params)

        # create an environment to run both the mass spec and controller
        env = Environment(mass_spec, controller, BEER_MIN_BOUND, BEER_MAX_BOUND, progress_bar=True)
        run_environment(env)

        # write simulated output to mzML file
        filename = 'ms1_controller_qcbeer_chems_narrow.mzML'
        check_mzML(env, OUT_DIR, filename)

        for scan_level, scans in controller.scans.items():
            for s in scans:
                assert min(s.mzs) >= min_mz
                assert max(s.mzs) <= max_mz
Ejemplo n.º 5
0
    def generate_mzmls(self, output_dir, params):
        scan_duration_dicts = self.time_gen(params)
        if (self.filenames is None):
            self.filenames = [
                os.path.join(output_dir, "time_exp_data_{:04d}.mzML".format(i))
                for i, _ in enumerate(scan_duration_dicts)
            ]
        self.file_counter += len(scan_duration_dicts)
        if (len(params) != len(self.filenames)):
            raise ValueError(
                "Parameter and filename list not the same length!")

        for f, d in zip(self.filenames, scan_duration_dicts):
            mass_spec = IndependentMassSpectrometer(POSITIVE,
                                                    self.chems,
                                                    None,
                                                    scan_duration_dict=d)
            controller = SimpleMs1Controller()

            env = Environment(mass_spec,
                              controller,
                              self.min_rt,
                              self.max_rt,
                              progress_bar=True)
            set_log_level_warning()
            env.run()

            set_log_level_warning()
            env.write_mzML(output_dir, os.path.basename(f))
Ejemplo n.º 6
0
def simple_ms1_processor():
    print(
        '#' * 10,
        'Load previously trained spectral feature database and the list of extracted metabolites, \
        created in 01. Download Data')
    #-----------------
    mypath = 'documents/simple_ms1/example_data'
    #-----------------
    base_dir = os.path.abspath(mypath)
    ps = load_obj(Path(base_dir, 'peak_sampler_mz_rt_int_19_beers_fullscan.p'))
    hmdb = load_obj(Path(base_dir, 'hmdb_compounds.p'))

    # set_log_level_debug()
    out_dir = Path(base_dir, 'results', 'MS1_single')
    # the list of ROI sources created in the previous notebook '01. Download Data.ipynb'
    ROI_Sources = [
        str(Path(base_dir, 'DsDA', 'DsDA_Beer', 'beer_t10_simulator_files'))
    ]

    # minimum MS1 intensity of chemicals
    min_ms1_intensity = 1.75E5

    # m/z and RT range of chemicals
    rt_range = [(0, 1440)]
    mz_range = [(0, 1050)]

    # the number of chemicals in the sample
    n_chems = 6500

    # maximum MS level (we do not generate fragmentation peaks when this value is 1)
    ms_level = 1

    chems = ChemicalCreator(ps, ROI_Sources, hmdb)
    dataset = chems.sample(mz_range, rt_range, min_ms1_intensity, n_chems,
                           ms_level)
    save_obj(dataset, Path(out_dir, 'dataset.p'))

    for chem in dataset[0:10]:
        print(chem)
    print('#' * 10,
          'Run MS1 controller on the samples and generate .mzML files')
    min_rt = rt_range[0][0]
    max_rt = rt_range[0][1]

    mass_spec = IndependentMassSpectrometer(POSITIVE, dataset, ps)
    controller = SimpleMs1Controller()

    # create an environment to run both the mass spec and controller
    env = Environment(mass_spec, controller, min_rt, max_rt, progress_bar=True)

    # set the log level to WARNING so we don't see too many messages when environment is running
    set_log_level_warning()

    # run the simulation
    env.run()
    set_log_level_debug()
    mzml_filename = 'ms1_controller.mzML'
    env.write_mzML(out_dir, mzml_filename)
    return str(Path(mypath, 'results', 'MS1_single')) + '/' + mzml_filename
Ejemplo n.º 7
0
 def test_fullscan_from_mzml(self, chems_from_mzml):
     ionisation_mode = POSITIVE
     controller = SimpleMs1Controller()
     ms = IndependentMassSpectrometer(ionisation_mode, chems_from_mzml)
     env = Environment(ms, controller, 500, 600, progress_bar=True)
     set_log_level_warning()
     env.run()
     filename = 'fullscan_from_mzml.mzML'
     check_mzML(env, OUT_DIR, filename)
Ejemplo n.º 8
0
    def test_ms1_controller_with_qcbeer_chems(self):
        logger.info('Testing MS1 controller with QC beer chemicals')

        # create a simulated mass spec and MS1 controller
        mass_spec = IndependentMassSpectrometer(POSITIVE, BEER_CHEMS)
        controller = SimpleMs1Controller()

        # create an environment to run both the mass spec and controller
        env = Environment(mass_spec, controller, BEER_MIN_BOUND, BEER_MAX_BOUND, progress_bar=True)
        run_environment(env)

        # write simulated output to mzML file
        filename = 'ms1_controller_qcbeer_chems.mzML'
        check_mzML(env, OUT_DIR, filename)
Ejemplo n.º 9
0
    def test_ms1_controller_with_simulated_chems(self, fragscan_dataset):
        logger.info('Testing MS1 controller with simulated chemicals')

        min_bound, max_bound = get_rt_bounds(fragscan_dataset, CENTRE_RANGE)
        logger.info('RT bounds %s %s' % (min_bound, max_bound))
        assert len(fragscan_dataset) == N_CHEMS

        # create a simulated mass spec and MS1 controller
        mass_spec = IndependentMassSpectrometer(POSITIVE, fragscan_dataset)
        controller = SimpleMs1Controller()

        # create an environment to run both the mass spec and controller
        env = Environment(mass_spec, controller, min_bound, max_bound, progress_bar=True)
        run_environment(env)

        # write simulated output to mzML file
        filename = 'ms1_controller_simulated_chems.mzML'
        check_mzML(env, OUT_DIR, filename)