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)
def test_neg(self, even_chems): mass_spec = IndependentMassSpectrometer(NEGATIVE, even_chems) N = 10 controller = TopNController(NEGATIVE, N, 0.7, 10, 15, 0, force_N=True) env = Environment(mass_spec, controller, 200, 300, progress_bar=True) run_environment(env) for level in controller.scans: for scan in controller.scans[level]: assert scan.scan_params.get( ScanParameters.POLARITY) == NEGATIVE ms1_peaks = [int(m) for m in controller.scans[1][0].mzs] ms1_peaks.sort() assert 98 in ms1_peaks assert 198 in ms1_peaks assert 298 in ms1_peaks assert 398 in ms1_peaks filename = 'topn_negative.mzML' check_mzML(env, OUT_DIR, filename) # load the file and check polarity in the mzml run = pymzml.run.Reader(os.path.join(OUT_DIR, filename)) for n, spec in enumerate(run): assert spec.get( 'MS:1000129') # this is the negative scan accession
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)
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)
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
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)
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)
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)
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)
def test_exclusion_simple_data(self): # three chemicals, both will get fragmented # first time around and exclusion such that neither # should be fragmented second time fs = EvenMZFormulaSampler() ch = ConstantChromatogramSampler() rti = UniformRTAndIntensitySampler(min_rt=0, max_rt=5) cs = ChemicalMixtureCreator(fs, chromatogram_sampler=ch, rt_and_intensity_sampler=rti) n_chems = 3 dataset = cs.sample(n_chems, 2) ionisation_mode = POSITIVE initial_exclusion_list = [] min_ms1_intensity = 0 N = 10 mz_tol = 10 rt_tol = 30 isolation_width = 1 all_controllers = [] for i in range(3): mass_spec = IndependentMassSpectrometer(ionisation_mode, dataset) 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, 0, 20, 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) all_controllers.append(controller) assert len(all_controllers[0].scans[2]) == n_chems assert len(all_controllers[1].scans[2]) == 0 assert len(all_controllers[2].scans[2]) == 0
def test_TopN_forceN(self, ten_chems): mass_spec = IndependentMassSpectrometer(POSITIVE, ten_chems) N = 20 controller = TopNController(POSITIVE, N, 0.7, 10, 15, 0, force_N=True) env = Environment(mass_spec, controller, 200, 300, progress_bar=True) run_environment(env) all_scans = controller.scans[1] + controller.scans[2] # sort by RT all_scans.sort(key=lambda x: x.rt) ms1_pos = [] for i, s in enumerate(all_scans): if s.ms_level == 1: ms1_pos.append(i) for i, mp in enumerate(ms1_pos[:-1]): assert ms1_pos[i + 1] - (mp + 1) == N
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)
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)
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)
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)
def test_TopN_controller_advanced_params(self): # set some values that are not the defaults, so we know they're passed correctly params = AdvancedParams(default_ms1_scan_window=(10.0, 2000.0), ms1_agc_target=100000, ms1_max_it=500, ms1_collision_energy=200, ms1_orbitrap_resolution=100000, ms1_activation_type='CID', ms1_mass_analyser='IonTrap', ms1_isolation_mode='IonTrap', ms1_source_cid_energy=10, ms2_agc_target=50000, ms2_max_it=250, ms2_collision_energy=300, ms2_orbitrap_resolution=100000, ms2_activation_type='CID', ms2_mass_analyser='IonTrap', ms2_isolation_mode='IonTrap', ms2_source_cid_energy=20) 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, BEER_CHEMS) controller = TopNController(ionisation_mode, N, isolation_width, mz_tol, rt_tol, MIN_MS1_INTENSITY, 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) # check that some of the scan parameters returned are actually what we set # ms1 check scan = controller.scans[1][0] scan_params = scan.scan_params assert scan_params.get( ScanParameters.FIRST_MASS) == params.default_ms1_scan_window[0] assert scan_params.get( ScanParameters.LAST_MASS) == params.default_ms1_scan_window[1] assert scan_params.get( ScanParameters.AGC_TARGET) == params.ms1_agc_target assert scan_params.get(ScanParameters.MAX_IT) == params.ms1_max_it assert scan_params.get( ScanParameters.COLLISION_ENERGY) == params.ms1_collision_energy assert scan_params.get(ScanParameters.ORBITRAP_RESOLUTION ) == params.ms1_orbitrap_resolution # noqa assert scan_params.get( ScanParameters.ACTIVATION_TYPE) == params.ms1_activation_type assert scan_params.get( ScanParameters.MASS_ANALYSER) == params.ms1_mass_analyser assert scan_params.get( ScanParameters.ISOLATION_MODE) == params.ms1_isolation_mode assert scan_params.get( ScanParameters.SOURCE_CID_ENERGY) == params.ms1_source_cid_energy # ms2 check scan = controller.scans[2][0] scan_params = scan.scan_params assert scan_params.get( ScanParameters.AGC_TARGET) == params.ms2_agc_target assert scan_params.get(ScanParameters.MAX_IT) == params.ms2_max_it assert scan_params.get( ScanParameters.COLLISION_ENERGY) == params.ms2_collision_energy assert scan_params.get(ScanParameters.ORBITRAP_RESOLUTION ) == params.ms2_orbitrap_resolution # noqa assert scan_params.get( ScanParameters.ACTIVATION_TYPE) == params.ms2_activation_type assert scan_params.get( ScanParameters.MASS_ANALYSER) == params.ms2_mass_analyser assert scan_params.get( ScanParameters.ISOLATION_MODE) == params.ms2_isolation_mode assert scan_params.get( ScanParameters.SOURCE_CID_ENERGY) == params.ms2_source_cid_energy