Skip to content

Commit

Permalink
Preserve peak lists where the last m/z is repeated, or where there is…
Browse files Browse the repository at this point in the history
… only a single peak
  • Loading branch information
mobiusklein committed Oct 11, 2023
1 parent 23d1041 commit a4fd93e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
26 changes: 18 additions & 8 deletions src/ms_peak_picker/_c/peak_picker.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,23 @@ cdef class PeakProcessor(object):
double low_intensity, high_intensity
double full_width_at_half_max, current_mz, mz
size_t offset
bint infer_range

FittedPeak peak

verbose = self.verbose
is_centroid = self.peak_mode == PeakMode.centroid

size = len(intensity_array) - 1

if size < 1:
return 0
if is_centroid:
if size == -1:
return 0
else:
if size < 1:
return 0

infer_range = start_mz <= -1 and stop_mz <= -1

if start_mz <= -1:
start_mz = mz_array[0]
Expand All @@ -290,15 +300,16 @@ cdef class PeakProcessor(object):

peak_data = []

verbose = self.verbose
is_centroid = self.peak_mode == PeakMode.centroid

intensity_threshold = self.intensity_threshold
signal_to_noise_threshold = self.signal_to_noise_threshold

if start_index == stop_index == 0:
start_index = get_nearest_binary(mz_array, start_mz, 0, size)
stop_index = get_nearest_binary(mz_array, stop_mz, start_index, size)
if infer_range and is_centroid:
start_index = 0
stop_index = size
else:
start_index = get_nearest_binary(mz_array, start_mz, 0, size)
stop_index = get_nearest_binary(mz_array, stop_mz, start_index, size)

if start_index <= 0 and not is_centroid:
start_index = 1
Expand All @@ -311,7 +322,6 @@ cdef class PeakProcessor(object):

index = start_index
while index <= stop_index:
# for index in range(start_index, stop_index + 1):
self.partial_fit_state.reset()
full_width_at_half_max = -1
current_intensity = intensity_array[index]
Expand Down
41 changes: 28 additions & 13 deletions src/ms_peak_picker/peak_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ def set_intensity_threshold(self, intensity_threshold):
get_intensity_threshold, set_intensity_threshold)

def discover_peaks(self, mz_array, intensity_array, start_mz=None, stop_mz=None):
"""Carries out the peak picking process on `mz_array` and `intensity_array`. All
"""
Carries out the peak picking process on `mz_array` and `intensity_array`. All
peaks picked are appended to :attr:`peak_data`.
Parameters
Expand All @@ -191,9 +192,15 @@ def discover_peaks(self, mz_array, intensity_array, start_mz=None, stop_mz=None)
"""
size = len(intensity_array) - 1

if size < 1:
is_centroid = self.peak_mode == CENTROID

if is_centroid and size < 0:
return 0
elif not is_centroid and size < 1:
return 0

infer_range = start_mz is None and stop_mz is None

if start_mz is None:
start_mz = mz_array[0]
if stop_mz is None:
Expand All @@ -206,16 +213,20 @@ def discover_peaks(self, mz_array, intensity_array, start_mz=None, stop_mz=None)
intensity_threshold = self.intensity_threshold
signal_to_noise_threshold = self.signal_to_noise_threshold

start_index = get_nearest_binary(mz_array, start_mz, 0, size)
stop_index = get_nearest_binary(mz_array, stop_mz, start_index, size)
if infer_range and is_centroid:
start_index = 0
stop_index = size
else:
start_index = get_nearest_binary(mz_array, start_mz, 0, size)
stop_index = get_nearest_binary(mz_array, stop_mz, start_index, size)

if start_index <= 0 and self.peak_mode != CENTROID:
if start_index <= 0 and not is_centroid:
start_index = 1
elif start_index < 0 and self.peak_mode == CENTROID:
elif start_index < 0 and is_centroid:
start_index = 0
if stop_index >= size - 1 and self.peak_mode != CENTROID:
if stop_index >= size - 1 and not is_centroid:
stop_index = size - 1
elif stop_index >= size and self.peak_mode == CENTROID:
elif stop_index >= size and is_centroid:
stop_index = size

for index in range(start_index, stop_index + 1):
Expand All @@ -225,7 +236,7 @@ def discover_peaks(self, mz_array, intensity_array, start_mz=None, stop_mz=None)

current_mz = mz_array[index]

if self.peak_mode == CENTROID:
if is_centroid:
if current_intensity <= 0:
continue
mz = mz_array[index]
Expand Down Expand Up @@ -319,7 +330,8 @@ def discover_peaks(self, mz_array, intensity_array, start_mz=None, stop_mz=None)
return len(peak_data)

def find_full_width_at_half_max(self, index, mz_array, intensity_array, signal_to_noise):
"""Calculate full-width-at-half-max for a peak centered at `index` from
"""
Calculate full-width-at-half-max for a peak centered at `index` from
`mz_array` and `intensity_array`, using the `signal_to_noise` to detect
when to stop searching.
Expand Down Expand Up @@ -366,7 +378,8 @@ def find_full_width_at_half_max(self, index, mz_array, intensity_array, signal_t
return fwhm

def fit_peak(self, index, mz_array, intensity_array):
"""Performs the peak shape fitting procedure.
"""
Performs the peak shape fitting procedure.
Parameters
----------
Expand Down Expand Up @@ -397,7 +410,8 @@ def fit_peak(self, index, mz_array, intensity_array):
return 0.0

def area(self, mz_array, intensity_array, mz, full_width_at_half_max, index):
"""Integrate the peak found at `index` with width `full_width_at_half_max`,
"""
Integrate the peak found at `index` with width `full_width_at_half_max`,
centered at `mz`.
Parameters
Expand Down Expand Up @@ -432,7 +446,8 @@ def pick_peaks(mz_array, intensity_array, fit_type='quadratic', peak_mode=PROFIL
signal_to_noise_threshold=1., intensity_threshold=1., threshold_data=False,
target_envelopes=None, transforms=None, verbose=False,
start_mz=None, stop_mz=None, integrate=True, target_indices=None):
"""Picks peaks for the given m/z, intensity array pair, producing a centroid-containing
"""
Picks peaks for the given m/z, intensity array pair, producing a centroid-containing
PeakIndex instance.
Applies each :class:`.FilterBase` in `transforms` in order to
Expand Down

0 comments on commit a4fd93e

Please sign in to comment.