for (PeakList peakList : peakLists) {
// Create a sorted set of scores matching
TreeSet<RowVsRowScore> scoreSet = new TreeSet<RowVsRowScore>();
PeakListRow allRows[] = peakList.getRows();
// Calculate scores for all possible alignments of this row
for (PeakListRow row : allRows) {
if (isCanceled())
return;
// Calculate limits for a row with which the row can be aligned
Range mzRange = mzTolerance.getToleranceRange(row
.getAverageMZ());
Range rtRange = rtTolerance.getToleranceRange(row
.getAverageRT());
// Get all rows of the aligned peaklist within parameter limits
PeakListRow candidateRows[] = alignedPeakList
.getRowsInsideScanAndMZRange(rtRange, mzRange);
// Calculate scores and store them
for (PeakListRow candidate : candidateRows) {
if (sameChargeRequired) {
if (!PeakUtils.compareChargeState(row, candidate))
continue;
}
if (sameIDRequired) {
if (!PeakUtils.compareIdentities(row, candidate))
continue;
}
if (compareIsotopePattern) {
IsotopePattern ip1 = row.getBestIsotopePattern();
IsotopePattern ip2 = candidate.getBestIsotopePattern();
if ((ip1 != null) && (ip2 != null)) {
ParameterSet isotopeParams = parameters
.getParameter(
JoinAlignerParameters.compareIsotopePattern)
.getEmbeddedParameters();
if (!IsotopePatternScoreCalculator.checkMatch(ip1,
ip2, isotopeParams)) {
continue;
}
}
}
RowVsRowScore score = new RowVsRowScore(row, candidate,
mzRange.getSize() / 2, mzWeight,
rtRange.getSize() / 2, rtWeight);
scoreSet.add(score);
}
processedRows++;
}
// Create a table of mappings for best scores
Hashtable<PeakListRow, PeakListRow> alignmentMapping = new Hashtable<PeakListRow, PeakListRow>();
// Iterate scores by descending order
Iterator<RowVsRowScore> scoreIterator = scoreSet.iterator();
while (scoreIterator.hasNext()) {
RowVsRowScore score = scoreIterator.next();
// Check if the row is already mapped
if (alignmentMapping.containsKey(score.getPeakListRow()))
continue;
// Check if the aligned row is already filled
if (alignmentMapping.containsValue(score.getAlignedRow()))
continue;
alignmentMapping.put(score.getPeakListRow(),
score.getAlignedRow());
}
// Align all rows using mapping
for (PeakListRow row : allRows) {
PeakListRow targetRow = alignmentMapping.get(row);
// If we have no mapping for this row, add a new one
if (targetRow == null) {
targetRow = new SimplePeakListRow(newRowID);
newRowID++;
alignedPeakList.addRow(targetRow);
}
// Add all peaks from the original row to the aligned row
for (RawDataFile file : row.getRawDataFiles()) {
targetRow.addPeak(file, row.getPeak(file));
}
// Add all non-existing identities from the original row to the
// aligned row
PeakUtils.copyPeakListRowProperties(row, targetRow);