Centroiding
Functions for extracting and centroiding timsTOF mass spectra. Both functions use a Rust-backed implementation when available, with an automatic Python fallback.
tdfpy.get_centroided_spectrum
get_centroided_spectrum(
td: TimsData,
frame_id: int,
spectrum_index: int | None = None,
ion_mobility_type: Literal[
"ook0", "ccs", "voltage"
] = "ook0",
mz_tolerance: float = 8.0,
mz_tolerance_type: Literal["ppm", "da"] = "ppm",
im_tolerance: float = 0.05,
im_tolerance_type: Literal[
"relative", "absolute"
] = "relative",
min_peaks: int = 3,
max_peaks: int | None = None,
noise_filter: None | (
Literal[
"mad",
"percentile",
"histogram",
"baseline",
"iterative_median",
]
| float
| int
) = None,
use_numba: bool = True,
) -> np.ndarray
Extract a centroided MS1 spectrum for a single frame.
This function reads raw profile-like scans from the frame, converts indices to m/z values, collects all raw peaks with their ion mobility values, and applies peak centroiding based on m/z and ion mobility tolerances to produce a centroided spectrum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
td
|
TimsData
|
TimsData instance connected to the analysis directory |
required |
frame_id
|
int
|
Frame ID to extract |
required |
spectrum_index
|
int | None
|
Optional index for this spectrum (defaults to frame_id) |
None
|
ion_mobility_type
|
Literal['ook0', 'ccs', 'voltage']
|
Type of ion mobility to calculate and include for each peak - "ook0": 1/K0 (reciprocal reduced mobility) [default] - "ccs": Collision Cross Section in Ų (requires charge state estimation) |
'ook0'
|
mz_tolerance
|
float
|
Tolerance for m/z matching during centroiding |
8.0
|
mz_tolerance_type
|
Literal['ppm', 'da']
|
Type of m/z tolerance - "ppm" or "da" (daltons) |
'ppm'
|
im_tolerance
|
float
|
Tolerance for ion mobility matching during centroiding |
0.05
|
im_tolerance_type
|
Literal['relative', 'absolute']
|
Type of ion mobility tolerance - "relative" or "absolute" |
'relative'
|
min_peaks
|
int
|
Minimum number of nearby raw peaks required to form a centroid (0 or 1 keeps all) |
3
|
max_peaks
|
int | None
|
Maximum number of centroided peaks to return |
None
|
noise_filter
|
None | (Literal['mad', 'percentile', 'histogram', 'baseline', 'iterative_median'] | float | int)
|
Noise filtering method to apply before centroiding. Options: - None: No noise filtering (default) - "mad": Median Absolute Deviation method - "percentile": 75th percentile threshold - "histogram": Histogram mode-based estimation - "baseline": Bottom quartile statistics - "iterative_median": Iterative median filtering - float/int: Direct intensity threshold value |
None
|
Returns:
| Type | Description |
|---|---|
np.ndarray
|
np.ndarray: Array of shape (N, 3) containing centroided peaks. Columns are: [mz, intensity, ion_mobility] |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the frame_id doesn't exist or is not an MS1 frame |
RuntimeError
|
If the TimsData connection is not open |
Example
with timsdata_connect('path/to/data.d') as td:
# Get centroided spectrum with 1/K0 (default)
peaks = get_centroided_ms1_spectrum(td, frame_id=1)
print(f"Found {len(peaks)} centroided peaks")
# Get spectrum with CCS values
spectrum = get_centroided_ms1_spectrum(td, frame_id=1, ion_mobility_type="ccs")
# Custom centroiding tolerances
spectrum = get_centroided_ms1_spectrum(
td, frame_id=1, mz_tolerance=10, im_tolerance=0.1
)
# With noise filtering
spectrum = get_centroided_ms1_spectrum(td, frame_id=1, noise_filter="mad")
# With custom noise threshold
spectrum = get_centroided_ms1_spectrum(td, frame_id=1, noise_filter=1000.0)
Source code in src/tdfpy/centroiding.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | |
tdfpy.merge_peaks
merge_peaks(
mz_array: np.ndarray,
intensity_array: np.ndarray,
ion_mobility_array: np.ndarray,
mz_tolerance: float = 8.0,
mz_tolerance_type: Literal["ppm", "da"] = "ppm",
im_tolerance: float = 0.05,
im_tolerance_type: Literal[
"relative", "absolute"
] = "relative",
min_peaks: int = 3,
max_peaks: int | None = None,
use_numba: bool = True,
) -> np.ndarray
Centroid profile-like peaks using m/z and ion mobility tolerances.
This function implements a greedy clustering algorithm that centroids raw peaks (similar to profile mode data) within specified m/z and ion mobility windows. Peaks are processed in descending order of intensity, and nearby peaks are combined using intensity-weighted averaging to produce centroided peaks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mz_array
|
np.ndarray
|
Array of m/z values from raw/profile-like data |
required |
intensity_array
|
np.ndarray
|
Array of intensity values |
required |
ion_mobility_array
|
np.ndarray
|
Array of ion mobility values (1/K0 or CCS) |
required |
mz_tolerance
|
float
|
Tolerance for m/z matching during centroiding |
8.0
|
mz_tolerance_type
|
Literal['ppm', 'da']
|
Type of m/z tolerance - "ppm" or "da" (daltons) |
'ppm'
|
im_tolerance
|
float
|
Tolerance for ion mobility matching during centroiding |
0.05
|
im_tolerance_type
|
Literal['relative', 'absolute']
|
Type of ion mobility tolerance - "relative" or "absolute" |
'relative'
|
min_peaks
|
int
|
Minimum number of nearby raw peaks required to form a centroid. Set to 0 or 1 to keep all peaks (no filtering). |
3
|
max_peaks
|
int | None
|
Maximum number of centroided peaks to return (keeps highest intensity) |
None
|
Returns:
| Type | Description |
|---|---|
np.ndarray
|
np.ndarray: Array of shape (N, 3) containing centroided peaks. Columns are: [mz, intensity, ion_mobility] |
Example
mz = np.array([100.0, 100.001, 200.0])
intensity = np.array([1000.0, 500.0, 2000.0])
im = np.array([0.8, 0.8, 0.9])
peaks = merge_peaks(mz, intensity, im, mz_tolerance=10, mz_tolerance_type="ppm")
Source code in src/tdfpy/centroiding.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |