Example usage for com.google.common.collect Range open

List of usage examples for com.google.common.collect Range open

Introduction

In this page you can find the example usage for com.google.common.collect Range open.

Prototype

public static <C extends Comparable<?>> Range<C> open(C lower, C upper) 

Source Link

Document

Returns a range that contains all values strictly greater than lower and strictly less than upper .

Usage

From source file:net.sf.mzmine.modules.masslistmethods.ADAPchromatogrambuilder.ADAPChromatogramBuilderTask.java

/**
 * @see Runnable#run()//from   ww w  .  ja va2  s.  c o  m
 */
public void run() {
    boolean writeChromCDF = true;

    setStatus(TaskStatus.PROCESSING);

    logger.info("Started chromatogram builder on " + dataFile);

    scans = scanSelection.getMatchingScans(dataFile);
    int allScanNumbers[] = scanSelection.getMatchingScanNumbers(dataFile);

    List<Double> rtListForChromCDF = new ArrayList<Double>();

    // Check if the scans are properly ordered by RT
    double prevRT = Double.NEGATIVE_INFINITY;
    for (Scan s : scans) {
        if (isCanceled()) {
            return;
        }

        if (writeChromCDF) {
            rtListForChromCDF.add(s.getRetentionTime());
        }

        if (s.getRetentionTime() < prevRT) {
            setStatus(TaskStatus.ERROR);
            final String msg = "Retention time of scan #" + s.getScanNumber()
                    + " is smaller then the retention time of the previous scan."
                    + " Please make sure you only use scans with increasing retention times."
                    + " You can restrict the scan numbers in the parameters, or you can use the Crop filter module";
            setErrorMessage(msg);
            return;
        }
        prevRT = s.getRetentionTime();
    }

    // Create new peak list
    newPeakList = new SimplePeakList(dataFile + " " + suffix, dataFile);

    // make a list of all the data points
    // sort data points by intensity
    // loop through list 
    //      add data point to chromatogrm or make new one
    //      update mz avg and other stuff
    // 

    // make a list of all the data points
    List<ExpandedDataPoint> allMzValues = new ArrayList<ExpandedDataPoint>();

    for (Scan scan : scans) {
        if (isCanceled())
            return;

        MassList massList = scan.getMassList(massListName);
        if (massList == null) {
            setStatus(TaskStatus.ERROR);
            setErrorMessage("Scan " + dataFile + " #" + scan.getScanNumber() + " does not have a mass list "
                    + massListName);
            return;
        }

        DataPoint mzValues[] = massList.getDataPoints();

        if (mzValues == null) {
            setStatus(TaskStatus.ERROR);
            setErrorMessage("Mass list " + massListName + " does not contain m/z values for scan #"
                    + scan.getScanNumber() + " of file " + dataFile);
            return;
        }

        for (DataPoint mzPeak : mzValues) {
            ExpandedDataPoint curDatP = new ExpandedDataPoint(mzPeak, scan.getScanNumber());
            allMzValues.add(curDatP);
            //corespondingScanNum.add(scan.getScanNumber());
        }

    }

    //Integer[] simpleCorespondingScanNums = new Integer[corespondingScanNum.size()];
    //corespondingScanNum.toArray(simpleCorespondingScanNums );

    ExpandedDataPoint[] simpleAllMzVals = new ExpandedDataPoint[allMzValues.size()];
    allMzValues.toArray(simpleAllMzVals);

    // sort data points by intensity
    Arrays.sort(simpleAllMzVals, new DataPointSorter(SortingProperty.Intensity, SortingDirection.Descending));

    //Set<Chromatogram> buildingChromatograms;
    //buildingChromatograms = new LinkedHashSet<Chromatogram>();

    double maxIntensity = simpleAllMzVals[0].getIntensity();

    // count starts at 1 since we already have added one with a single point.

    //Stopwatch stopwatch = Stopwatch.createUnstarted(); 
    // stopwatch2 = Stopwatch.createUnstarted(); 
    //Stopwatch stopwatch3 = Stopwatch.createUnstarted(); 

    processedPoints = 0;
    totalPoints = simpleAllMzVals.length;

    for (ExpandedDataPoint mzPeak : simpleAllMzVals) {

        processedPoints++;

        if (isCanceled()) {
            return;
        }

        if (mzPeak == null) {
            //System.out.println("null Peak");
            continue;
        }

        //////////////////////////////////////////////////

        Range<Double> containsPointRange = rangeSet.rangeContaining(mzPeak.getMZ());

        Range<Double> toleranceRange = mzTolerance.getToleranceRange(mzPeak.getMZ());
        if (containsPointRange == null) {
            // skip it entierly if the intensity is not high enough
            if (mzPeak.getIntensity() < minIntensityForStartChrom) {
                continue;
            }
            // look +- mz tolerance to see if ther is a range near by. 
            // If there is use the proper boundry of that range for the 
            // new range to insure than NON OF THE RANGES OVERLAP.
            Range<Double> plusRange = rangeSet.rangeContaining(toleranceRange.upperEndpoint());
            Range<Double> minusRange = rangeSet.rangeContaining(toleranceRange.lowerEndpoint());
            Double toBeLowerBound;
            Double toBeUpperBound;

            double cur_max_testing_mz = mzPeak.getMZ();

            // If both of the above ranges are null then we make the new range spaning the full
            // mz tolerance range. 
            // If one or both are not null we need to properly modify the range of the new
            // chromatogram so that none of the points are overlapping.
            if ((plusRange == null) && (minusRange == null)) {
                toBeLowerBound = toleranceRange.lowerEndpoint();
                toBeUpperBound = toleranceRange.upperEndpoint();
            } else if ((plusRange == null) && (minusRange != null)) {
                // the upper end point of the minus range will be the lower 
                // range of the new one
                toBeLowerBound = minusRange.upperEndpoint();
                toBeUpperBound = toleranceRange.upperEndpoint();

            } else if ((minusRange == null) && (plusRange != null)) {
                toBeLowerBound = toleranceRange.lowerEndpoint();
                toBeUpperBound = plusRange.lowerEndpoint();
                //                    double tmp_this = plusRange.upperEndpoint();
                //                    System.out.println("tmp_this");
            } else if ((minusRange != null) && (plusRange != null)) {
                toBeLowerBound = minusRange.upperEndpoint();
                toBeUpperBound = plusRange.lowerEndpoint();
            } else {
                toBeLowerBound = 0.0;
                toBeUpperBound = 0.0;
            }
            Range<Double> newRange = Range.open(toBeLowerBound, toBeUpperBound);
            ADAPChromatogram newChrom = new ADAPChromatogram(dataFile, allScanNumbers);

            newChrom.addMzPeak(mzPeak.getScanNumber(), mzPeak);

            newChrom.setHighPointMZ(mzPeak.getMZ());

            rangeToChromMap.put(newRange, newChrom);
            // also need to put it in the set -> this is where the range can be efficiently found.

            rangeSet.add(newRange);
        } else {
            // In this case we do not need to update the rangeSet

            ADAPChromatogram curChrom = rangeToChromMap.get(containsPointRange);

            curChrom.addMzPeak(mzPeak.getScanNumber(), mzPeak);

            //update the entry in the map
            rangeToChromMap.put(containsPointRange, curChrom);

        }
    }

    //System.out.println("search chroms (ms): " +  stopwatch.elapsed(TimeUnit.MILLISECONDS));
    //System.out.println("making new chrom (ms): " +  stopwatch2.elapsed(TimeUnit.MILLISECONDS));

    // finish chromatograms
    Iterator<Range<Double>> RangeIterator = rangeSet.asRanges().iterator();

    List<ADAPChromatogram> buildingChromatograms = new ArrayList<ADAPChromatogram>();

    while (RangeIterator.hasNext()) {
        if (isCanceled()) {
            return;
        }

        Range<Double> curRangeKey = RangeIterator.next();

        ADAPChromatogram chromatogram = rangeToChromMap.get(curRangeKey);

        chromatogram.finishChromatogram();

        // And remove chromatograms who dont have a certian number of continous points above the
        // IntensityThresh2 level.
        double numberOfContinuousPointsAboveNoise = chromatogram
                .findNumberOfContinuousPointsAboveNoise(IntensityThresh2);
        if (numberOfContinuousPointsAboveNoise < minimumScanSpan) {
            //System.out.println("skipping chromatogram because it does not meet the min point scan requirements");
            continue;
        } else {
            buildingChromatograms.add(chromatogram);
        }

    }

    ADAPChromatogram[] chromatograms = buildingChromatograms.toArray(new ADAPChromatogram[0]);

    // Sort the final chromatograms by m/z
    Arrays.sort(chromatograms, new PeakSorter(SortingProperty.MZ, SortingDirection.Ascending));

    // Add the chromatograms to the new peak list
    for (Feature finishedPeak : chromatograms) {
        SimplePeakListRow newRow = new SimplePeakListRow(newPeakID);
        newPeakID++;
        newRow.addPeak(dataFile, finishedPeak);
        newPeakList.addRow(newRow);

        //            finishedPeak.outputChromToFile();
    }

    // Add new peaklist to the project
    project.addPeakList(newPeakList);

    // Add quality parameters to peaks
    QualityParameters.calculateQualityParameters(newPeakList);

    setStatus(TaskStatus.FINISHED);

    logger.info("Finished chromatogram builder on " + dataFile);
}