for (int ind = 0; ind < totalPeaks; ind++) {
if (isCanceled())
return;
ChromatographicPeak aPeak = sortedPeaks[ind];
// Check if peak was already deleted
if (aPeak == null) {
processedPeaks++;
continue;
}
// Check which charge state fits best around this peak
int bestFitCharge = 0;
int bestFitScore = -1;
Vector<ChromatographicPeak> bestFitPeaks = null;
for (int charge : charges) {
Vector<ChromatographicPeak> fittedPeaks = new Vector<ChromatographicPeak>();
fittedPeaks.add(aPeak);
fitPattern(fittedPeaks, aPeak, charge, sortedPeaks);
int score = fittedPeaks.size();
if ((score > bestFitScore)
|| ((score == bestFitScore) && (bestFitCharge > charge))) {
bestFitScore = score;
bestFitCharge = charge;
bestFitPeaks = fittedPeaks;
}
}
PeakListRow oldRow = peakList.getPeakRow(aPeak);
assert bestFitPeaks != null;
// Verify the number of detected isotopes. If there is only one
// isotope, we skip this left the original peak in the peak list.
if (bestFitPeaks.size() == 1) {
deisotopedPeakList.addRow(oldRow);
processedPeaks++;
continue;
}
// Convert the peak pattern to array
ChromatographicPeak originalPeaks[] = bestFitPeaks
.toArray(new ChromatographicPeak[0]);
// Create a new SimpleIsotopePattern
DataPoint isotopes[] = new DataPoint[bestFitPeaks.size()];
for (int i = 0; i < isotopes.length; i++) {
ChromatographicPeak p = originalPeaks[i];
isotopes[i] = new SimpleDataPoint(p.getMZ(), p.getHeight());
}
SimpleIsotopePattern newPattern = new SimpleIsotopePattern(
isotopes, IsotopePatternStatus.DETECTED, aPeak.toString());
// Depending on user's choice, we leave either the most intenst, or
// the lowest m/z peak
if (chooseMostIntense) {
Arrays.sort(originalPeaks, new PeakSorter(
SortingProperty.Height, SortingDirection.Descending));
} else {
Arrays.sort(originalPeaks, new PeakSorter(SortingProperty.MZ,
SortingDirection.Ascending));
}
ChromatographicPeak newPeak = new SimpleChromatographicPeak(
originalPeaks[0]);
newPeak.setIsotopePattern(newPattern);
newPeak.setCharge(bestFitCharge);
// Keep old ID
int oldID = oldRow.getID();
SimplePeakListRow newRow = new SimplePeakListRow(oldID);
PeakUtils.copyPeakListRowProperties(oldRow, newRow);