Ejemplo n.º 1
0
def test_spectrum_none_exception(none_spectrum, filename):
    """ Test for exception being thrown if the spectrum to be saved. """
    with pytest.raises(AttributeError) as exception:
        save_as_msp(none_spectrum, filename)

    message = exception.value.args[0]
    assert message == "'NoneType' object has no attribute 'metadata'"
Ejemplo n.º 2
0
    def write(self, filename: str):
        """Write data to back to 'msp' file

        Args:
            filename (str): Path to filename under which to store the data.
        """
        save_as_msp(self._spectra, filename)
Ejemplo n.º 3
0
def test_wrong_filename_exception():
    """ Test for exception being thrown if output file doesn't end with .msp. """
    with tempfile.TemporaryDirectory() as temp_dir:
        filename = os.path.join(temp_dir, "test.mzml")

        with pytest.raises(AssertionError) as exception:
            save_as_msp(None, filename)

        message = exception.value.args[0]
        assert message == "File extension must be 'msp'."
Ejemplo n.º 4
0
def save_and_reload_spectra(filename, spectra: List[Spectrum]):
    """ Utility function to save spectra to msp and load them again.

    Params:
    -------
    spectra: Spectra objects to store

    Returns:
    --------
    reloaded_spectra: Spectra loaded from saved msp file.
    """

    save_as_msp(spectra, filename)
    reloaded_spectra = list(load_from_msp(filename))
    return reloaded_spectra
Ejemplo n.º 5
0
def test_num_peaks_last_metadata_field(filename, data):
    """ Test to check whether the last line before the peaks is NUM PEAKS: ... """
    save_as_msp(data, filename)

    with open(filename, mode='r', encoding="utf-8") as file:
        content = file.readlines()
        for idx, line in enumerate(content):
            if line.startswith('NUM PEAKS: '):
                num_peaks = int(line.split()[2])
                peaks = content[idx + 1:idx + num_peaks + 1]
                for peak in peaks:
                    mz, intensity = peak.split()[:2]
                    mz = float(mz)
                    intensity = float(intensity)

                    assert isinstance(mz, float)
                    assert isinstance(intensity, float)
Ejemplo n.º 6
0
def test_file_exists_single_spectrum(spectrum, filename):
    """ Test checking if the file is created. """
    save_as_msp(spectrum, filename)

    assert os.path.isfile(filename)
Ejemplo n.º 7
0
def main(argv):
    parser = argparse.ArgumentParser(
        description="Compute MSP similarity scores")
    parser.add_argument("--spectra",
                        type=str,
                        required=True,
                        help="Mass spectra file to be filtered.")
    parser.add_argument("--spectra_format",
                        type=str,
                        required=True,
                        help="Format of spectra file.")
    parser.add_argument("--output",
                        type=str,
                        required=True,
                        help="Filtered mass spectra file.")
    parser.add_argument(
        "-normalise_intensities",
        action='store_true',
        help="Normalize intensities of peaks (and losses) to unit height.")
    parser.add_argument(
        "-default_filters",
        action='store_true',
        help=
        "Collection of filters that are considered default and that do no require any (factory) arguments."
    )
    parser.add_argument(
        "-clean_metadata",
        action='store_true',
        help=
        "Apply all adding and cleaning filters if possible, so that the spectra have canonical metadata."
    )
    parser.add_argument(
        "-relative_intensity",
        action='store_true',
        help=
        "Keep only peaks within set relative intensity range (keep if to_intensity >= intensity >= from_intensity)."
    )
    parser.add_argument("--from_intensity",
                        type=float,
                        help="Lower bound for intensity filter")
    parser.add_argument("--to_intensity",
                        type=float,
                        help="Upper bound for intensity filter")
    parser.add_argument(
        "-mz_range",
        action='store_true',
        help=
        "Keep only peaks between set m/z range (keep if to_mz >= m/z >= from_mz)."
    )
    parser.add_argument("--from_mz",
                        type=float,
                        help="Lower bound for m/z  filter")
    parser.add_argument("--to_mz",
                        type=float,
                        help="Upper bound for m/z  filter")
    args = parser.parse_args()

    if not (args.normalise_intensities or args.default_filters or
            args.clean_metadata or args.relative_intensity or args.mz_range):
        raise ValueError('No filter selected.')

    if args.spectra_format == 'msp':
        spectra = list(load_from_msp(args.spectra))
    elif args.queries_format == 'mgf':
        spectra = list(load_from_mgf(args.spectra))
    else:
        raise ValueError(
            f'File format {args.spectra_format} not supported for mass spectra file.'
        )

    filtered_spectra = []
    for spectrum in spectra:
        if args.normalise_intensities:
            spectrum = normalize_intensities(spectrum)

        if args.default_filters:
            spectrum = default_filters(spectrum)

        if args.clean_metadata:
            filters = [
                add_compound_name, add_precursor_mz, add_fingerprint,
                add_losses, add_parent_mass, add_retention_index,
                add_retention_time, clean_compound_name
            ]
            for metadata_filter in filters:
                spectrum = metadata_filter(spectrum)

        if args.relative_intensity:
            spectrum = select_by_relative_intensity(spectrum,
                                                    args.from_intensity,
                                                    args.to_intensity)

        if args.mz_range:
            spectrum = select_by_mz(spectrum, args.from_mz, args.to_mz)

        filtered_spectra.append(spectrum)

    if args.spectra_format == 'msp':
        save_as_msp(filtered_spectra, args.output)
    else:
        save_as_mgf(filtered_spectra, args.output)

    return 0