示例#1
0
    def test_mass_spec(self):
        logger.info(
            'Testing mass spec using the Top-N controller and QC beer chemicals'
        )

        isolation_width = 1
        N = 10
        rt_tol = 15
        mz_tol = 10
        ionisation_mode = POSITIVE

        task_manager = TaskManager(buffer_size=3)
        mass_spec = IndependentMassSpectrometer(ionisation_mode,
                                                BEER_CHEMS,
                                                task_manager=task_manager)
        controller = TopNController(ionisation_mode, N, isolation_width,
                                    mz_tol, rt_tol, MIN_MS1_INTENSITY)

        # 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)
        env.run()

        # check that there is at least one non-empty MS2 scan
        check_non_empty_MS2(controller)

        # write simulated output to mzML file
        filename = 'test_mass_spec.mzML'
        check_mzML(env, OUT_DIR, filename)
示例#2
0
    def test_TopN_controller_with_beer_chems_and_scan_duration_dict(self):
        logger.info('Testing Top-N controller with QC beer chemicals '
                    'passing in the scan durations')

        isolation_width = 1
        N = 10
        rt_tol = 15
        mz_tol = 10
        ionisation_mode = POSITIVE

        # ps = None
        scan_duration_dict = {1: 0.2, 2: 0.1}

        # create a simulated mass spec without noise and Top-N controller and passing
        # in the scan_duration dict
        mass_spec = IndependentMassSpectrometer(
            ionisation_mode, BEER_CHEMS, scan_duration=scan_duration_dict)
        controller = TopNController(ionisation_mode, N, isolation_width,
                                    mz_tol, rt_tol, MIN_MS1_INTENSITY)

        # 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)

        # check that there is at least one non-empty MS2 scan
        check_non_empty_MS2(controller)

        # write simulated output to mzML file
        filename = 'topN_controller_qcbeer_chems_no_noise_with_scan_duration.mzML'
        check_mzML(env, OUT_DIR, filename)
示例#3
0
    def test_TopN_controller_with_simulated_chems(self, fragscan_dataset):
        logger.info(
            'Testing Top-N controller with simulated chemicals -- no noise')
        assert len(fragscan_dataset) == N_CHEMS

        isolation_width = 1
        N = 10
        rt_tol = 15
        mz_tol = 10
        ionisation_mode = POSITIVE

        # create a simulated mass spec without noise and Top-N controller
        mass_spec = IndependentMassSpectrometer(ionisation_mode,
                                                fragscan_dataset)
        controller = TopNController(ionisation_mode, N, isolation_width,
                                    mz_tol, rt_tol, MIN_MS1_INTENSITY)
        min_bound, max_bound = get_rt_bounds(fragscan_dataset, CENTRE_RANGE)

        # 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)

        # check that there is at least one non-empty MS2 scan
        check_non_empty_MS2(controller)

        filename = 'topN_controller_simulated_chems_no_noise.mzML'
        check_mzML(env, OUT_DIR, filename)
示例#4
0
    def test_roi_controller_with_beer_chems(self):
        logger.info('Testing ROI controller with QC beer chemicals')

        isolation_width = 1  # the isolation window in Dalton around a selected precursor ion
        N = 10
        rt_tol = 15
        mz_tol = 10
        min_roi_intensity = 5000
        min_roi_length = 10
        ionisation_mode = POSITIVE

        # create a simulated mass spec with noise and ROI controller
        mass_spec = IndependentMassSpectrometer(ionisation_mode, BEER_CHEMS)
        controller = TopN_RoiController(ionisation_mode, isolation_width,
                                        mz_tol, MIN_MS1_INTENSITY,
                                        min_roi_intensity, min_roi_length, N,
                                        rt_tol)

        # 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)

        # check that there is at least one non-empty MS2 scan
        check_non_empty_MS2(controller)

        # write simulated output to mzML file
        filename = 'roi_controller_qcbeer_chems.mzML'
        check_mzML(env, OUT_DIR, filename)
示例#5
0
    def test_TreeDiaController_percentile(self, simple_dataset):
        logger.info('Testing TreeDiaController percentile')

        # some parameters
        window_type = 'percentile'
        kaufmann_design = 'tree'
        num_windows = 64
        scan_overlap = 0
        ionisation_mode = POSITIVE
        scan_time_dict = {1: 0.12, 2: 0.06}
        min_rt = 0
        max_rt = 400
        min_mz = 100
        max_mz = 1000

        # run controller
        mass_spec = IndependentMassSpectrometer(ionisation_mode, simple_dataset,
                                                scan_duration=scan_time_dict)
        controller = DiaController(min_mz, max_mz, window_type, kaufmann_design, num_windows,
                                   scan_overlap=scan_overlap)
        env = Environment(mass_spec, controller, min_rt, max_rt, progress_bar=True)
        set_log_level_warning()
        env.run()

        # check that there is at least one non-empty MS2 scan
        check_non_empty_MS2(controller)

        # write simulated output to mzML file
        filename = 'tree_dia_percentile.mzml'
        check_mzML(env, OUT_DIR, filename)
示例#6
0
    def test_swath(self, ten_chems):
        min_mz = 100
        max_mz = 1000
        width = 100
        scan_overlap = 10

        ionisation_mode = POSITIVE

        controller = SWATH(min_mz, max_mz, width, scan_overlap=scan_overlap)
        scan_time_dict = {1: 0.124, 2: 0.124}

        spike_noise = UniformSpikeNoise(0.1, 1)

        mass_spec = IndependentMassSpectrometer(ionisation_mode, ten_chems,
                                                spike_noise=spike_noise,
                                                scan_duration=scan_time_dict)

        env = Environment(mass_spec, controller, 200, 300, progress_bar=True)

        set_log_level_warning()

        env.run()

        check_non_empty_MS2(controller)

        filename = 'SWATH_ten_chems.mzML'
        check_mzML(env, OUT_DIR, filename)
示例#7
0
    def test_TopNDEW_agent(self):
        set_log_level_debug()
        fs = UniformMZFormulaSampler()
        ri = UniformRTAndIntensitySampler(min_rt=0, max_rt=80)
        cr = GaussianChromatogramSampler(sigma=1)
        ms = FixedMS2Sampler()
        cs = ChemicalMixtureCreator(fs,
                                    rt_and_intensity_sampler=ri,
                                    chromatogram_sampler=cr,
                                    ms2_sampler=ms)
        d = cs.sample(500, 2)
        ionisation_mode = POSITIVE

        # Example shows how the same Agent object can be used in consecutive controllers

        agent = TopNDEWAgent(ionisation_mode, 10, 0.7, 10, 15, 1500)
        controller = AgentBasedController(agent)
        spike_noise = UniformSpikeNoise(0.1, 1000)
        mass_spec = IndependentMassSpectrometer(ionisation_mode,
                                                d,
                                                spike_noise=spike_noise)
        env = Environment(mass_spec, controller, 0, 100, progress_bar=True)
        set_log_level_warning()
        env.run()

        check_non_empty_MS1(controller)
        check_non_empty_MS2(controller)
        check_mzML(env, OUT_DIR, 'shell.mzML')

        controller = AgentBasedController(agent)
        mass_spec = IndependentMassSpectrometer(ionisation_mode,
                                                d,
                                                spike_noise=spike_noise)
        env = Environment(mass_spec, controller, 0, 100, progress_bar=True)
        set_log_level_warning()
        env.run()

        check_non_empty_MS1(controller)
        check_non_empty_MS2(controller)
        check_mzML(env, OUT_DIR, 'shell2.mzML')

        controller = AgentBasedController(agent)
        mass_spec = IndependentMassSpectrometer(ionisation_mode,
                                                d,
                                                spike_noise=spike_noise)
        env = Environment(mass_spec, controller, 0, 100, progress_bar=True)
        set_log_level_warning()
        env.run()

        check_non_empty_MS1(controller)
        # check_non_empty_MS2(controller) # ms2 scans have been exhausted at this point
        check_mzML(env, OUT_DIR, 'shell3.mzML')
示例#8
0
    def test_intensity_nonoverlap_controller_with_simulated_chems(
            self, fragscan_dataset):
        logger.info(
            'Testing intensity non-overlap controller with simulated chemicals'
        )
        assert len(fragscan_dataset) == N_CHEMS

        isolation_width = 1  # the isolation window in Dalton around a selected precursor ion
        N = 10
        rt_tol = 15
        mz_tol = 10
        min_roi_intensity = 50
        min_roi_length = 0
        ionisation_mode = POSITIVE
        min_bound, max_bound = get_rt_bounds(fragscan_dataset, CENTRE_RANGE)
        rt_box_size, mz_box_size = 1, 0.3

        # create a simulated mass spec with noise and ROI controller
        mass_spec = IndependentMassSpectrometer(ionisation_mode,
                                                fragscan_dataset)
        grid = GridEstimator(
            AllOverlapGrid(min_bound, max_bound, rt_box_size, 0, 3000,
                           mz_box_size), IdentityDrift())
        controller = IntensityNonOverlapController(
            ionisation_mode,
            isolation_width,
            mz_tol,
            MIN_MS1_INTENSITY,
            min_roi_intensity,
            min_roi_length,
            N,
            grid,
            rt_tol=rt_tol,
            min_roi_length_for_fragmentation=0)

        # 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)

        assert len(controller.scans[2]) > 0

        # check that there is at least one non-empty MS2 scan
        check_non_empty_MS2(controller)

        # write simulated output to mzML file
        filename = 'intensity_non_overlap_controller_simulated_chems.mzML'
        check_mzML(env, OUT_DIR, filename)
示例#9
0
    def test_flexible_non_overlap_controller_with_beer_chems_and_smartROI_rules(
            self):
        logger.info(
            'Testing flexible non-overlap controller with QC beer chemicals and '
            'SmartROI rules')

        isolation_width = 1  # the isolation window in Dalton around a selected precursor ion
        N = 10
        rt_tol = 15
        mz_tol = 10
        min_roi_intensity = 5000
        min_roi_length = 10
        ionisation_mode = POSITIVE
        rt_box_size, mz_box_size = 1, 0.3

        # create a simulated mass spec with noise and ROI controller
        mass_spec = IndependentMassSpectrometer(ionisation_mode, BEER_CHEMS)
        grid = GridEstimator(
            AllOverlapGrid(BEER_MIN_BOUND, BEER_MAX_BOUND, rt_box_size, 0,
                           3000, mz_box_size), IdentityDrift())
        controller = FlexibleNonOverlapController(
            ionisation_mode,
            isolation_width,
            mz_tol,
            MIN_MS1_INTENSITY,
            min_roi_intensity,
            min_roi_length,
            N,
            grid,
            rt_tol=rt_tol,
            min_roi_length_for_fragmentation=0,
            roi_type=ROI_TYPE_SMART,
            reset_length_seconds=1e6,
            intensity_increase_factor=10,
            drop_perc=0.1 / 100)

        # 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)

        # check that there is at least one non-empty MS2 scan
        check_non_empty_MS2(controller)

        # write simulated output to mzML file
        filename = 'flexible_non_overlap_qcbeer_chems_smartroi.mzML'
        check_mzML(env, OUT_DIR, filename)
示例#10
0
    def test_intensity_non_overlap_controller_with_beer_chems_and_weighteddew_rules(
            self):
        logger.info(
            'Testing intensity non-overlap controller with QC beer chemicals '
            'and WeightedDEW rules')

        isolation_width = 1  # the isolation window in Dalton around a selected precursor ion
        N = 10
        rt_tol = 120
        exclusion_t_0 = 15
        mz_tol = 10
        min_roi_intensity = 5000
        min_roi_length = 10
        ionisation_mode = POSITIVE
        rt_box_size, mz_box_size = 1, 0.3

        # create a simulated mass spec with noise and ROI controller
        mass_spec = IndependentMassSpectrometer(ionisation_mode, BEER_CHEMS)
        grid = GridEstimator(
            AllOverlapGrid(BEER_MIN_BOUND, BEER_MAX_BOUND, rt_box_size, 0,
                           3000, mz_box_size), IdentityDrift())
        controller = IntensityNonOverlapController(
            ionisation_mode,
            isolation_width,
            mz_tol,
            MIN_MS1_INTENSITY,
            min_roi_intensity,
            min_roi_length,
            N,
            grid,
            rt_tol=rt_tol,
            min_roi_length_for_fragmentation=0,
            exclusion_method=ROI_EXCLUSION_WEIGHTED_DEW,
            exclusion_t_0=exclusion_t_0)

        # 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)

        # check that there is at least one non-empty MS2 scan
        check_non_empty_MS2(controller)

        # write simulated output to mzML file
        filename = 'intensity_non_overlap_qcbeer_chems_weighteddew.mzML'
        check_mzML(env, OUT_DIR, filename)
示例#11
0
 def test_topn_from_mzml(self, chems_from_mzml):
     ionisation_mode = POSITIVE
     N = 10
     isolation_width = 0.7
     mz_tol = 0.01
     rt_tol = 15
     min_ms1_intensity = 10
     controller = TopNController(ionisation_mode, N, isolation_width,
                                 mz_tol, rt_tol, min_ms1_intensity)
     ms = IndependentMassSpectrometer(ionisation_mode, chems_from_mzml)
     env = Environment(ms, controller, 500, 600, progress_bar=True)
     set_log_level_warning()
     env.run()
     check_non_empty_MS2(controller)
     filename = 'topn_from_mzml.mzML'
     check_mzML(env, OUT_DIR, filename)
示例#12
0
    def test_TopN_controller_with_beer_chems_and_initial_exclusion_list(self):
        logger.info('Testing Top-N controller with QC beer chemicals and '
                    'an initial exclusion list')

        isolation_width = 1
        N = 10
        rt_tol = 15
        mz_tol = 10
        ionisation_mode = POSITIVE

        initial_exclusion_list = []
        for i in range(3):
            mass_spec = IndependentMassSpectrometer(ionisation_mode,
                                                    BEER_CHEMS)
            controller = TopNController(
                ionisation_mode,
                N,
                isolation_width,
                mz_tol,
                rt_tol,
                MIN_MS1_INTENSITY,
                initial_exclusion_list=initial_exclusion_list)
            env = Environment(mass_spec,
                              controller,
                              BEER_MIN_BOUND,
                              BEER_MAX_BOUND,
                              progress_bar=True)
            run_environment(env)

            mz_intervals = list(
                controller.exclusion.exclusion_list.boxes_mz.items())
            rt_intervals = list(
                controller.exclusion.exclusion_list.boxes_rt.items())
            unique_items_mz = set(i.data for i in mz_intervals)
            unique_items_rt = set(i.data for i in rt_intervals)
            assert len(unique_items_mz) == len(unique_items_rt)

            initial_exclusion_list = list(unique_items_mz)

            # check that there is at least one non-empty MS2 scan
            check_non_empty_MS2(controller)

            # write simulated output to mzML file
            filename = 'topN_controller_qcbeer_exclusion_%d.mzML' % i
            check_mzML(env, OUT_DIR, filename)
示例#13
0
    def test_smart_roi_controller_with_simulated_chems(self, fragscan_dataset):
        logger.info('Testing ROI controller with simulated chemicals')
        assert len(fragscan_dataset) == N_CHEMS

        isolation_width = 1  # the isolation window in Dalton around a selected precursor ion
        N = 10
        rt_tol = 15
        mz_tol = 10
        min_roi_intensity = 50
        min_roi_length = 0
        ionisation_mode = POSITIVE

        # create a simulated mass spec with noise and ROI controller
        mass_spec = IndependentMassSpectrometer(ionisation_mode,
                                                fragscan_dataset)
        controller = TopN_SmartRoiController(
            ionisation_mode,
            isolation_width,
            mz_tol,
            MIN_MS1_INTENSITY,
            min_roi_intensity,
            min_roi_length,
            N,
            rt_tol,
            min_roi_length_for_fragmentation=0)

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

        assert len(controller.scans[2]) > 0

        # check that there is at least one non-empty MS2 scan
        check_non_empty_MS2(controller)

        # write simulated output to mzML file
        filename = 'smart_roi_controller_simulated_chems.mzML'
        check_mzML(env, OUT_DIR, filename)
示例#14
0
    def test_WeightedDEW_controller_with_beer_chems(self):
        logger.info(
            'Testing excluding Top-N controller with QC beer chemicals')
        test_shift = 0
        isolation_width = 1
        N = 10
        mz_tol = 10
        ionisation_mode = POSITIVE
        exclusion_t_0 = 15.0
        rt_tol = 120
        scan_duration_dict = {1: 0.2, 2: 0.1}

        # create a simulated mass spec without noise and Top-N controller
        mass_spec = IndependentMassSpectrometer(
            ionisation_mode, BEER_CHEMS, scan_duration=scan_duration_dict)
        controller = WeightedDEWController(ionisation_mode,
                                           N,
                                           isolation_width,
                                           mz_tol,
                                           rt_tol,
                                           MIN_MS1_INTENSITY,
                                           ms1_shift=test_shift,
                                           exclusion_t_0=exclusion_t_0,
                                           log_intensity=True)

        # 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)

        # check that there is at least one non-empty MS2 scan
        check_non_empty_MS2(controller)

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