Skip to content

Lookup

mzmlpy.lookup.SpectrumLookup

SpectrumLookup(
    file_object: FileInterface,
    count: int | None = None,
    id_regex: str | None = None,
)

Bases: BaseLookup[Spectrum]

Lookup interface for spectra.

Source code in src/mzmlpy/lookup.py
15
16
17
18
19
def __init__(self, file_object: FileInterface, count: int | None = None, id_regex: str | None = None) -> None:
    self.file_object = file_object
    self._count = count
    self._id_regex = id_regex
    self._cursor: Iterator[T] | None = None

count property

count: int | None

Get count of items.

filter

filter(
    *,
    ms_level: int | None = None,
    retention_time: (
        tuple[float | None, float | None] | None
    ) = None,
    polarity: Literal["positive", "negative"] | None = None,
    precursor_mz: (
        tuple[float | None, float | None] | None
    ) = None,
    spectrum_type: (
        Literal["centroid", "profile"] | None
    ) = None,
    mobility_type: (
        Literal["inverse_reduced", "drift_time"] | None
    ) = None,
    ion_mobility: (
        tuple[float | None, float | None] | None
    ) = None,
    faims_voltage: (
        tuple[float | None, float | None] | None
    ) = None
) -> Iterator[Spectrum]

Lazily select spectra by metadata, using inclusive time bounds in seconds.

Criteria are combined with AND. Retention time matches any scan. Precursor m/z matches overlapping isolation windows, with selected ions as a fallback. Missing metadata does not match a requested criterion. Keep the reader open while iterating.

Source code in src/mzmlpy/lookup.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def filter(
    self,
    *,
    ms_level: int | None = None,
    retention_time: tuple[float | None, float | None] | None = None,
    polarity: Literal["positive", "negative"] | None = None,
    precursor_mz: tuple[float | None, float | None] | None = None,
    spectrum_type: Literal["centroid", "profile"] | None = None,
    mobility_type: Literal["inverse_reduced", "drift_time"] | None = None,
    ion_mobility: tuple[float | None, float | None] | None = None,
    faims_voltage: tuple[float | None, float | None] | None = None,
) -> Iterator[Spectrum]:
    """Lazily select spectra by metadata, using inclusive time bounds in seconds.

    Criteria are combined with AND. Retention time matches any scan. Precursor m/z
    matches overlapping isolation windows, with selected ions as a fallback. Missing
    metadata does not match a requested criterion. Keep the reader open while iterating.
    """
    predicate = SpectrumFilter(
        ms_level, retention_time, polarity, precursor_mz, spectrum_type, mobility_type, ion_mobility, faims_voltage
    )
    return (spectrum for spectrum in self if predicate.matches(spectrum))

get_by_index

get_by_index(index: int | str) -> T

Get item by index. Negative indices count from the end, like a list.

Source code in src/mzmlpy/lookup.py
40
41
42
43
44
45
46
47
48
49
50
51
def get_by_index(self, index: int | str) -> T:
    """Get item by index. Negative indices count from the end, like a list."""
    if isinstance(index, str):
        index = int(index)
    if index < 0:
        # Normalize against the count so lookup[-1] mirrors slice behavior (lookup[-1:]).
        count = self.count
        if count is not None:
            index += count
        if index < 0:
            raise IndexError("Index out of range")
    return self._get_by_index_impl(index)

get_by_id

get_by_id(identifier: str) -> T

Get item by ID.

Source code in src/mzmlpy/lookup.py
53
54
55
def get_by_id(self, identifier: str) -> T:
    """Get item by ID."""
    return self._get_by_id_impl(identifier)

get_by_slice

get_by_slice(slice_obj: slice) -> list[T]

Get items by slice notation.

Source code in src/mzmlpy/lookup.py
57
58
59
60
61
62
63
64
65
def get_by_slice(self, slice_obj: slice) -> list[T]:
    """Get items by slice notation."""
    if self.count is None:
        # Don't know count - must iterate all and slice
        items: list[T] = list(self)
        return items[slice_obj]
    # Know count - use slice.indices() to handle all cases
    start, stop, step = slice_obj.indices(self.count)
    return [self.get_by_index(i) for i in range(start, stop, step)]

next

next() -> T

Advance a persistent cursor and return the next item.

The cursor is created on first call and advances on each subsequent call, raising StopIteration once every item has been returned. Call :meth:reset to start over.

Source code in src/mzmlpy/lookup.py
 92
 93
 94
 95
 96
 97
 98
 99
100
def next(self) -> T:
    """Advance a persistent cursor and return the next item.

    The cursor is created on first call and advances on each subsequent call, raising
    ``StopIteration`` once every item has been returned. Call :meth:`reset` to start over.
    """
    if self._cursor is None:
        self._cursor = iter(self)
    return next(self._cursor)

reset

reset() -> None

Reset the cursor used by :meth:next so the next call starts from the first item.

Source code in src/mzmlpy/lookup.py
102
103
104
def reset(self) -> None:
    """Reset the cursor used by :meth:`next` so the next call starts from the first item."""
    self._cursor = None

mzmlpy.lookup.ChromatogramLookup

ChromatogramLookup(
    file_object: FileInterface,
    count: int | None = None,
    id_regex: str | None = None,
)

Bases: BaseLookup[Chromatogram]

Lookup interface for chromatograms.

Source code in src/mzmlpy/lookup.py
15
16
17
18
19
def __init__(self, file_object: FileInterface, count: int | None = None, id_regex: str | None = None) -> None:
    self.file_object = file_object
    self._count = count
    self._id_regex = id_regex
    self._cursor: Iterator[T] | None = None

TIC property

Access Total Ion Chromatogram.

count property

count: int | None

Get count of items.

get_by_index

get_by_index(index: int | str) -> T

Get item by index. Negative indices count from the end, like a list.

Source code in src/mzmlpy/lookup.py
40
41
42
43
44
45
46
47
48
49
50
51
def get_by_index(self, index: int | str) -> T:
    """Get item by index. Negative indices count from the end, like a list."""
    if isinstance(index, str):
        index = int(index)
    if index < 0:
        # Normalize against the count so lookup[-1] mirrors slice behavior (lookup[-1:]).
        count = self.count
        if count is not None:
            index += count
        if index < 0:
            raise IndexError("Index out of range")
    return self._get_by_index_impl(index)

get_by_id

get_by_id(identifier: str) -> T

Get item by ID.

Source code in src/mzmlpy/lookup.py
53
54
55
def get_by_id(self, identifier: str) -> T:
    """Get item by ID."""
    return self._get_by_id_impl(identifier)

get_by_slice

get_by_slice(slice_obj: slice) -> list[T]

Get items by slice notation.

Source code in src/mzmlpy/lookup.py
57
58
59
60
61
62
63
64
65
def get_by_slice(self, slice_obj: slice) -> list[T]:
    """Get items by slice notation."""
    if self.count is None:
        # Don't know count - must iterate all and slice
        items: list[T] = list(self)
        return items[slice_obj]
    # Know count - use slice.indices() to handle all cases
    start, stop, step = slice_obj.indices(self.count)
    return [self.get_by_index(i) for i in range(start, stop, step)]

next

next() -> T

Advance a persistent cursor and return the next item.

The cursor is created on first call and advances on each subsequent call, raising StopIteration once every item has been returned. Call :meth:reset to start over.

Source code in src/mzmlpy/lookup.py
 92
 93
 94
 95
 96
 97
 98
 99
100
def next(self) -> T:
    """Advance a persistent cursor and return the next item.

    The cursor is created on first call and advances on each subsequent call, raising
    ``StopIteration`` once every item has been returned. Call :meth:`reset` to start over.
    """
    if self._cursor is None:
        self._cursor = iter(self)
    return next(self._cursor)

reset

reset() -> None

Reset the cursor used by :meth:next so the next call starts from the first item.

Source code in src/mzmlpy/lookup.py
102
103
104
def reset(self) -> None:
    """Reset the cursor used by :meth:`next` so the next call starts from the first item."""
    self._cursor = None