def test_GaussNoiseInjector_generate_intensity_noise(): i_array = np.array([1e6, 2e6, 3e6], dtype="float32") scan = Scan({ "mz": np.array([100, 200, 300], dtype="float32"), "i": i_array, "ms_level": 1, }) noise_injector = GaussNoiseInjector() noise = noise_injector._generate_intensity_noise(scan) assert (abs(noise) < i_array * 0.2).all()
def test_GaussNoiseInjector_intensity_noise_ms2(): i_array = np.array([1e6, 2e6, 3e6], dtype="float32") scan = Scan({ "mz": np.array([100, 200, 300], dtype="float32"), "i": i_array, "ms_level": 2, }) noise_injector = GaussNoiseInjector() scan = noise_injector.inject_noise(scan, dropout=0.0) assert (scan.i - i_array < i_array * 0.2).all()
def test_GaussNoiseInjector_generate_mz_noise(): i_array = np.array([1e6, 2e6, 3e6], dtype="float32") scan = Scan({ "mz": np.array([100, 200, 300], dtype="float32"), "i": i_array, "ms_level": 1, }) noise_injector = GaussNoiseInjector() noise = noise_injector._generate_mz_noise(scan) # breakpoint() assert (abs(noise) < scan.mz * 1e-5).all()
def test_GaussNoiseInjector_intensity_noise_ms2_drop_all(): i_array = np.array([1e6, 2e6, 3e6], dtype="float32") scan = Scan({ "mz": np.array([100, 200, 300], dtype="float32"), "i": i_array, "ms_level": 2, }) noise_injector = GaussNoiseInjector() scan = noise_injector.inject_noise(scan, dropout=1.0) assert len(scan.mz) == 0 assert len(scan.i) == 0
def test_gauss_mz_noise_ms1(): i_array = np.array([1e6, 2e6, 3e6], dtype="float32") scan = Scan({ "mz": np.array([100, 200, 300], dtype="float32"), "i": i_array, "ms_level": 1, }) noise_injector = GaussNoiseInjector() scan = noise_injector.inject_noise(scan) # breakpoint() assert (abs(scan.mz - np.array([100, 200, 300], dtype="float32")) < np.array([100, 200, 300], dtype="float32") * 1e-5).all()
def test_GaussNoiseInjector_intensity_noise_ms1(): i_array = np.array([1e6, 2e6, 3e6], dtype="float32") scan = Scan({ "mz": np.array([100, 200, 300], dtype="float32"), "i": i_array, "ms_level": 1, }) noise_injector = GaussNoiseInjector(variance=0.2) scan = noise_injector.inject_noise(scan) # breakpoint() # assert abs((scan.i - 3e6) < 3e6 * 0.05).all() assert (scan.i - i_array < i_array * 0.2).all()
def test_write_mzml_get_TIC(): file = NamedTemporaryFile("wb") peak_props = { "inosine": { "chemical_formula": "+C(10)H(12)N(4)O(5)", "trivial_name": "inosine", "charge": 2, "scan_start_time": 0, "peak_width": 30, # seconds "peak_function": None, "peak_params": {}, } } mzml_params = { "gradient_length": 30, } noise_injector = GaussNoiseInjector(variance=0.0) mzml_path = write_mzml(file, peak_props, fragmentor, noise_injector, mzml_params) reader = pymzml.run.Reader(mzml_path) assert reader.get_spectrum_count() == 1000 reader = pymzml.run.Reader(mzml_path) tics = [] for spec in reader: if spec.ms_level == 1: tics.append(sum(spec.i)) tic = reader["TIC"] assert tic.peaks()[:, 1] == pytest.approx(np.array(tics, dtype="float32"))
def main(): file = NamedTemporaryFile("wb") peak_props = { "ELVISLIVES": { "trivial_name": "ELVISLIVES", "charge": 2, "scan_start_time": 0, "peak_width": 30, # seconds "peak_function": "gauss", "peak_params": { "sigma": 3 }, # 10% of peak width }, "ELVISLIVSE": { "charge": 2, "trivial_name": "ELVISLIVSE", "scan_start_time": 15, "peak_width": 30, # seconds "peak_function": "gauss", "peak_params": { "sigma": 3 }, # 10% of peak width, }, } mzml_params = { "gradient_length": 45, } fragmentor = PeptideFragmentor() noise_injector = GaussNoiseInjector(variance=0.05) mzml_path = write_mzml(file, peak_props, fragmentor, noise_injector, mzml_params)
def test_write_2_mols_same_cc(): file = NamedTemporaryFile("wb") peak_props = { "uridine": { "charge": 2, "chemical_formula": "+C(9)H(11)N(2)O(6)", "trivial_name": "uridine", "scan_start_time": 0, "peak_width": 30, # seconds "peak_function": "gauss", "peak_params": { "sigma": 1 }, # 10% of peak width, "peak_scaling_factor": 0.5 * 1e6, }, "pseudouridine": { "chemical_formula": "+C(9)H(11)N(2)O(6)", "trivial_name": "pseudouridine", "charge": 2, "scan_start_time": 15, "peak_width": 30, # seconds "peak_function": "gauss", "peak_params": { "sigma": 1 }, # 10% of peak width, "peak_scaling_factor": 1e6, }, } mzml_params = { "gradient_length": 45, "min_intensity": 10, } mzml_params = { "gradient_length": 45, } fragmentor = NucleosideFragmentor() noise_injector = GaussNoiseInjector(variance=0.0) mzml_path = write_mzml(file, peak_props, fragmentor, noise_injector, mzml_params) reader = pymzml.run.Reader(mzml_path) # assert reader.get_spectrum_count() == 1499 intensities = [] for spec in reader: if spec.ms_level == 1: if len(spec.i) > 0: intensities.append(sum(spec.i)) peaks, _ = find_peaks(intensities) assert len(peaks) == 2
def test_write_inosine_adenosine_gauss_shift_mzml(): file = NamedTemporaryFile("wb") peak_props = { "inosine": { "chemical_formula": "+C(10)H(12)N(4)O(5)", "charge": 1, "trivial_name": "inosine", "scan_start_time": 0, "peak_width": 30, # seconds "peak_function": "gauss", "peak_params": { "sigma": 3 }, # 10% of peak width, "peak_scaling_factor": 1e6, }, "+C(10)H(13)N(5)O(4)": { "chemical_formula": "+C(10)H(13)N(5)O(4)", "trivial_name": "adenosine", "charge": 1, "scan_start_time": 15, "peak_width": 30, # seconds "peak_function": "gauss", "peak_params": { "sigma": 3 }, # 10% of peak width, "peak_scaling_factor": 1e6, }, } mzml_params = { "gradient_length": 45, } noise_injector = GaussNoiseInjector(variance=0.0) # noise_injector = UniformformNoiseInjector() mzml_path = write_mzml(file, peak_props, fragmentor, noise_injector, mzml_params) reader = pymzml.run.Reader(mzml_path) assert reader.get_spectrum_count() == 1500 ino_rt = [] ino_intensities = [] adeno_rt = [] adeno_intensities = [] ino_mono = 269.0880 adeno_mono = 268.1040 for spec in reader: if spec.ms_level == 1: ino_i = spec.i[abs(spec.mz - ino_mono) < 0.001] adeno_i = spec.i[abs(spec.mz - adeno_mono) < 0.001] if len(adeno_i) > 0: adeno_intensities.append(adeno_i[0]) adeno_rt.append(spec.scan_time[0]) if len(ino_i) > 0: ino_intensities.append(ino_i[0]) ino_rt.append(spec.scan_time[0]) # assert rt max diff is about 15 m_i = np.argmax(ino_intensities) m_a = np.argmax(adeno_intensities) mean_i_rt = ino_rt[m_i] mean_a_rt = adeno_rt[m_a] assert 14 < (mean_a_rt - mean_i_rt) < 16
generate_molecule_isotopologue_lib, generate_scans, write_mzml, ) class TestFragmentor(AbstractFragmentor): def __init__(self): print("Fragmentor goes chop chop chop chop chop ...") def fragment(self, mol): return np.array([(200, 1e5)]) fragmentor = TestFragmentor() noise_injector = GaussNoiseInjector(variance=0.05) def test_write_mzml(): """Write a mzML without spectra readable by pymzML.""" file = NamedTemporaryFile("wb") peak_props = {} mzml_params = { "gradient_length": 0, } mzml_path = write_mzml(file, peak_props, fragmentor, noise_injector, mzml_params) reader = pymzml.run.Reader(mzml_path) assert reader.get_spectrum_count() == 0