Skip to content

Commit

Permalink
#316 - Combined Mass Spectrum
Browse files Browse the repository at this point in the history
Further improvements are requires to enable mean and median
calculations.
  • Loading branch information
eselmeister committed Apr 14, 2020
1 parent f157fca commit f616cda
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 177 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2010, 2019 Lablicate GmbH.
* Copyright (c) 2010, 2020 Lablicate GmbH.
*
* All rights reserved. This
* program and the accompanying materials are made available under the terms of
Expand All @@ -25,7 +25,6 @@
import org.eclipse.chemclipse.msd.model.core.support.IMarkedIons;
import org.eclipse.chemclipse.msd.model.exceptions.FilterException;
import org.eclipse.chemclipse.msd.model.support.CombinedMassSpectrumCalculator;
import org.eclipse.chemclipse.msd.model.support.ICombinedMassSpectrumCalculator;
import org.eclipse.chemclipse.msd.model.xic.IExtractedIonSignal;
import org.eclipse.chemclipse.msd.model.xic.IExtractedIonSignals;
import org.eclipse.chemclipse.numeric.statistics.Calculations;
Expand Down Expand Up @@ -53,7 +52,7 @@ public Calculator() {
*/
public ICombinedMassSpectrum getNoiseMassSpectrum(List<ICombinedMassSpectrum> noiseMassSpectra, IMarkedIons ionsToPreserve, IProgressMonitor monitor) {

ICombinedMassSpectrumCalculator combinedMassSpectrumCalculator = new CombinedMassSpectrumCalculator();
CombinedMassSpectrumCalculator combinedMassSpectrumCalculator = new CombinedMassSpectrumCalculator();
/*
* Iterate through all given noise mass spectra.
*/
Expand Down Expand Up @@ -126,7 +125,7 @@ private List<INoiseSegment> calculateNoiseSegments(List<IAnalysisSegment> analys
* with the segment. The noise segment will be stored in the
* noise segment list.
*/
ICombinedMassSpectrumCalculator combinedMassSpectrumCalculator = calculatorSupport.getCombinedMassSpectrumCalculator(analysisSegment, extractedIonSignals);
CombinedMassSpectrumCalculator combinedMassSpectrumCalculator = calculatorSupport.getCombinedMassSpectrumCalculator(analysisSegment, extractedIonSignals);
ICombinedMassSpectrum noiseMassSpectrum = calculatorSupport.getNoiseMassSpectrum(combinedMassSpectrumCalculator, ionsToPreserve, monitor);
INoiseSegment noiseSegment = new NoiseSegment(analysisSegment, noiseMassSpectrum);
noiseSegments.add(noiseSegment);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2010, 2019 Lablicate GmbH.
* Copyright (c) 2010, 2020 Lablicate GmbH.
*
* All rights reserved. This
* program and the accompanying materials are made available under the terms of
Expand Down Expand Up @@ -29,7 +29,6 @@
import org.eclipse.chemclipse.msd.model.implementation.CombinedMassSpectrum;
import org.eclipse.chemclipse.msd.model.implementation.Ion;
import org.eclipse.chemclipse.msd.model.support.CombinedMassSpectrumCalculator;
import org.eclipse.chemclipse.msd.model.support.ICombinedMassSpectrumCalculator;
import org.eclipse.chemclipse.msd.model.xic.IExtractedIonSignal;
import org.eclipse.chemclipse.msd.model.xic.IExtractedIonSignals;
import org.eclipse.core.runtime.IProgressMonitor;
Expand Down Expand Up @@ -61,10 +60,10 @@ public boolean acceptSegment(double[] values, double mean) {
*
* @return
*/
public ICombinedMassSpectrumCalculator getCombinedMassSpectrumCalculator(IAnalysisSegment analysisSegment, IExtractedIonSignals extractedIonSignals) {
public CombinedMassSpectrumCalculator getCombinedMassSpectrumCalculator(IAnalysisSegment analysisSegment, IExtractedIonSignals extractedIonSignals) {

IExtractedIonSignal extractedIonSignal;
ICombinedMassSpectrumCalculator combinedMassSpectrumCalculator = new CombinedMassSpectrumCalculator();
CombinedMassSpectrumCalculator combinedMassSpectrumCalculator = new CombinedMassSpectrumCalculator();
for(int scan = analysisSegment.getStartScan(); scan <= analysisSegment.getStopScan(); scan++) {
try {
extractedIonSignal = extractedIonSignals.getExtractedIonSignal(scan);
Expand All @@ -83,7 +82,7 @@ public ICombinedMassSpectrumCalculator getCombinedMassSpectrumCalculator(IAnalys

public static ICombinedMassSpectrum getCombinedMassSpectrum(IExtractedIonSignals extractedIonSignals, IScanRange range) {

ICombinedMassSpectrumCalculator combinedMassSpectrumCalculator = new CombinedMassSpectrumCalculator();
CombinedMassSpectrumCalculator combinedMassSpectrumCalculator = new CombinedMassSpectrumCalculator();
for(int scan = range.getStartScan(); scan <= range.getStopScan(); scan++) {
try {
IExtractedIonSignal extractedIonSignal = extractedIonSignals.getExtractedIonSignal(scan);
Expand All @@ -100,7 +99,7 @@ public static ICombinedMassSpectrum getCombinedMassSpectrum(IExtractedIonSignals
float abundance;
ICombinedMassSpectrum noiseMassSpectrum = new CombinedMassSpectrum();
IIon noiseIon;
Map<Integer, Double> ions = combinedMassSpectrumCalculator.getValues();
Map<Integer, Double> ions = combinedMassSpectrumCalculator.getValuesIntensities();
for(Integer ion : ions.keySet()) {
/*
* Check the abundance.
Expand All @@ -121,7 +120,7 @@ public static ICombinedMassSpectrum getCombinedMassSpectrum(IExtractedIonSignals
/*
* Returns a combined mass spectrum.
*/
public ICombinedMassSpectrum getNoiseMassSpectrum(ICombinedMassSpectrumCalculator combinedMassSpectrumCalculator, IMarkedIons ionsToPreserve, IProgressMonitor monitor) {
public ICombinedMassSpectrum getNoiseMassSpectrum(CombinedMassSpectrumCalculator combinedMassSpectrumCalculator, IMarkedIons ionsToPreserve, IProgressMonitor monitor) {

/*
* Remove the ions to preserve.
Expand All @@ -138,7 +137,7 @@ public ICombinedMassSpectrum getNoiseMassSpectrum(ICombinedMassSpectrumCalculato
float abundance;
ICombinedMassSpectrum noiseMassSpectrum = new CombinedMassSpectrum();
IIon noiseIon;
Map<Integer, Double> ions = combinedMassSpectrumCalculator.getValues();
Map<Integer, Double> ions = combinedMassSpectrumCalculator.getValuesIntensities();
for(Integer ion : ions.keySet()) {
/*
* Check the abundance.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2018 Lablicate GmbH.
* Copyright (c) 2008, 2020 Lablicate GmbH.
*
* All rights reserved.
* This program and the accompanying materials are made available under the
Expand All @@ -11,6 +11,7 @@
*******************************************************************************/
package org.eclipse.chemclipse.msd.model.support;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -20,41 +21,38 @@
import org.eclipse.chemclipse.msd.model.core.AbstractIon;
import org.eclipse.chemclipse.msd.model.core.IIon;
import org.eclipse.chemclipse.msd.model.core.support.IMarkedIons;
import org.eclipse.chemclipse.numeric.statistics.Calculations;

public class CombinedMassSpectrumCalculator implements ICombinedMassSpectrumCalculator {
public class CombinedMassSpectrumCalculator {

private Map<Integer, Double> combinedMassSpectrum;
private Map<Integer, List<Double>> combinedMassSpectrum = new HashMap<Integer, List<Double>>();

public CombinedMassSpectrumCalculator() {
combinedMassSpectrum = new HashMap<Integer, Double>();
}

@Override
public void addIon(double ion, float abundance) {
public void addIon(double ion, double abundance) {

/*
* If the abundance is zero, do nothing and return.
*/
if(abundance == 0.0f) {
if(abundance == 0.0d) {
return;
}
int key = AbstractIon.getIon(ion);
/*
* Add the abundance if still a ion exists, otherwise still add the ion.
*/
if(combinedMassSpectrum.containsKey(key)) {
combinedMassSpectrum.put(key, combinedMassSpectrum.get(key) + abundance);
} else {
combinedMassSpectrum.put(key, (double)abundance);
List<Double> intensities = combinedMassSpectrum.get(key);
if(intensities == null) {
intensities = new ArrayList<Double>();
combinedMassSpectrum.put(key, intensities);
}
intensities.add(abundance);
}

@Override
public void addIons(List<IIon> ions, IMarkedIons excludedIons) {

if(ions == null || excludedIons == null) {
return;
}
//
Set<Integer> excludedIonsNominal = excludedIons.getIonsNominal();
for(IIon ion : ions) {
if(!excludedIonsNominal.contains(ion.getIon())) {
Expand All @@ -63,39 +61,39 @@ public void addIons(List<IIon> ions, IMarkedIons excludedIons) {
}
}

@Override
public void removeIon(double ion) {

int key = AbstractIon.getIon(ion);
combinedMassSpectrum.remove(key);
}

@Override
public void removeIons(IMarkedIons excludedIons) {

for(Integer ion : excludedIons.getIonsNominal()) {
combinedMassSpectrum.remove(ion);
}
}

@Override
public double getAbundance(double ion) {

double result = 0.0d;
int key = AbstractIon.getIon(ion);
if(combinedMassSpectrum.containsKey(key)) {
result = combinedMassSpectrum.get(key);
}
return result;
return calculateSumIntensity(combinedMassSpectrum.get(key));
}

@Override
public Map<Integer, Double> getValues() {
public Map<Integer, List<Double>> getValues() {

return combinedMassSpectrum;
}

@Override
public Map<Integer, Double> getValuesIntensities() {

Map<Integer, Double> map = new HashMap<>();
for(Integer key : combinedMassSpectrum.keySet()) {
map.put(key, calculateSumIntensity(combinedMassSpectrum.get(key)));
}
return map;
}

public void normalize(float normalizationFactor) {

/*
Expand All @@ -107,15 +105,38 @@ public void normalize(float normalizationFactor) {
/*
* If max is zero it doesn't even make sense to go further on.
*/
double max = Collections.max(combinedMassSpectrum.values());
double max = Collections.max(getValuesIntensities().values());
if(max == 0.0d) {
return;
}
//
double correlationFactor = normalizationFactor / max;
double value;
for(Integer key : combinedMassSpectrum.keySet()) {
value = correlationFactor * combinedMassSpectrum.get(key);
combinedMassSpectrum.put(key, value);
List<Double> adjustedIntensities = new ArrayList<>();
for(double intensity : combinedMassSpectrum.get(key)) {
adjustedIntensities.add(correlationFactor * intensity);
}
combinedMassSpectrum.put(key, adjustedIntensities);
}
}

public double calculateSumIntensity(List<Double> intensities) {

double sum = 0.0d;
if(intensities != null) {
int size = intensities.size();
double[] values = new double[size];
for(int i = 0; i < size; i++) {
values[i] = intensities.get(i);
}
/*
* Add an option here to calculate the sum,
* mean or median signal.
*/
sum = Calculations.getSum(values);
// sum = Calculations.getMean(values);
// sum = Calculations.getMedian(values);
}
return sum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static IScanMSD getCombinedMassSpectrum(IChromatogramSelectionMSD chromat
IChromatogramMSD chromatogram = chromatogramSelection.getChromatogram();
int startScan = chromatogram.getScanNumber(chromatogramSelection.getStartRetentionTime());
int stopScan = chromatogram.getScanNumber(chromatogramSelection.getStopRetentionTime());
ICombinedMassSpectrumCalculator massSpectrumCalculator = new CombinedMassSpectrumCalculator();
CombinedMassSpectrumCalculator massSpectrumCalculator = new CombinedMassSpectrumCalculator();
/*
* Merge all selected scans.<br/> All scan intensities will be added.
* Afterwards, they will be normalized.<br/> It would be also possible
Expand Down Expand Up @@ -88,7 +88,7 @@ public static IScanMSD getCombinedMassSpectrum(IScanMSD massSpectrum1, IScanMSD
* If one of the mass spectra is null, take only the valid one to calculate the normalized mass spectrum.
*/
excludedIons = validateExcludedIons(excludedIons);
ICombinedMassSpectrumCalculator massSpectrumCalculator = new CombinedMassSpectrumCalculator();
CombinedMassSpectrumCalculator massSpectrumCalculator = new CombinedMassSpectrumCalculator();
//
if(massSpectrum1 == null) {
addIonsToCalculator(massSpectrum2, excludedIons, massSpectrumCalculator, useNormalize);
Expand All @@ -104,7 +104,7 @@ public static IScanMSD getCombinedMassSpectrum(IScanMSD massSpectrum1, IScanMSD
return getMassSpectrum(massSpectrumCalculator, useNormalize);
}

private static void addIonsToCalculator(IScanMSD massSpectrum, IMarkedIons excludedIons, ICombinedMassSpectrumCalculator massSpectrumCalculator, boolean useNormalize) {
private static void addIonsToCalculator(IScanMSD massSpectrum, IMarkedIons excludedIons, CombinedMassSpectrumCalculator massSpectrumCalculator, boolean useNormalize) {

IScanMSD massSpectrumCalculated = getCalculatedMassSpectrum(massSpectrum, excludedIons, useNormalize);
massSpectrumCalculator.addIons(massSpectrumCalculated.getIons(), excludedIons);
Expand All @@ -125,7 +125,7 @@ public static IScanMSD getCalculatedMassSpectrum(IScanMSD massSpectrum, IMarkedI
}
excludedIons = validateExcludedIons(excludedIons);
//
ICombinedMassSpectrumCalculator massSpectrumCalculator = new CombinedMassSpectrumCalculator();
CombinedMassSpectrumCalculator massSpectrumCalculator = new CombinedMassSpectrumCalculator();
massSpectrumCalculator.addIons(massSpectrum.getIons(), excludedIons);
return getMassSpectrum(massSpectrumCalculator, useNormalize);
}
Expand Down Expand Up @@ -153,12 +153,12 @@ private static IMarkedIons validateExcludedIons(IMarkedIons excludedIons) {
* @param massSpectrumCalculator
* @return IMassSpectrum
*/
private static IScanMSD getMassSpectrum(ICombinedMassSpectrumCalculator massSpectrumCalculator, boolean useNormalize) {
private static IScanMSD getMassSpectrum(CombinedMassSpectrumCalculator massSpectrumCalculator, boolean useNormalize) {

if(useNormalize) {
massSpectrumCalculator.normalize(NORMALIZATION_FACTOR);
}
Map<Integer, Double> ions = massSpectrumCalculator.getValues();
Map<Integer, Double> ions = massSpectrumCalculator.getValuesIntensities();
return getMassSpectrum(ions);
}

Expand Down

This file was deleted.

Loading

0 comments on commit f616cda

Please sign in to comment.