예제 #1
0
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"))
예제 #2
0
def test_write_inosine_gauss_mzml():
    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": "gauss",
            "peak_params": {
                "sigma": 3
            },  # 10% of peak width
        }
    }
    mzml_params = {
        "gradient_length": 30,
    }
    mzml_path = write_mzml(file, peak_props, fragmentor, noise_injector,
                           mzml_params)
    reader = pymzml.run.Reader(mzml_path)
    assert reader.get_spectrum_count() == 1000
    intensities = []
    for spec in reader:
        if spec.ms_level == 1:
            if len(spec.i) > 0:
                intensities.append(spec.i[0])
    _, p = normaltest(intensities)
    print(intensities)
예제 #3
0
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)
예제 #4
0
def test_write_inosine_adenosine_gauss_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": 1e3,
        },
        "+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": 0,
            "peak_width": 30,  # seconds
            "peak_function": "gauss",
            "peak_params": {
                "sigma": 3
            },  # 10% of peak width,
        },
    }
    mzml_params = {"gradient_length": 30, "min_intensity": 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

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

    _, p = normaltest(ino_intensities)
    # assert p < 5e-4
    _, p = normaltest(adeno_intensities)
예제 #5
0
def test_write_empty_mzml():
    """Write a mzML without spectra readable by pymzML."""
    file = NamedTemporaryFile("wb")
    peak_props = {}
    mzml_params = {
        "gradient_length": 5,
    }
    mzml_path = write_mzml(file, peak_props, fragmentor, noise_injector,
                           mzml_params)
    reader = pymzml.run.Reader(mzml_path)
    assert reader.get_spectrum_count() == 167
예제 #6
0
def test_write_mzml_one_spec():
    tempfile = 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":
            0.03,  # peak as long as ms_rt_diff and gradient length
            "peak_function": "gauss",
            "peak_params": {
                "sigma": 1
            },  # 10% of peak width,
            "peak_scaling_factor": 1,
        }
    }
    fragmentor = NucleosideFragmentor()
    noise = UniformNoiseInjector()
    mzml_params = {"gradient_length": 0.03, "ms_rt_diff": 0.03}

    write_mzml(tempfile, peak_props, fragmentor, noise, mzml_params)
    reader = pymzml.run.Reader(tempfile.name)
    assert reader.get_spectrum_count() == 1
예제 #7
0
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
예제 #8
0
def test_write_inosine_proper_fragments_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,
        },
    }
    mzml_params = {
        "gradient_length": 30,
    }

    nucl_fragmentor = NucleosideFragmentor()
    mzml_path = write_mzml(file, peak_props, nucl_fragmentor, noise_injector,
                           mzml_params)
    reader = pymzml.run.Reader(mzml_path)
    assert reader.get_spectrum_count() == 1000

    ino_rt = []
    ino_intensities = []
    ino_fragments = []

    ino_mono = 269.0880

    for spec in reader:
        if spec.ms_level == 1:
            ino_i = spec.i[abs(spec.mz - ino_mono) < 0.001]
            if len(ino_i) > 0:
                ino_intensities.append(ino_i[0])
                ino_rt.append(spec.scan_time[0])

    _, p = normaltest(ino_intensities)
    expected_frags = np.array([137.0457872316])

    for frag_list in ino_fragments:
        assert len(frag_list) == len(expected_frags)
        sorted_frags = np.sort(frag_list)
        assert abs(sorted_frags - expected_frags) < 0.001
예제 #9
0
def test_write_inosine_flat_mzml():
    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,
    }
    mzml_path = write_mzml(file, peak_props, fragmentor, noise_injector,
                           mzml_params)
    reader = pymzml.run.Reader(mzml_path)
    assert reader.get_spectrum_count() == 1000
예제 #10
0
def main():
    peak_props = {
        "inosine": {
            "charge": 1,
            "scan_start_time": 0,
            "peak_width": 30,  # seconds
            "peak_function": "gauss",
            "peak_params": {
                "sigma": 3
            },
            "trivial_name": "inosine",
            "chemical_formula": "+C(10)H(12)N(4)O(5)",
        }
    }
    mzml_params = {"gradient_length": 30, "ms_rt_diff": 0.01}
    file = "inosine.mzML"
    fragmentor = NucleosideFragmentor()
    noise_injector = UniformNoiseInjector()
    mzml_path = write_mzml(file, peak_props, fragmentor, noise_injector,
                           mzml_params)
예제 #11
0
def test_write_peptide_gauss_mzml():
    file = NamedTemporaryFile("wb")
    peak_props = {
        "ELVISLIVES": {
            "chemical_formula": "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",
            "chemical_formula": "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()
    mzml_path = write_mzml(file, peak_props, fragmentor, noise_injector,
                           mzml_params)
    reader = pymzml.run.Reader(mzml_path)
    assert reader.get_spectrum_count() == 1500
    intensities = []
    for spec in reader:
        if spec.ms_level == 1:
            if len(spec.i) > 0:
                intensities.append(sum(spec.i))
    _, p = normaltest(intensities)
def main(file_path):
    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,
    }
    fragmentor = NucleosideFragmentor()
    # noise_injector= UniformNoiseInjector()
    noise_injector = JamssNoiseInjector()
    with open(file_path, "wb") as fin:
        mzml_path = write_mzml(fin, peak_props, fragmentor, noise_injector,
                               mzml_params)

    reader = pymzml.run.Reader(file_path)

    ino_rt = []
    ino_intensities = []
    adeno_rt = []
    adeno_intensities = []

    ino_mono = 244.06898754897
    adeno_mono = 244.06898754897

    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])
    plt.plot(ino_rt, ino_intensities)
    plt.plot(adeno_rt, adeno_intensities)
    plt.savefig(os.path.splitext(file_path)[0])
예제 #13
0
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
def main():
    peak_props = {
        "inosine": {
            "trivial_name": "inosine",
            "chemical_formula": "+C(10)H(12)N(4)O(5)",
            "charge": 2,
            "scan_start_time": 0,
            "peak_width": 30,  # seconds
            "peak_function": "gauss_tail",
            "peak_params": {
                "sigma": 3
            },
            "peak_scaling_factor": 1e6,
        },
        "+C(9)H(12)N(2)O(6)": {
            "trivial_name": "pseudouridine",
            "charge": 2,
            "scan_start_time": 30,
            "peak_width": 30,  # seconds
            "peak_function": "gauss_tail",
            "peak_scaling_factor": 2e6,
            "peak_params": {
                "sigma": 3
            },
        },
        "+C(10)H(13)N(5)O(7)": {
            "trivial_name": "spiroiminodihydantoin",
            "charge": 2,
            "scan_start_time": 60,
            "peak_width": 30,  # seconds
            "peak_function": "gauss_tail",
            "peak_params": {
                "sigma": 3
            },
            "peak_scaling_factor": 3e6,
        }
    }
    mzml_params = {
        "gradient_length": 30,
        "min_intensity": 1,
    }

    fragmentor = NucleosideFragmentor()
    noise_injector = UniformNoiseInjector()
    noise_injector = UniformNoiseInjector(dropout=0.0,
                                          ppm_noise=0,
                                          intensity_noise=0)
    noise_injector = JamssNoiseInjector()

    file = NamedTemporaryFile("wb")
    mzml_path = write_mzml(file, peak_props, fragmentor, noise_injector,
                           mzml_params)
    reader = pymzml.run.Reader(mzml_path)
    tic_tuples = []
    for pos, spec in enumerate(reader):
        if spec.ms_level == 1:
            scan_time, scan_unit = spec.scan_time
            if scan_unit == "second":
                scan_time /= 60
            tic_tuples.append((scan_time, spec.TIC))

    pf = Factory()
    pf.new_plot()
    pf.add(tic_tuples, color=(0, 0, 0), style="lines", title="TIC")
    pf.save(
        "example_chromatogram.html",
        layout={
            "xaxis": {
                "title": "Retention time [minutes]"
            },
            "yaxis": {
                "title": "TIC"
            },
        },
    )