continue rowIteration;
}
}
// Copy comment and identification
SimplePeakListRow normalizedRow = new SimplePeakListRow(row.getID());
PeakUtils.copyPeakListRowProperties(row, normalizedRow);
// Get m/z and RT of the current row
double mz = row.getAverageMZ();
double rt = row.getAverageRT();
// Loop through all raw data files
for (RawDataFile file : originalPeakList.getRawDataFiles()) {
double normalizationFactors[] = null;
double normalizationFactorWeights[] = null;
if (normalizationType == StandardUsageType.Nearest) {
// Search for nearest standard
PeakListRow nearestStandardRow = null;
double nearestStandardRowDistance = Double.MAX_VALUE;
for (int standardRowIndex = 0; standardRowIndex < standardRows.length; standardRowIndex++) {
PeakListRow standardRow = standardRows[standardRowIndex];
double stdMZ = standardRow.getAverageMZ();
double stdRT = standardRow.getAverageRT();
double distance = MZvsRTBalance * Math.abs(mz - stdMZ)
+ Math.abs(rt - stdRT);
if (distance <= nearestStandardRowDistance) {
nearestStandardRow = standardRow;
nearestStandardRowDistance = distance;
}
}
assert nearestStandardRow != null;
// Calc and store a single normalization factor
normalizationFactors = new double[1];
normalizationFactorWeights = new double[1];
ChromatographicPeak standardPeak = nearestStandardRow
.getPeak(file);
if (peakMeasurementType == PeakMeasurementType.HEIGHT) {
normalizationFactors[0] = standardPeak.getHeight();
} else {
normalizationFactors[0] = standardPeak.getArea();
}
logger.finest("Normalizing row #" + row.getID()
+ " using standard peak " + standardPeak
+ ", factor " + normalizationFactors[0]);
normalizationFactorWeights[0] = 1.0f;
}
if (normalizationType == StandardUsageType.Weighted) {
// Add all standards as factors, and use distance as weight
normalizationFactors = new double[standardRows.length];
normalizationFactorWeights = new double[standardRows.length];
for (int standardRowIndex = 0; standardRowIndex < standardRows.length; standardRowIndex++) {
PeakListRow standardRow = standardRows[standardRowIndex];
double stdMZ = standardRow.getAverageMZ();
double stdRT = standardRow.getAverageRT();
double distance = MZvsRTBalance * Math.abs(mz - stdMZ)
+ Math.abs(rt - stdRT);
ChromatographicPeak standardPeak = standardRow
.getPeak(file);
if (standardPeak == null) {
// What to do if standard peak is not
// available? (Currently this is ruled out by the
// setup dialog, which shows only peaks that are
// present in all samples)
normalizationFactors[standardRowIndex] = 1.0;
normalizationFactorWeights[standardRowIndex] = 0.0;
} else {
if (peakMeasurementType == PeakMeasurementType.HEIGHT) {
normalizationFactors[standardRowIndex] = standardPeak
.getHeight();
} else {
normalizationFactors[standardRowIndex] = standardPeak
.getArea();
}
normalizationFactorWeights[standardRowIndex] = 1 / distance;
}
}
}
assert normalizationFactors != null;
assert normalizationFactorWeights != null;
// Calculate a single normalization factor as weighted average
// of all factors
double weightedSum = 0.0f;
double sumOfWeights = 0.0f;
for (int factorIndex = 0; factorIndex < normalizationFactors.length; factorIndex++) {
weightedSum += normalizationFactors[factorIndex]
* normalizationFactorWeights[factorIndex];
sumOfWeights += normalizationFactorWeights[factorIndex];
}
double normalizationFactor = weightedSum / sumOfWeights;
// For simple scaling of the normalized values
normalizationFactor = normalizationFactor / 100.0f;
logger.finest("Normalizing row #" + row.getID() + "[" + file
+ "] using factor " + normalizationFactor);
// How to handle zero normalization factor?
if (normalizationFactor == 0.0)
normalizationFactor = Double.MIN_VALUE;
// Normalize peak
ChromatographicPeak originalPeak = row.getPeak(file);
if (originalPeak != null) {
SimpleChromatographicPeak normalizedPeak = new SimpleChromatographicPeak(
originalPeak);
PeakUtils.copyPeakProperties(originalPeak, normalizedPeak);
double normalizedHeight = originalPeak.getHeight()
/ normalizationFactor;
double normalizedArea = originalPeak.getArea()
/ normalizationFactor;
normalizedPeak.setHeight(normalizedHeight);
normalizedPeak.setArea(normalizedArea);
normalizedRow.addPeak(file, normalizedPeak);
}
}
normalizedPeakList.addRow(normalizedRow);