Search in sources :

Example 1 with PolynomialFunction

use of org.apache.commons.math.analysis.polynomials.PolynomialFunction in project mzmine2 by mzmine.

the class RANSAC method fittPolinomialFunction.

private void fittPolinomialFunction(List<AlignStructMol> data, boolean linear) {
    List<AlignStructMol> points = new ArrayList<AlignStructMol>();
    int degree = 3;
    if (linear) {
        degree = 1;
    }
    PolynomialFitter fitter = new PolynomialFitter(degree, new GaussNewtonOptimizer(true));
    for (int i = 0; i < data.size(); i++) {
        AlignStructMol point = data.get(i);
        if (point.ransacMaybeInLiers) {
            points.add(point);
            fitter.addObservedPoint(1, point.RT, point.RT2);
        }
    }
    try {
        PolynomialFunction function = fitter.fit();
        for (AlignStructMol point : data) {
            double y = point.RT2;
            double bestY = function.value(point.RT);
            if (Math.abs(y - bestY) < t) {
                point.ransacAlsoInLiers = true;
                AlsoNumber++;
            } else {
                point.ransacAlsoInLiers = false;
            }
        }
    } catch (Exception ex) {
    }
}
Also used : PolynomialFitter(org.apache.commons.math.optimization.fitting.PolynomialFitter) GaussNewtonOptimizer(org.apache.commons.math.optimization.general.GaussNewtonOptimizer) ArrayList(java.util.ArrayList) PolynomialFunction(org.apache.commons.math.analysis.polynomials.PolynomialFunction)

Example 2 with PolynomialFunction

use of org.apache.commons.math.analysis.polynomials.PolynomialFunction in project mzmine2 by mzmine.

the class AlignmentRansacPlot method addSeries.

/**
 * Add new serie.
 *
 * @param v Vector with the alignments
 * @param Name Name of the type of lipids in this alignment
 */
public void addSeries(Vector<AlignStructMol> data, String title, boolean linear) {
    try {
        chart.setTitle(title);
        XYSeries s1 = new XYSeries("Aligned pairs");
        XYSeries s2 = new XYSeries("Non-aligned pairs");
        XYSeries s3 = new XYSeries("Model");
        PolynomialFunction function = getPolynomialFunction(data, linear);
        for (AlignStructMol point : data) {
            if (point.Aligned) {
                s1.add(point.row1.getPeaks()[0].getRT(), point.row2.getPeaks()[0].getRT());
            } else {
                s2.add(point.row1.getPeaks()[0].getRT(), point.row2.getPeaks()[0].getRT());
            }
            try {
                s3.add(function.value(point.row2.getPeaks()[0].getRT()), point.row2.getPeaks()[0].getRT());
            } catch (Exception e) {
            }
        }
        this.dataset.addSeries(s1);
        this.dataset.addSeries(s2);
        this.dataset.addSeries(s3);
    } catch (Exception e) {
    }
}
Also used : XYSeries(org.jfree.data.xy.XYSeries) PolynomialFunction(org.apache.commons.math.analysis.polynomials.PolynomialFunction)

Example 3 with PolynomialFunction

use of org.apache.commons.math.analysis.polynomials.PolynomialFunction in project mzmine2 by mzmine.

the class RansacAlignerTask method getAlignmentMap.

/**
 * @param peakList
 * @return
 */
private HashMap<PeakListRow, PeakListRow> getAlignmentMap(PeakList peakList) {
    // Create a table of mappings for best scores
    HashMap<PeakListRow, PeakListRow> alignmentMapping = new HashMap<PeakListRow, PeakListRow>();
    if (alignedPeakList.getNumberOfRows() < 1) {
        return alignmentMapping;
    }
    // Create a sorted set of scores matching
    TreeSet<RowVsRowScore> scoreSet = new TreeSet<RowVsRowScore>();
    // RANSAC algorithm
    List<AlignStructMol> list = ransacPeakLists(alignedPeakList, peakList);
    PolynomialFunction function = this.getPolynomialFunction(list);
    PeakListRow[] allRows = peakList.getRows();
    for (PeakListRow row : allRows) {
        // Calculate limits for a row with which the row can be aligned
        Range<Double> mzRange = mzTolerance.getToleranceRange(row.getAverageMZ());
        double rt;
        try {
            rt = function.value(row.getAverageRT());
        } catch (NullPointerException e) {
            rt = row.getAverageRT();
        }
        if (Double.isNaN(rt) || rt == -1) {
            rt = row.getAverageRT();
        }
        Range<Double> rtRange = rtToleranceAfter.getToleranceRange(rt);
        // Get all rows of the aligned peaklist within parameter limits
        PeakListRow[] candidateRows = alignedPeakList.getRowsInsideScanAndMZRange(rtRange, mzRange);
        for (PeakListRow candidate : candidateRows) {
            RowVsRowScore score;
            if (sameChargeRequired && (!PeakUtils.compareChargeState(row, candidate))) {
                continue;
            }
            try {
                score = new RowVsRowScore(row, candidate, RangeUtils.rangeLength(mzRange) / 2.0, RangeUtils.rangeLength(rtRange) / 2.0, rt);
                scoreSet.add(score);
                setErrorMessage(score.getErrorMessage());
            } catch (Exception e) {
                e.printStackTrace();
                setStatus(TaskStatus.ERROR);
                return null;
            }
        }
        processedRows++;
    }
    // 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());
    }
    return alignmentMapping;
}
Also used : HashMap(java.util.HashMap) PolynomialFunction(org.apache.commons.math.analysis.polynomials.PolynomialFunction) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) TreeSet(java.util.TreeSet)

Aggregations

PolynomialFunction (org.apache.commons.math.analysis.polynomials.PolynomialFunction)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 PeakListRow (net.sf.mzmine.datamodel.PeakListRow)1 SimplePeakListRow (net.sf.mzmine.datamodel.impl.SimplePeakListRow)1 PolynomialFitter (org.apache.commons.math.optimization.fitting.PolynomialFitter)1 GaussNewtonOptimizer (org.apache.commons.math.optimization.general.GaussNewtonOptimizer)1 XYSeries (org.jfree.data.xy.XYSeries)1