Skip to content

Commit

Permalink
Merge pull request #801 from Mailaender/wsd-1st-derivative-filter-wav…
Browse files Browse the repository at this point in the history
…elengths

Added support to filter per wavelength in DAD 1st derivative peak detection
  • Loading branch information
eselmeister authored Nov 13, 2021
2 parents bab8237 + 9f716bb commit a66ae19
Show file tree
Hide file tree
Showing 21 changed files with 754 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@
import org.eclipse.chemclipse.chromatogram.xxd.peak.detector.supplier.firstderivative.support.IFirstDerivativeDetectorSlopes;
import org.eclipse.chemclipse.model.core.IChromatogram;
import org.eclipse.chemclipse.model.core.IPeak;
import org.eclipse.chemclipse.model.core.IScan;
import org.eclipse.chemclipse.model.support.ScanRange;
import org.eclipse.chemclipse.msd.model.core.IPeakModelMSD;
import org.eclipse.chemclipse.msd.model.core.IScanMSD;
import org.eclipse.chemclipse.msd.model.core.support.IMarkedIons;
import org.eclipse.chemclipse.numeric.core.IPoint;
import org.eclipse.chemclipse.numeric.core.Point;
import org.eclipse.chemclipse.numeric.equations.Equations;
import org.eclipse.chemclipse.numeric.equations.LinearEquation;
import org.eclipse.chemclipse.numeric.miscellaneous.Evaluation;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
Expand Down Expand Up @@ -111,76 +103,6 @@ public static List<IRawPeak> getRawPeaks(IFirstDerivativeDetectorSlopes slopes,
return rawPeaks;
}

protected ScanRange optimizeBaseline(IChromatogram<? extends IPeak> chromatogram, int startScan, int centerScan, int stopScan, IMarkedIons ions) {

/*
* Right and left baseline optimization
*/
int stopScanOptimized = optimizeRightBaseline(chromatogram, startScan, centerScan, stopScan, ions);
int startScanOptimized = optimizeLeftBaseline(chromatogram, startScan, centerScan, stopScanOptimized, ions);
//
return new ScanRange(startScanOptimized, stopScanOptimized);
}

protected float getScanSignal(IChromatogram<? extends IPeak> chromatogram, int scanNumber, IMarkedIons ions) {

float scanSignal = 0.0f;
IScan scan = chromatogram.getScan(scanNumber);
if(scan instanceof IScanMSD) {
IScanMSD scanMSD = (IScanMSD)scan;
scanSignal = scanMSD.getTotalSignal(ions);
} else {
scanSignal = scan.getTotalSignal();
}
return scanSignal;
}

private int optimizeRightBaseline(IChromatogram<? extends IPeak> chromatogram, int startScan, int centerScan, int stopScan, IMarkedIons ions) {

IPoint p1 = new Point(getRetentionTime(chromatogram, startScan), getScanSignal(chromatogram, startScan, ions));
IPoint p2 = new Point(getRetentionTime(chromatogram, stopScan), getScanSignal(chromatogram, stopScan, ions));
LinearEquation backgroundEquation = Equations.createLinearEquation(p1, p2);
/*
* Right border optimization
*/
int stopScanOptimized = stopScan;
for(int i = stopScan; i > centerScan; i--) {
float signal = getScanSignal(chromatogram, i, ions);
int retentionTime = chromatogram.getScan(i).getRetentionTime();
if(signal < backgroundEquation.calculateY(retentionTime)) {
stopScanOptimized = i;
}
}
//
return stopScanOptimized;
}

private int optimizeLeftBaseline(IChromatogram<? extends IPeak> chromatogram, int startScan, int centerScan, int stopScan, IMarkedIons ions) {

IPoint p1 = new Point(getRetentionTime(chromatogram, startScan), getScanSignal(chromatogram, startScan, ions));
IPoint p2 = new Point(getRetentionTime(chromatogram, stopScan), getScanSignal(chromatogram, stopScan, ions));
LinearEquation backgroundEquation = Equations.createLinearEquation(p1, p2);
/*
* Right border optimization
*/
int startScanOptimized = startScan;
for(int i = startScan; i < centerScan; i++) {
float signal = getScanSignal(chromatogram, i, ions);
int retentionTime = chromatogram.getScan(i).getRetentionTime();
if(signal < backgroundEquation.calculateY(retentionTime)) {
/*
* Create a new equation
*/
startScanOptimized = i;
p1 = new Point(getRetentionTime(chromatogram, startScanOptimized), getScanSignal(chromatogram, startScanOptimized, ions));
p2 = new Point(getRetentionTime(chromatogram, stopScan), getScanSignal(chromatogram, stopScan, ions));
backgroundEquation = Equations.createLinearEquation(p1, p2);
}
}
//
return startScanOptimized;
}

protected int getRetentionTime(IChromatogram<? extends IPeak> chromatogram, int scanNumber) {

return chromatogram.getScan(scanNumber).getRetentionTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public <T extends IMeasurement> Map<T, PeakList> detectIMeasurementPeaks(Collect
} else {
configuration = globalConfiguration;
}
slopes = PeakDetectorWSD.getFirstDerivativeSlopes(new ChromatogramSelectionWSD((IChromatogramWSD)measurement), configuration.getMovingAverageWindowSize());
// TODO: filter wavelengths
slopes = PeakDetectorWSD.getFirstDerivativeSlopes(new ChromatogramSelectionWSD((IChromatogramWSD)measurement), configuration.getMovingAverageWindowSize(), null);
} else if(measurement instanceof SpectrumMeasurement) {
if(globalConfiguration == null) {
configuration = new FirstDerivativePeakDetectorSettings(DataType.NMR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.chemclipse.logging.core.Logger;
import org.eclipse.chemclipse.model.core.IChromatogram;
import org.eclipse.chemclipse.model.core.IPeak;
import org.eclipse.chemclipse.model.core.IScan;
import org.eclipse.chemclipse.model.exceptions.PeakException;
import org.eclipse.chemclipse.model.signals.ITotalScanSignal;
import org.eclipse.chemclipse.model.signals.ITotalScanSignals;
Expand All @@ -45,6 +46,8 @@
import org.eclipse.chemclipse.msd.model.core.IChromatogramPeakMSD;
import org.eclipse.chemclipse.numeric.core.IPoint;
import org.eclipse.chemclipse.numeric.core.Point;
import org.eclipse.chemclipse.numeric.equations.Equations;
import org.eclipse.chemclipse.numeric.equations.LinearEquation;
import org.eclipse.chemclipse.processing.core.IProcessingInfo;
import org.eclipse.chemclipse.processing.core.MessageType;
import org.eclipse.chemclipse.processing.core.ProcessingMessage;
Expand Down Expand Up @@ -197,7 +200,7 @@ private List<IChromatogramPeakCSD> extractPeaks(List<IRawPeak> rawPeaks, IChroma
*/
ScanRange scanRange = new ScanRange(rawPeak.getStartScan(), rawPeak.getStopScan());
if(includeBackground && optimizeBaseline) {
scanRange = optimizeBaseline(chromatogram, scanRange.getStartScan(), rawPeak.getMaximumScan(), scanRange.getStopScan(), null);
scanRange = optimizeBaseline(chromatogram, scanRange.getStartScan(), rawPeak.getMaximumScan(), scanRange.getStopScan());
}
/*
* includeBackground
Expand Down Expand Up @@ -272,9 +275,66 @@ public static IFirstDerivativeDetectorSlopes getFirstDerivativeSlopes(IChromatog
*/
private boolean isValidPeak(IChromatogramPeakCSD peak, PeakDetectorSettingsCSD peakDetectorSettings) {

if(peak != null && peak.getSignalToNoiseRatio() >= peakDetectorSettings.getMinimumSignalToNoiseRatio()) {
return true;
return (peak != null && peak.getSignalToNoiseRatio() >= peakDetectorSettings.getMinimumSignalToNoiseRatio());
}

private ScanRange optimizeBaseline(IChromatogramCSD chromatogram, int startScan, int centerScan, int stopScan) {

int stopScanOptimized = optimizeRightBaseline(chromatogram, startScan, centerScan, stopScan);
int startScanOptimized = optimizeLeftBaseline(chromatogram, startScan, centerScan, stopScanOptimized);
//
return new ScanRange(startScanOptimized, stopScanOptimized);
}

private int optimizeRightBaseline(IChromatogramCSD chromatogram, int startScan, int centerScan, int stopScan) {

IPoint p1 = new Point(getRetentionTime(chromatogram, startScan), getScanSignal(chromatogram, startScan));
IPoint p2 = new Point(getRetentionTime(chromatogram, stopScan), getScanSignal(chromatogram, stopScan));
LinearEquation backgroundEquation = Equations.createLinearEquation(p1, p2);
/*
* Right border optimization
*/
int stopScanOptimized = stopScan;
for(int i = stopScan; i > centerScan; i--) {
float signal = getScanSignal(chromatogram, i);
int retentionTime = chromatogram.getScan(i).getRetentionTime();
if(signal < backgroundEquation.calculateY(retentionTime)) {
stopScanOptimized = i;
}
}
//
return stopScanOptimized;
}

private int optimizeLeftBaseline(IChromatogramCSD chromatogram, int startScan, int centerScan, int stopScan) {

IPoint p1 = new Point(getRetentionTime(chromatogram, startScan), getScanSignal(chromatogram, startScan));
IPoint p2 = new Point(getRetentionTime(chromatogram, stopScan), getScanSignal(chromatogram, stopScan));
LinearEquation backgroundEquation = Equations.createLinearEquation(p1, p2);
/*
* Right border optimization
*/
int startScanOptimized = startScan;
for(int i = startScan; i < centerScan; i++) {
float signal = getScanSignal(chromatogram, i);
int retentionTime = chromatogram.getScan(i).getRetentionTime();
if(signal < backgroundEquation.calculateY(retentionTime)) {
/*
* Create a new equation
*/
startScanOptimized = i;
p1 = new Point(getRetentionTime(chromatogram, startScanOptimized), getScanSignal(chromatogram, startScanOptimized));
p2 = new Point(getRetentionTime(chromatogram, stopScan), getScanSignal(chromatogram, stopScan));
backgroundEquation = Equations.createLinearEquation(p1, p2);
}
}
return false;
//
return startScanOptimized;
}

protected float getScanSignal(IChromatogramCSD chromatogram, int scanNumber) {

IScan scan = chromatogram.getScan(scanNumber);
return scan.getTotalSignal();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.chemclipse.logging.core.Logger;
import org.eclipse.chemclipse.model.core.IChromatogram;
import org.eclipse.chemclipse.model.core.IPeak;
import org.eclipse.chemclipse.model.core.IScan;
import org.eclipse.chemclipse.model.exceptions.ChromatogramIsNullException;
import org.eclipse.chemclipse.model.signals.ITotalScanSignal;
import org.eclipse.chemclipse.model.signals.ITotalScanSignals;
Expand All @@ -44,6 +45,7 @@
import org.eclipse.chemclipse.model.support.ScanRange;
import org.eclipse.chemclipse.msd.model.core.IChromatogramMSD;
import org.eclipse.chemclipse.msd.model.core.IChromatogramPeakMSD;
import org.eclipse.chemclipse.msd.model.core.IScanMSD;
import org.eclipse.chemclipse.msd.model.core.selection.IChromatogramSelectionMSD;
import org.eclipse.chemclipse.msd.model.core.support.IMarkedIons;
import org.eclipse.chemclipse.msd.model.core.support.IMarkedIons.IonMarkMode;
Expand All @@ -53,6 +55,8 @@
import org.eclipse.chemclipse.msd.model.xic.TotalIonSignalExtractor;
import org.eclipse.chemclipse.numeric.core.IPoint;
import org.eclipse.chemclipse.numeric.core.Point;
import org.eclipse.chemclipse.numeric.equations.Equations;
import org.eclipse.chemclipse.numeric.equations.LinearEquation;
import org.eclipse.chemclipse.processing.core.IProcessingInfo;
import org.eclipse.chemclipse.processing.core.MessageType;
import org.eclipse.chemclipse.processing.core.ProcessingMessage;
Expand Down Expand Up @@ -131,12 +135,13 @@ public List<IChromatogramPeakMSD> detectPeaks(IChromatogramSelectionMSD chromato

List<IChromatogramPeakMSD> extractPeaks = new ArrayList<>();
Collection<IMarkedIons> filterIons = peakDetectorSettings.getFilterIons();
IChromatogramMSD chromatogram = chromatogramSelection.getChromatogram();
for(IMarkedIons ions : filterIons) {
Threshold threshold = peakDetectorSettings.getThreshold();
int windowSize = peakDetectorSettings.getMovingAverageWindowSize();
List<IRawPeak> rawPeaks = new ArrayList<>();
//
if(noiseSegments != null && noiseSegments.size() > 0) {
if(noiseSegments != null && !noiseSegments.isEmpty()) {
/*
* Initial retention time range before running the detection using
* noise segments.
Expand All @@ -159,11 +164,11 @@ public List<IChromatogramPeakMSD> detectPeaks(IChromatogramSelectionMSD chromato
* Ranges between the noise segments
* [S] --- [N] --- [E]
*/
while(iterator.hasNext()) {
int startRetentionTimeSegment = noiseSegment.getStopRetentionTime();
while(iterator.hasNext() && noiseSegment != null) {
int previousStopRetentionTimeSegment = noiseSegment.getStopRetentionTime();
noiseSegment = iterator.next();
int stopRetentionTimeSegment = noiseSegment.getStartRetentionTime();
chromatogramSelection.setRangeRetentionTime(startRetentionTimeSegment, stopRetentionTimeSegment);
int nextStartRetentionTimeSegment = noiseSegment.getStartRetentionTime();
chromatogramSelection.setRangeRetentionTime(previousStopRetentionTimeSegment, nextStartRetentionTimeSegment);
IFirstDerivativeDetectorSlopes slopes = getFirstDerivativeSlopes(chromatogramSelection, windowSize, ions);
rawPeaks.addAll(getRawPeaks(slopes, threshold, monitor));
}
Expand All @@ -187,7 +192,7 @@ public List<IChromatogramPeakMSD> detectPeaks(IChromatogramSelectionMSD chromato
IFirstDerivativeDetectorSlopes slopes = getFirstDerivativeSlopes(chromatogramSelection, windowSize, ions);
rawPeaks.addAll(getRawPeaks(slopes, threshold, monitor));
}
List<IChromatogramPeakMSD> peaks = extractPeaks(rawPeaks, chromatogramSelection.getChromatogram(), peakDetectorSettings, ions);
List<IChromatogramPeakMSD> peaks = extractPeaks(rawPeaks, chromatogram, peakDetectorSettings, ions);
if(peakDetectorSettings.isUseIndividualTraces()) {
String classifier = "Trace " + ions.getIonsNominal().iterator().next();
for(IChromatogramPeakMSD msd : peaks) {
Expand Down Expand Up @@ -326,9 +331,74 @@ private static IonMarkMode buildFilterMode(FilterMode mode) {
*/
private boolean isValidPeak(IChromatogramPeakMSD peak, PeakDetectorSettingsMSD peakDetectorSettings) {

if(peak != null && peak.getSignalToNoiseRatio() >= peakDetectorSettings.getMinimumSignalToNoiseRatio()) {
return true;
return (peak != null && peak.getSignalToNoiseRatio() >= peakDetectorSettings.getMinimumSignalToNoiseRatio());
}

protected ScanRange optimizeBaseline(IChromatogramMSD chromatogram, int startScan, int centerScan, int stopScan, IMarkedIons ions) {

/*
* Right and left baseline optimization
*/
int stopScanOptimized = optimizeRightBaseline(chromatogram, startScan, centerScan, stopScan, ions);
int startScanOptimized = optimizeLeftBaseline(chromatogram, startScan, centerScan, stopScanOptimized, ions);
//
return new ScanRange(startScanOptimized, stopScanOptimized);
}

protected float getScanSignal(IChromatogramMSD chromatogram, int scanNumber, IMarkedIons ions) {

IScan scan = chromatogram.getScan(scanNumber);
if(scan instanceof IScanMSD) {
IScanMSD scanMSD = (IScanMSD)scan;
return scanMSD.getTotalSignal(ions);
} else {
return scan.getTotalSignal();
}
}

private int optimizeRightBaseline(IChromatogramMSD chromatogram, int startScan, int centerScan, int stopScan, IMarkedIons ions) {

IPoint p1 = new Point(getRetentionTime(chromatogram, startScan), getScanSignal(chromatogram, startScan, ions));
IPoint p2 = new Point(getRetentionTime(chromatogram, stopScan), getScanSignal(chromatogram, stopScan, ions));
LinearEquation backgroundEquation = Equations.createLinearEquation(p1, p2);
/*
* Right border optimization
*/
int stopScanOptimized = stopScan;
for(int i = stopScan; i > centerScan; i--) {
float signal = getScanSignal(chromatogram, i, ions);
int retentionTime = chromatogram.getScan(i).getRetentionTime();
if(signal < backgroundEquation.calculateY(retentionTime)) {
stopScanOptimized = i;
}
}
return false;
//
return stopScanOptimized;
}

private int optimizeLeftBaseline(IChromatogramMSD chromatogram, int startScan, int centerScan, int stopScan, IMarkedIons ions) {

IPoint p1 = new Point(getRetentionTime(chromatogram, startScan), getScanSignal(chromatogram, startScan, ions));
IPoint p2 = new Point(getRetentionTime(chromatogram, stopScan), getScanSignal(chromatogram, stopScan, ions));
LinearEquation backgroundEquation = Equations.createLinearEquation(p1, p2);
/*
* Right border optimization
*/
int startScanOptimized = startScan;
for(int i = startScan; i < centerScan; i++) {
float signal = getScanSignal(chromatogram, i, ions);
int retentionTime = chromatogram.getScan(i).getRetentionTime();
if(signal < backgroundEquation.calculateY(retentionTime)) {
/*
* Create a new equation
*/
startScanOptimized = i;
p1 = new Point(getRetentionTime(chromatogram, startScanOptimized), getScanSignal(chromatogram, startScanOptimized, ions));
p2 = new Point(getRetentionTime(chromatogram, stopScan), getScanSignal(chromatogram, stopScan, ions));
backgroundEquation = Equations.createLinearEquation(p1, p2);
}
}
//
return startScanOptimized;
}
}
Loading

0 comments on commit a66ae19

Please sign in to comment.