Example usage for org.apache.commons.math.analysis.polynomials PolynomialFunction value

List of usage examples for org.apache.commons.math.analysis.polynomials PolynomialFunction value

Introduction

In this page you can find the example usage for org.apache.commons.math.analysis.polynomials PolynomialFunction value.

Prototype

public double value(double x) 

Source Link

Document

Compute the value of the function for the given argument.

Usage

From source file:io.github.msdk.features.ransacaligner.RansacAlignerMethod.java

private Hashtable<FeatureTableRow, FeatureTableRow> getAlignmentMap(FeatureTable featureTable) {

    // Create a table of mappings for best scores
    Hashtable<FeatureTableRow, FeatureTableRow> alignmentMapping = new Hashtable<FeatureTableRow, FeatureTableRow>();

    // Create a sorted set of scores matching
    TreeSet<RowVsRowScore> scoreSet = new TreeSet<RowVsRowScore>();

    // RANSAC algorithm
    List<AlignStructMol> list = ransacPeakLists(result, featureTable);
    PolynomialFunction function = this.getPolynomialFunction(list);

    List<FeatureTableRow> allRows = featureTable.getRows();

    for (FeatureTableRow row : allRows) {
        // Calculate limits for a row with which the row can be aligned
        Range<Double> mzRange = mzTolerance.getToleranceRange(row.getMz());

        double rt;
        try {//from w  ww .ja va  2 s .  c om
            rt = function.value(row.getChromatographyInfo().getRetentionTime());
        } catch (NullPointerException e) {
            rt = row.getChromatographyInfo().getRetentionTime();
        }
        if (Double.isNaN(rt) || rt == -1) {
            rt = row.getChromatographyInfo().getRetentionTime();
        }

        Range<Double> rtRange = rtToleranceAfterCorrection.getToleranceRange(rt);

        // Get all rows of the aligned feature table within the m/z and
        // RT limits
        List<FeatureTableRow> candidateRows = result.getRowsInsideRange(rtRange, mzRange);

        for (FeatureTableRow candidateRow : candidateRows) {
            RowVsRowScore score;
            if (requireSameCharge) {
                FeatureTableColumn<Integer> chargeColumn1 = featureTable.getColumn(ColumnName.CHARGE, null);
                FeatureTableColumn<Integer> chargeColumn2 = result.getColumn(ColumnName.CHARGE, null);
                Integer charge1 = row.getData(chargeColumn1);
                Integer charge2 = candidateRow.getData(chargeColumn2);
                if (!charge1.equals(charge2))
                    continue;
            }

            // Check ion annotation
            if (requireSameAnnotation) {
                FeatureTableColumn<List<IonAnnotation>> ionAnnotationColumn1 = featureTable
                        .getColumn(ColumnName.IONANNOTATION, null);
                FeatureTableColumn<List<IonAnnotation>> ionAnnotationColumn2 = result
                        .getColumn(ColumnName.IONANNOTATION, null);
                List<IonAnnotation> ionAnnotations1 = row.getData(ionAnnotationColumn1);
                List<IonAnnotation> ionAnnotations2 = candidateRow.getData(ionAnnotationColumn2);

                // Check that all ion annotations in first row are in
                // the candidate row
                boolean equalIons = false;
                if (ionAnnotations1 != null && ionAnnotations2 != null) {
                    for (IonAnnotation ionAnnotation : ionAnnotations1) {
                        for (IonAnnotation targetIonAnnotation : ionAnnotations2) {
                            if (targetIonAnnotation.compareTo(ionAnnotation) == 0)
                                equalIons = true;
                        }
                    }
                }
                if (!equalIons)
                    continue;

            }

            try {
                double mzLength = mzRange.upperEndpoint() - mzRange.lowerEndpoint();
                double rtLength = rtRange.upperEndpoint() - rtRange.lowerEndpoint();
                score = new RowVsRowScore(row, candidateRow, mzLength, rtLength, new Float(rt));

                scoreSet.add(score);

            } catch (Exception e) {
                return null;
            }
        }
    }

    // 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.getFeatureTableRow())) {
            continue;
        }

        // Check if the aligned row is already filled
        if (alignmentMapping.containsValue(score.getAlignedRow())) {
            continue;
        }

        alignmentMapping.put(score.getFeatureTableRow(), score.getAlignedRow());

    }

    return alignmentMapping;
}

From source file:guineu.modules.filter.Alignment.SerumHuNormalization.SerumHuNormalizationTask.java

private void normalize(Dataset data, Dataset newData) {
    // First row => ids of the samples ( 0 == standard serum, 1 == normal sample)
    this.ids = data.getRow(0).clone();

    // Second row => run order
    this.runOrder = data.getRow(0).clone();

    // Third row => different data sets
    this.batches = data.getRow(0).clone();

    for (String name : data.getAllColumnNames()) {
        ids.setPeak(name, data.getParametersValue(name, this.id));
        runOrder.setPeak(name, data.getParametersValue(name, this.order));
        batches.setPeak(name, data.getParametersValue(name, this.batchesName));
    }/*  ww  w .  j a v a  2  s  . c om*/

    int numBatches = 1;
    double n = (Double) batches.getPeak(data.getAllColumnNames().get(0));

    for (String name : data.getAllColumnNames()) {
        if ((Double) batches.getPeak(name) > n) {
            numBatches++;
            n = (Double) batches.getPeak(name);
        }
    }

    this.createCurves(data, numBatches);
    for (int batch = 0; batch < numBatches; batch++) {
        message = "Normalizing";
        this.totalRows = data.getNumberRows();
        this.processedRows = 0;
        List<String> names = data.getAllColumnNames();
        for (int i = 0; i < data.getNumberRows(); i++) {
            this.processedRows++;
            PeakListRow row = data.getRow(i);
            PeakListRow newrow = newData.getRow(i);
            try {
                // Get the interpolation of all the human serum points using Loess 
                PolynomialSplineFunction function = functions.get(row.getID()).get(batch);

                if (function != null) {
                    // Prepare the points for the extrapolation
                    PolynomialFunction extrapolationFunction = null;
                    if (this.extrapolation) {
                        List<Double> points = new ArrayList<Double>();
                        for (int e = 0; e < row.getNumberPeaks(); e++) {
                            if ((Double) batches.getPeak(names.get(e)) == batch) {
                                try {
                                    points.add(function.value((Double) runOrder.getPeak(names.get(e))));
                                } catch (ArgumentOutsideDomainException ex) {
                                    Logger.getLogger(SerumHuNormalizationTask.class.getName()).log(Level.SEVERE,
                                            null, ex);
                                }
                            }
                        }

                        // Extrapolation function
                        extrapolationFunction = this.fittPolinomialFunction(batches, runOrder, names, batch,
                                points);
                    }
                    double lastPoint = 0;
                    for (int e = 0; e < row.getNumberPeaks(); e++) {
                        String sampleName = names.get(e);
                        if ((Double) ids.getPeak(sampleName) > 0.0) {
                            if ((Double) batches.getPeak(sampleName) == batch) {
                                try {

                                    if ((Double) ids.getPeak(sampleName) == 0) {
                                        lastPoint = function.value((Double) runOrder.getPeak(sampleName));
                                    }
                                    double value = 0;
                                    try {
                                        Double controlMol = function
                                                .value((Double) runOrder.getPeak(names.get(e)));
                                        if (controlMol < 0.0 || controlMol == Double.NaN
                                                || controlMol == Double.POSITIVE_INFINITY
                                                || controlMol == Double.NEGATIVE_INFINITY) {
                                            controlMol = getAverage(ids, row, e, names);
                                        }

                                        value = (Double) row.getPeak(sampleName) / controlMol;

                                        if (value < 0.0 || value == Double.NaN
                                                || value == Double.POSITIVE_INFINITY
                                                || value == Double.NEGATIVE_INFINITY) {
                                            controlMol = getAverage(ids, row, e, names);
                                        }

                                        value = (Double) row.getPeak(sampleName) / controlMol;
                                    } catch (ClassCastException exception) {
                                        value = -100;
                                    }
                                    newrow.setPeak(sampleName, value);
                                } catch (ArgumentOutsideDomainException ex) {
                                    // ex.printStackTrace();
                                    //if the value has to be extrapolated
                                    if (extrapolation && extrapolationFunction != null) {
                                        double value = 0;
                                        try {

                                            Double controlMol = extrapolationFunction
                                                    .value((Double) runOrder.getPeak(names.get(e)));
                                            if (controlMol < 0.0 || controlMol == Double.NaN
                                                    || controlMol == Double.POSITIVE_INFINITY
                                                    || controlMol == Double.NEGATIVE_INFINITY) {
                                                controlMol = getAverage(ids, row, e, names);
                                            }
                                            value = (Double) row.getPeak(sampleName) / controlMol;

                                            if (value < 0.0 || value == Double.NaN
                                                    || value == Double.POSITIVE_INFINITY
                                                    || value == Double.NEGATIVE_INFINITY) {
                                                controlMol = getAverage(ids, row, e, names);
                                            }

                                            value = (Double) row.getPeak(sampleName) / controlMol;
                                        } catch (ClassCastException exception) {
                                            value = -100;
                                        }
                                        newrow.setPeak(sampleName, value);
                                    } else {
                                        double value = 0;
                                        try {
                                            value = (Double) row.getPeak(sampleName) / lastPoint;//extrapolationFunction.value((Double) runOrder.getPeak(names.elementAt(e)));
                                        } catch (ClassCastException exception) {
                                            value = -100;
                                        }
                                        newrow.setPeak(sampleName, value);
                                    }

                                }
                            }
                        }
                    }
                } else {
                    System.out.println("Function is null" + row.getID());
                }
            } catch (Exception exception) {
                exception.printStackTrace();
                System.out.println(row.getID());

            }
        }
    }

}