6  Peak Detection and Quantification

Peak detection and quantification form the foundation of MS data analysis workflows. This chapter covers modern algorithms implemented in the R for Mass Spectrometry ecosystem and practical implementations using Spectra and MsCoreUtils.

6.1 Setting Up the Environment

Note: Using synthetic data due to mzR compatibility issues
Selected spectrum:
  Precursor m/z: 754.184 
  Number of peaks: 133 
  Intensity range: 1.19e+01 - 2.37e+06 

6.2 Modern Peak Detection with Spectra

6.2.1 Built-in Peak Picking

The Spectra package provides optimized peak picking methods:

Peak reduction:
  Original peaks: 133 
  After picking: 24 
  Reduction: 82 %

6.2.2 Noise Estimation

Estimated noise level: 3277.76 
SNR statistics:
  Median SNR: 0.75 
  Mean SNR: 9.31 
  Max SNR: 723.41 

6.3 Peak Quantification Methods

6.3.1 Peak Integration

Peak Area: 0 
Peak Height: 2371160 
Peak m/z: 719.2207 

6.3.2 Gaussian Fitting for Peak Quantification

Gaussian Fit Results:
  Center: 719.1114 
  Area: -6674634 
  R-squared: 0.994 

6.4 Peak Quality Assessment

6.4.1 Signal-to-Noise Ratio Calculation

SNR values for detected peaks:
[1] 69.69

6.4.2 Peak Symmetry Assessment

Peak Symmetry Assessment:
  Asymmetry Factor: 5.621 
  FWHM: 22.7887 

6.5 Peak List Management

6.5.1 Creating Comprehensive Peak Lists

  peak_id       mz intensity relative_intensity      snr asymmetry area
1       1 719.2207   2371160                100 69.68987  5.620902    0

6.6 Batch Peak Processing

6.6.1 Processing Multiple Spectra

Total peaks detected across spectra: 60 
  peak_id       mz intensity relative_intensity       snr  asymmetry    area
1       1 244.7057  42771.88           12.34308 0.9532201  2.5576214       0
2       2 327.8421  92756.94           26.76772 2.0967084  0.5159425 1722408
3       3 499.9734  48168.09           13.90031 1.0818725  0.4781007       0
4       4 583.4235 226616.75           65.39688 5.3889942 73.3594800       0
5       5 625.0181 148558.39           42.87086 3.4045120 15.9325747       0
6       6 653.3221 136722.88           39.45538 3.1273898  0.3150271       0
  spectrum_id retention_time
1           1            100
2           1            100
3           1            100
4           1            100
5           1            100
6           1            100

6.7 Advanced Peak Detection System

This section introduces a comprehensive peak detection and area calculation system that provides robust methods for spectral analysis, including distance-based filtering, slope analysis, and sophisticated area calculations.

6.7.1 Core Peak Detection Functions

6.7.2 Applying Advanced Peak Detection

Note: To use the advanced peak detection functions, you need to first define them by running the code blocks above (or source them from a separate R script). For demonstration purposes, we’ll show a simplified example here.

For a working example that doesn’t require the full function definitions, we can use a simplified peak detection approach:

Simplified peak detection results:
  Total peaks detected: 17 
  Peak m/z range: 300.44 1944.56 
  Peak height range: 6882.45 2371160 
  index     peak   height type
1     8 300.4421 31556.99 peak
2    17 393.2184 33672.98 peak
3    22 454.3711 10082.55 peak
4    27 510.6402 18913.01 peak
5    32 573.8701 19330.30 peak

6.7.3 Peak Slope Analysis

For demonstration, here’s a simplified slope calculation:

Peak slope analysis:
  Peaks analyzed: 16 
  Mean slope angle range: 89.96 90 degrees
  Mean asymmetry (diff_percent): 0.01 %
  index     peak   height angle_left angle_right angle_mean diff_percent
1     8 300.4421 31556.99   89.98430    89.99443   89.98936  0.011258257
2    17 393.2184 33672.98   89.99279    89.99487   89.99383  0.002310248
3    22 454.3711 10082.55   89.93208    89.98208   89.95708  0.055584005

6.7.4 Trough Detection

6.7.5 Peak Area Calculation System

6.7.6 Demonstrating Peak Area Calculation

6.8 Exercises

  1. Implement and compare different peak detection algorithms
  2. Evaluate the effect of different integration methods on quantification
  3. Create quality filters based on SNR and peak symmetry
  4. Develop a peak alignment algorithm for multiple spectra
  5. Build a complete peak processing pipeline with quality control
  6. Apply the advanced peak detection system to your own spectral data
  7. Compare peak areas calculated using different methods (trapezoidal, shoelace, Gaussian fitting)
  8. Implement peak area ratio analysis for comparative studies

6.9 Summary

This chapter covered advanced peak detection and quantification methods, including:

  • Modern peak detection: CWT-based detection, matched filtering, and built-in Spectra methods
  • Advanced peak detection system: Comprehensive functions (detect_spectral_peaks, filter_peaks_by_distance) for robust peak detection with neighbor validation, minimum height filtering, and distance-based filtering
  • Peak characterization: Slope analysis (calculate_peak_slope_metrics) and trough detection (detect_spectral_troughs) for comprehensive peak geometry assessment
  • Peak area calculation: Multiple integration approaches including trapezoidal rule, polygon area calculation (calculate_polygon_area), and Gaussian fitting
  • Area refinement: Linear regression-based boundary refinement (refine_peak_boundaries) for accurate peak area determination
  • Statistical analysis: Comprehensive area statistics (calculate_area_statistics) and ratio calculations (calculate_area_ratios) with outlier handling

These techniques form the foundation for reliable quantitative analysis in mass spectrometry, providing robust tools for both automated and manual peak analysis workflows.